Giter Site home page Giter Site logo

soql-query-builder's Introduction

soql-query-builder

SOQL Query Builder lets you write your long SOQL queries in more organized and readable fashion. It also supports "*" and automatically queries all the fields for a given object.

How to use?

1. Quering all the fields

String soql = new QueryBuilder()
                  .sel('*')
                  .frm('Contact')
                  .soql();
System.debug(soql);

Note: This is a costly operations and could slowdown your application. Please make sure that the queried object doesn't have any Rich-Text or Long-Text area fields.

2. Selecting specific fields

String soql = new QueryBuilder()
                  .sel('FirstName')
                  .sel(new Set<String>{ 'LastName', 'Email' })
                  .sel(new List<String>{ 'Phone', 'Department' })
                  .frm('Contact')
                  .soql();
System.debug(soql);

Outputs to

SELECT FirstName,LastName,Email,Phone,Department FROM Contact

3. Filtering records

String soql = new QueryBuilder()
                  .sel('FirstName')
                  .sel('LastName')
                  .frm('Contact')
                  .whr('Department != null')
                  .whr('Level__c = \'Primary\'')
                  .soql();
System.debug(soql);

Outputs to

SELECT FirstName,LastName FROM Contact WHERE Department != null AND Level__c = 'Primary'

4. Advanced Filtering

String soql = new QueryBuilder()
                  .sel('FirstName')
                  .sel('LastName')
                  .frm('Contact')
                  .whr('Department != null', 1)
                  .whr('Department = \'Internal Operations\'', 2)
                  .whr('Level__c != null', 3)
                  .whr('Level__c = \'Primary\'', 4)
                  .advwhr('(1 AND 2) OR (3 AND 4)')
                  .soql();
System.debug(soql);

Outputs to

SELECT FirstName,LastName FROM Contact WHERE (Department != null AND Department = 'Internal Operations') OR (Level__c != null AND Level__c = 'Primary')

5. Subqueries / Nested queries

String soql = new QueryBuilder()
                  .sel('Id')
                  .sel('Name')
                  .sub(new QueryBuilder()
                           .sel('FirstName')
                           .sel('LastName')
                           .frm('Contacts')
                           .lmt(5))
                  .frm('Account')
                  .soql();
System.debug(soql);

Outputs to

SELECT Id,Name,(SELECT FirstName,LastName FROM Contacts LIMIT 5) FROM Account

6. Sorting / Limiting records

String soql = new QueryBuilder()
                  .sel('FirstName')
                  .sel('LastName')
                  .frm('Contact')
                  .sort('FirstName')
                  .sort('LastName', 'DESC')
                  .lmt(5)
                  .soql();
System.debug(soql);

Outputs to

SELECT FirstName,LastName FROM Contact ORDER BY FirstName ASC,LastName DESC LIMIT 5

Authors

  • Jagdeep Sharma

soql-query-builder's People

Contributors

jagdeep-sharma avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.