function Connection::open

Same name in this branch
  1. 10 core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::open()
  2. 10 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::open()
  3. 10 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::open()
Same name and namespace in other branches
  1. 9 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::open()
  2. 9 core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::open()
  3. 9 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::open()
  4. 9 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::open()
  5. 8.9.x core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::open()
  6. 8.9.x core/lib/Drupal/Core/Database/Driver/mysql/Connection.php \Drupal\Core\Database\Driver\mysql\Connection::open()
  7. 8.9.x core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php \Drupal\Core\Database\Driver\pgsql\Connection::open()
  8. 8.9.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::open()
  9. 11.x core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::open()
  10. 11.x core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\Connection::open()
  11. 11.x core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::open()
  12. 11.x core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::open()
  13. 11.x core/modules/mysqli/src/Driver/Database/mysqli/Connection.php \Drupal\mysqli\Driver\Database\mysqli\Connection::open()

Opens a client connection.

Parameters

array $connection_options: The database connection settings array.

Return value

object A client connection object.

Overrides Connection::open

4 calls to Connection::open()
MigrateSqlIdMapTest::testGetQualifiedMapTablePrefix in core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php
Tests the getQualifiedMapTable method with a prefixed database.
MigrateSqlSourceTestBase::getDatabase in core/modules/migrate/tests/src/Kernel/MigrateSqlSourceTestBase.php
Builds an in-memory SQLite database from a set of source data.
MigrateTestCase::getDatabase in core/modules/migrate/tests/src/Unit/MigrateTestCase.php
Gets an SQLite database connection object for use in tests.
QueryBatchTest::getDatabase in core/modules/migrate/tests/src/Kernel/QueryBatchTest.php
Builds an in-memory SQLite database from a set of source data.

File

core/modules/sqlite/src/Driver/Database/sqlite/Connection.php, line 108

Class

Connection
SQLite implementation of \Drupal\Core\Database\Connection.

Namespace

Drupal\sqlite\Driver\Database\sqlite

Code

public static function open(array &$connection_options = []) {
  // Allow PDO options to be overridden.
  $connection_options += [
    'pdo' => [],
  ];
  $connection_options['pdo'] += [
    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
    // Convert numeric values to strings when fetching.
\PDO::ATTR_STRINGIFY_FETCHES => TRUE,
  ];
  try {
    $pdo = new PDOConnection('sqlite:' . $connection_options['database'], '', '', $connection_options['pdo']);
  } catch (\PDOException $e) {
    if ($e->getCode() == static::DATABASE_NOT_FOUND) {
      throw new DatabaseNotFoundException($e->getMessage(), $e->getCode(), $e);
    }
    // SQLite doesn't have a distinct error code for access denied, so don't
    // deal with that case.
    throw $e;
  }
  // Create functions needed by SQLite.
  $pdo->sqliteCreateFunction('if', [
    __CLASS__,
    'sqlFunctionIf',
  ]);
  $pdo->sqliteCreateFunction('greatest', [
    __CLASS__,
    'sqlFunctionGreatest',
  ]);
  $pdo->sqliteCreateFunction('least', [
    __CLASS__,
    'sqlFunctionLeast',
  ]);
  $pdo->sqliteCreateFunction('pow', 'pow', 2);
  $pdo->sqliteCreateFunction('exp', 'exp', 1);
  $pdo->sqliteCreateFunction('length', 'strlen', 1);
  $pdo->sqliteCreateFunction('md5', 'md5', 1);
  $pdo->sqliteCreateFunction('concat', [
    __CLASS__,
    'sqlFunctionConcat',
  ]);
  $pdo->sqliteCreateFunction('concat_ws', [
    __CLASS__,
    'sqlFunctionConcatWs',
  ]);
  $pdo->sqliteCreateFunction('substring', [
    __CLASS__,
    'sqlFunctionSubstring',
  ], 3);
  $pdo->sqliteCreateFunction('substring_index', [
    __CLASS__,
    'sqlFunctionSubstringIndex',
  ], 3);
  $pdo->sqliteCreateFunction('rand', [
    __CLASS__,
    'sqlFunctionRand',
  ]);
  $pdo->sqliteCreateFunction('regexp', [
    __CLASS__,
    'sqlFunctionRegexp',
  ]);
  // SQLite does not support the LIKE BINARY operator, so we overload the
  // non-standard GLOB operator for case-sensitive matching. Another option
  // would have been to override another non-standard operator, MATCH, but
  // that does not support the NOT keyword prefix.
  $pdo->sqliteCreateFunction('glob', [
    __CLASS__,
    'sqlFunctionLikeBinary',
  ]);
  // Create a user-space case-insensitive collation with UTF-8 support.
  $pdo->sqliteCreateCollation('NOCASE_UTF8', [
    'Drupal\\Component\\Utility\\Unicode',
    'strcasecmp',
  ]);
  // Set SQLite init_commands if not already defined. Enable the Write-Ahead
  // Logging (WAL) for SQLite. See https://www.drupal.org/node/2348137 and
  // https://www.sqlite.org/wal.html.
  $connection_options += [
    'init_commands' => [],
  ];
  $connection_options['init_commands'] += [
    'wal' => "PRAGMA journal_mode=WAL",
  ];
  // Execute sqlite init_commands.
  if (isset($connection_options['init_commands'])) {
    $pdo->exec(implode('; ', $connection_options['init_commands']));
  }
  return $pdo;
}

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