function Connection::open

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

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/lib/Drupal/Core/Database/Driver/sqlite/Connection.php, line 104

Class

Connection
SQLite implementation of <a href="/api/drupal/core%21lib%21Drupal%21Core%21Database%21Connection.php/class/Connection/8.9.x" title="Base Database API class." class="local">\Drupal\Core\Database\Connection</a>.

Namespace

Drupal\Core\Database\Driver\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('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.