Download the PHP package efoft/query-builder without Composer
On this page you can find all versions of the php package efoft/query-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download efoft/query-builder
More information about efoft/query-builder
Files in efoft/query-builder
Package query-builder
Short Description Helper classes allowing to generate native syntax queries to databases
License
Informations about the package query-builder
QueryBuilder: SQL
=== The package allows to produce native SQL queries making multiple and chanable calls on class object. SQL queries are injection-protected with substitutors. Also there is the method to get all binded values for such substitutors.
Installation
The package is intended to be used via Composer. Add this to you composer.json:
or just run via command-line:
Initialization
There are 2 optional argument for class construct:
- $quoting - type of quoting: one of 'none','sql','mysql','mssql', 'sqlite'. See http://www.sqlite.org/lang_keywords.html for explanation.
- $debug - true of false With specified quoting type of mysql and enabled debug mode:
Usage
You can call methods on created $q object one by one in a chain way to fill all required parameters to generate a query. Important! If you reuse $q for new query, first call ->newQuery() method.
It's possible to run the following methods multiple times, the parameters will be aggregated. It's useful if some query changes are conditional.
- table / from
- select
- insert
- update
- order
- group
- where
Methods table() and from() are just aliases. Specifiing multiple table is worth only for SELECT queries. For all the rest only first table will be used. All above methods except from where() can accept arguments in several possible ways:
- 'item1','item2'...
- 'item1, item2...'
- array('item1','item2'...)
- and mixes of the above.
Method where() specifies WHERE conditions via array. The format is similar to MongoDB find() syntax, but so far limited with $or and $and operators.
Examples below illustrate it in more details.
Select
Method ->select(). Can chain and can be called multiple times. Duplicates are automatically removed. For specifying aliases use associative arrays: array('field'=>'alias', ...). The following methods allow to set ORDER BY, GROUP BY, LIMIT and DISTINCT directives:
- order()
- group()
- limit()
- distinct()
Insert
Method ->insert(). Can chain and can be called multiple times. Arguments must be associative arrays like array('field'=>'value'...).
Update
Method ->update(). Can chain and can be called multiple times. Arguments must be associative arrays like array('field'=>'value'...).
Delete
Method ->delete(). Does not require any arguments, but the conditions must follow with where().
Join
Method ->join(). It has 3 mandatory arguments (joined table, key field in main table, key fields in joined table) and 1 optional - type of join (by default LEFT is used). For key fields there is no need to specify them as fully-qualified, it's done automatically. Join is applicable to select, update and delete statements and can be run multiple time if many tables are joined.
Retrieve results
To get resulted query:
To get binded values: