lyonstahl / soql-builder
SOQL builder that simplifies the process of constructing complex queries to retrieve data from Salesforce databases
Requires
- php: >=7.3
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-12-10 15:55:16 UTC
README
SOQL builder that simplifies the process of constructing complex queries to retrieve data from Salesforce databases.
Installation
Ensure you have composer installed, then run the following command:
composer require lyonstahl/soql-builder
That will fetch the library and its dependencies inside your vendor folder. Then you need to use the relevant class, for example:
use LyonStahl\SoqlBuilder\SoqlBuilder;
Features
- Select
- Conditionals (where)
- Conditionals for date values
- Grouped conditional statements
- Where in
- Where not in
- Limit
- Offset
- Order By
Usage
Builder has two entry points for comfortable static usage: SoqlBuilder::select()
and SoqlBuilder::from()
. Both methods return a SoqlBuilder
instance.
In any other context, you must call addSelect()
and setFrom()
to add the "SELECT" and "FROM" statements. See examples below.
SoqlBuilder::select(['Id', 'Name', 'created_at']) ->setFrom('Account') ->where('Name', '=', 'Test') ->limit(20) ->orderBy('created_at', 'DESC') ->toSoql();
> SELECT Id, Name, created_at FROM Account WHERE Name = 'Test' ORDER BY created_at DESC LIMIT 20
SoqlBuilder::from('Account') ->addSelect(['Id', 'Name']) ->where('Name', '=', 'Test') ->orWhere('Name', '=', 'Testing') ->toSoql();
> SELECT Id, Name FROM Account WHERE Name = 'Test' OR Name = 'Testing'
SoqlBuilder::select(['Id', 'Name']) ->setFrom('Account') ->startWhere() ->where('Name', '=', 'Test') ->where('Testing__c', '=', true) ->endWhere() ->orWhere('Email__c', '=', 'test@test.com') ->toSoql();
> SELECT Id, Name FROM Account WHERE (Name = 'Test' AND Testing__c = true) OR Email__c = 'test@test.com'
Requirements
- PHP 7.3+
- Composer 2.0+
- PHPUnit is required to run the unit tests
Running for development with Docker
We have included a Dockerfile to make it easy to run the tests and debug the code. You must have Docker installed. The following commands will build the image and run the container:
docker build -t lyonstahl/soql-builder --build-arg PHP_VERSION=8 .
docker run -it --rm -v ${PWD}:/var/www/soql lyonstahl/soql-builder sh
Debugging with XDebug in VSCode
Docker image is configured with XDebug. To debug the code with VSCode, follow these steps:
-
Install the PHP Debug extension in VSCode
-
Add a new PHP Debug configuration in VSCode:
{ "name": "XDebug Docker", "type": "php", "request": "launch", "port": 9003, "pathMappings": { "/var/www/soql/": "${workspaceRoot}/" } }
-
docker run -it --rm -v ${PWD}:/var/www/soql --add-host host.docker.internal:host-gateway lyonstahl/soql-builder sh
-
Start debugging in VSCode with the 'XDebug Docker' configuration.
Testing
This library ships with PHPUnit for development. Composer file has been configured with some scripts, run the following command to run the tests:
composer test