Connection.php 45.8 KB
Newer Older
1 2
<?php

Crell's avatar
Crell committed
3 4
/**
 * @file
5
 * Contains \Drupal\Core\Database\Connection.
Crell's avatar
Crell committed
6 7
 */

8
namespace Drupal\Core\Database;
9 10 11 12 13 14 15 16 17

/**
 * Base Database API class.
 *
 * This class provides a Drupal-specific extension of the PDO database
 * abstraction class in PHP. Every database driver implementation must provide a
 * concrete implementation of it to support special handling required by that
 * database.
 *
18
 * @see http://php.net/manual/book.pdo.php
19
 */
20
abstract class Connection {
21 22 23 24 25 26

  /**
   * The database target this connection is for.
   *
   * We need this information for later auditing and logging.
   *
27
   * @var string|null
28 29 30 31 32 33 34
   */
  protected $target = NULL;

  /**
   * The key representing this connection.
   *
   * The key is a unique string which identifies a database connection. A
35 36
   * connection can be a single server or a cluster of primary and replicas
   * (use target to pick between primary and replica).
37
   *
38
   * @var string|null
39 40 41 42 43 44
   */
  protected $key = NULL;

  /**
   * The current database logging object for this connection.
   *
45
   * @var \Drupal\Core\Database\Log|null
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
   */
  protected $logger = NULL;

  /**
   * Tracks the number of "layers" of transactions currently active.
   *
   * On many databases transactions cannot nest.  Instead, we track
   * nested calls to transactions and collapse them into a single
   * transaction.
   *
   * @var array
   */
  protected $transactionLayers = array();

  /**
   * Index of what driver-specific class to use for various operations.
   *
   * @var array
   */
  protected $driverClasses = array();

  /**
   * The name of the Statement class for this connection.
   *
   * @var string
   */
72
  protected $statementClass = 'Drupal\Core\Database\Statement';
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96

  /**
   * Whether this database connection supports transactions.
   *
   * @var bool
   */
  protected $transactionSupport = TRUE;

  /**
   * Whether this database connection supports transactional DDL.
   *
   * Set to FALSE by default because few databases support this feature.
   *
   * @var bool
   */
  protected $transactionalDDLSupport = FALSE;

  /**
   * An index used to generate unique temporary table names.
   *
   * @var integer
   */
  protected $temporaryNameIndex = 0;

97 98 99 100 101 102 103
  /**
   * The actual PDO connection.
   *
   * @var \PDO
   */
  protected $connection;

104 105 106 107 108 109 110 111 112 113
  /**
   * The connection information for this connection object.
   *
   * @var array
   */
  protected $connectionOptions = array();

  /**
   * The schema object for this connection.
   *
114 115 116
   * Set to NULL when the schema is destroyed.
   *
   * @var \Drupal\Core\Database\Schema|null
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
   */
  protected $schema = NULL;

  /**
   * The prefixes used by this database connection.
   *
   * @var array
   */
  protected $prefixes = array();

  /**
   * List of search values for use in prefixTables().
   *
   * @var array
   */
  protected $prefixSearch = array();

  /**
   * List of replacement values for use in prefixTables().
   *
   * @var array
   */
  protected $prefixReplace = array();

141 142 143 144 145 146 147
  /**
   * List of un-prefixed table names, keyed by prefixed table names.
   *
   * @var array
   */
  protected $unprefixedTablesMap = [];

148 149
  /**
   * Constructs a Connection object.
150 151 152 153 154 155 156 157
   *
   * @param \PDO $connection
   *   An object of the PDO class representing a database connection.
   * @param array $connection_options
   *   An array of options for the connection. May include the following:
   *   - prefix
   *   - namespace
   *   - Other driver-specific options.
158
   */
159
  public function __construct(\PDO $connection, array $connection_options) {
160
    // Initialize and prepare the connection prefix.
161
    $this->setPrefix(isset($connection_options['prefix']) ? $connection_options['prefix'] : '');
162

163
    // Set a Statement class, unless the driver opted out.
164
    if (!empty($this->statementClass)) {
165
      $connection->setAttribute(\PDO::ATTR_STATEMENT_CLASS, array($this->statementClass, array($this)));
166
    }
167 168 169

    $this->connection = $connection;
    $this->connectionOptions = $connection_options;
170 171
  }

172 173 174 175 176 177 178 179 180 181 182
  /**
   * Opens a PDO connection.
   *
   * @param array $connection_options
   *   The database connection settings array.
   *
   * @return \PDO
   *   A \PDO object.
   */
  public static function open(array &$connection_options = array()) { }

183 184 185 186 187 188 189 190 191 192 193 194
  /**
   * Destroys this Connection object.
   *
   * PHP does not destruct an object if it is still referenced in other
   * variables. In case of PDO database connection objects, PHP only closes the
   * connection when the PDO object is destructed, so any references to this
   * object may cause the number of maximum allowed connections to be exceeded.
   */
  public function destroy() {
    // Destroy all references to this connection by setting them to NULL.
    // The Statement class attribute only accepts a new value that presents a
    // proper callable, so we reset it to PDOStatement.
195 196 197
    if (!empty($this->statementClass)) {
      $this->connection->setAttribute(\PDO::ATTR_STATEMENT_CLASS, array('PDOStatement', array()));
    }
198 199 200
    $this->schema = NULL;
  }

201 202 203 204 205 206
  /**
   * Returns the default query options for any given query.
   *
   * A given query can be customized with a number of option flags in an
   * associative array:
   * - target: The database "target" against which to execute a query. Valid
207
   *   values are "default" or "replica". The system will first try to open a
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
   *   connection to a database specified with the user-supplied key. If one
   *   is not available, it will silently fall back to the "default" target.
   *   If multiple databases connections are specified with the same target,
   *   one will be selected at random for the duration of the request.
   * - fetch: This element controls how rows from a result set will be
   *   returned. Legal values include PDO::FETCH_ASSOC, PDO::FETCH_BOTH,
   *   PDO::FETCH_OBJ, PDO::FETCH_NUM, or a string representing the name of a
   *   class. If a string is specified, each record will be fetched into a new
   *   object of that class. The behavior of all other values is defined by PDO.
   *   See http://php.net/manual/pdostatement.fetch.php
   * - return: Depending on the type of query, different return values may be
   *   meaningful. This directive instructs the system which type of return
   *   value is desired. The system will generally set the correct value
   *   automatically, so it is extremely rare that a module developer will ever
   *   need to specify this value. Setting it incorrectly will likely lead to
   *   unpredictable results or fatal errors. Legal values include:
   *   - Database::RETURN_STATEMENT: Return the prepared statement object for
   *     the query. This is usually only meaningful for SELECT queries, where
   *     the statement object is how one accesses the result set returned by the
   *     query.
   *   - Database::RETURN_AFFECTED: Return the number of rows affected by an
   *     UPDATE or DELETE query. Be aware that means the number of rows actually
   *     changed, not the number of rows matched by the WHERE clause.
   *   - Database::RETURN_INSERT_ID: Return the sequence ID (primary key)
   *     created by an INSERT statement on a table that contains a serial
   *     column.
   *   - Database::RETURN_NULL: Do not return anything, as there is no
   *     meaningful value to return. That is the case for INSERT queries on
   *     tables that do not contain a serial column.
   * - throw_exception: By default, the database system will catch any errors
   *   on a query as an Exception, log it, and then rethrow it so that code
   *   further up the call chain can take an appropriate action. To suppress
   *   that behavior and simply return NULL on failure, set this option to
   *   FALSE.
   *
243
   * @return array
244 245 246 247 248
   *   An array of default query options.
   */
  protected function defaultOptions() {
    return array(
      'target' => 'default',
249
      'fetch' => \PDO::FETCH_OBJ,
250 251 252 253 254 255 256 257 258 259 260 261 262
      'return' => Database::RETURN_STATEMENT,
      'throw_exception' => TRUE,
    );
  }

  /**
   * Returns the connection information for this connection object.
   *
   * Note that Database::getConnectionInfo() is for requesting information
   * about an arbitrary database connection that is defined. This method
   * is for requesting the connection information of this specific
   * open connection object.
   *
263
   * @return array
264 265 266 267 268 269 270 271 272 273
   *   An array of the connection information. The exact list of
   *   properties is driver-dependent.
   */
  public function getConnectionOptions() {
    return $this->connectionOptions;
  }

  /**
   * Set the list of prefixes used by this database connection.
   *
274 275 276
   * @param array|string $prefix
   *   Either a single prefix, or an array of prefixes, in any of the multiple
   *   forms documented in default.settings.php.
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
   */
  protected function setPrefix($prefix) {
    if (is_array($prefix)) {
      $this->prefixes = $prefix + array('default' => '');
    }
    else {
      $this->prefixes = array('default' => $prefix);
    }

    // Set up variables for use in prefixTables(). Replace table-specific
    // prefixes first.
    $this->prefixSearch = array();
    $this->prefixReplace = array();
    foreach ($this->prefixes as $key => $val) {
      if ($key != 'default') {
        $this->prefixSearch[] = '{' . $key . '}';
        $this->prefixReplace[] = $val . $key;
      }
    }
    // Then replace remaining tables with the default prefix.
    $this->prefixSearch[] = '{';
    $this->prefixReplace[] = $this->prefixes['default'];
    $this->prefixSearch[] = '}';
    $this->prefixReplace[] = '';
301 302 303 304 305 306 307

    // Set up a map of prefixed => un-prefixed tables.
    foreach ($this->prefixes as $table_name => $prefix) {
      if ($table_name !== 'default') {
        $this->unprefixedTablesMap[$prefix . $table_name] = $table_name;
      }
    }
308 309 310 311 312 313 314 315 316 317
  }

  /**
   * Appends a database prefix to all tables in a query.
   *
   * Queries sent to Drupal should wrap all table names in curly brackets. This
   * function searches for this syntax and adds Drupal's table prefix to all
   * tables, allowing Drupal to coexist with other systems in the same database
   * and/or schema if necessary.
   *
318
   * @param string $sql
319 320
   *   A string containing a partial or entire SQL query.
   *
321
   * @return string
322 323 324 325 326 327 328 329 330 331 332
   *   The properly-prefixed string.
   */
  public function prefixTables($sql) {
    return str_replace($this->prefixSearch, $this->prefixReplace, $sql);
  }

  /**
   * Find the prefix for a table.
   *
   * This function is for when you want to know the prefix of a table. This
   * is not used in prefixTables due to performance reasons.
333 334 335
   *
   * @param string $table
   *   (optional) The table to find the prefix for.
336 337 338 339 340 341 342 343 344 345
   */
  public function tablePrefix($table = 'default') {
    if (isset($this->prefixes[$table])) {
      return $this->prefixes[$table];
    }
    else {
      return $this->prefixes['default'];
    }
  }

346 347 348 349 350 351 352 353 354 355 356
  /**
   * Gets a list of individually prefixed table names.
   *
   * @return array
   *   An array of un-prefixed table names, keyed by their fully qualified table
   *   names (i.e. prefix + table_name).
   */
  public function getUnprefixedTablesMap() {
    return $this->unprefixedTablesMap;
  }

357 358 359 360 361 362 363 364 365 366 367 368 369 370
  /**
   * Get a fully qualified table name.
   *
   * @param string $table
   *   The name of the table in question.
   *
   * @return string
   */
  public function getFullQualifiedTableName($table) {
    $options = $this->getConnectionOptions();
    $prefix = $this->tablePrefix($table);
    return $options['database'] . '.' . $prefix . $table;
  }

371 372 373 374 375 376 377 378 379 380
  /**
   * Prepares a query string and returns the prepared statement.
   *
   * This method caches prepared statements, reusing them when
   * possible. It also prefixes tables names enclosed in curly-braces.
   *
   * @param $query
   *   The query string as SQL, with curly-braces surrounding the
   *   table names.
   *
381
   * @return \Drupal\Core\Database\StatementInterface
382 383 384 385 386
   *   A PDO prepared statement ready for its execute() method.
   */
  public function prepareQuery($query) {
    $query = $this->prefixTables($query);

387
    return $this->connection->prepare($query);
388 389 390 391 392 393 394 395 396 397
  }

  /**
   * Tells this connection object what its target value is.
   *
   * This is needed for logging and auditing. It's sloppy to do in the
   * constructor because the constructor for child classes has a different
   * signature. We therefore also ensure that this function is only ever
   * called once.
   *
398 399
   * @param string $target
   *   (optional) The target this connection is for.
400 401 402 403 404 405 406 407 408 409
   */
  public function setTarget($target = NULL) {
    if (!isset($this->target)) {
      $this->target = $target;
    }
  }

  /**
   * Returns the target this connection is associated with.
   *
410 411
   * @return string|null
   *   The target string of this connection, or NULL if no target is set.
412 413 414 415 416 417 418 419
   */
  public function getTarget() {
    return $this->target;
  }

  /**
   * Tells this connection object what its key is.
   *
420
   * @param string $key
421 422 423 424 425 426 427 428 429 430 431
   *   The key this connection is for.
   */
  public function setKey($key) {
    if (!isset($this->key)) {
      $this->key = $key;
    }
  }

  /**
   * Returns the key this connection is associated with.
   *
432 433
   * @return string|null
   *   The key of this connection, or NULL if no key is set.
434 435 436 437 438 439 440 441
   */
  public function getKey() {
    return $this->key;
  }

  /**
   * Associates a logging object with this connection.
   *
442
   * @param \Drupal\Core\Database\Log $logger
443 444
   *   The logging object we want to use.
   */
445
  public function setLogger(Log $logger) {
446 447 448 449 450 451
    $this->logger = $logger;
  }

  /**
   * Gets the current logging object for this connection.
   *
452
   * @return \Drupal\Core\Database\Log|null
453 454 455 456 457 458 459 460 461 462 463 464 465
   *   The current logging object for this connection. If there isn't one,
   *   NULL is returned.
   */
  public function getLogger() {
    return $this->logger;
  }

  /**
   * Creates the appropriate sequence name for a given table and serial field.
   *
   * This information is exposed to all database drivers, although it is only
   * useful on some of them. This method is table prefix-aware.
   *
466
   * @param string $table
467
   *   The table name to use for the sequence.
468
   * @param string $field
469 470
   *   The field name to use for the sequence.
   *
471
   * @return string
472 473 474 475 476 477 478 479 480 481 482
   *   A table prefix-parsed string for the sequence name.
   */
  public function makeSequenceName($table, $field) {
    return $this->prefixTables('{' . $table . '}_' . $field . '_seq');
  }

  /**
   * Flatten an array of query comments into a single comment string.
   *
   * The comment string will be sanitized to avoid SQL injection attacks.
   *
483
   * @param string[] $comments
484 485
   *   An array of query comment strings.
   *
486
   * @return string
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
   *   A sanitized comment string.
   */
  public function makeComment($comments) {
    if (empty($comments))
      return '';

    // Flatten the array of comments.
    $comment = implode('; ', $comments);

    // Sanitize the comment string so as to avoid SQL injection attacks.
    return '/* ' . $this->filterComment($comment) . ' */ ';
  }

  /**
   * Sanitize a query comment string.
   *
   * Ensure a query comment does not include strings such as "* /" that might
   * terminate the comment early. This avoids SQL injection attacks via the
   * query comment. The comment strings in this example are separated by a
   * space to avoid PHP parse errors.
   *
   * For example, the comment:
   * @code
   * db_update('example')
   *  ->condition('id', $id)
   *  ->fields(array('field2' => 10))
   *  ->comment('Exploit * / DROP TABLE node; --')
   *  ->execute()
   * @endcode
   *
   * Would result in the following SQL statement being generated:
   * @code
   * "/ * Exploit * / DROP TABLE node; -- * / UPDATE example SET field2=..."
   * @endcode
   *
   * Unless the comment is sanitised first, the SQL server would drop the
   * node table and ignore the rest of the SQL statement.
   *
525
   * @param string $comment
526 527
   *   A query comment string.
   *
528
   * @return string
529 530 531 532 533 534 535 536 537 538 539 540 541
   *   A sanitized version of the query comment string.
   */
  protected function filterComment($comment = '') {
    return preg_replace('/(\/\*\s*)|(\s*\*\/)/', '', $comment);
  }

  /**
   * Executes a query string against the database.
   *
   * This method provides a central handler for the actual execution of every
   * query. All queries executed by Drupal are executed as PDO prepared
   * statements.
   *
542
   * @param string|\Drupal\Core\Database\StatementInterface $query
543 544
   *   The query to execute. In most cases this will be a string containing
   *   an SQL query with placeholders. An already-prepared instance of
545
   *   StatementInterface may also be passed in order to allow calling
546
   *   code to manually bind variables to a query. If a
547
   *   StatementInterface is passed, the $args array will be ignored.
548 549 550
   *   It is extremely rare that module code will need to pass a statement
   *   object to this method. It is used primarily for database drivers for
   *   databases that require special LOB field handling.
551
   * @param array $args
552 553 554
   *   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.
555 556 557 558 559 560
   * @param array $options
   *   An associative array of options to control how the query is run. The
   *   given options will be merged with self::defaultOptions(). See the
   *   documentation for self::defaultOptions() for details.
   *   Typically, $options['return'] will be set by a default or by a query
   *   builder, and should not be set by a user.
561
   *
562
   * @return \Drupal\Core\Database\StatementInterface|int|null
563 564 565 566 567 568 569 570 571 572 573 574
   *   This method will return one of the following:
   *   - If either $options['return'] === self::RETURN_STATEMENT, or
   *     $options['return'] is not set (due to self::defaultOptions()),
   *     returns the executed statement.
   *   - If $options['return'] === self::RETURN_AFFECTED,
   *     returns the number of rows affected by the query
   *     (not the number matched).
   *   - If $options['return'] === self::RETURN_INSERT_ID,
   *     returns the generated insert ID of the last query.
   *   - If either $options['return'] === self::RETURN_NULL, or
   *     an exception occurs and $options['throw_exception'] evaluates to FALSE,
   *     returns NULL.
575
   *
576
   * @throws \Drupal\Core\Database\DatabaseExceptionWrapper
577
   * @throws \Drupal\Core\Database\IntegrityConstraintViolationException
578
   * @throws \InvalidArgumentException
579 580
   *
   * @see \Drupal\Core\Database\Connection::defaultOptions()
581 582 583 584 585 586 587 588 589
   */
  public function query($query, array $args = array(), $options = array()) {
    // Use default values if not already set.
    $options += $this->defaultOptions();

    try {
      // We allow either a pre-bound statement object or a literal string.
      // In either case, we want to end up with an executed statement object,
      // which we pass to PDOStatement::execute.
590
      if ($query instanceof StatementInterface) {
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
        $stmt = $query;
        $stmt->execute(NULL, $options);
      }
      else {
        $this->expandArguments($query, $args);
        $stmt = $this->prepareQuery($query);
        $stmt->execute($args, $options);
      }

      // Depending on the type of query we may need to return a different value.
      // See DatabaseConnection::defaultOptions() for a description of each
      // value.
      switch ($options['return']) {
        case Database::RETURN_STATEMENT:
          return $stmt;
        case Database::RETURN_AFFECTED:
607
          $stmt->allowRowCount = TRUE;
608 609
          return $stmt->rowCount();
        case Database::RETURN_INSERT_ID:
610 611
          $sequence_name = isset($options['sequence_name']) ? $options['sequence_name'] : NULL;
          return $this->connection->lastInsertId($sequence_name);
612
        case Database::RETURN_NULL:
613
          return NULL;
614
        default:
615
          throw new \PDOException('Invalid return directive: ' . $options['return']);
616 617
      }
    }
618
    catch (\PDOException $e) {
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
      // Most database drivers will return NULL here, but some of them
      // (e.g. the SQLite driver) may need to re-run the query, so the return
      // value will be the same as for static::query().
      return $this->handleQueryException($e, $query, $args, $options);
    }
  }

  /**
   * Wraps and re-throws any PDO exception thrown by static::query().
   *
   * @param \PDOException $e
   *   The exception thrown by static::query().
   * @param $query
   *   The query executed by static::query().
   * @param array $args
   *   An array of arguments for the prepared statement.
   * @param array $options
   *   An associative array of options to control how the query is run.
   *
   * @return \Drupal\Core\Database\StatementInterface|int|null
   *   Most database drivers will return NULL when a PDO exception is thrown for
   *   a query, but some of them may need to re-run the query, so they can also
   *   return a \Drupal\Core\Database\StatementInterface object or an integer.
   *
   * @throws \Drupal\Core\Database\DatabaseExceptionWrapper
   * @throws \Drupal\Core\Database\IntegrityConstraintViolationException
   */
  protected function handleQueryException(\PDOException $e, $query, array $args = array(), $options = array()) {
    if ($options['throw_exception']) {
      // Wrap the exception in another exception, because PHP does not allow
      // overriding Exception::getMessage(). Its message is the extra database
      // debug information.
      $query_string = ($query instanceof StatementInterface) ? $query->getQueryString() : $query;
      $message = $e->getMessage() . ": " . $query_string . "; " . print_r($args, TRUE);
      // Match all SQLSTATE 23xxx errors.
      if (substr($e->getCode(), -6, -3) == '23') {
        $exception = new IntegrityConstraintViolationException($message, $e->getCode(), $e);
656
      }
657 658 659 660 661
      else {
        $exception = new DatabaseExceptionWrapper($message, 0, $e);
      }

      throw $exception;
662
    }
663 664

    return NULL;
665 666 667 668 669 670 671 672
  }

  /**
   * Expands out shorthand placeholders.
   *
   * Drupal supports an alternate syntax for doing arrays of values. We
   * therefore need to expand them out into a full, executable query string.
   *
673
   * @param string $query
674
   *   The query string to modify.
675
   * @param array $args
676 677
   *   The arguments for the query.
   *
678
   * @return bool
679
   *   TRUE if the query was modified, FALSE otherwise.
680 681 682 683 684 685 686
   *
   * @throws \InvalidArgumentException
   *   This exception is thrown when:
   *   - A placeholder that ends in [] is supplied, and the supplied value is
   *     not an array.
   *   - A placeholder that does not end in [] is supplied, and the supplied
   *     value is an array.
687 688 689 690
   */
  protected function expandArguments(&$query, &$args) {
    $modified = FALSE;

691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
    // If the placeholder indicated the value to use is an array,  we need to
    // expand it out into a comma-delimited set of placeholders.
    foreach ($args as $key => $data) {
      $is_bracket_placeholder = substr($key, -2) === '[]';
      $is_array_data = is_array($data);
      if ($is_bracket_placeholder && !$is_array_data) {
        throw new \InvalidArgumentException('Placeholders with a trailing [] can only be expanded with an array of values.');
      }
      elseif (!$is_bracket_placeholder) {
        if ($is_array_data) {
          throw new \InvalidArgumentException('Placeholders must have a trailing [] if they are to be expanded with an array of values.');
        }
        // Scalar placeholder - does not need to be expanded.
        continue;
      }
      // Handle expansion of arrays.
      $key_name = str_replace('[]', '__', $key);
708
      $new_keys = array();
709 710
      // We require placeholders to have trailing brackets if the developer
      // intends them to be expanded to an array to make the intent explicit.
711
      foreach (array_values($data) as $i => $value) {
712
        // This assumes that there are no other placeholders that use the same
713
        // name.  For example, if the array placeholder is defined as :example[]
714 715 716
        // and there is already an :example_2 placeholder, this will generate
        // a duplicate key.  We do not account for that as the calling code
        // is already broken if that happens.
717
        $new_keys[$key_name . $i] = $value;
718 719 720
      }

      // Update the query with the new placeholders.
721
      $query = str_replace($key, implode(', ', array_keys($new_keys)), $query);
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740

      // Update the args array with the new placeholders.
      unset($args[$key]);
      $args += $new_keys;

      $modified = TRUE;
    }

    return $modified;
  }

  /**
   * Gets the driver-specific override class if any for the specified class.
   *
   * @param string $class
   *   The class for which we want the potentially driver-specific class.
   * @return string
   *   The name of the class that should be used for this driver.
   */
741
  public function getDriverClass($class) {
742 743
    if (empty($this->driverClasses[$class])) {
      $driver = $this->driver();
744 745 746 747 748 749 750
      if (!empty($this->connectionOptions['namespace'])) {
        $driver_class  = $this->connectionOptions['namespace'] . '\\' . $class;
      }
      else {
        // Fallback for Drupal 7 settings.php.
        $driver_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\{$class}";
      }
751
      $this->driverClasses[$class] = class_exists($driver_class) ? $driver_class : $class;
752 753 754 755 756 757 758
    }
    return $this->driverClasses[$class];
  }

  /**
   * Prepares and returns a SELECT query object.
   *
759
   * @param string $table
760 761 762
   *   The base table for this query, that is, the first table in the FROM
   *   clause. This table will also be used as the "base" table for query_alter
   *   hook implementations.
763 764
   * @param string $alias
   *   (optional) The alias of the base table of this query.
765 766 767
   * @param $options
   *   An array of options on the query.
   *
768
   * @return \Drupal\Core\Database\Query\SelectInterface
769 770 771 772
   *   An appropriate SelectQuery object for this database connection. Note that
   *   it may be a driver-specific subclass of SelectQuery, depending on the
   *   driver.
   *
773
   * @see \Drupal\Core\Database\Query\Select
774 775
   */
  public function select($table, $alias = NULL, array $options = array()) {
776
    $class = $this->getDriverClass('Select');
777 778 779 780 781 782
    return new $class($table, $alias, $this, $options);
  }

  /**
   * Prepares and returns an INSERT query object.
   *
783 784 785 786
   * @param string $table
   *   The table to use for the insert statement.
   * @param array $options
   *   (optional) An array of options on the query.
787
   *
788
   * @return \Drupal\Core\Database\Query\Insert
789
   *   A new Insert query object.
790
   *
791
   * @see \Drupal\Core\Database\Query\Insert
792 793
   */
  public function insert($table, array $options = array()) {
794
    $class = $this->getDriverClass('Insert');
795 796 797 798 799 800
    return new $class($this, $table, $options);
  }

  /**
   * Prepares and returns a MERGE query object.
   *
801 802 803 804
   * @param string $table
   *   The table to use for the merge statement.
   * @param array $options
   *   (optional) An array of options on the query.
805
   *
806
   * @return \Drupal\Core\Database\Query\Merge
807
   *   A new Merge query object.
808
   *
809
   * @see \Drupal\Core\Database\Query\Merge
810 811
   */
  public function merge($table, array $options = array()) {
812
    $class = $this->getDriverClass('Merge');
813 814 815
    return new $class($this, $table, $options);
  }

816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
  /**
   * Prepares and returns an UPSERT query object.
   *
   * @param string $table
   *   The table to use for the upsert query.
   * @param array $options
   *   (optional) An array of options on the query.
   *
   * @return \Drupal\Core\Database\Query\Upsert
   *   A new Upsert query object.
   *
   * @see \Drupal\Core\Database\Query\Upsert
   */
  public function upsert($table, array $options = array()) {
    $class = $this->getDriverClass('Upsert');
    return new $class($this, $table, $options);
  }
833 834 835 836

  /**
   * Prepares and returns an UPDATE query object.
   *
837 838 839 840
   * @param string $table
   *   The table to use for the update statement.
   * @param array $options
   *   (optional) An array of options on the query.
841
   *
842
   * @return \Drupal\Core\Database\Query\Update
843
   *   A new Update query object.
844
   *
845
   * @see \Drupal\Core\Database\Query\Update
846 847
   */
  public function update($table, array $options = array()) {
848
    $class = $this->getDriverClass('Update');
849 850 851 852 853 854
    return new $class($this, $table, $options);
  }

  /**
   * Prepares and returns a DELETE query object.
   *
855 856 857 858
   * @param string $table
   *   The table to use for the delete statement.
   * @param array $options
   *   (optional) An array of options on the query.
859
   *
860
   * @return \Drupal\Core\Database\Query\Delete
861
   *   A new Delete query object.
862
   *
863
   * @see \Drupal\Core\Database\Query\Delete
864 865
   */
  public function delete($table, array $options = array()) {
866
    $class = $this->getDriverClass('Delete');
867 868 869 870 871 872
    return new $class($this, $table, $options);
  }

  /**
   * Prepares and returns a TRUNCATE query object.
   *
873 874 875 876
   * @param string $table
   *   The table to use for the truncate statement.
   * @param array $options
   *   (optional) An array of options on the query.
877
   *
878
   * @return \Drupal\Core\Database\Query\Truncate
879
   *   A new Truncate query object.
880
   *
881
   * @see \Drupal\Core\Database\Query\Truncate
882 883
   */
  public function truncate($table, array $options = array()) {
884
    $class = $this->getDriverClass('Truncate');
885 886 887 888 889 890 891 892
    return new $class($this, $table, $options);
  }

  /**
   * Returns a DatabaseSchema object for manipulating the schema.
   *
   * This method will lazy-load the appropriate schema library file.
   *
893
   * @return \Drupal\Core\Database\Schema
894
   *   The database Schema object for this connection.
895 896 897
   */
  public function schema() {
    if (empty($this->schema)) {
898
      $class = $this->getDriverClass('Schema');
ayelet_Cr's avatar
ayelet_Cr committed
899
      $this->schema = new $class($this);
900 901 902 903
    }
    return $this->schema;
  }

904 905 906 907 908 909 910
  /**
   * Escapes a database name string.
   *
   * Force all database names to be strictly alphanumeric-plus-underscore.
   * For some database drivers, it may also wrap the database name in
   * database-specific escape characters.
   *
911 912 913
   * @param string $database
   *   An unsanitized database name.
   *
914
   * @return string
915
   *   The sanitized database name.
916 917 918 919 920
   */
  public function escapeDatabase($database) {
    return preg_replace('/[^A-Za-z0-9_.]+/', '', $database);
  }

921 922 923 924 925 926 927
  /**
   * Escapes a table name string.
   *
   * Force all table names to be strictly alphanumeric-plus-underscore.
   * For some database drivers, it may also wrap the table name in
   * database-specific escape characters.
   *
928 929 930 931 932
   * @param string $table
   *   An unsanitized table name.
   *
   * @return string
   *   The sanitized table name.
933 934 935 936 937 938 939 940 941 942 943 944
   */
  public function escapeTable($table) {
    return preg_replace('/[^A-Za-z0-9_.]+/', '', $table);
  }

  /**
   * Escapes a field name string.
   *
   * Force all field names to be strictly alphanumeric-plus-underscore.
   * For some database drivers, it may also wrap the field name in
   * database-specific escape characters.
   *
945 946 947 948 949
   * @param string $field
   *   An unsanitized field name.
   *
   * @return string
   *   The sanitized field name.
950 951 952 953 954 955 956 957 958 959 960 961 962
   */
  public function escapeField($field) {
    return preg_replace('/[^A-Za-z0-9_.]+/', '', $field);
  }

  /**
   * Escapes an alias name string.
   *
   * Force all alias names to be strictly alphanumeric-plus-underscore. In
   * contrast to DatabaseConnection::escapeField() /
   * DatabaseConnection::escapeTable(), this doesn't allow the period (".")
   * because that is not allowed in aliases.
   *
963 964 965 966 967
   * @param string $field
   *   An unsanitized alias name.
   *
   * @return string
   *   The sanitized alias name.
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
   */
  public function escapeAlias($field) {
    return preg_replace('/[^A-Za-z0-9_]+/', '', $field);
  }

  /**
   * Escapes characters that work as wildcard characters in a LIKE pattern.
   *
   * The wildcard characters "%" and "_" as well as backslash are prefixed with
   * a backslash. Use this to do a search for a verbatim string without any
   * wildcard behavior.
   *
   * For example, the following does a case-insensitive query for all rows whose
   * name starts with $prefix:
   * @code
   * $result = db_query(
   *   'SELECT * FROM person WHERE name LIKE :pattern',
   *   array(':pattern' => db_like($prefix) . '%')
   * );
   * @endcode
   *
   * Backslash is defined as escape character for LIKE patterns in
990
   * Drupal\Core\Database\Query\Condition::mapConditionOperator().
991
   *
992
   * @param string $string
993 994
   *   The string to escape.
   *
995
   * @return string
996 997 998 999 1000 1001 1002 1003 1004
   *   The escaped string.
   */
  public function escapeLike($string) {
    return addcslashes($string, '\%_');
  }

  /**
   * Determines if there is an active transaction open.
   *
1005
   * @return bool
1006 1007 1008 1009 1010 1011 1012
   *   TRUE if we're currently in a transaction, FALSE otherwise.
   */
  public function inTransaction() {
    return ($this->transactionDepth() > 0);
  }

  /**
1013 1014 1015 1016
   * Determines the current transaction depth.
   *
   * @return int
   *   The current transaction depth.
1017 1018 1019 1020 1021 1022 1023 1024
   */
  public function transactionDepth() {
    return count($this->transactionLayers);
  }

  /**
   * Returns a new DatabaseTransaction object on this connection.
   *
1025 1026
   * @param string $name
   *   (optional) The name of the savepoint.
1027
   *
1028 1029
   * @return \Drupal\Core\Database\Transaction
   *   A Transaction object.
1030
   *
1031
   * @see \Drupal\Core\Database\Transaction
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
   */
  public function startTransaction($name = '') {
    $class = $this->getDriverClass('Transaction');
    return new $class($this, $name);
  }

  /**
   * Rolls back the transaction entirely or to a named savepoint.
   *
   * This method throws an exception if no transaction is active.
   *
1043 1044 1045
   * @param string $savepoint_name
   *   (optional) The name of the savepoint. The default, 'drupal_transaction',
   *    will roll the entire transaction back.
1046
   *
1047
   * @throws \Drupal\Core\Database\TransactionOutOfOrderException
1048
   * @throws \Drupal\Core\Database\TransactionNoActiveException
1049
   *
1050
   * @see \Drupal\Core\Database\Transaction::rollback()
1051 1052 1053 1054 1055 1056
   */
  public function rollback($savepoint_name = 'drupal_transaction') {
    if (!$this->supportsTransactions()) {
      return;
    }
    if (!$this->inTransaction()) {
1057
      throw new TransactionNoActiveException();
1058 1059
    }
    // A previous rollback to an earlier savepoint may mean that the savepoint
Crell's avatar
Crell committed
1060 1061
    // in question has already been accidentally committed.
    if (!isset($this->transactionLayers[$savepoint_name])) {
1062
      throw new TransactionNoActiveException();
1063
    }
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079

    // We need to find the point we're rolling back to, all other savepoints
    // before are no longer needed. If we rolled back other active savepoints,
    // we need to throw an exception.
    $rolled_back_other_active_savepoints = FALSE;
    while ($savepoint = array_pop($this->transactionLayers)) {
      if ($savepoint == $savepoint_name) {
        // If it is the last the transaction in the stack, then it is not a
        // savepoint, it is the transaction itself so we will need to roll back
        // the transaction rather than a savepoint.
        if (empty($this->transactionLayers)) {
          break;
        }
        $this->query('ROLLBACK TO SAVEPOINT ' . $savepoint);
        $this->popCommittableTransactions();
        if ($rolled_back_other_active_savepoints) {
1080
          throw new TransactionOutOfOrderException();
1081 1082 1083 1084 1085 1086 1087
        }
        return;
      }
      else {
        $rolled_back_other_active_savepoints = TRUE;
      }
    }
1088
    $this->connection->rollBack();
1089
    if ($rolled_back_other_active_savepoints) {
1090
      throw new TransactionOutOfOrderException();
1091 1092 1093 1094 1095 1096 1097 1098
    }
  }

  /**
   * Increases the depth of transaction nesting.
   *
   * If no transaction is already active, we begin a new transaction.
   *
1099 1100 1101
   * @param string $name
   *   The name of the transaction.
   *
1102
   * @throws \Drupal\Core\Database\TransactionNameNonUniqueException
1103
   *
1104
   * @see \Drupal\Core\Database\Transaction
1105 1106 1107 1108 1109 1110
   */
  public function pushTransaction($name) {
    if (!$this->supportsTransactions()) {
      return;
    }
    if (isset($this->transactionLayers[$name])) {
1111
      throw new TransactionNameNonUniqueException($name . " is already in use.");
1112 1113 1114 1115 1116 1117 1118
    }
    // If we're already in a transaction then we want to create a savepoint
    // rather than try to create another transaction.
    if ($this->inTransaction()) {
      $this->query('SAVEPOINT ' . $name);
    }
    else {
1119
      $this->connection->beginTransaction();
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
    }
    $this->transactionLayers[$name] = $name;
  }

  /**
   * Decreases the depth of transaction nesting.
   *
   * If we pop off the last transaction layer, then we either commit or roll
   * back the transaction as necessary. If no transaction is active, we return
   * because the transaction may have manually been rolled back.
   *
1131 1132
   * @param string $name
   *   The name of the savepoint.
1133
   *
1134 1135
   * @throws \Drupal\Core\Database\TransactionNoActiveException
   * @throws \Drupal\Core\Database\TransactionCommitFailedException
1136
   *
1137
   * @see \Drupal\Core\Database\Transaction
1138 1139 1140 1141 1142
   */
  public function popTransaction($name) {
    if (!$this->supportsTransactions()) {
      return;
    }
Crell's avatar
Crell committed
1143 1144 1145 1146
    // The transaction has already been committed earlier. There is nothing we
    // need to do. If this transaction was part of an earlier out-of-order
    // rollback, an exception would already have been thrown by
    // Database::rollback().
1147
    if (!isset($this->transactionLayers[$name])) {
Crell's avatar
Crell committed
1148
      return;
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
    }

    // Mark this layer as committable.
    $this->transactionLayers[$name] = FALSE;
    $this->popCommittableTransactions();
  }

  /**
   * Internal function: commit all the transaction layers that can commit.
   */
  protected function popCommittableTransactions() {
    // Commit all the committable layers.
    foreach (array_reverse($this->transactionLayers) as $name => $active) {
      // Stop once we found an active transaction.
      if ($active) {
        break;
      }

      // If there are no more layers left then we should commit.
      unset($this->transactionLayers[$name]);
      if (empty($this->transactionLayers)) {
1170
        if (!$this->connection->commit()) {
1171
          throw new TransactionCommitFailedException();
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
        }
      }
      else {
        $this->query('RELEASE SAVEPOINT ' . $name);
      }
    }
  }

  /**
   * Runs a limited-range query on this database object.
   *
   * Use this as a substitute for ->query() when a subset of the query is to be
   * returned. User-supplied arguments to the query should be passed in as
   * separate parameters so that they can be properly escaped to avoid SQL
   * injection attacks.
   *
1188
   * @param string $query
1189
   *   A string containing an SQL query.
1190
   * @param int $from
1191
   *   The first result row to return.
1192
   * @param int $count
1193
   *   The maximum number of result rows to return.
1194 1195 1196 1197 1198
   * @param array $args
   *   (optional) An array of values to substitute into the query at placeholder
   *    markers.
   * @param array $options
   *   (optional) An array of options on the query.
1199
   *
1200
   * @return \Drupal\Core\Database\StatementInterface
1201 1202 1203 1204 1205 1206 1207 1208
   *   A database query result resource, or NULL if the query was not executed
   *   correctly.
   */
  abstract public function queryRange($query, $from, $count, array $args = array(), array $options = array());

  /**
   * Generates a temporary table name.
   *
1209
   * @return string
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
   *   A table name.
   */
  protected function generateTemporaryTableName() {
    return "db_temporary_" . $this->temporaryNameIndex++;
  }

  /**
   * Runs a SELECT query and stores its results in a temporary table.
   *
   * Use this as a substitute for ->query() when the results need to stored
   * in a temporary table. Temporary tables exist for the duration of the page
   * request. User-supplied arguments to the query should be passed in as
   * separate parameters so that they can be properly escaped to avoid SQL
   * injection attacks.
   *
   * Note that if you need to know how many results were returned, you should do
   * a SELECT COUNT(*) on the temporary table afterwards.
   *
1228
   * @param string $query
1229
   *   A string containing a normal SELECT SQL query.
alexpott's avatar