1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 05:37:39 +02:00

* added configuration aliases (user -> username, pass -> password)

* dibi::$defaultDriver
This commit is contained in:
David Grudl
2007-10-26 17:44:24 +00:00
parent 879bbeba1f
commit 39c62c1cd5
11 changed files with 79 additions and 73 deletions

View File

@@ -140,6 +140,12 @@ class dibi
*/
public static $numOfQueries = 0;
/**
* Default dibi driver
* @var string
*/
public static $defaultDriver = 'mysql';
/**
* Start time
* @var int
@@ -165,15 +171,15 @@ class dibi
* @return DibiDriver
* @throws DibiException
*/
public static function connect($config = 'driver=mysql', $name = 0)
public static function connect($config = array(), $name = 0)
{
// DSN string
if (is_string($config)) {
parse_str($config, $config);
}
if (empty($config['driver'])) {
throw new DibiException('Driver is not specified.');
if (!isset($config['driver'])) {
$config['driver'] = self::$defaultDriver;
}
$class = "Dibi$config[driver]Driver";

View File

@@ -32,13 +32,16 @@ class DibiMySqlDriver extends DibiDriver
*/
public function __construct($config)
{
self::prepare($config, 'username', 'user');
self::prepare($config, 'password', 'pass');
// default values
if (empty($config['username'])) $config['username'] = ini_get('mysql.default_user');
if (empty($config['password'])) $config['password'] = ini_get('mysql.default_password');
if (empty($config['host'])) {
if ($config['username'] === NULL) $config['username'] = ini_get('mysql.default_user');
if ($config['password'] === NULL) $config['password'] = ini_get('mysql.default_password');
if (!isset($config['host'])) {
$config['host'] = ini_get('mysql.default_host');
if (empty($config['port'])) ini_get('mysql.default_port');
if (empty($config['host'])) $config['host'] = 'localhost';
if (!isset($config['port'])) ini_get('mysql.default_port');
if (!isset($config['host'])) $config['host'] = 'localhost';
}
parent::__construct($config);
@@ -57,7 +60,7 @@ class DibiMySqlDriver extends DibiDriver
if (isset($config['protocol']) && $config['protocol'] === 'unix') { // host can be socket
$host = ':' . $config['host'];
} else {
$host = $config['host'] . (empty($config['port']) ? '' : ':'.$config['port']);
$host = $config['host'] . (isset($config['port']) ? ':'.$config['port'] : '');
}
// some errors aren't handled. Must use $php_errormsg
@@ -83,12 +86,12 @@ class DibiMySqlDriver extends DibiDriver
throw new DibiDatabaseException($msg, mysql_errno());
}
if (!empty($config['charset'])) {
if (isset($config['charset'])) {
@mysql_query("SET NAMES '" . $config['charset'] . "'", $connection);
// don't handle this error...
}
if (!empty($config['database']) && !@mysql_select_db($config['database'], $connection)) {
if (isset($config['database']) && !@mysql_select_db($config['database'], $connection)) {
throw new DibiDatabaseException(mysql_error($connection), mysql_errno($connection));
}

View File

@@ -33,15 +33,18 @@ class DibiMySqliDriver extends DibiDriver
*/
public function __construct($config)
{
self::prepare($config, 'username', 'user');
self::prepare($config, 'password', 'pass');
self::prepare($config, 'database');
// default values
if (empty($config['username'])) $config['username'] = ini_get('mysqli.default_user');
if (empty($config['password'])) $config['password'] = ini_get('mysqli.default_password');
if (empty($config['host'])) {
if ($config['username'] === NULL) $config['username'] = ini_get('mysqli.default_user');
if ($config['password'] === NULL) $config['password'] = ini_get('mysqli.default_password');
if (!isset($config['host'])) {
$config['host'] = ini_get('mysqli.default_host');
if (empty($config['port'])) ini_get('mysqli.default_port');
if (empty($config['host'])) $config['host'] = 'localhost';
if (!isset($config['port'])) ini_get('mysqli.default_port');
if (!isset($config['host'])) $config['host'] = 'localhost';
}
if (!isset($config['database'])) $config['database'] = NULL;
parent::__construct($config);
}
@@ -62,7 +65,7 @@ class DibiMySqliDriver extends DibiDriver
throw new DibiDatabaseException(mysqli_connect_error(), mysqli_connect_errno());
}
if (!empty($config['charset'])) {
if (isset($config['charset'])) {
mysqli_query($connection, "SET NAMES '" . $config['charset'] . "'");
}

View File

@@ -39,22 +39,14 @@ class DibiOdbcDriver extends DibiDriver
*/
public function __construct($config)
{
self::prepare($config, 'username', 'user');
self::prepare($config, 'password', 'pass');
self::prepare($config, 'database');
// default values
if (empty($config['username'])) $config['username'] = ini_get('odbc.default_user');
if (empty($config['password'])) $config['password'] = ini_get('odbc.default_pw');
if (empty($config['database'])) $config['database'] = ini_get('odbc.default_db');
if (empty($config['username'])) {
throw new DibiException("Username must be specified");
}
if (empty($config['password'])) {
throw new DibiException("Password must be specified");
}
if (empty($config['database'])) {
throw new DibiException("Database must be specified");
}
if ($config['username'] === NULL) $config['username'] = ini_get('odbc.default_user');
if ($config['password'] === NULL) $config['password'] = ini_get('odbc.default_pw');
if ($config['database'] === NULL) $config['database'] = ini_get('odbc.default_db');
parent::__construct($config);
}

View File

@@ -39,12 +39,8 @@ class DibiPostgreDriver extends DibiDriver
*/
public function __construct($config)
{
if (empty($config['string'])) {
throw new DibiException("Connection string must be specified");
}
if (empty($config['type'])) $config['type'] = NULL;
self::prepare($config, 'database', 'string');
self::prepare($config, 'type');
parent::__construct($config);
}
@@ -59,16 +55,16 @@ class DibiPostgreDriver extends DibiDriver
$config = $this->getConfig();
if (isset($config['persistent'])) {
$connection = @pg_connect($config['string'], $config['type']);
$connection = @pg_connect($config['database'], $config['type']);
} else {
$connection = @pg_pconnect($config['string'], $config['type']);
$connection = @pg_pconnect($config['database'], $config['type']);
}
if (!is_resource($connection)) {
throw new DibiDatabaseException(pg_last_error());
}
if (!empty($config['charset'])) {
if (isset($config['charset'])) {
@pg_set_client_encoding($connection, $config['charset']);
// don't handle this error...
}
@@ -115,7 +111,7 @@ class DibiPostgreDriver extends DibiDriver
public function insertId($sequence = NULL)
{
if (empty($sequence)) {
if ($sequence === NULL) {
// PostgreSQL 8.1 is needed
$res = $this->doQuery("SELECT LASTVAL() AS seq");
} else {

View File

@@ -33,12 +33,8 @@ class DibiSqliteDriver extends DibiDriver
*/
public function __construct($config)
{
if (empty($config['database'])) {
throw new DibiException("Database must be specified");
}
self::prepare($config, 'database', 'file');
if (!isset($config['mode'])) $config['mode'] = 0666;
parent::__construct($config);
}

View File

@@ -31,10 +31,9 @@ class DibiMsSqlDriver extends DibiDriver
*/
public function __construct($config)
{
if (!isset($config['host'])) $config['host'] = NULL;
if (!isset($config['username'])) $config['username'] = NULL;
if (!isset($config['password'])) $config['password'] = NULL;
self::prepare($config, 'username', 'user');
self::prepare($config, 'password', 'pass');
self::prepare($config, 'host');
parent::__construct($config);
}
@@ -58,7 +57,7 @@ class DibiMsSqlDriver extends DibiDriver
throw new DibiDatabaseException("Can't connect to DB");
}
if (!empty($config['database']) && !@mssql_select_db($config['database'], $connection)) {
if (isset($config['database']) && !@mssql_select_db($config['database'], $connection)) {
throw new DibiDatabaseException("Can't select DB '$config[database]'");
}

View File

@@ -35,17 +35,10 @@ class DibiOracleDriver extends DibiDriver
*/
public function __construct($config)
{
if (empty($config['username'])) {
throw new DibiException("Username must be specified");
}
if (empty($config['password'])) {
throw new DibiException("Password must be specified");
}
if (!isset($config['db'])) $config['db'] = NULL;
if (!isset($config['charset'])) $config['charset'] = NULL;
self::prepare($config, 'username', 'user');
self::prepare($config, 'password', 'pass');
self::prepare($config, 'database', 'db');
self::prepare($config, 'charset');
parent::__construct($config);
}
@@ -58,7 +51,7 @@ class DibiOracleDriver extends DibiDriver
}
$config = $this->getConfig();
$connection = @oci_new_connect($config['username'], $config['password'], $config['db'], $config['charset']);
$connection = @oci_new_connect($config['username'], $config['password'], $config['database'], $config['charset']);
if (!$connection) {
$err = oci_error();

View File

@@ -33,13 +33,9 @@ class DibiPdoDriver extends DibiDriver
*/
public function __construct($config)
{
if (empty($config['dsn'])) {
throw new DibiException("DSN must be specified");
}
if (empty($config['username'])) $config['username'] = NULL;
if (empty($config['password'])) $config['password'] = NULL;
self::prepare($config, 'username', 'user');
self::prepare($config, 'password', 'pass');
self::prepare($config, 'dsn');
parent::__construct($config);
}

View File

@@ -166,6 +166,28 @@ abstract class DibiDriver
/**
* Apply configuration alias or default values
*
* @param array connect configuration
* @param string key
* @param string alias key
* @return void
*/
protected static function prepare(&$config, $key, $alias=NULL)
{
if (isset($config[$key])) return;
if ($alias !== NULL && isset($config[$alias])) {
$config[$key] = $config[$alias];
unset($config[$alias]);
} else {
$config[$key] = NULL;
}
}
/**
* Internal: Executes the SQL query
*

View File

@@ -38,4 +38,4 @@ whitespaces are removed.
-----
For more information, visit the author's weblog (in czech language):
http://www.dgx.cz/trine/
http://latrine.dgx.cz/