Connection::open

public static Connection::open(array &$connection_options = array())

Opens a PDO connection.

Parameters

array $connection_options: The database connection settings array.

Return value

\PDO A \PDO object.

Overrides Connection::open

File

core/lib/Drupal/Core/Database/Driver/sqlite/Connection.php, line 92

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'));

  // 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 += array(
    'init_commands' => array(),
  );
  $connection_options['init_commands'] += array(
    'wal' => "PRAGMA journal_mode=WAL",
  );

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

  return $pdo;
}
doc_Drupal
2016-10-29 08:55:55
Comments
Leave a Comment

Please login to continue.