Download the PHP package krinkle/sql-query-builder without Composer
On this page you can find all versions of the php package krinkle/sql-query-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download krinkle/sql-query-builder
More information about krinkle/sql-query-builder
Files in krinkle/sql-query-builder
Package sql-query-builder
Short Description An elegant lightweight and efficient SQL QueryInterface BuilderInterface supporting bindings and complicated query generation.
License MIT
Homepage http://nilportugues.com
Informations about the package sql-query-builder
SQL Query Builder
An elegant lightweight and efficient SQL Query Builder with fluid interface SQL syntax supporting bindings and complicated query generation. Works without establishing a connection to the database.
- 1. Installation
- 2. The Builder
- 2.1. Generic Builder
- 2.2. MySQL Builder
- 2.3. Human Readable Output
- 3. Building Queries
- 3.1. SELECT Statement
- 3.1.1. Basic SELECT statement
- 3.1.2. Aliased SELECT statement
- 3.1.3. SELECT with WHERE statement
- 3.1.4. Complex WHERE conditions
- 3.1.5. JOIN & LEFT/RIGHT/INNER/CROSS JOIN SELECT statements
- 3.1.6. COUNT rows
- 3.2. INSERT Statement
- 3.2.1. Basic INSERT statement
- 3.3. UPDATE Statement
- 3.3.1. Basic UPDATE statement
- 3.3.2. Elaborated UPDATE statement
- 3.4. DELETE Statement
- 3.4.1. Empty table with DELETE statement
- 3.4.2. Basic DELETE statement
- 3.4.3. Elaborated DELETE statement
- 3.5. INTERSECT Statement
- 3.6. MINUS Statement
- 3.7. UNION Statement
- 3.8. UNION ALL Statement
- 3.1. SELECT Statement
- 4. Advanced Quering
- 4.1. Filtering using WHERE
- 4.1.1. Changing WHERE logical operator
- 4.1.2. Writing complicated WHERE conditions
- 4.3. Grouping with GROUP BY and HAVING
- 4.3.1 Available HAVING operators
- 4.4. Changing HAVING logical operator
- 4.5. Columns as SELECT statements
- 4.6. Columns being Values
- 4.7. Columns using FUNCTIONS
- 4.1. Filtering using WHERE
- 5. Commenting queries
- 6. Quality Code
- 7. Author
- 8. License
1. Installation ↑
The recommended way to install the SQL Query Builder is through Composer. Run the following command to install it:
2. The Builder ↑
The SQL Query Builder allows to generate complex SQL queries standard using the SQL-2003
dialect (default) and the MySQL
dialect, that extends the SQL-2003
dialect.
2.1. Generic Builder ↑
The Generic Query Builder is the default builder for this class and writes standard SQL-2003.
All column aliases are escaped using the '
sign by default.
Usage:
Output:
2.2. MySQL Builder ↑
The MySQL Query Builder has its own class, that inherits from the SQL-2003 builder. All columns will be wrapped with the tilde ` sign.
All table and column aliases are escaped using the tilde sign by default.
Usage:
Output:
2.3. Human Readable Output ↑
Both Generic and MySQL Query Builder can write complex SQL queries.
Every developer out there needs at some point revising the output of a complicated query, the SQL Query Builder includes a human-friendly output method, and therefore the writeFormatted
method is there to aid the developer when need.
Keep in mind writeFormatted
is to be avoided at all cost in production mode as it adds unneeded overhead due to parsing and re-formatting of the generated statement.
Usage:
Output:
More complicated examples can be found in the documentation.
3. Building Queries ↑
3.1. SELECT Statement ↑
3.1.1. Basic SELECT statement ↑
Usage:
Output:
3.1.2. Aliased SELECT statement ↑
Usage:
Output:
3.1.3. SELECT with WHERE statement ↑
Default logical operator for filtering using WHERE
conditions is AND
.
Usage:
Output:
3.1.4. Complex WHERE conditions ↑
Usage:
Output:
3.1.5. JOIN & LEFT/RIGHT/INNER/CROSS JOIN SELECT statements ↑
Syntax for JOIN
, LEFT JOIN
, RIGHT JOIN
, INNER JOIN
, CROSS JOIN
work the exactly same way.
Here's an example selecting both table and joined table columns and doing sorting using columns from both the table and the joined table.
Usage:
Output:
3.1.6. COUNT rows ↑
Counting rows comes in 3 possible ways, using the ALL selector *
, stating a column or stating a column and its alias.
3.1.6.1. Count using ALL selector
Usage:
Output:
3.1.6.2. Count using column as a selector
Usage:
Output:
3.1.6.3. Count using column as a selector
Usage:
Output:
3.2. INSERT Statement ↑
The INSERT
statement is really straightforward.
3.2.1 Basic INSERT statement ↑
Usage:
Output
3.3. UPDATE Statement ↑
The UPDATE
statement works just like expected, set the values and the conditions to match the row and you're set.
Examples provided below.
3.3.1 Basic UPDATE statement ↑
Important including the the where
statement is critical, or all table rows will be replaced with the provided values if the statement is executed.
Usage:
Output:
3.3.2. Elaborated UPDATE statement ↑
Usage:
Output:
3.4. DELETE Statement ↑
The DELETE
statement is used just like UPDATE
, but no values are set.
Examples provided below.
3.4.1. Empty table with DELETE statement ↑
Usage:
Output:
3.4.2. Basic DELETE statement ↑
Important including the the where
statement is critical, or all table rows will be deleted with the provided values if the statement is executed.
Usage:
Output:
3.4.2. Elaborated DELETE statement ↑
Usage:
Output:
3.5. INTERSECT Statement ↑
INTERSECT is not supported by MySQL. Same results can be achieved by using INNER JOIN statement instead.
The INTERSECT
statement is really straightforward.
3.5.1 Basic INTERSECT statement ↑
Usage:
Output
3.6. MINUS Statement ↑
MINUS is not supported by MySQL. Same results can be achieved by using a LEFT JOIN statement in combination with an IS NULL or NOT IN condition instead.
The MINUS
statement is really straightforward.
3.6.1 Basic MINUS statement ↑
Usage:
Output
3.7. UNION Statement ↑
The UNION
statement is really straightforward.
3.7.1 Basic UNION statement ↑
Usage:
Output
3.8. UNION ALL Statement ↑
The UNION ALL
statement is really straightforward.
3.8.1 Basic UNION ALL statement ↑
Usage:
Output
4. Advanced Quering ↑
4.1. Filtering using WHERE ↑
The following operators are available for filtering using WHERE conditionals:
4.2. Changing WHERE logical operator ↑
WHERE
default's operator must be changed passing to the where
method the logical operator OR
.
Usage:
Output:
4.3. Grouping with GROUP BY and HAVING ↑
Default logical operator for joining more than one HAVING
condition is AND
.
Usage:
Output:
4.3.1 Available HAVING operators ↑
Same operators used in the WHERE statement are available for HAVING operations.
4.4. Changing HAVING logical operator ↑
HAVING
default's operator must be changed passing to the having
method the logical operator OR
.
Usage:
Output:
4.5. Columns as SELECT statements ↑
Sometimes, a column needs to be set as a column. SQL Query Builder got you covered on this one too! Check the example below.
Usage:
Output:
4.6. Columns being Values ↑
There are time where you need to force the same column structure (eg: UNIONs) even when lacking of a column or value. Forcing column with values gets you covered.
Usage:
Output:
4.7. Columns using FUNCTIONS ↑
Example for MAX function.
Usage:
Output:
Example for CURRENT_TIMESTAMP function.
Usage:
Output:
5. Commenting queries ↑
The query builder allows adding comments to all query methods by using the setComment
method.
Some useful use cases examples can be :
- Explain difficult queries or why of its existence.
- Finding slow queries from its comments.
Usage:
Output:
6. Quality Code ↑
Testing has been done using PHPUnit and Travis-CI. All code has been tested to be compatible from PHP 5.4 up to PHP 5.6 and HHVM.
To run the test suite, you need Composer:
7. Author ↑
Nil Portugués Calderó
8. License ↑
SQL Query Builder is licensed under the MIT license.