You are here

public static function Connection::open in Zircon Profile 8

Same name in this branch
  1. 8 core/lib/Drupal/Core/Database/Connection.php \Drupal\Core\Database\Connection::open()
  2. 8 core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::open()
  3. 8 core/lib/Drupal/Core/Database/Driver/pgsql/Connection.php \Drupal\Core\Database\Driver\pgsql\Connection::open()
  4. 8 core/lib/Drupal/Core/Database/Driver/mysql/Connection.php \Drupal\Core\Database\Driver\mysql\Connection::open()
Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php \Drupal\Core\Database\Driver\sqlite\Connection::open()

Opens a PDO connection.

Parameters

array $connection_options: The database connection settings array.

Return value

\PDO A \PDO object.

Overrides Connection::open

2 calls to Connection::open()
MigrateSqlIdMapTest::testGetQualifiedMapTablePrefix in core/modules/migrate/tests/src/Unit/MigrateSqlIdMapTest.php
Tests the getQualifiedMapTable method with a prefixed database.
MigrateTestCase::getDatabase in core/modules/migrate/tests/src/Unit/MigrateTestCase.php
Get an SQLite database connection object for use in tests.

File

core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php, line 97
Contains \Drupal\Core\Database\Driver\sqlite\Connection.

Class

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

Namespace

Drupal\Core\Database\Driver\sqlite

Code

public static function open(array &$connection_options = array()) {

  // Allow PDO options to be overridden.
  $connection_options += array(
    'pdo' => array(),
  );
  $connection_options['pdo'] += array(
    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
    // Convert numeric values to strings when fetching.
    \PDO::ATTR_STRINGIFY_FETCHES => TRUE,
  );
  $pdo = new \PDO('sqlite:' . $connection_options['database'], '', '', $connection_options['pdo']);

  // Create functions needed by SQLite.
  $pdo
    ->sqliteCreateFunction('if', array(
    __CLASS__,
    'sqlFunctionIf',
  ));
  $pdo
    ->sqliteCreateFunction('greatest', array(
    __CLASS__,
    'sqlFunctionGreatest',
  ));
  $pdo
    ->sqliteCreateFunction('pow', 'pow', 2);
  $pdo
    ->sqliteCreateFunction('exp', 'exp', 1);
  $pdo
    ->sqliteCreateFunction('length', 'strlen', 1);
  $pdo
    ->sqliteCreateFunction('md5', 'md5', 1);
  $pdo
    ->sqliteCreateFunction('concat', array(
    __CLASS__,
    'sqlFunctionConcat',
  ));
  $pdo
    ->sqliteCreateFunction('concat_ws', array(
    __CLASS__,
    'sqlFunctionConcatWs',
  ));
  $pdo
    ->sqliteCreateFunction('substring', array(
    __CLASS__,
    'sqlFunctionSubstring',
  ), 3);
  $pdo
    ->sqliteCreateFunction('substring_index', array(
    __CLASS__,
    'sqlFunctionSubstringIndex',
  ), 3);
  $pdo
    ->sqliteCreateFunction('rand', array(
    __CLASS__,
    'sqlFunctionRand',
  ));
  $pdo
    ->sqliteCreateFunction('regexp', array(
    __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', array(
    __CLASS__,
    'sqlFunctionLikeBinary',
  ));

  // Create a user-space case-insensitive collation with UTF-8 support.
  $pdo
    ->sqliteCreateCollation('NOCASE_UTF8', array(
    'Drupal\\Component\\Utility\\Unicode',
    'strcasecmp',
  ));

  // Execute sqlite init_commands.
  if (isset($connection_options['init_commands'])) {
    $pdo
      ->exec(implode('; ', $connection_options['init_commands']));
  }
  return $pdo;
}