function Connection::prepareStatement
Same name in this branch
- 11.x core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::prepareStatement()
- 11.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::prepareStatement()
Same name and namespace in other branches
- 10 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::prepareStatement()
- 10 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::prepareStatement()
- 10 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::prepareStatement()
- 9 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::prepareStatement()
- 9 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::prepareStatement()
- 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::prepareStatement()
- main core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::prepareStatement()
- main core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::prepareStatement()
- main core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::prepareStatement()
Returns a prepared statement given a SQL string.
This method caches prepared statements, reusing them when possible. It also prefixes tables names enclosed in curly braces and, optionally, quotes identifiers enclosed in square brackets.
Parameters
string $query: The query string as SQL, with curly braces surrounding the table names, and square brackets surrounding identifiers.
array $options: An associative array of options to control how the query is run. See the documentation for self::defaultOptions() for details. The content of the 'pdo' key will be passed to the prepared statement.
bool $allow_row_count: (optional) A flag indicating if row count is allowed on the statement object. Defaults to FALSE.
Return value
\Drupal\Core\Database\StatementInterface A prepared statement ready for its execute() method.
Overrides Connection::prepareStatement
File
-
core/
modules/ pgsql/ src/ Driver/ Database/ pgsql/ Connection.php, line 270
Class
- Connection
- PostgreSQL implementation of \Drupal\Core\Database\Connection.
Namespace
Drupal\pgsql\Driver\Database\pgsqlCode
public function prepareStatement(string $query, array $options, bool $allow_row_count = FALSE) : StatementInterface {
// PostgreSQL doesn't automatically cast fields to the right type for the
// (I)LIKE and regex operators. Query builder queries get a '::text'
// type-cast when their conditions are compiled (see
// static::$postgresqlConditionOperatorMap), but queries passed as raw SQL
// strings may still contain uncast fields, so add the type-cast here. The
// guard skips the rewrite for the many queries that use neither operator.
// The optional '::text' in the pattern makes the rewrite idempotent, so
// fields that are already cast are not cast again.
if (stripos($query, 'LIKE') !== FALSE || str_contains($query, '~*')) {
$rewritten = preg_replace('/ ([^ ]+?)(?:::text)? +(I*LIKE|NOT +I*LIKE|~\\*|!~\\*) /i', ' ${1}::text ${2} ', $query);
if ($rewritten !== $query) {
@trigger_error('Relying on the PostgreSQL database driver to add a ::text type-cast to fields used with LIKE and regular expression operators in SQL passed as a string is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Add the ::text cast to the field explicitly or use the query builder. See https://www.drupal.org/node/3615418', E_USER_DEPRECATED);
$query = $rewritten;
}
}
return parent::prepareStatement($query, $options, $allow_row_count);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.