diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php index c6ce48e5..23e067b5 100644 --- a/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -489,17 +489,23 @@ public function getFullQualifiedTableName($table) { * @param bool $quote_identifiers * (optional) Quote any identifiers enclosed in square brackets. Defaults to * TRUE. + * @param array $args + * An array of arguments for the prepared statement. If the prepared + * statement uses ? placeholders, this array must be an indexed array. + * If it contains named placeholders, it must be an associative array. + * @param array $pdo_driver_options + * An associative array of PDO options to control how the query is run. * * @return \Drupal\Core\Database\StatementInterface * A PDO prepared statement ready for its execute() method. */ - public function prepareQuery($query, $quote_identifiers = TRUE) { + public function prepareQuery($query, $quote_identifiers = TRUE, array $args = [], array $pdo_driver_options = []) { $query = $this->prefixTables($query); if ($quote_identifiers) { $query = $this->quoteIdentifiers($query); } - return $this->connection->prepare($query); + return $this->connection->prepare($query, $pdo_driver_options); } /** @@ -730,7 +736,7 @@ public function query($query, array $args = [], $options = []) { if (strpos($query, ';') !== FALSE && empty($options['allow_delimiter_in_query'])) { throw new \InvalidArgumentException('; is not supported in SQL strings. Use only one statement at a time.'); } - $stmt = $this->prepareQuery($query, !$options['allow_square_brackets']); + $stmt = $this->prepareQuery($query, !$options['allow_square_brackets'], $args, $options); $stmt->execute($args, $options); } @@ -1652,6 +1658,7 @@ public function commit() { * @see \PDO::prepare() */ public function prepare($statement, array $driver_options = []) { + @trigger_error('Connection::prepare() is deprecated in drupal:9.1.0 and is removed in drupal:10.0.0. See https://www.drupal.org/node/TODO', E_USER_DEPRECATED); return $this->connection->prepare($statement, $driver_options); } diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php index c2aef48a..55ff30d0 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php @@ -184,7 +184,7 @@ public function query($query, array $args = [], $options = []) { return $return; } - public function prepareQuery($query, $quote_identifiers = TRUE) { + public function prepareQuery($query, $quote_identifiers = TRUE, array $args = [], array $pdo_driver_options = []) { // mapConditionOperator converts some operations (LIKE, REGEXP, etc.) to // PostgreSQL equivalents (ILIKE, ~*, etc.). However PostgreSQL doesn't // automatically cast the fields to the right type for these operators, diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php index f00ffb75..652c831f 100644 --- a/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php +++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php @@ -336,6 +336,7 @@ public static function sqlFunctionLikeBinary($pattern, $subject) { * {@inheritdoc} */ public function prepare($statement, array $driver_options = []) { + @trigger_error('Connection::prepare() is deprecated in drupal:9.1.0 and is removed in drupal:10.0.0. See https://www.drupal.org/node/TODO', E_USER_DEPRECATED); return new Statement($this->connection, $this, $statement, $driver_options); } @@ -403,12 +404,12 @@ public function mapConditionOperator($operator) { /** * {@inheritdoc} */ - public function prepareQuery($query, $quote_identifiers = TRUE) { + public function prepareQuery($query, $quote_identifiers = TRUE, array $args = [], array $pdo_driver_options = []) { $query = $this->prefixTables($query); if ($quote_identifiers) { $query = $this->quoteIdentifiers($query); } - return $this->prepare($query); + return new Statement($this->connection, $this, $query, $pdo_driver_options); } public function nextId($existing_id = 0) { diff --git a/core/lib/Drupal/Core/Database/StatementPrefetch.php b/core/lib/Drupal/Core/Database/StatementPrefetch.php index 4294b72d..c7f06823 100644 --- a/core/lib/Drupal/Core/Database/StatementPrefetch.php +++ b/core/lib/Drupal/Core/Database/StatementPrefetch.php @@ -220,7 +220,7 @@ protected function throwPDOException() { * A PDOStatement object. */ protected function getStatement($query, &$args = []) { - return $this->dbh->prepare($query); + return $this->dbh->prepare($query, $this->driverOptions); } /** diff --git a/core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php b/core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php index 54cc6cd3..b393e3dd 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/DatabaseExceptionWrapperTest.php @@ -15,6 +15,9 @@ class DatabaseExceptionWrapperTest extends KernelTestBase { /** * Tests the expected database exception thrown for prepared statements. + * + * @group legacy + * @expectedDeprecation Connection::prepare() is deprecated in drupal:9.1.0 and is removed in drupal:10.0.0. See https://www.drupal.org/node/TODO */ public function testPreparedStatement() { $connection = Database::getConnection();