changed dbSimple version to use e1d92f1f2f

removed unused driver-implementations
This commit is contained in:
Sarjuuk
2014-09-06 11:27:17 +02:00
parent 13e45827e4
commit ba49c55c6b
11 changed files with 670 additions and 970 deletions

View File

@@ -85,8 +85,17 @@ class DbSimple_Generic
* Universal static function to connect ANY database using DSN syntax.
* Choose database driver according to DSN. Return new instance
* of this driver.
*
* You can connect to MySQL by socket using this new syntax (like PDO DSN):
* $dsn = 'mysqli:unix_socket=/cloudsql/app:instance;user=root;pass=;dbname=testdb';
* $dsn = 'mypdo:unix_socket=/cloudsql/app:instance;charset=utf8;user=testuser;pass=mypassword;dbname=testdb';
*
* Connection by host also can be made with this syntax.
* Or you can use old syntax:
* $dsn = 'mysql://testuser:mypassword@127.0.0.1/testdb';
*
*/
function connect($dsn)
public static function connect($dsn)
{
// Load database driver and create its instance.
$parsed = DbSimple_Generic::parseDSN($dsn);
@@ -96,7 +105,7 @@ class DbSimple_Generic
}
$class = 'DbSimple_'.ucfirst($parsed['scheme']);
if (!class_exists($class)) {
$file = dirname(__FILE__).'/'.ucfirst($parsed['scheme']). ".php";
$file = __DIR__.'/'.ucfirst($parsed['scheme']). ".php";
if (is_file($file)) {
require_once($file);
} else {
@@ -118,19 +127,67 @@ class DbSimple_Generic
* Parse a data source name.
* See parse_url() for details.
*/
function parseDSN($dsn)
public static function parseDSN($dsn)
{
if (is_array($dsn)) return $dsn;
$parsed = parse_url($dsn);
if (!$parsed) return null;
$params = null;
if (!empty($parsed['query'])) {
parse_str($parsed['query'], $params);
$parsed += $params;
}
if ( empty($parsed['host']) && empty($parsed['socket']) ) {
// Parse as DBO DSN string
$parsedPdo = self::parseDsnPdo($parsed['path']);
unset($parsed['path']);
$parsed = array_merge($parsed, $parsedPdo);
}
$parsed['dsn'] = $dsn;
return $parsed;
}
}
} // parseDSN
?>
/**
* Parse string as DBO DSN string.
*
* @param $str
* @return array
*/
public static function parseDsnPdo($str) {
if (substr($str, 0, strlen('mysql:')) == 'mysql:') {
$str = substr($str, strlen('mysql:'));
}
$arr = explode(';', $str);
$result = array();
foreach ($arr as $k=>$v) {
$v = explode('=', $v);
if (count($v) == 2)
$result[ $v[0] ] = $v[1];
}
if ( isset($result['unix_socket']) ) {
$result['socket'] = $result['unix_socket'];
unset($result['unix_socket']);
}
if ( isset($result['dbname']) ) {
$result['path'] = $result['dbname'];
unset($result['dbname']);
}
if ( isset($result['charset']) ) {
$result['enc'] = $result['charset'];
unset($result['charset']);
}
return $result;
} // parseDsnPdo
} // DbSimple_Generic class