Connection.php

Same filename in this branch
  1. 11.x core/modules/sqlite/src/Driver/Database/sqlite/Connection.php
  2. 11.x core/modules/mysql/src/Driver/Database/mysql/Connection.php
  3. 11.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DriverTestMysql/Connection.php
  4. 11.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DriverTestMysqlDeprecatedVersion/Connection.php
  5. 11.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DriverTestPgsql/Connection.php
  6. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Connection.php
  7. 11.x core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFake/Connection.php
  8. 11.x core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Connection.php
  9. 11.x core/tests/fixtures/database_drivers/core/CoreFake/Connection.php
  10. 11.x core/tests/fixtures/database_drivers/custom/CoreFake/Connection.php
  11. 11.x core/tests/fixtures/database_drivers/custom/fake/Connection.php
  12. 11.x core/lib/Drupal/Core/Database/Connection.php
  13. 11.x core/modules/system/tests/modules/dummydb/src/Driver/Database/dummydb/Connection.php
Same filename and directory in other branches
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php
  2. 9 core/modules/mysql/src/Driver/Database/mysql/Connection.php
  3. 9 core/modules/system/tests/modules/database_statement_monitoring_test/src/sqlite/Connection.php
  4. 9 core/modules/system/tests/modules/database_statement_monitoring_test/src/mysql/Connection.php
  5. 9 core/modules/system/tests/modules/database_statement_monitoring_test/src/pgsql/Connection.php
  6. 9 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql/Connection.php
  7. 9 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysqlDeprecatedVersion/Connection.php
  8. 9 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestPgsql/Connection.php
  9. 9 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php
  10. 9 core/tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefake/Connection.php
  11. 9 core/tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses/Connection.php
  12. 9 core/tests/fixtures/database_drivers/core/corefake/Connection.php
  13. 9 core/tests/fixtures/database_drivers/custom/corefake/Connection.php
  14. 9 core/tests/fixtures/database_drivers/custom/fake/Connection.php
  15. 9 core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
  16. 9 core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
  17. 9 core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
  18. 9 core/lib/Drupal/Core/Database/Connection.php
  19. 8.9.x core/modules/system/tests/modules/database_statement_monitoring_test/src/sqlite/Connection.php
  20. 8.9.x core/modules/system/tests/modules/database_statement_monitoring_test/src/mysql/Connection.php
  21. 8.9.x core/modules/system/tests/modules/database_statement_monitoring_test/src/pgsql/Connection.php
  22. 8.9.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql/Connection.php
  23. 8.9.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysqlDeprecatedVersion/Connection.php
  24. 8.9.x core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestPgsql/Connection.php
  25. 8.9.x core/tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefake/Connection.php
  26. 8.9.x core/tests/fixtures/database_drivers/core/corefake/Connection.php
  27. 8.9.x core/tests/fixtures/database_drivers/custom/corefake/Connection.php
  28. 8.9.x core/tests/fixtures/database_drivers/custom/fake/Connection.php
  29. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
  30. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
  31. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
  32. 8.9.x core/lib/Drupal/Core/Database/Connection.php
  33. 10 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php
  34. 10 core/modules/mysql/src/Driver/Database/mysql/Connection.php
  35. 10 core/modules/system/tests/modules/database_statement_monitoring_test/src/sqlite/Connection.php
  36. 10 core/modules/system/tests/modules/database_statement_monitoring_test/src/mysql/Connection.php
  37. 10 core/modules/system/tests/modules/database_statement_monitoring_test/src/pgsql/Connection.php
  38. 10 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysql/Connection.php
  39. 10 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestMysqlDeprecatedVersion/Connection.php
  40. 10 core/modules/system/tests/modules/driver_test/src/Driver/Database/DrivertestPgsql/Connection.php
  41. 10 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php
  42. 10 core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFake/Connection.php
  43. 10 core/tests/fixtures/database_drivers/module/core_fake/src/Driver/Database/CoreFakeWithAllCustomClasses/Connection.php
  44. 10 core/tests/fixtures/database_drivers/core/CoreFake/Connection.php
  45. 10 core/tests/fixtures/database_drivers/custom/CoreFake/Connection.php
  46. 10 core/tests/fixtures/database_drivers/custom/fake/Connection.php
  47. 10 core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php
  48. 10 core/lib/Drupal/Core/Database/Driver/mysql/Connection.php
  49. 10 core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php
  50. 10 core/lib/Drupal/Core/Database/Connection.php

Namespace

Drupal\mysqli\Driver\Database\mysqli

File

core/modules/mysqli/src/Driver/Database/mysqli/Connection.php

View source
<?php

declare (strict_types=1);
namespace Drupal\mysqli\Driver\Database\mysqli;

use Drupal\Core\Database\Connection as BaseConnection;
use Drupal\Core\Database\ConnectionNotDefinedException;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseAccessDeniedException;
use Drupal\Core\Database\DatabaseNotFoundException;
use Drupal\Core\Database\Transaction\TransactionManagerInterface;
use Drupal\mysql\Driver\Database\mysql\Connection as BaseMySqlConnection;

/**
 * MySQLi implementation of \Drupal\Core\Database\Connection.
 */
class Connection extends BaseMySqlConnection {
  
  /**
   * {@inheritdoc}
   */
  protected $statementWrapperClass = Statement::class;
  public function __construct(\mysqli $connection, array $connectionOptions = []) {
    // If the SQL mode doesn't include 'ANSI_QUOTES' (explicitly or via a
    // combination mode), then MySQL doesn't interpret a double quote as an
    // identifier quote, in which case use the non-ANSI-standard backtick.
    //
    // @see https://dev.mysql.com/doc/refman/8.0/en/sql-mode.html#sqlmode_ansi_quotes
    $ansiQuotesModes = [
      'ANSI_QUOTES',
      'ANSI',
    ];
    $isAnsiQuotesMode = FALSE;
    if (isset($connectionOptions['init_commands']['sql_mode'])) {
      foreach ($ansiQuotesModes as $mode) {
        // None of the modes in $ansiQuotesModes are substrings of other modes
        // that are not in $ansiQuotesModes, so a simple stripos() does not
        // return false positives.
        if (stripos($connectionOptions['init_commands']['sql_mode'], $mode) !== FALSE) {
          $isAnsiQuotesMode = TRUE;
          break;

        }
      }
    }
    if ($this->identifierQuotes === [
      '"',
      '"',
    ] && !$isAnsiQuotesMode) {
      $this->identifierQuotes = [
        '`',
        '`',
      ];
    }
    BaseConnection::__construct($connection, $connectionOptions);
  }
  
  /**
   * {@inheritdoc}
   */
  public static function open(array &$connection_options = []) {
    // Sets mysqli error reporting mode to report errors from mysqli function
    // calls and to throw mysqli_sql_exception for errors.
    // @see https://www.php.net/manual/en/mysqli-driver.report-mode.php
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    // Allow PDO options to be overridden.
    $connection_options += [
      'pdo' => [],
    ];
    try {
      $mysqli = @new \mysqli($connection_options['host'], $connection_options['username'], $connection_options['password'], $connection_options['database'] ?? '', !empty($connection_options['port']) ? (int) $connection_options['port'] : 3306, $connection_options['unix_socket'] ?? '');
      if (!$mysqli->set_charset('utf8mb4')) {
        throw new InvalidCharsetException('Invalid charset utf8mb4');
      }
    } catch (\mysqli_sql_exception $e) {
      if ($e->getCode() === static::DATABASE_NOT_FOUND) {
        throw new DatabaseNotFoundException($e->getMessage(), $e->getCode(), $e);
      }
      elseif ($e->getCode() === static::ACCESS_DENIED) {
        throw new DatabaseAccessDeniedException($e->getMessage(), $e->getCode(), $e);
      }
      throw new ConnectionNotDefinedException('Invalid database connection: ' . $e->getMessage(), $e->getCode(), $e);
    }
    // Force MySQL to use the UTF-8 character set. Also set the collation, if a
    // certain one has been set; otherwise, MySQL defaults to
    // 'utf8mb4_0900_ai_ci' for the 'utf8mb4' character set.
    if (!empty($connection_options['collation'])) {
      $mysqli->query('SET NAMES utf8mb4 COLLATE ' . $connection_options['collation']);
    }
    else {
      $mysqli->query('SET NAMES utf8mb4');
    }
    // Set MySQL init_commands if not already defined.  Default Drupal's MySQL
    // behavior to conform more closely to SQL standards.  This allows Drupal
    // to run almost seamlessly on many different kinds of database systems.
    // These settings force MySQL to behave the same as postgresql, or sqlite
    // in regard to syntax interpretation and invalid data handling.  See
    // https://www.drupal.org/node/344575 for further discussion. Also, as MySQL
    // 5.5 changed the meaning of TRADITIONAL we need to spell out the modes one
    // by one.
    $connection_options += [
      'init_commands' => [],
    ];
    $connection_options['init_commands'] += [
      'sql_mode' => "SET sql_mode = 'ANSI,TRADITIONAL'",
    ];
    if (!empty($connection_options['isolation_level'])) {
      $connection_options['init_commands'] += [
        'isolation_level' => 'SET SESSION TRANSACTION ISOLATION LEVEL ' . strtoupper($connection_options['isolation_level']),
      ];
    }
    // Execute initial commands.
    foreach ($connection_options['init_commands'] as $sql) {
      $mysqli->query($sql);
    }
    return $mysqli;
  }
  
  /**
   * {@inheritdoc}
   */
  public function driver() {
    return 'mysqli';
  }
  
  /**
   * {@inheritdoc}
   */
  public function clientVersion() {
    return \mysqli_get_client_info();
  }
  
  /**
   * {@inheritdoc}
   */
  public function createDatabase($database) : void {
    // Escape the database name.
    $database = Database::getConnection()->escapeDatabase($database);
    try {
      // Create the database and set it as active.
      $this->connection
        ->query("CREATE DATABASE {$database}");
      $this->connection
        ->query("USE {$database}");
    } catch (\Exception $e) {
      throw new DatabaseNotFoundException($e->getMessage());
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function quote($string, $parameter_type = \PDO::PARAM_STR) {
    return "'" . $this->connection
      ->escape_string((string) $string) . "'";
  }
  
  /**
   * {@inheritdoc}
   */
  public function lastInsertId(?string $name = NULL) : string {
    return (string) $this->connection->insert_id;
  }
  
  /**
   * {@inheritdoc}
   */
  public function exceptionHandler() {
    return new ExceptionHandler();
  }
  
  /**
   * {@inheritdoc}
   */
  protected function driverTransactionManager() : TransactionManagerInterface {
    return new TransactionManager($this);
  }

}

Classes

Title Deprecated Summary
Connection MySQLi implementation of \Drupal\Core\Database\Connection.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.