Public Member Functions | |
__construct () | |
setDelim (string $delim) | |
table (string $table) | |
reset () | |
getPredicates () | |
getParams () | |
insert (array $data) | |
select (array $cols) | |
update (array $cols) | |
delete () | |
where (string $column, $value, string $lOpr=Predicate::OPR_EQUAL, string $cOpr="AND") | |
orWhere (string $column, $value, string $opr=Predicate::OPR_EQUAL) | |
groupWhere (\Closure $predicates, string $cOpr="AND") | |
orGroupWhere (\Closure $predicates) | |
nestedWhere (string $column,\Closure $nested, string $lOpr=Predicate::OPR_IN, string $cOpr="AND") | |
orNestedWhere (string $column,\Closure $nested, string $lOpr=Predicate::OPR_IN) | |
join (string $table, string $type="INNER JOIN") | |
joinCond (string $primKey, string $forKey, string $cOpr=Predicate::OPR_EQUAL, $lOpr="AND") | |
orJoinCond (string $primKey, string $forKey, string $cOpr=Predicate::OPR_EQUAL) | |
joinCols (array $cols) | |
groupBy (string $col) | |
orderBy (string $col, string $direction=self::ORDER_ASC, string $func="") | |
limit (int $limit, int $offset=0) | |
Public Attributes | |
const | ORDER_ASC = "ASC" |
const | ORDER_DESC = "DESC" |
Protected Member Functions | |
buildColList (array $cols, string $table) | |
getJoinData () | |
Protected Attributes | |
$table = "" | |
$params = [] | |
$delim = "" | |
$predicates = null | |
$joins = [] | |
$groupCols = [] | |
$orderCols = [] | |
$limit = 0 | |
$offset = 0 | |
SlaxWeb\Database\Query\Builder::__construct | ( | ) |
Class constructor
Prepare the predictes list by instantiating the first predicate group object.
|
protected |
Build column list
Builds a column list from the input column list and the table name. It automatically prepends columns with the supplied table name and wraps the columns in the database object delimiters.
array | $cols | Array of columns to be added to the list |
string | $table | Table name used to prepend the columns with |
SlaxWeb\Database\Query\Builder::delete | ( | ) |
Delete
Create delete statement with the where predicates.
|
protected |
Get Join Data
Constructs the join statement, and the column list with the joined table(s). Return is an array of two strings, "statement" containing the join statment to be appended after "... FROM table" in SQL, and "colList" cotaining comma separated list of columns to be included in the SELECT statement.
SlaxWeb\Database\Query\Builder::getParams | ( | ) |
Get Bind Parameters
Returns the parameters prepared for binding into the prepared statement.
SlaxWeb\Database\Query\Builder::getPredicates | ( | ) |
Get predicates
Returns the main group of predicates.
SlaxWeb\Database\Query\Builder::groupBy | ( | string | $col | ) |
Group by
Add a column to the group by list.
string | $col | Column name to be added to the group by list. |
SlaxWeb\Database\Query\Builder::groupWhere | ( | \Closure | $predicates, |
string | $cOpr = "AND" |
||
) |
Add Where Predicate Group
Adds a group of predicates to the list. The closure received as input must receive the builder instance for building groups.
closure | $predicates | Grouped predicates definition closure |
string | $cOpr | Comparisson operator, default string("AND") |
SlaxWeb\Database\Query\Builder::insert | ( | array | $data | ) |
Get INSERT query
Create the insert query based on the array of data. It prepares the query for parameter binding, and stores the parameter values in the local parameters property which can be retrieved with 'getParams' method call.
array | $data | Data to be inserted |
SlaxWeb\Database\Query\Builder::join | ( | string | $table, |
string | $type = "INNER JOIN" |
||
) |
Add table to join
Adds a new table to join with the main table to the list of joins. If only a table is added without a condition with the 'joinCond', an exception will be thrown when an attempt to create a query is made.
string | $table | Table to join to |
string | $type | Join type, default "INNER JOIN" |
SlaxWeb\Database\Query\Builder::joinCols | ( | array | $cols | ) |
Join Columns
Add columns to include in the select column list. If no table for joining was yet added, an exception is raised. Same rules apply to the column list as in the 'select' method.
array | $cols | Column list |
SlaxWeb\Database\Query\Builder::joinCond | ( | string | $primKey, |
string | $forKey, | ||
string | $cOpr = Predicate::OPR_EQUAL , |
||
$lOpr = "AND" |
|||
) |
Add join condition
Adds a JOIN condition to the last join added. If no join was yet added, an exception is raised.
string | $primKey | Key of the main table for the condition |
string | $forKey | Key of the joining table |
string | $cOpr | Comparison operator for the two keys |
string | $lOpr | Logical operator for multiple JOIN conditions |
SlaxWeb\Database\Query\Builder::limit | ( | int | $limit, |
int | $offset = 0 |
||
) |
Limit
Limit number of rows to be returned from the database. Second parameter will also add an offset to the statement.
int | $limit | Number of rows to limit the result set to |
int | $offset | Number of rows for the result to be offset from, default int(0) |
SlaxWeb\Database\Query\Builder::nestedWhere | ( | string | $column, |
\Closure | $nested, | ||
string | $lOpr = Predicate::OPR_IN , |
||
string | $cOpr = "AND" |
||
) |
Where Nested Select
Add a nested select as a value to the where predicate.
string | $column | Column name |
closure | $nested | Nested builder |
string | $lOpr | Logical operator, default Predicate::OPR_IN |
string | $cOpr | Comparisson operator, default string("AND") |
SlaxWeb\Database\Query\Builder::orderBy | ( | string | $col, |
string | $direction = self::ORDER_ASC , |
||
string | $func = "" |
||
) |
Order by
Add a column to the order by list.
string | $col | Column name to be added to the group by list |
string | $direction | Direction of order, default self::ORDER_ASC |
string | $func | SQL function to use ontop of the column, default string("") |
SlaxWeb\Database\Query\Builder::orGroupWhere | ( | \Closure | $predicates | ) |
Or Where Predicate Group
Alias for 'whereGroup' method call with OR logical operator.
closure | $predicates | Grouped predicates definition closure |
SlaxWeb\Database\Query\Builder::orJoinCond | ( | string | $primKey, |
string | $forKey, | ||
string | $cOpr = Predicate::OPR_EQUAL |
||
) |
Add OR join condition
Alias for the 'joinCond' with the "OR" logical operator.
string | $primKey | Key of the main table for the condition |
string | $forKey | Key of the joining table |
string | $cOpr | Comparison operator for the two keys |
SlaxWeb\Database\Query\Builder::orNestedWhere | ( | string | $column, |
\Closure | $nested, | ||
string | $lOpr = Predicate::OPR_IN |
||
) |
Or Where Nested Select
Alias for 'nestedWhere' method call with OR logical operator.
string | $column | Column name |
closure | $nested | Nested builder |
string | $lOpr | Logical operator, default Predicate::OPR_IN |
SlaxWeb\Database\Query\Builder::orWhere | ( | string | $column, |
$value, | |||
string | $opr = Predicate::OPR_EQUAL |
||
) |
Or Where predicate
Alias for 'where' method call with OR logical operator.
string | $column | Column name |
mixed | $value | Value of the predicate |
string | $opr | Logical operator |
SlaxWeb\Database\Query\Builder::reset | ( | ) |
Reset the builder
Restes the builder, by re-initializing the main predicate group, clearing out parameters, and join definitions.
SlaxWeb\Database\Query\Builder::select | ( | array | $cols | ) |
Get SELECT query
Construct the query with all the information gathered and return it. The Method retrieves a column list as an input parameter. All columns are wrapped in the delimiters to prevent collision with reserved keywords. If the array item is another array, it needs to hold the "func" and "col" keys at least, defining the SQL DML function, as well as the column name. A third item with the key name "as" can be added, and this name will be used in the "AS" statement in the SQL DML for that column.
array | $cols | Column definitions |
SlaxWeb\Database\Query\Builder::setDelim | ( | string | $delim | ) |
Set DB Object Delimiter
Sets the Database Object Delimiter character that will be used for creating the query.
string | $delim | Delimiter character |
SlaxWeb\Database\Query\Builder::table | ( | string | $table | ) |
Set table
Sets the table name for the query. Before setting it wraps it in the delimiters.
string | $table | Name of the table |
SlaxWeb\Database\Query\Builder::update | ( | array | $cols | ) |
Update
Create the update statement with the where predicates. As input it takes an array of columns as array item keys and their new values as the array item values.
array | $cols | Array of column names and their new values |
SlaxWeb\Database\Query\Builder::where | ( | string | $column, |
$value, | |||
string | $lOpr = Predicate::OPR_EQUAL , |
||
string | $cOpr = "AND" |
||
) |
Add Where Predicate
Adds a SQL DML WHERE predicate to the group of predicates. If the group does not yet exist it will create one.
string | $column | Column name |
mixed | $value | Value of the predicate |
string | $lOpr | Logical operator, default Predicate::OPR_EQUAL |
string | $cOpr | Comparisson operator, default string("AND") |
const SlaxWeb\Database\Query\Builder::ORDER_ASC = "ASC" |
Order by directions