diff --git a/dibi/drivers/mssql.php b/dibi/drivers/mssql.php index c500526..3aa9e19 100644 --- a/dibi/drivers/mssql.php +++ b/dibi/drivers/mssql.php @@ -29,6 +29,7 @@ * - 'persistent' - try to find a persistent link? * - 'database' - the database name to select * - 'lazy' - if TRUE, connection will be established only when required + * - 'resource' - connection resource (optional) * * @author David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl @@ -67,7 +68,9 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver DibiConnection::alias($config, 'password', 'pass'); DibiConnection::alias($config, 'host', 'hostname'); - if (empty($config['persistent'])) { + if (isset($config['resource'])) { + $this->connection = $config['resource']; + } elseif (empty($config['persistent'])) { $this->connection = @mssql_connect($config['host'], $config['username'], $config['password'], TRUE); // intentionally @ } else { $this->connection = @mssql_pconnect($config['host'], $config['username'], $config['password']); // intentionally @ diff --git a/dibi/drivers/mssql2005.php b/dibi/drivers/mssql2005.php index 12af80c..7ee422d 100644 --- a/dibi/drivers/mssql2005.php +++ b/dibi/drivers/mssql2005.php @@ -27,6 +27,7 @@ * - 'options' - connection info array {@link http://msdn.microsoft.com/en-us/library/cc296161(SQL.90).aspx} * - 'lazy' - if TRUE, connection will be established only when required * - 'charset' - character encoding to set (default is UTF-8) + * - 'resource' - connection resource (optional) * * @author David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl @@ -66,7 +67,9 @@ class DibiMsSql2005Driver extends DibiObject implements IDibiDriver { DibiConnection::alias($config, 'host', 'hostname'); - if (isset($config['options'])) { + if (isset($config['resource'])) { + $this->connection = $config['resource']; + } elseif (isset($config['options'])) { $this->connection = sqlsrv_connect($config['host'], $config['options']); } else { $this->connection = sqlsrv_connect($config['host']); diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index 3b8be27..7d02f65 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -35,6 +35,7 @@ * - 'options' - driver specific constants (MYSQL_*) * - 'sqlmode' - see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html * - 'lazy' - if TRUE, connection will be established only when required + * - 'resource' - connection resource (optional) * * @author David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl @@ -77,30 +78,35 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver DibiConnection::alias($config, 'host', 'hostname'); DibiConnection::alias($config, 'options'); - // default values - if (!isset($config['username'])) $config['username'] = ini_get('mysql.default_user'); - if (!isset($config['password'])) $config['password'] = ini_get('mysql.default_password'); - if (!isset($config['host'])) { - $host = ini_get('mysql.default_host'); - if ($host) { - $config['host'] = $host; - $config['port'] = ini_get('mysql.default_port'); - } else { - if (!isset($config['socket'])) $config['socket'] = ini_get('mysql.default_socket'); - $config['host'] = NULL; + if (isset($config['resource'])) { + $this->connection = $config['resource']; + + } else { + // default values + if (!isset($config['username'])) $config['username'] = ini_get('mysql.default_user'); + if (!isset($config['password'])) $config['password'] = ini_get('mysql.default_password'); + if (!isset($config['host'])) { + $host = ini_get('mysql.default_host'); + if ($host) { + $config['host'] = $host; + $config['port'] = ini_get('mysql.default_port'); + } else { + if (!isset($config['socket'])) $config['socket'] = ini_get('mysql.default_socket'); + $config['host'] = NULL; + } } - } - if (empty($config['socket'])) { - $host = $config['host'] . (empty($config['port']) ? '' : ':' . $config['port']); - } else { - $host = ':' . $config['socket']; - } + if (empty($config['socket'])) { + $host = $config['host'] . (empty($config['port']) ? '' : ':' . $config['port']); + } else { + $host = ':' . $config['socket']; + } - if (empty($config['persistent'])) { - $this->connection = @mysql_connect($host, $config['username'], $config['password'], TRUE, $config['options']); // intentionally @ - } else { - $this->connection = @mysql_pconnect($host, $config['username'], $config['password'], $config['options']); // intentionally @ + if (empty($config['persistent'])) { + $this->connection = @mysql_connect($host, $config['username'], $config['password'], TRUE, $config['options']); // intentionally @ + } else { + $this->connection = @mysql_pconnect($host, $config['username'], $config['password'], $config['options']); // intentionally @ + } } if (!is_resource($this->connection)) { diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index 2c4fa92..9ddad7b 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -35,6 +35,7 @@ * - 'options' - driver specific constants (MYSQLI_*) * - 'sqlmode' - see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html * - 'lazy' - if TRUE, connection will be established only when required + * - 'resource' - connection resource (optional) * * @author David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl @@ -78,27 +79,32 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver DibiConnection::alias($config, 'options'); DibiConnection::alias($config, 'database'); - // default values - if (!isset($config['username'])) $config['username'] = ini_get('mysqli.default_user'); - if (!isset($config['password'])) $config['password'] = ini_get('mysqli.default_pw'); - if (!isset($config['socket'])) $config['socket'] = ini_get('mysqli.default_socket'); - if (!isset($config['port'])) $config['port'] = NULL; - if (!isset($config['host'])) { - $host = ini_get('mysqli.default_host'); - if ($host) { - $config['host'] = $host; - $config['port'] = ini_get('mysqli.default_port'); - } else { - $config['host'] = NULL; - $config['port'] = NULL; + if (isset($config['resource'])) { + $this->connection = $config['resource']; + + } else { + // default values + if (!isset($config['username'])) $config['username'] = ini_get('mysqli.default_user'); + if (!isset($config['password'])) $config['password'] = ini_get('mysqli.default_pw'); + if (!isset($config['socket'])) $config['socket'] = ini_get('mysqli.default_socket'); + if (!isset($config['port'])) $config['port'] = NULL; + if (!isset($config['host'])) { + $host = ini_get('mysqli.default_host'); + if ($host) { + $config['host'] = $host; + $config['port'] = ini_get('mysqli.default_port'); + } else { + $config['host'] = NULL; + $config['port'] = NULL; + } } - } - $this->connection = mysqli_init(); - @mysqli_real_connect($this->connection, $config['host'], $config['username'], $config['password'], $config['database'], $config['port'], $config['socket'], $config['options']); // intentionally @ + $this->connection = mysqli_init(); + @mysqli_real_connect($this->connection, $config['host'], $config['username'], $config['password'], $config['database'], $config['port'], $config['socket'], $config['options']); // intentionally @ - if ($errno = mysqli_connect_errno()) { - throw new DibiDriverException(mysqli_connect_error(), $errno); + if ($errno = mysqli_connect_errno()) { + throw new DibiDriverException(mysqli_connect_error(), $errno); + } } if (isset($config['charset'])) { diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index 9477312..9341dcf 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -28,6 +28,7 @@ * - 'password' (or 'pass') * - 'persistent' - try to find a persistent link? * - 'lazy' - if TRUE, connection will be established only when required + * - 'resource' - connection resource (optional) * * @author David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl @@ -68,15 +69,19 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver DibiConnection::alias($config, 'username', 'user'); DibiConnection::alias($config, 'password', 'pass'); - // default values - if (!isset($config['username'])) $config['username'] = ini_get('odbc.default_user'); - if (!isset($config['password'])) $config['password'] = ini_get('odbc.default_pw'); - if (!isset($config['dsn'])) $config['dsn'] = ini_get('odbc.default_db'); - - if (empty($config['persistent'])) { - $this->connection = @odbc_connect($config['dsn'], $config['username'], $config['password']); // intentionally @ + if (isset($config['resource'])) { + $this->connection = $config['resource']; } else { - $this->connection = @odbc_pconnect($config['dsn'], $config['username'], $config['password']); // intentionally @ + // default values + if (!isset($config['username'])) $config['username'] = ini_get('odbc.default_user'); + if (!isset($config['password'])) $config['password'] = ini_get('odbc.default_pw'); + if (!isset($config['dsn'])) $config['dsn'] = ini_get('odbc.default_db'); + + if (empty($config['persistent'])) { + $this->connection = @odbc_connect($config['dsn'], $config['username'], $config['password']); // intentionally @ + } else { + $this->connection = @odbc_pconnect($config['dsn'], $config['username'], $config['password']); // intentionally @ + } } if (!is_resource($this->connection)) { diff --git a/dibi/drivers/oracle.php b/dibi/drivers/oracle.php index 78b7565..1a18dd0 100644 --- a/dibi/drivers/oracle.php +++ b/dibi/drivers/oracle.php @@ -28,6 +28,7 @@ * - 'password' (or 'pass') * - 'charset' - character encoding to set * - 'lazy' - if TRUE, connection will be established only when required + * - 'resource' - connection resource (optional) * * @author David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl @@ -70,7 +71,11 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver DibiConnection::alias($config, 'database', 'db'); DibiConnection::alias($config, 'charset'); - $this->connection = @oci_new_connect($config['username'], $config['password'], $config['database'], $config['charset']); // intentionally @ + if (isset($config['resource'])) { + $this->connection = $config['resource']; + } else { + $this->connection = @oci_new_connect($config['username'], $config['password'], $config['database'], $config['charset']); // intentionally @ + } if (!$this->connection) { $err = oci_error(); diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index 4af4ce2..7f6a51d 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -27,7 +27,7 @@ * - 'username' (or 'user') * - 'password' (or 'pass') * - 'options' - driver specific options array - * - 'pdo' - PDO object (optional) + * - 'resource' - PDO object (optional) * - 'lazy' - if TRUE, connection will be established only when required * * @author David Grudl @@ -69,11 +69,11 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver DibiConnection::alias($config, 'username', 'user'); DibiConnection::alias($config, 'password', 'pass'); DibiConnection::alias($config, 'dsn'); - DibiConnection::alias($config, 'pdo'); + DibiConnection::alias($config, 'resource', 'pdo'); DibiConnection::alias($config, 'options'); - if ($config['pdo'] instanceof PDO) { - $this->connection = $config['pdo']; + if ($config['resource'] instanceof PDO) { + $this->connection = $config['resource']; } else try { $this->connection = new PDO($config['dsn'], $config['username'], $config['password'], $config['options']); diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index 5d23cbf..3ef1630 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -29,6 +29,7 @@ * - 'charset' - character encoding to set * - 'schema' - the schema search path * - 'lazy' - if TRUE, connection will be established only when required + * - 'resource' - connection resource (optional) * * @author David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl @@ -66,23 +67,28 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver */ public function connect(array &$config) { - if (isset($config['string'])) { - $string = $config['string']; - } else { - $string = ''; - foreach (array('host','hostaddr','port','dbname','user','password','connect_timeout','options','sslmode','service') as $key) { - if (isset($config[$key])) $string .= $key . '=' . $config[$key] . ' '; - } - } + if (isset($config['resource'])) { + $this->connection = $config['resource']; - DibiDriverException::tryError(); - if (empty($config['persistent'])) { - $this->connection = pg_connect($string, PGSQL_CONNECT_FORCE_NEW); } else { - $this->connection = pg_pconnect($string, PGSQL_CONNECT_FORCE_NEW); - } - if (DibiDriverException::catchError($msg)) { - throw new DibiDriverException($msg, 0); + if (isset($config['string'])) { + $string = $config['string']; + } else { + $string = ''; + foreach (array('host','hostaddr','port','dbname','user','password','connect_timeout','options','sslmode','service') as $key) { + if (isset($config[$key])) $string .= $key . '=' . $config[$key] . ' '; + } + } + + DibiDriverException::tryError(); + if (empty($config['persistent'])) { + $this->connection = pg_connect($string, PGSQL_CONNECT_FORCE_NEW); + } else { + $this->connection = pg_pconnect($string, PGSQL_CONNECT_FORCE_NEW); + } + if (DibiDriverException::catchError($msg)) { + throw new DibiDriverException($msg, 0); + } } if (!is_resource($this->connection)) { diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index 9fcc833..9da4be3 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -31,6 +31,7 @@ * - 'formatDateTime' - how to format datetime in SQL (@see date) * - 'dbcharset' - database character encoding (will be converted to 'charset') * - 'charset' - character encoding to set (default is UTF-8) + * - 'resource' - connection resource (optional) * * @author David Grudl * @copyright Copyright (c) 2005, 2009 David Grudl @@ -79,7 +80,9 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver $this->fmtDateTime = isset($config['formatDateTime']) ? $config['formatDateTime'] : 'U'; $errorMsg = ''; - if (empty($config['persistent'])) { + if (isset($config['resource'])) { + $this->connection = $config['resource']; + } elseif (empty($config['persistent'])) { $this->connection = @sqlite_open($config['database'], 0666, $errorMsg); // intentionally @ } else { $this->connection = @sqlite_popen($config['database'], 0666, $errorMsg); // intentionally @ diff --git a/dibi/libs/DibiFluent.php b/dibi/libs/DibiFluent.php index fd6c2c8..d5fa606 100644 --- a/dibi/libs/DibiFluent.php +++ b/dibi/libs/DibiFluent.php @@ -119,7 +119,7 @@ class DibiFluent extends DibiObject if ($arg === TRUE) { // flag $args = array(); - } elseif (is_string($arg) && preg_match('#^[a-z][a-z0-9_.]*$#i', $arg)) { // identifier + } elseif (is_string($arg) && preg_match('#^[a-z:_][a-z0-9_.:]*$#i', $arg)) { // identifier $args = array('%n', $arg); } elseif ($arg instanceof self) {