function 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 101
Class
- Connection
- SQLite implementation of \Drupal\Core\Database\Connection.
Namespace
Drupal\sqlite\Driver\Database\sqliteCode
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 {
if (\PHP_VERSION_ID >= 80400) {
// @phpstan-ignore class.notFound
$sqlite = new Sqlite('sqlite:' . $connection_options['database'], '', '', $connection_options['pdo']);
}
else {
$sqlite = 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.
if (\PHP_VERSION_ID >= 80400) {
// Use PHP 8.4+ createFunction with fast callable syntax.
$sqlite->createFunction('if', self::sqlFunctionIf(...));
$sqlite->createFunction('greatest', self::sqlFunctionGreatest(...));
$sqlite->createFunction('least', self::sqlFunctionLeast(...));
$sqlite->createFunction('pow', pow(...), 2);
$sqlite->createFunction('exp', exp(...), 1);
$sqlite->createFunction('length', strlen(...), 1);
$sqlite->createFunction('md5', md5(...), 1);
$sqlite->createFunction('concat', self::sqlFunctionConcat(...));
$sqlite->createFunction('concat_ws', self::sqlFunctionConcatWs(...));
$sqlite->createFunction('substring', self::sqlFunctionSubstring(...), 3);
$sqlite->createFunction('substring_index', self::sqlFunctionSubstringIndex(...), 3);
$sqlite->createFunction('rand', self::sqlFunctionRand(...));
$sqlite->createFunction('regexp', self::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.
$sqlite->createFunction('glob', self::sqlFunctionLikeBinary(...));
// Create a user-space case-insensitive collation with UTF-8 support.
$sqlite->createCollation('NOCASE_UTF8', Unicode::strcasecmp(...));
}
else {
// Fallback for PHP < 8.4
$sqlite->sqliteCreateFunction('if', [
__CLASS__,
'sqlFunctionIf',
]);
$sqlite->sqliteCreateFunction('greatest', [
__CLASS__,
'sqlFunctionGreatest',
]);
$sqlite->sqliteCreateFunction('least', [
__CLASS__,
'sqlFunctionLeast',
]);
$sqlite->sqliteCreateFunction('pow', 'pow', 2);
$sqlite->sqliteCreateFunction('exp', 'exp', 1);
$sqlite->sqliteCreateFunction('length', 'strlen', 1);
$sqlite->sqliteCreateFunction('md5', 'md5', 1);
$sqlite->sqliteCreateFunction('concat', [
__CLASS__,
'sqlFunctionConcat',
]);
$sqlite->sqliteCreateFunction('concat_ws', [
__CLASS__,
'sqlFunctionConcatWs',
]);
$sqlite->sqliteCreateFunction('substring', [
__CLASS__,
'sqlFunctionSubstring',
], 3);
$sqlite->sqliteCreateFunction('substring_index', [
__CLASS__,
'sqlFunctionSubstringIndex',
], 3);
$sqlite->sqliteCreateFunction('rand', [
__CLASS__,
'sqlFunctionRand',
]);
$sqlite->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.
$sqlite->sqliteCreateFunction('glob', [
__CLASS__,
'sqlFunctionLikeBinary',
]);
// Create a user-space case-insensitive collation with UTF-8 support.
$sqlite->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'])) {
$sqlite->exec(implode('; ', $connection_options['init_commands']));
}
return $sqlite;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.