- class Atk4\Data\Persistence\Sql\Expression
Expressions
Expression class implements a flexible way for you to define any custom expression then execute it as-is or as a part of another query or expression. Expression is supported anywhere in DSQL to allow you to express SQL syntax properly.
Quick Example:
$query->where('time', $query->expr(
'between "[]" and "[]"',
[$fromTime, $toTime]
));
// produces: .. where `time` between :a and :b
Another use of expression is to supply field instead of value and vice versa:
$query->where($query->expr(
'[] between time_from and time_to',
[$time]
));
// produces: where :a between time_from and time_to
Yet another curious use for the DSQL library is if you have certain object in
your ORM implementing Expressionable
interface. Then you can also
use it within expressions:
$query->where($query->expr(
'[] between [] and []',
[$time, $model->getElement('time_form'), $model->getElement('time_to')]
));
// produces: where :a between `time_from` and `time_to`
Another uses for expressions could be:
Sub-Queries
SQL functions, e.g. IF, CASE
nested AND / OR clauses
vendor-specific queries - “describe table”
non-traditional constructions, UNIONS or SELECT INTO
Properties, Arguments, Parameters
Be careful when using those similar terms as they refer to different things:
Properties refer to object properties, e.g.
$expr->template
, see Other PropertiesArguments refer to template arguments, e.g.
select * from [table]
, see Expression TemplateParameters refer to the way of passing user values within a query
where id=:a
and are further explained below.
Parameters
Because some values are un-safe to use in the query and can contain dangerous values they are kept outside of the SQL query string and are using PDO’s bindValue instead. DSQL can consist of multiple objects and each object may have some parameters. During rendering those parameters are joined together to produce one complete query.
Creating Expression
$expr = $connection->expr('NOW()');
You can also use expr()
method to create expression, in which case
you do not have to define “use” block:
$query->where('time', '>', $query->expr('NOW()'));
// produces: .. where `time` > NOW()
You can specify some of the expression properties through first argument of the constructor:
$expr = $connection->expr(['template' => 'NOW()']);
Scroll down for full list of properties.
Expression Template
When you create a template the first argument is the template. It will be stored
in $template
property. Template string can contain arguments in a
square brackets:
coalesce([], [])
is same ascoalesce([0], [1])
coalesce([one], [two])
Arguments can be specified immediately through an array as a second argument into constructor or you can specify arguments later:
$expr = $connection->expr(
'coalesce([name], [surname])',
['name' => $name, 'surname' => $surname]
);
// is the same as
$expr = $connection->expr('coalesce([name], [surname])');
$expr['name'] = $name;
$expr['surname'] = $surname;
Nested expressions
Expressions can be nested several times:
$age = $connection->expr('coalesce([age], [default_age])');
$age['age'] = $connection->expr("year(now()) - year(birth_date)");
$age['default_age'] = 18;
$query->table('user')->field($age, 'calculated_age');
// select coalesce(year(now()) - year(birth_date), :a) `calculated_age` from `user`
When you include one query into another query, it will automatically take care
of all user-defined parameters (such as value 18
above) which will make sure
that SQL injections could not be introduced at any stage.
Rendering
An expression can be rendered into a valid SQL code by calling render() method. The method will return an array with string and params.
- Atk4\Data\Persistence\Sql\Expression::render()
Converts
Expression
object to an array with string and params. Parameters are replaced with :a, :b, etc.
Executing Expressions
If your expression is a valid SQL query, (such as show databases
) you
might want to execute it. Expression class offers you various ways to execute
your expression. Before you do, however, you need to have $connection
property set. (See Connecting to Database
on more details). In short the
following code will connect your expression with the database:
$expr = $connection->expr();
If you are looking to use connection Query
class, you may want to
consider using a proper vendor-specific subclass:
$query = new \Atk4\Data\Persistence\Sql\Mysql\Query('connection' => $connection);
Finally, you can pass connection class into executeQuery
directly.
- Atk4\Data\Persistence\Sql\Expression::executeQuery($connection = null)
Executes expression using current database connection or the one you specify as the argument:
$stmt = $expr->executeQuery($connection);
returns
Doctrine\DBAL\Result
.
- Atk4\Data\Persistence\Sql\Expression::expr($template, $arguments)
Creates a new
Expression
object that will inherit current$connection
property. Also if you are creating a vendor-specific expression/query support, this method must return instance of your own version of Expression class.The main principle here is that the new object must be capable of working with database connection.
- Atk4\Data\Persistence\Sql\Expression::getRows()
Executes expression and return whole result-set in form of array of hashes:
$data = $connection->expr('show databases')->getRows(); echo json_encode($data);
The output would be
[ { "Database": "mydb1" }, { "Database": "mysql" }, { "Database": "test" }, ]
- Atk4\Data\Persistence\Sql\Expression::getRow()
Executes expression and returns first row of data from result-set as a hash:
$data = $connection->expr('SELECT @@global.time_zone, @@session.time_zone')->getRow() echo json_encode($data);
The output would be
{ "@@global.time_zone": "SYSTEM", "@@session.time_zone": "SYSTEM" }
- Atk4\Data\Persistence\Sql\Expression::getOne()
Executes expression and return first value of first row of data from result-set:
$time = $connection->expr('NOW()')->getOne();
Magic an Debug Methods
- Atk4\Data\Persistence\Sql\Expression::__debugInfo()
This method is used to prepare a sensible information about your query when you are executing
var_dump($expr)
. The output will be HTML-safe.
- Atk4\Data\Persistence\Sql\Expression::getDebugQuery()
Outputs query as a string by placing parameters into their respective places. The parameters will be escaped, but you should still avoid using generated query as it can potentially make you vulnerable to SQL injection.
This method will use HTML formatting if argument is passed.
In order for HTML parsing to work and to make your debug queries better
formatted, install doctrine/sql-formatter
:
composer require doctrine/sql-formatter
Escaping Methods
The following methods are useful if you’re building your own code for rendering parts of the query. You must not call them in normal circumstances.
- Atk4\Data\Persistence\Sql\Expression::consume($expr, string $escapeMode)
Makes
$expr
part of$this
expression. Argument may be either a string (which will be escaped) or anotherExpression
orQuery
. If specifiedQuery
is in “select” mode, then it’s automatically placed inside parentheses:$query->consume('first_name', Expression::ESCAPE_PARAM); // :a $query->consume('first_name', Expression::ESCAPE_IDENTIFIER); // `first_name` $query->consume($otherQuery, Expression::ESCAPE_PARAM); // will merge parameters and return string
- Atk4\Data\Persistence\Sql\Expression::escapeIdentifier(string $value)
Always surrounds
$value
with back-ticks.This escaping method is automatically used for
{...}
expression template tags .
- Atk4\Data\Persistence\Sql\Expression::escapeIdentifierSoft(string $value)
Surrounds
$value
with back-ticks.This escaping method is automatically used for
{{...}}
expression template tags .It will smartly escape table.field type of strings resulting in
table
.field
.Will do nothing if it finds “*”, “
" or "(" character in
$value`:$query->escapeIdentifierSoft('first_name'); // `first_name` $query->escapeIdentifierSoft('first.name'); // `first`.`name` $query->escapeIdentifierSoft('(2 + 2)'); // (2 + 2) $query->escapeIdentifierSoft('*'); // *
- Atk4\Data\Persistence\Sql\Expression::escapeParam($value)
Converts value into parameter and returns reference. Used only during query rendering. Consider using
consume()
instead, which will also handle nested expressions properly.This escaping method is automatically used for
[...]
expression template tags .
Other Properties
- property Atk4\Data\Persistence\Sql\Expression::$template
Template which is used when rendering. You can set this with either
$connection->expr('show tables')
or$connection->expr(['show tables'])
or$connection->expr(['template' => 'show tables'])
.
- property Atk4\Data\Persistence\Sql\Expression::$connection
DB connection object.
- property Atk4\Data\Persistence\Sql\Expression::$paramBase
Normally parameters are named :a, :b, :c. You can specify a different param base such as :param_00 and it will be automatically increased into :param_01 etc.
- property Atk4\Data\Persistence\Sql\Expression::$debug
If true, then next call of
execute
willecho
results ofgetDebugQuery
.