SlaxWeb Framework
 All Classes Namespaces Functions Variables Pages
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
SlaxWeb\Database\Query\Builder Class Reference

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
 

Constructor & Destructor Documentation

SlaxWeb\Database\Query\Builder::__construct ( )

Class constructor

Prepare the predictes list by instantiating the first predicate group object.

Member Function Documentation

SlaxWeb\Database\Query\Builder::buildColList ( array  $cols,
string  $table 
)
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.

Parameters
array$colsArray of columns to be added to the list
string$tableTable name used to prepend the columns with
Returns
string
SlaxWeb\Database\Query\Builder::delete ( )

Delete

Create delete statement with the where predicates.

Returns
string
SlaxWeb\Database\Query\Builder::getJoinData ( )
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.

Returns
array
SlaxWeb\Database\Query\Builder::getParams ( )

Get Bind Parameters

Returns the parameters prepared for binding into the prepared statement.

Returns
array
SlaxWeb\Database\Query\Builder::getPredicates ( )

Get predicates

Returns the main group of predicates.

Returns
SlaxWeb\Database\Query\Builder::groupBy ( string  $col)

Group by

Add a column to the group by list.

Parameters
string$colColumn name to be added to the group by list.
Returns
self
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.

Parameters
closure$predicatesGrouped predicates definition closure
string$cOprComparisson operator, default string("AND")
Returns
self
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.

Parameters
array$dataData to be inserted
Returns
string
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.

Parameters
string$tableTable to join to
string$typeJoin type, default "INNER JOIN"
Returns
self
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.

Parameters
array$colsColumn list
Returns
self
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.

Parameters
string$primKeyKey of the main table for the condition
string$forKeyKey of the joining table
string$cOprComparison operator for the two keys
string$lOprLogical operator for multiple JOIN conditions
Returns
self
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.

Parameters
int$limitNumber of rows to limit the result set to
int$offsetNumber of rows for the result to be offset from, default int(0)
Returns
self
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.

Parameters
string$columnColumn name
closure$nestedNested builder
string$lOprLogical operator, default Predicate::OPR_IN
string$cOprComparisson operator, default string("AND")
Returns
self
SlaxWeb\Database\Query\Builder::orderBy ( string  $col,
string  $direction = self::ORDER_ASC,
string  $func = "" 
)

Order by

Add a column to the order by list.

Parameters
string$colColumn name to be added to the group by list
string$directionDirection of order, default self::ORDER_ASC
string$funcSQL function to use ontop of the column, default string("")
Returns
self
SlaxWeb\Database\Query\Builder::orGroupWhere ( \Closure  $predicates)

Or Where Predicate Group

Alias for 'whereGroup' method call with OR logical operator.

Parameters
closure$predicatesGrouped predicates definition closure
Returns
self
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.

Parameters
string$primKeyKey of the main table for the condition
string$forKeyKey of the joining table
string$cOprComparison operator for the two keys
Returns
self
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.

Parameters
string$columnColumn name
closure$nestedNested builder
string$lOprLogical operator, default Predicate::OPR_IN
Returns
self
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.

Parameters
string$columnColumn name
mixed$valueValue of the predicate
string$oprLogical operator
Returns
self
SlaxWeb\Database\Query\Builder::reset ( )

Reset the builder

Restes the builder, by re-initializing the main predicate group, clearing out parameters, and join definitions.

Returns
self
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.

Parameters
array$colsColumn definitions
Returns
string
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.

Parameters
string$delimDelimiter character
Returns
self
SlaxWeb\Database\Query\Builder::table ( string  $table)

Set table

Sets the table name for the query. Before setting it wraps it in the delimiters.

Parameters
string$tableName of the table
Returns
self
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.

Parameters
array$colsArray of column names and their new values
Returns
string
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.

Parameters
string$columnColumn name
mixed$valueValue of the predicate
string$lOprLogical operator, default Predicate::OPR_EQUAL
string$cOprComparisson operator, default string("AND")
Returns
self

Member Data Documentation

const SlaxWeb\Database\Query\Builder::ORDER_ASC = "ASC"

Order by directions


The documentation for this class was generated from the following file: