Class FunctionExpression
This class represents a function call string in a SQL statement. Calls can be constructed by passing the name of the function and a list of params. For security reasons, all params passed are quoted by default unless explicitly told otherwise.
Property Summary
-
$_conditions protected
array
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 protected
string
String to be used for joining each of the internal expressions this object internally stores for example "AND", "OR", etc.
-
$_name protected
string
The name of the function to be constructed when generating the SQL string
-
$_typeMap protected
Cake\Database\TypeMap
Method Summary
-
__call() public
Helps calling the
and()
andor()
methods transparently. -
__construct() public
Constructor. Takes a name for the function to be invoked and a list of params to be passed into the function. Optionally you can pass a list of types to be used for each bound param.
-
_addConditions() protected
Auxiliary function used for decomposing a nested array of conditions and build a tree structure inside this object to represent the full SQL expression. String conditions are stored directly in the conditions, while any other representation is wrapped around an adequate instance or of this class.
-
_bindMultiplePlaceholders() protected
Returns an array of placeholders that will have a bound value corresponding to each value in the first argument.
-
_parseCondition() protected
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.
-
add() public
Adds one or more arguments for the function call.
-
addCase() public
Adds a new case expression to the expression object
-
and_() public
Returns a new QueryExpression object containing all the conditions passed and set up the conjunction to be "AND"
-
between() public
Adds a new condition to the expression object in the form "field BETWEEN from AND to".
-
count() public
Returns the number of internal conditions that are stored in this expression. Useful to determine if this expression object is void or it will generate a non-empty string when compiled
-
defaultTypes() public
Allows setting default types when chaining query
-
eq() public
Adds a new condition to the expression object in the form "field = value".
-
gt() public
Adds a new condition to the expression object in the form "field > value".
-
gte() public
Adds a new condition to the expression object in the form "field >= value".
-
in() public
Adds a new condition to the expression object in the form "field IN (value1, value2)".
-
isCallable() public
Check whether or not a callable is acceptable.
-
isNotNull() public
Adds a new condition to the expression object in the form "field IS NOT NULL".
-
isNull() public
Adds a new condition to the expression object in the form "field IS NULL".
-
iterateParts() public
Executes a callable function for each of the parts that form this expression.
-
like() public
Adds a new condition to the expression object in the form "field LIKE value".
-
lt() public
Adds a new condition to the expression object in the form "field < value".
-
lte() public
Adds a new condition to the expression object in the form "field <= value".
-
name() public
Sets the name of the SQL function to be invoke in this expression, if no value is passed it will return current name
-
not() public
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() public
Adds a new condition to the expression object in the form "field != value".
-
notIn() public
Adds a new condition to the expression object in the form "field NOT IN (value1, value2)".
-
notLike() public
Adds a new condition to the expression object in the form "field NOT LIKE value".
-
or_() public
Returns a new QueryExpression object containing all the conditions passed and set up the conjunction to be "OR"
-
sql() public
Returns the string representation of this object so that it can be used in a SQL query. Note that values condition values are not included in the string, in their place placeholders are put and can be replaced by the quoted values accordingly.
-
traverse() public
Traverses the tree structure of this query expression by executing a callback function for each of the conditions that are included in this object. Useful for compiling the final expression, or doing introspection in the structure.
-
type() public
Changes the conjunction for the conditions at this level of the expression tree. If called with no arguments it will return the currently configured value.
-
typeMap() public
Creates a new TypeMap if $typeMap is an array, otherwise returns the existing type map or exchanges it for the given one.
Method Detail
__call() ¶ public
__call(string $method, array $args): Cake\Database\Expression\QueryExpression
Helps calling the and()
and or()
methods transparently.
Parameters
-
string
$method The method name.
-
array
$args The arguments to pass to the method.
Returns
Cake\Database\Expression\QueryExpression
Throws
BadMethodCallException
__construct() ¶ public
__construct(string $name, array $params = [], arrayCake\Database\TypeMap $types = [])
Constructor. Takes a name for the function to be invoked and a list of params to be passed into the function. Optionally you can pass a list of types to be used for each bound param.
By default, all params that are passed will be quoted. If you wish to use literal arguments, you need to explicitly hint this function.
Examples:
$f = new FunctionExpression('CONCAT', ['CakePHP', ' rules']);
Previous line will generate CONCAT('CakePHP', ' rules')
$f = new FunctionExpression('CONCAT', ['name' => 'literal', ' rules']);
Will produce CONCAT(name, ' rules')
Parameters
-
string
$name the name of the function to be constructed
-
array
$params optional list of arguments to be passed to the function If associative the key would be used as argument when value is 'literal'
-
arrayCake\Database\TypeMap
$types optional associative array of types to be associated with the passed arguments
_addConditions() ¶ protected
_addConditions(array $conditions, array $types): 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. String conditions are stored directly in the conditions, while any other representation is wrapped around an adequate instance or of this class.
Parameters
-
array
$conditions list of conditions to be stored in this object
-
array
$types list of types associated on fields referenced in $conditions
Returns
void
_bindMultiplePlaceholders() ¶ protected
_bindMultiplePlaceholders(string $field, array $values, string $type): array
Returns an array of placeholders that will have a bound value corresponding to each value in the first argument.
Parameters
-
string
$field Database field to be used to bind values
-
array
$values The values to bind
-
string
$type the type to be used to bind the values
Returns
array
_parseCondition() ¶ protected
_parseCondition(string $field, mixed $value): stringCake\Database\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.
Parameters
-
string
$field The value from with the actual field and operator will be extracted.
-
mixed
$value The value to be bound to a placeholder for the field
Returns
stringCake\Database\ExpressionInterface
add() ¶ public
add(array $params, array $types = [], bool $prepend = false): $this
Adds one or more arguments for the function call.
Parameters
-
array
$params list of arguments to be passed to the function If associative the key would be used as argument when value is 'literal'
-
array
$types optional associative array of types to be associated with the passed arguments
-
bool
$prepend optional Whether to prepend or append to the list of arguments
Returns
$this
See Also
addCase() ¶ public
addCase(array|ExpressionInterface $conditions, array|ExpressionInterface $values = [], array $types = []): $this
Adds a new case expression to the expression object
Parameters
-
array|ExpressionInterface
$conditions The conditions to test. Must be a ExpressionInterface instance, or an array of ExpressionInterface instances.
-
array|ExpressionInterface
$values optional 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-
array
$types optional associative array of types to be associated with the values passed in $values
Returns
$this
and_() ¶ public
and_(string|array|QueryExpression $conditions, array $types = []): Cake\Database\Expression\QueryExpression
Returns a new QueryExpression object containing all the conditions passed and set up the conjunction to be "AND"
Parameters
-
string|array|QueryExpression
$conditions to be joined with AND
-
array
$types optional associative array of fields pointing to the type of the values that are being passed. Used for correctly binding values to statements.
Returns
Cake\Database\Expression\QueryExpression
between() ¶ public
between(mixed $field, mixed $from, mixed $to, string $type = null): $this
Adds a new condition to the expression object in the form "field BETWEEN from AND to".
Parameters
-
mixed
$field The field name to compare for values in between the range.
-
mixed
$from The initial value of the range.
-
mixed
$to The ending value in the comparison range.
-
string
$type optional the type name for $value as configured using the Type map.
Returns
$this
count() ¶ public
count(): int
Returns the number of internal conditions that are stored in this expression. Useful to determine if this expression object is void or it will generate a non-empty string when compiled
Returns
int
defaultTypes() ¶ public
defaultTypes(array $types = null): $this|array
Allows setting default types when chaining query
Parameters
-
array
$types optional The array of types to set.
Returns
$this|array
eq() ¶ public
eq(string $field, mixed $value, string $type = null): $this
Adds a new condition to the expression object in the form "field = value".
Parameters
-
string
$field Database field to be compared against value
-
mixed
$value The value to be bound to $field for comparison
-
string
$type optional 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.
Returns
$this
gt() ¶ public
gt(string $field, mixed $value, string $type = null): $this
Adds a new condition to the expression object in the form "field > value".
Parameters
-
string
$field Database field to be compared against value
-
mixed
$value The value to be bound to $field for comparison
-
string
$type optional the type name for $value as configured using the Type map.
Returns
$this
gte() ¶ public
gte(string $field, mixed $value, string $type = null): $this
Adds a new condition to the expression object in the form "field >= value".
Parameters
-
string
$field Database field to be compared against value
-
mixed
$value The value to be bound to $field for comparison
-
string
$type optional the type name for $value as configured using the Type map.
Returns
$this
in() ¶ public
in(string $field, string|array $values, string $type = null): $this
Adds a new condition to the expression object in the form "field IN (value1, value2)".
Parameters
-
string
$field Database field to be compared against value
-
string|array
$values the value to be bound to $field for comparison
-
string
$type optional the type name for $value as configured using the Type map.
Returns
$this
isCallable() ¶ public
isCallable(callable $c): bool
Check whether or not a callable is acceptable.
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
$c The callable to check.
Returns
bool
isNotNull() ¶ public
isNotNull(stringCake\Database\ExpressionInterface $field): $this
Adds a new condition to the expression object in the form "field IS NOT NULL".
Parameters
-
stringCake\Database\ExpressionInterface
$field database field to be tested for not null
Returns
$this
isNull() ¶ public
isNull(stringCake\Database\ExpressionInterface $field): $this
Adds a new condition to the expression object in the form "field IS NULL".
Parameters
-
stringCake\Database\ExpressionInterface
$field database field to be tested for null
Returns
$this
iterateParts() ¶ public
iterateParts(callable $callable): $this
Executes a callable function for each of the parts that form this expression.
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
-
callable
$callable The callable to apply to each part.
Returns
$this
like() ¶ public
like(string $field, mixed $value, string $type = null): $this
Adds a new condition to the expression object in the form "field LIKE value".
Parameters
-
string
$field Database field to be compared against value
-
mixed
$value The value to be bound to $field for comparison
-
string
$type optional the type name for $value as configured using the Type map.
Returns
$this
lt() ¶ public
lt(string $field, mixed $value, string $type = null): $this
Adds a new condition to the expression object in the form "field < value".
Parameters
-
string
$field Database field to be compared against value
-
mixed
$value The value to be bound to $field for comparison
-
string
$type optional the type name for $value as configured using the Type map.
Returns
$this
lte() ¶ public
lte(string $field, mixed $value, string $type = null): $this
Adds a new condition to the expression object in the form "field <= value".
Parameters
-
string
$field Database field to be compared against value
-
mixed
$value The value to be bound to $field for comparison
-
string
$type optional the type name for $value as configured using the Type map.
Returns
$this
name() ¶ public
name(string $name = null): string|$this
Sets the name of the SQL function to be invoke in this expression, if no value is passed it will return current name
Parameters
-
string
$name optional The name of the function
Returns
string|$this
not() ¶ public
not(string|array|QueryExpression $conditions, array $types = []): $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.
Parameters
-
string|array|QueryExpression
$conditions to be added and negated
-
array
$types optional associative array of fields pointing to the type of the values that are being passed. Used for correctly binding values to statements.
Returns
$this
notEq() ¶ public
notEq(string $field, mixed $value, string $type = null): $this
Adds a new condition to the expression object in the form "field != value".
Parameters
-
string
$field Database field to be compared against value
-
mixed
$value The value to be bound to $field for comparison
-
string
$type optional 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.
Returns
$this
notIn() ¶ public
notIn(string $field, array $values, string $type = null): $this
Adds a new condition to the expression object in the form "field NOT IN (value1, value2)".
Parameters
-
string
$field Database field to be compared against value
-
array
$values the value to be bound to $field for comparison
-
string
$type optional the type name for $value as configured using the Type map.
Returns
$this
notLike() ¶ public
notLike(string $field, mixed $value, string $type = null): $this
Adds a new condition to the expression object in the form "field NOT LIKE value".
Parameters
-
string
$field Database field to be compared against value
-
mixed
$value The value to be bound to $field for comparison
-
string
$type optional the type name for $value as configured using the Type map.
Returns
$this
or_() ¶ public
or_(string|array|QueryExpression $conditions, array $types = []): Cake\Database\Expression\QueryExpression
Returns a new QueryExpression object containing all the conditions passed and set up the conjunction to be "OR"
Parameters
-
string|array|QueryExpression
$conditions to be joined with OR
-
array
$types optional associative array of fields pointing to the type of the values that are being passed. Used for correctly binding values to statements.
Returns
Cake\Database\Expression\QueryExpression
sql() ¶ public
sql(Cake\Database\ValueBinder $generator): string
Returns the string representation of this object so that it can be used in a SQL query. Note that values condition values are not included in the string, in their place placeholders are put and can be replaced by the quoted values accordingly.
Parameters
-
Cake\Database\ValueBinder
$generator Placeholder generator object
Returns
string
traverse() ¶ public
traverse(callable $callable): void
Traverses the tree structure of this query expression by executing a callback function for each of the conditions that are included in this object. Useful for compiling the final expression, or doing introspection in the structure.
Callback function receives as only argument an instance of a QueryExpression
Parameters
-
callable
$callable The callable to apply to all sub-expressions.
Returns
void
type() ¶ public
type(string $conjunction = null): string|$this
Changes the conjunction for the conditions at this level of the expression tree. If called with no arguments it will return the currently configured value.
Parameters
-
string
$conjunction optional value to be used for joining conditions. If null it will not set any value, but return the currently stored one
Returns
string|$this
typeMap() ¶ public
typeMap(array|TypeMap $typeMap = null): $this|TypeMap
Creates a new TypeMap if $typeMap is an array, otherwise returns the existing type map or exchanges it for the given one.
Parameters
-
array|TypeMap
$typeMap optional Creates a TypeMap if array, otherwise sets the given TypeMap
Returns
$this|TypeMap
Property Detail
$_conditions ¶ protected
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"
Type
array
$_conjunction ¶ protected
String to be used for joining each of the internal expressions this object internally stores for example "AND", "OR", etc.
Type
string
$_name ¶ protected
The name of the function to be constructed when generating the SQL string
Type
string