Skip to content

Commit a2cdd8a

Browse files
committed
use "memory" connection parameter instead of "path" for SQLite URLs if possible
also refactors URL path evaluation into smaller methods fixes doctrine#1106
1 parent b19626a commit a2cdd8a

File tree

2 files changed

+61
-21
lines changed

2 files changed

+61
-21
lines changed

lib/Doctrine/DBAL/DriverManager.php

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public static function getConnection(
142142
}
143143

144144
$params = self::parseDatabaseUrl($params);
145-
145+
146146
// check for existing pdo object
147147
if (isset($params['pdo']) && ! $params['pdo'] instanceof \PDO) {
148148
throw DBALException::invalidPdoInstance();
@@ -228,23 +228,23 @@ private static function parseDatabaseUrl(array $params)
228228
if (!isset($params['url'])) {
229229
return $params;
230230
}
231-
231+
232232
// (pdo_)?sqlite3?:///... => (pdo_)?sqlite3?://localhost/... or else the URL will be invalid
233233
$url = preg_replace('#^((?:pdo_)?sqlite3?):///#', '$1://localhost/', $params['url']);
234-
234+
235235
$url = parse_url($url);
236-
236+
237237
if ($url === false) {
238238
throw new DBALException('Malformed parameter "url".');
239239
}
240-
240+
241241
if (isset($url['scheme'])) {
242242
$params['driver'] = str_replace('-', '_', $url['scheme']); // URL schemes must not contain underscores, but dashes are ok
243243
if (isset(self::$driverSchemeAliases[$params['driver']])) {
244244
$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)
245245
}
246246
}
247-
247+
248248
if (isset($url['host'])) {
249249
$params['host'] = $url['host'];
250250
}
@@ -257,25 +257,65 @@ private static function parseDatabaseUrl(array $params)
257257
if (isset($url['pass'])) {
258258
$params['password'] = $url['pass'];
259259
}
260-
261-
if (isset($url['path'])) {
262-
$nameKey = isset($url['scheme']) && strpos($url['scheme'], 'sqlite') !== false
263-
? 'path' // pdo_sqlite driver uses 'path' instead of 'dbname' key
264-
: 'dbname';
265260

266-
if (!isset($url['scheme']) || (strpos($url['scheme'], 'sqlite') !== false && $url['path'] == ':memory:')) {
267-
$params[$nameKey] = $url['path']; // if the URL was just "sqlite::memory:", which parses to scheme and path only
268-
} else {
269-
$params[$nameKey] = substr($url['path'], 1); // strip the leading slash from the URL
270-
}
261+
if (isset($url['path'])) {
262+
$params = self::parseDatabaseUrlPath($url, $params);
271263
}
272-
264+
273265
if (isset($url['query'])) {
274266
$query = array();
275267
parse_str($url['query'], $query); // simply ingest query as extra params, e.g. charset or sslmode
276268
$params = array_merge($params, $query); // parse_str wipes existing array elements
277269
}
278-
270+
271+
return $params;
272+
}
273+
274+
/**
275+
* Parses the given URL and resolves the given connection parameters.
276+
*
277+
* @param array $url The URL parts to evaluate.
278+
* @param array $params The connection parameters to resolve.
279+
*
280+
* @return array The resolved connection parameters.
281+
*/
282+
private static function parseDatabaseUrlPath(array $url, array $params)
283+
{
284+
if (!isset($url['scheme'])) {
285+
$params['dbname'] = $url['path'];
286+
287+
return $params;
288+
}
289+
290+
$url['path'] = substr($url['path'], 1);
291+
292+
if (strpos($url['scheme'], 'sqlite') !== false) {
293+
return self::parseSqliteDatabaseUrlPath($url, $params);
294+
}
295+
296+
$params['dbname'] = $url['path'];
297+
298+
return $params;
299+
}
300+
301+
/**
302+
* Parses the given SQLite URL and resolves the given connection parameters.
303+
*
304+
* @param array $url The SQLite URL parts to evaluate.
305+
* @param array $params The connection parameters to resolve.
306+
*
307+
* @return array The resolved connection parameters.
308+
*/
309+
private static function parseSqliteDatabaseUrlPath(array $url, array $params)
310+
{
311+
if ($url['path'] === ':memory:') {
312+
$params['memory'] = true;
313+
314+
return $params;
315+
}
316+
317+
$params['path'] = $url['path']; // pdo_sqlite driver uses 'path' instead of 'dbname' key
318+
279319
return $params;
280320
}
281321
}

tests/Doctrine/Tests/DBAL/DriverManagerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ public function databaseUrls()
167167
),
168168
'sqlite memory' => array(
169169
'sqlite:///:memory:',
170-
array('path' => ':memory:', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'),
170+
array('memory' => true, 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'),
171171
),
172172
'sqlite memory with host' => array(
173173
'sqlite://localhost/:memory:',
174-
array('path' => ':memory:', 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'),
174+
array('memory' => true, 'driver' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver'),
175175
),
176176
'params parsed from URL override individual params' => array(
177177
array('url' => 'mysql://foo:bar@localhost/baz', 'password' => 'lulz'),
@@ -199,4 +199,4 @@ public function databaseUrls()
199199
),
200200
);
201201
}
202-
}
202+
}

0 commit comments

Comments
 (0)