You are here

public static function Connection::open in Drupal 10

Same name in this branch
  1. 10 core/modules/sqlite/src/Driver/Database/sqlite/Connection.php \Drupal\sqlite\Driver\Database\sqlite\Connection::open()
  2. 10 core/modules/pgsql/src/Driver/Database/pgsql/Connection.php \Drupal\pgsql\Driver\Database\pgsql\Connection::open()
  3. 10 core/modules/mysql/src/Driver/Database/mysql/Connection.php \Drupal\mysql\Driver\Database\mysql\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 98

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 \PDO('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;
}