From 39c62c1cd514b30db16067da7921a4c54406a84d Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 26 Oct 2007 17:44:24 +0000 Subject: [PATCH] * added configuration aliases (user -> username, pass -> password) * dibi::$defaultDriver --- dibi/dibi.php | 12 +++++++++--- dibi/drivers/mysql.php | 19 +++++++++++-------- dibi/drivers/mysqli.php | 17 ++++++++++------- dibi/drivers/odbc.php | 22 +++++++--------------- dibi/drivers/postgre.php | 16 ++++++---------- dibi/drivers/sqlite.php | 6 +----- dibi/drivers/untested/mssql.php | 9 ++++----- dibi/drivers/untested/oracle.php | 17 +++++------------ dibi/drivers/untested/pdo.php | 10 +++------- dibi/libs/driver.php | 22 ++++++++++++++++++++++ readme.txt | 2 +- 11 files changed, 79 insertions(+), 73 deletions(-) diff --git a/dibi/dibi.php b/dibi/dibi.php index 96cabdfd..5eb15715 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -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"; diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index d08284ec..d44564e4 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -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)); } diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index 8200fe5d..597fb23b 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -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'] . "'"); } diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index 824ed7c6..6216ef4c 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -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); } diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index 297e0141..509e378e 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -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 { diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index 73bbc8ad..72d1b146 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -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); } diff --git a/dibi/drivers/untested/mssql.php b/dibi/drivers/untested/mssql.php index ca73fa3a..da86fbba 100644 --- a/dibi/drivers/untested/mssql.php +++ b/dibi/drivers/untested/mssql.php @@ -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]'"); } diff --git a/dibi/drivers/untested/oracle.php b/dibi/drivers/untested/oracle.php index 5a616693..3cd308a7 100644 --- a/dibi/drivers/untested/oracle.php +++ b/dibi/drivers/untested/oracle.php @@ -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(); diff --git a/dibi/drivers/untested/pdo.php b/dibi/drivers/untested/pdo.php index 3324d4e6..cf06d89c 100644 --- a/dibi/drivers/untested/pdo.php +++ b/dibi/drivers/untested/pdo.php @@ -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); } diff --git a/dibi/libs/driver.php b/dibi/libs/driver.php index b8e02aed..f903a533 100644 --- a/dibi/libs/driver.php +++ b/dibi/libs/driver.php @@ -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 * diff --git a/readme.txt b/readme.txt index adecc88f..f723df10 100644 --- a/readme.txt +++ b/readme.txt @@ -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/