- 🇷🇺Russia Chi
It there a way to escape brackets in SQL query? Seems like this change imposed severe restrictions on using JSON and array data types in Drupal.
Over many years Drupal has held off checking queries for reserved words in column names, table names and aliases because it was a design decision. The idea is that we just just should not use reserved words. However, time has made it clear that this position (as stated in the long discussions in #371: resolve ANSI SQL-92/99/2003 reserved words conflict in query statements → ) is not really tenable. Here are the problems:
OFFSET
is reserved in PostgreSQL but not in MySQL[COLUMN_NAME]
to match {TABLE_NAME}
. This supports db_query()
, addExpression()
and addWhere()
[]
to identify identifiers (field names / aliases)There is precedence - this is the quote character in MSSQL.
The one consideration is that :variable[]
is used to identify and expand array variables.
We could deprecate SELECT, UPDATE, INSERT queries via \Drupal\Core\Database\Connection::query()
since the methods like \Drupal\Core\Database\Connection::select()
require fields to be specified. If we do that we have to worry about performance - see
#1067802: Compare $connection->query() and $connection->select() performance →
. Also this does not work for columns added using ->addExpression()
or ->addWhere()
. Therefore this resolution has been discarded.
File follow-ups:
[]
None
At the moment \Drupal\Core\Database\Connection::escapeField()
and \Drupal\Core\Database\Connection::escapeAlias()
in their core implementations are re-entrant. And some core code relies on this. That said the docs hint that this should not be case. In the new implementation methods also now quote identifiers BUT because the code removes the quotes before adding them they still are re-entrant. The new implementations result in strings like users_data.uid
becoming "users_data"."uid"
.
\Drupal\Core\Database\Query\ConditionInterface::condition()
's first argument $field
must be a field and cannot be (ab)used to do ->condition('1 <> 1')
. The way to do this is to use the new API added by
#2986334: Add a way to enforce an empty result set for a database condition query →
\Drupal\Core\Database\Connection::prepareQuery() now takes additionally takes a boolean that is derived from a query's $options['allow_square_brackets']
value that allows specific queries to use square brackets and to not them for identifier quotes. The default value for $options['allow_square_brackets']
is FALSE
.
\Drupal\Core\Database\Connection::prefixTables
now quotes tables so {node}
becomes "node"
.
None
All identifiers (table names, database names, field names and aliases) are quoted in database queries. Column names or aliases should be wrapped in square brackets when using Connection::query()
, db_query()
, ConditionInterface::where()
or SelectInterface::addExpression
. Custom or contrib database drivers should read
https://www.drupal.org/node/2986894 →
to see what changes are necessary.
Fixed
9.0
It denotes an issue that prevents porting of a contributed project to the stable version of Drupal due to missing APIs, regressions, and so on.
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.
It there a way to escape brackets in SQL query? Seems like this change imposed severe restrictions on using JSON and array data types in Drupal.