Commit a2cdd8a5 authored by Steve Müller's avatar Steve Müller

use "memory" connection parameter instead of "path" for SQLite URLs if possible

also refactors URL path evaluation into smaller methods

fixes #1106
parent b19626ab
...@@ -259,15 +259,7 @@ final class DriverManager ...@@ -259,15 +259,7 @@ final class DriverManager
} }
if (isset($url['path'])) { if (isset($url['path'])) {
$nameKey = isset($url['scheme']) && strpos($url['scheme'], 'sqlite') !== false $params = self::parseDatabaseUrlPath($url, $params);
? '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['query'])) { if (isset($url['query'])) {
...@@ -278,4 +270,52 @@ final class DriverManager ...@@ -278,4 +270,52 @@ final class DriverManager
return $params; 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;
}
} }
...@@ -167,11 +167,11 @@ class DriverManagerTest extends \Doctrine\Tests\DbalTestCase ...@@ -167,11 +167,11 @@ class DriverManagerTest extends \Doctrine\Tests\DbalTestCase
), ),
'sqlite memory' => array( 'sqlite memory' => array(
'sqlite:///:memory:', '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 memory with host' => array(
'sqlite://localhost/:memory:', '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( 'params parsed from URL override individual params' => array(
array('url' => 'mysql://foo:bar@localhost/baz', 'password' => 'lulz'), array('url' => 'mysql://foo:bar@localhost/baz', 'password' => 'lulz'),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment