Skip to content

Commit

Permalink
use "memory" connection parameter instead of "path" for SQLite URLs i…
Browse files Browse the repository at this point in the history
…f possible

also refactors URL path evaluation into smaller methods

fixes #1106
  • Loading branch information
deeky666 committed Jan 8, 2016
1 parent 096f59d commit b1cbe8c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
76 changes: 58 additions & 18 deletions lib/Doctrine/DBAL/DriverManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static function getConnection(
}

$params = self::parseDatabaseUrl($params);

// check for existing pdo object
if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
throw DBALException::invalidPdoInstance();
Expand Down Expand Up @@ -227,23 +227,23 @@ private static function parseDatabaseUrl(array $params)
if (!isset($params['url'])) {
return $params;
}

// (pdo_)?sqlite3?:///... => (pdo_)?sqlite3?://localhost/... or else the URL will be invalid
$url = preg_replace('#^((?:pdo_)?sqlite3?):///#', '$1://localhost/', $params['url']);

$url = parse_url($url);

if ($url === false) {
throw new DBALException('Malformed parameter "url".');
}

if (isset($url['scheme'])) {
$params['driver'] = str_replace('-', '_', $url['scheme']); // URL schemes must not contain underscores, but dashes are ok
if (isset(self::$driverSchemeAliases[$params['driver']])) {
$params['driver'] = self::$driverSchemeAliases[$params['driver']]; // use alias like "postgres", else we just let checkParams decide later if the driver exists (for literal "pdo-pgsql" etc)
}
}

if (isset($url['host'])) {
$params['host'] = $url['host'];
}
Expand All @@ -256,25 +256,65 @@ private static function parseDatabaseUrl(array $params)
if (isset($url['pass'])) {
$params['password'] = $url['pass'];
}

if (isset($url['path'])) {
$nameKey = isset($url['scheme']) && strpos($url['scheme'], 'sqlite') !== false
? 'path' // pdo_sqlite driver uses 'path' instead of 'dbname' key
: 'dbname';

if (!isset($url['scheme']) || (strpos($url['scheme'], 'sqlite') !== false && $url['path'] == ':memory:')) {
$params[$nameKey] = $url['path']; // if the URL was just "sqlite::memory:", which parses to scheme and path only
} else {
$params[$nameKey] = substr($url['path'], 1); // strip the leading slash from the URL
}
if (isset($url['path'])) {
$params = self::parseDatabaseUrlPath($url, $params);
}

if (isset($url['query'])) {
$query = array();
parse_str($url['query'], $query); // simply ingest query as extra params, e.g. charset or sslmode
$params = array_merge($params, $query); // parse_str wipes existing array elements
}


return $params;
}

/**
* Parses the given URL and resolves the given connection parameters.
*
* @param array $url The URL parts to evaluate.
* @param array $params The connection parameters to resolve.
*
* @return array The resolved connection parameters.
*/
private static function parseDatabaseUrlPath(array $url, array $params)
{
if (!isset($url['scheme'])) {
$params['dbname'] = $url['path'];

return $params;
}

$url['path'] = substr($url['path'], 1);

if (strpos($url['scheme'], 'sqlite') !== false) {
return self::parseSqliteDatabaseUrlPath($url, $params);
}

$params['dbname'] = $url['path'];

return $params;
}

/**
* Parses the given SQLite URL and resolves the given connection parameters.
*
* @param array $url The SQLite URL parts to evaluate.
* @param array $params The connection parameters to resolve.
*
* @return array The resolved connection parameters.
*/
private static function parseSqliteDatabaseUrlPath(array $url, array $params)
{
if ($url['path'] === ':memory:') {
$params['memory'] = true;

return $params;
}

$params['path'] = $url['path']; // pdo_sqlite driver uses 'path' instead of 'dbname' key

return $params;
}
}
6 changes: 3 additions & 3 deletions tests/Doctrine/Tests/DBAL/DriverManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ public function databaseUrls()
),
'sqlite memory' => array(
'sqlite:///:memory:',
array('path' => ':memory:', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'),
array('memory' => true, 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'),
),
'sqlite memory with host' => array(
'sqlite://localhost/:memory:',
array('path' => ':memory:', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'),
array('memory' => true, 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'),
),
'params parsed from URL override individual params' => array(
array('url' => 'mysql://foo:bar@localhost/baz', 'password' => 'lulz'),
Expand Down Expand Up @@ -201,4 +201,4 @@ public function databaseUrls()
),
);
}
}
}

0 comments on commit b1cbe8c

Please sign in to comment.