Download the PHP package sqltools/where without Composer
On this page you can find all versions of the php package sqltools/where. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package where
Where
The simplest fluent SQL query builder ever.
Built in PHP7.1 with immutability in mind.
Features
- Framework agnostic, connection agnostic (you just render a string and an array of values)
- Natural language: where, and, or, ...
- Support named and numeric placeholders
- Build complex, nested WHERE conditions
- Helpers for building
SELECT
,INSERT
,UPDATE
,DELETE
,REPLACE
queries - Helpers for SQL functions like
IN
,BETWEEN
,IS NULL
,CASE ... WHEN
Why?
In most cases simple SQL queries are fine.
But if your application logic is designed in a way that several classes / methods can modify an SQL query (like the Visitor pattern), then you'll probably need query builder (you can define LIMIT / OFFSET before WHERE for instance, and the query will be rendered in the correct order).
Conditions builder
Where allows you to build your conditions with Expressions. Expressions are objects that can be:
- Simple expressions:
date_added = CURRENT_DATE
- Composite expressions:
date_added = CURRENT_DATE OR date_added = SUBDATE(CURRENT_DATE, INTERVAL 1 DAY)
- Group expressions:
(country = 'UK' OR country = 'BE')
- Negated expressions:
NOT date_added = CURRENT_DATE
An Expression object can also contain an array of parameters to bind (to avoid SQL injections).
You don't need to instanciate them. Just rely on the powerful functions the library offers:
Outputs:
Every function where()
, group()
, not()
accepts either an already instanciated Expression object, or a string and some optionnal parameters.
Thanks to the fluent interface, let your IDE guide you for the rest. Don't forget Where is always immutable: reassign $where
everytime you do some changes.
Select Query Builder
Now you've learnt how to build conditions, you'll see how building a whole select query is a piece of cake:
Let your favorite IDE do the rest with autocompletion.
RulerZ
Where is mostly compatible with the awesome RulerZ DSL.
Be sure to group
your statements to avoid downside effects.
Example:
Installation
composer require bentools/where
Tests
./vendor/bin/phpunit
See also
bentools/simple-dbal - A PHP 7.1+ wrapper for PDO & Mysqli. Can bind DateTime
parameters.
bentools/pager - A PHP 7.1+ pager.
bentools/flatten-iterator - Flattens multiple array
or Traversable
into one iterator.
bentools/etl - A PHP7.1 ETL pattern implementation.
latitude/latitude - Another SQL Query builder Where was inspired of.