Fr3nch13/CakePHP Utilities

QueryExpression
in package
implements ExpressionInterface, Countable uses TypeMapTrait

Represents a SQL Query expression. Internally it stores a tree of expressions that can be compiled by converting this object to string and will contain a correctly parenthesized and nested expression.

Table of Contents

Interfaces

ExpressionInterface
An interface used by Expression objects.
Countable

Properties

$_conditions  : array<string|int, mixed>
A list of strings or other expression objects that represent the "branches" of the expression tree. For example one key of the array might look like "sum > :value"
$_conjunction  : string
String to be used for joining each of the internal expressions this object internally stores for example "AND", "OR", etc.
$_typeMap  : TypeMap|null

Methods

__clone()  : void
Clone this object and its subtree of expressions.
__construct()  : mixed
Constructor. A new expression object can be created without any params and be built dynamically. Otherwise, it is possible to pass an array of conditions containing either a tree-like array structure to be parsed and/or other expression objects. Optionally, you can set the conjunction keyword to be used for joining each part of this level of the expression tree.
add()  : $this
Adds one or more conditions to this expression object. Conditions can be expressed in a one dimensional array, that will cause all conditions to be added directly at this level of the tree or they can be nested arbitrarily making it create more expression objects that will be nested inside and configured to use the specified conjunction.
addCase()  : $this
Adds a new case expression to the expression object
and()  : QueryExpression
Returns a new QueryExpression object containing all the conditions passed and set up the conjunction to be "AND"
and_()  : QueryExpression
Returns a new QueryExpression object containing all the conditions passed and set up the conjunction to be "AND"
between()  : $this
Adds a new condition to the expression object in the form "field BETWEEN from AND to".
case()  : CaseStatementExpression
Returns a new case expression object.
count()  : int
Returns the number of internal conditions that are stored in this expression.
eq()  : $this
Adds a new condition to the expression object in the form "field = value".
equalFields()  : $this
Builds equal condition or assignment with identifier wrapping.
exists()  : $this
Adds a new condition to the expression object in the form "EXISTS (...)".
getConjunction()  : string
Gets the currently configured conjunction for the conditions at this level of the expression tree.
getDefaultTypes()  : array<int|string, string>
Gets default types of current type map.
getTypeMap()  : TypeMap
Returns the existing type map.
gt()  : $this
Adds a new condition to the expression object in the form "field > value".
gte()  : $this
Adds a new condition to the expression object in the form "field >= value".
hasNestedExpression()  : bool
Returns true if this expression contains any other nested ExpressionInterface objects
in()  : $this
Adds a new condition to the expression object in the form "field IN (value1, value2)".
isCallable()  : bool
Check whether a callable is acceptable.
isNotNull()  : $this
Adds a new condition to the expression object in the form "field IS NOT NULL".
isNull()  : $this
Adds a new condition to the expression object in the form "field IS NULL".
iterateParts()  : $this
Executes a callable function for each of the parts that form this expression.
like()  : $this
Adds a new condition to the expression object in the form "field LIKE value".
lt()  : $this
Adds a new condition to the expression object in the form "field < value".
lte()  : $this
Adds a new condition to the expression object in the form "field <= value".
not()  : $this
Adds a new set of conditions to this level of the tree and negates the final result by prepending a NOT, it will look like "NOT ( (condition1) AND (conditions2) )" conjunction depends on the one currently configured for this object.
notEq()  : $this
Adds a new condition to the expression object in the form "field != value".
notExists()  : $this
Adds a new condition to the expression object in the form "NOT EXISTS (...)".
notIn()  : $this
Adds a new condition to the expression object in the form "field NOT IN (value1, value2)".
notInOrNull()  : $this
Adds a new condition to the expression object in the form "(field NOT IN (value1, value2) OR field IS NULL".
notLike()  : $this
Adds a new condition to the expression object in the form "field NOT LIKE value".
or()  : QueryExpression
Returns a new QueryExpression object containing all the conditions passed and set up the conjunction to be "OR"
or_()  : QueryExpression
Returns a new QueryExpression object containing all the conditions passed and set up the conjunction to be "OR"
setConjunction()  : $this
Changes the conjunction for the conditions at this level of the expression tree.
setDefaultTypes()  : $this
Overwrite the default type mappings for fields in the implementing object.
setTypeMap()  : $this
Creates a new TypeMap if $typeMap is an array, otherwise exchanges it for the given one.
sql()  : string
Converts the Node into a SQL string fragment.
traverse()  : $this
Iterates over each part of the expression recursively for every level of the expressions tree and executes the $callback callable passing as first parameter the instance of the expression currently being iterated.
_addConditions()  : void
Auxiliary function used for decomposing a nested array of conditions and build a tree structure inside this object to represent the full SQL expression.
_calculateType()  : string|null
Returns the type name for the passed field if it was stored in the typeMap
_parseCondition()  : ExpressionInterface
Parses a string conditions by trying to extract the operator inside it if any and finally returning either an adequate QueryExpression object or a plain string representation of the condition. This function is responsible for generating the placeholders and replacing the values by them, while storing the value elsewhere for future binding.

Properties

$_conditions

A list of strings or other expression objects that represent the "branches" of the expression tree. For example one key of the array might look like "sum > :value"

protected array<string|int, mixed> $_conditions = []

$_conjunction

String to be used for joining each of the internal expressions this object internally stores for example "AND", "OR", etc.

protected string $_conjunction

Methods

__clone()

Clone this object and its subtree of expressions.

public __clone() : void

__construct()

Constructor. A new expression object can be created without any params and be built dynamically. Otherwise, it is possible to pass an array of conditions containing either a tree-like array structure to be parsed and/or other expression objects. Optionally, you can set the conjunction keyword to be used for joining each part of this level of the expression tree.

public __construct([ExpressionInterface|array<string|int, mixed>|string $conditions = [] ][, TypeMap|array<string|int, mixed> $types = [] ][, string $conjunction = 'AND' ]) : mixed
Parameters
$conditions : ExpressionInterface|array<string|int, mixed>|string = []

Tree like array structure containing all the conditions to be added or nested inside this expression object.

$types : TypeMap|array<string|int, mixed> = []

Associative array of types to be associated with the values passed in $conditions.

$conjunction : string = 'AND'

the glue that will join all the string conditions at this level of the expression tree. For example "AND", "OR", "XOR"...

Tags
see
QueryExpression::add()

for more details on $conditions and $types

add()

Adds one or more conditions to this expression object. Conditions can be expressed in a one dimensional array, that will cause all conditions to be added directly at this level of the tree or they can be nested arbitrarily making it create more expression objects that will be nested inside and configured to use the specified conjunction.

public add(ExpressionInterface|array<string|int, mixed>|string $conditions[, array<int|string, string> $types = [] ]) : $this

If the type passed for any of the fields is expressed "type[]" (note braces) then it will cause the placeholder to be re-written dynamically so if the value is an array, it will create as many placeholders as values are in it.

Parameters
$conditions : ExpressionInterface|array<string|int, mixed>|string

single or multiple conditions to be added. When using an array and the key is 'OR' or 'AND' a new expression object will be created with that conjunction and internal array value passed as conditions.

$types : array<int|string, string> = []

Associative array of fields pointing to the type of the values that are being passed. Used for correctly binding values to statements.

Tags
see
Query::where()

for examples on conditions

Return values
$this

addCase()

Adds a new case expression to the expression object

public addCase(ExpressionInterface|array<string|int, mixed> $conditions[, ExpressionInterface|array<string|int, mixed> $values = [] ][, array<string|int, string> $types = [] ]) : $this

Use QueryExpression::case() or CaseStatementExpression instead

Parameters
$conditions : ExpressionInterface|array<string|int, mixed>

The conditions to test. Must be a ExpressionInterface instance, or an array of ExpressionInterface instances.

$values : ExpressionInterface|array<string|int, mixed> = []

Associative array of values to be associated with the conditions passed in $conditions. If there are more $values than $conditions, the last $value is used as the ELSE value.

$types : array<string|int, string> = []

Associative array of types to be associated with the values passed in $values

Return values
$this

and()

Returns a new QueryExpression object containing all the conditions passed and set up the conjunction to be "AND"

public and(ExpressionInterface|Closure|array<string|int, mixed>|string $conditions[, array<string, string> $types = [] ]) : QueryExpression
Parameters
$conditions : ExpressionInterface|Closure|array<string|int, mixed>|string

to be joined with AND

$types : array<string, string> = []

Associative array of fields pointing to the type of the values that are being passed. Used for correctly binding values to statements.

Return values
QueryExpression

and_()

Returns a new QueryExpression object containing all the conditions passed and set up the conjunction to be "AND"

public and_(ExpressionInterface|Closure|array<string|int, mixed>|string $conditions[, array<string, string> $types = [] ]) : QueryExpression

Use instead.

Parameters
$conditions : ExpressionInterface|Closure|array<string|int, mixed>|string

to be joined with AND

$types : array<string, string> = []

Associative array of fields pointing to the type of the values that are being passed. Used for correctly binding values to statements.

Return values
QueryExpression

between()

Adds a new condition to the expression object in the form "field BETWEEN from AND to".

public between(ExpressionInterface|string $field, mixed $from, mixed $to[, string|null $type = null ]) : $this
Parameters
$field : ExpressionInterface|string

The field name to compare for values inbetween the range.

$from : mixed

The initial value of the range.

$to : mixed

The ending value in the comparison range.

$type : string|null = null

the type name for $value as configured using the Type map.

Return values
$this

case()

Returns a new case expression object.

public case([ExpressionInterface|object|scalar|null $value = null ][, string|null $type = null ]) : CaseStatementExpression

When a value is set, the syntax generated is CASE case_value WHEN when_value ... END (simple case), where the when_value's are compared against the case_value.

When no value is set, the syntax generated is CASE WHEN when_conditions ... END (searched case), where the conditions hold the comparisons.

Note that null is a valid case value, and thus should only be passed if you actually want to create the simple case expression variant!

Parameters
$value : ExpressionInterface|object|scalar|null = null

The case value.

$type : string|null = null

The case value type. If no type is provided, the type will be tried to be inferred from the value.

Return values
CaseStatementExpression

count()

Returns the number of internal conditions that are stored in this expression.

public count() : int

Useful to determine if this expression object is void or it will generate a non-empty string when compiled

Return values
int

eq()

Adds a new condition to the expression object in the form "field = value".

public eq(ExpressionInterface|string $field, mixed $value[, string|null $type = null ]) : $this
Parameters
$field : ExpressionInterface|string

Database field to be compared against value

$value : mixed

The value to be bound to $field for comparison

$type : string|null = null

the type name for $value as configured using the Type map. If it is suffixed with "[]" and the value is an array then multiple placeholders will be created, one per each value in the array.

Return values
$this

equalFields()

Builds equal condition or assignment with identifier wrapping.

public equalFields(string $leftField, string $rightField) : $this
Parameters
$leftField : string

Left join condition field name.

$rightField : string

Right join condition field name.

Return values
$this

getConjunction()

Gets the currently configured conjunction for the conditions at this level of the expression tree.

public getConjunction() : string
Return values
string

getDefaultTypes()

Gets default types of current type map.

public getDefaultTypes() : array<int|string, string>
Return values
array<int|string, string>

gt()

Adds a new condition to the expression object in the form "field > value".

public gt(ExpressionInterface|string $field, mixed $value[, string|null $type = null ]) : $this
Parameters
$field : ExpressionInterface|string

Database field to be compared against value

$value : mixed

The value to be bound to $field for comparison

$type : string|null = null

the type name for $value as configured using the Type map.

Return values
$this

gte()

Adds a new condition to the expression object in the form "field >= value".

public gte(ExpressionInterface|string $field, mixed $value[, string|null $type = null ]) : $this
Parameters
$field : ExpressionInterface|string

Database field to be compared against value

$value : mixed

The value to be bound to $field for comparison

$type : string|null = null

the type name for $value as configured using the Type map.

Return values
$this

hasNestedExpression()

Returns true if this expression contains any other nested ExpressionInterface objects

public hasNestedExpression() : bool
Return values
bool

in()

Adds a new condition to the expression object in the form "field IN (value1, value2)".

public in(ExpressionInterface|string $field, ExpressionInterface|array<string|int, mixed>|string $values[, string|null $type = null ]) : $this
Parameters
$field : ExpressionInterface|string

Database field to be compared against value

$values : ExpressionInterface|array<string|int, mixed>|string

the value to be bound to $field for comparison

$type : string|null = null

the type name for $value as configured using the Type map.

Return values
$this

isCallable()

Check whether a callable is acceptable.

public isCallable(ExpressionInterface|callable|array<string|int, mixed>|string $callable) : bool

This method is unused.

We don't accept ['class', 'method'] style callbacks, as they often contain user input and arrays of strings are easy to sneak in.

Parameters
$callable : ExpressionInterface|callable|array<string|int, mixed>|string

The callable to check.

Tags
codeCoverageIgnore
Return values
bool

Valid callable.

isNotNull()

Adds a new condition to the expression object in the form "field IS NOT NULL".

public isNotNull(ExpressionInterface|string $field) : $this
Parameters
$field : ExpressionInterface|string

database field to be tested for not null

Return values
$this

iterateParts()

Executes a callable function for each of the parts that form this expression.

public iterateParts(callable $callback) : $this

The callable function is required to return a value with which the currently visited part will be replaced. If the callable function returns null then the part will be discarded completely from this expression.

The callback function will receive each of the conditions as first param and the key as second param. It is possible to declare the second parameter as passed by reference, this will enable you to change the key under which the modified part is stored.

Parameters
$callback : callable

The callable to apply to each part.

Return values
$this

like()

Adds a new condition to the expression object in the form "field LIKE value".

public like(ExpressionInterface|string $field, mixed $value[, string|null $type = null ]) : $this
Parameters
$field : ExpressionInterface|string

Database field to be compared against value

$value : mixed

The value to be bound to $field for comparison

$type : string|null = null

the type name for $value as configured using the Type map.

Return values
$this

lt()

Adds a new condition to the expression object in the form "field < value".

public lt(ExpressionInterface|string $field, mixed $value[, string|null $type = null ]) : $this
Parameters
$field : ExpressionInterface|string

Database field to be compared against value

$value : mixed

The value to be bound to $field for comparison

$type : string|null = null

the type name for $value as configured using the Type map.

Return values
$this

lte()

Adds a new condition to the expression object in the form "field <= value".

public lte(ExpressionInterface|string $field, mixed $value[, string|null $type = null ]) : $this
Parameters
$field : ExpressionInterface|string

Database field to be compared against value

$value : mixed

The value to be bound to $field for comparison

$type : string|null = null

the type name for $value as configured using the Type map.

Return values
$this

not()

Adds a new set of conditions to this level of the tree and negates the final result by prepending a NOT, it will look like "NOT ( (condition1) AND (conditions2) )" conjunction depends on the one currently configured for this object.

public not(ExpressionInterface|Closure|array<string|int, mixed>|string $conditions[, array<string, string> $types = [] ]) : $this
Parameters
$conditions : ExpressionInterface|Closure|array<string|int, mixed>|string

to be added and negated

$types : array<string, string> = []

Associative array of fields pointing to the type of the values that are being passed. Used for correctly binding values to statements.

Return values
$this

notEq()

Adds a new condition to the expression object in the form "field != value".

public notEq(ExpressionInterface|string $field, mixed $value[, string|null $type = null ]) : $this
Parameters
$field : ExpressionInterface|string

Database field to be compared against value

$value : mixed

The value to be bound to $field for comparison

$type : string|null = null

the type name for $value as configured using the Type map. If it is suffixed with "[]" and the value is an array then multiple placeholders will be created, one per each value in the array.

Return values
$this

notIn()

Adds a new condition to the expression object in the form "field NOT IN (value1, value2)".

public notIn(ExpressionInterface|string $field, ExpressionInterface|array<string|int, mixed>|string $values[, string|null $type = null ]) : $this
Parameters
$field : ExpressionInterface|string

Database field to be compared against value

$values : ExpressionInterface|array<string|int, mixed>|string

the value to be bound to $field for comparison

$type : string|null = null

the type name for $value as configured using the Type map.

Return values
$this

notInOrNull()

Adds a new condition to the expression object in the form "(field NOT IN (value1, value2) OR field IS NULL".

public notInOrNull(ExpressionInterface|string $field, ExpressionInterface|array<string|int, mixed>|string $values[, string|null $type = null ]) : $this
Parameters
$field : ExpressionInterface|string

Database field to be compared against value

$values : ExpressionInterface|array<string|int, mixed>|string

the value to be bound to $field for comparison

$type : string|null = null

the type name for $value as configured using the Type map.

Return values
$this

notLike()

Adds a new condition to the expression object in the form "field NOT LIKE value".

public notLike(ExpressionInterface|string $field, mixed $value[, string|null $type = null ]) : $this
Parameters
$field : ExpressionInterface|string

Database field to be compared against value

$value : mixed

The value to be bound to $field for comparison

$type : string|null = null

the type name for $value as configured using the Type map.

Return values
$this

or()

Returns a new QueryExpression object containing all the conditions passed and set up the conjunction to be "OR"

public or(ExpressionInterface|Closure|array<string|int, mixed>|string $conditions[, array<string, string> $types = [] ]) : QueryExpression
Parameters
$conditions : ExpressionInterface|Closure|array<string|int, mixed>|string

to be joined with OR

$types : array<string, string> = []

Associative array of fields pointing to the type of the values that are being passed. Used for correctly binding values to statements.

Return values
QueryExpression

or_()

Returns a new QueryExpression object containing all the conditions passed and set up the conjunction to be "OR"

public or_(ExpressionInterface|Closure|array<string|int, mixed>|string $conditions[, array<string, string> $types = [] ]) : QueryExpression

Use instead.

Parameters
$conditions : ExpressionInterface|Closure|array<string|int, mixed>|string

to be joined with OR

$types : array<string, string> = []

Associative array of fields pointing to the type of the values that are being passed. Used for correctly binding values to statements.

Return values
QueryExpression

setConjunction()

Changes the conjunction for the conditions at this level of the expression tree.

public setConjunction(string $conjunction) : $this
Parameters
$conjunction : string

Value to be used for joining conditions

Return values
$this

setDefaultTypes()

Overwrite the default type mappings for fields in the implementing object.

public setDefaultTypes(array<int|string, string> $types) : $this

This method is useful if you need to set type mappings that are shared across multiple functions/expressions in a query.

To add a default without overwriting existing ones use getTypeMap()->addDefaults()

Parameters
$types : array<int|string, string>

The array of types to set.

Tags
see
TypeMap::setDefaults()
Return values
$this

setTypeMap()

Creates a new TypeMap if $typeMap is an array, otherwise exchanges it for the given one.

public setTypeMap(TypeMap|array<string|int, mixed> $typeMap) : $this
Parameters
$typeMap : TypeMap|array<string|int, mixed>

Creates a TypeMap if array, otherwise sets the given TypeMap

Return values
$this

sql()

Converts the Node into a SQL string fragment.

public sql(ValueBinder $binder) : string
Parameters
$binder : ValueBinder

Parameter binder

Tags
inheritDoc
Return values
string

traverse()

Iterates over each part of the expression recursively for every level of the expressions tree and executes the $callback callable passing as first parameter the instance of the expression currently being iterated.

public traverse(Closure $callback) : $this
Parameters
$callback : Closure

The callable to apply to all nodes.

Tags
inheritDoc
Return values
$this

_addConditions()

Auxiliary function used for decomposing a nested array of conditions and build a tree structure inside this object to represent the full SQL expression.

protected _addConditions(array<string|int, mixed> $conditions, array<int|string, string> $types) : void

String conditions are stored directly in the conditions, while any other representation is wrapped around an adequate instance or of this class.

Parameters
$conditions : array<string|int, mixed>

list of conditions to be stored in this object

$types : array<int|string, string>

list of types associated on fields referenced in $conditions

_calculateType()

Returns the type name for the passed field if it was stored in the typeMap

protected _calculateType(ExpressionInterface|string $field) : string|null
Parameters
$field : ExpressionInterface|string

The field name to get a type for.

Return values
string|null

The computed type or null, if the type is unknown.

_parseCondition()

Parses a string conditions by trying to extract the operator inside it if any and finally returning either an adequate QueryExpression object or a plain string representation of the condition. This function is responsible for generating the placeholders and replacing the values by them, while storing the value elsewhere for future binding.

protected _parseCondition(string $field, mixed $value) : ExpressionInterface
Parameters
$field : string

The value from which the actual field and operator will be extracted.

$value : mixed

The value to be bound to a placeholder for the field

Tags
throws
InvalidArgumentException

If operator is invalid or missing on NULL usage.

Return values
ExpressionInterface

        
On this page

Search results