1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 22:26:43 +02:00

* removed static factory DibiDriver::connect()

* added support for lazy connections
This commit is contained in:
David Grudl
2007-04-25 06:18:06 +00:00
parent 166f716091
commit af7c4de14a
9 changed files with 194 additions and 117 deletions

View File

@@ -12,7 +12,7 @@
* @license GNU GENERAL PUBLIC LICENSE version 2
* @package dibi
* @category Database
* @version 0.7h (Revision: $WCREV$, Date: $WCDATE$)
* @version 0.8 (Revision: $WCREV$, Date: $WCDATE$)
*/
@@ -26,7 +26,7 @@
*/
define('DIBI', '0.7h (Revision: $WCREV$, Date: $WCDATE$)');
define('DIBI', '0.8 (Revision: $WCREV$, Date: $WCDATE$)');
if (version_compare(PHP_VERSION , '5.0.3', '<'))
@@ -172,10 +172,10 @@ class dibi
throw new DibiException("Unable to create instance of dibi driver class '$className'.");
}
// create connection object and store in list
/** like $conn = $className::connect($config); */
self::$conn = self::$registry[$name] = call_user_func(array($className, 'connect'), $config);
self::$conn = self::$registry[$name] = new $className($config);
if (dibi::$logAll) dibi::log("OK: connected to DB '$config[driver]'");
}

View File

@@ -21,7 +21,6 @@ if (!defined('DIBI')) die();
class DibiMySqlDriver extends DibiDriver
{
private
$conn,
$insertId = FALSE,
$affectedRows = FALSE;
@@ -35,10 +34,10 @@ class DibiMySqlDriver extends DibiDriver
/**
* Driver factory
* @throw DibiException
* @param array connect configuration
* @throw DibiException
*/
public static function connect($config)
public function __construct($config)
{
if (!extension_loaded('mysql'))
throw new DibiException("PHP extension 'mysql' is not loaded");
@@ -57,6 +56,15 @@ class DibiMySqlDriver extends DibiDriver
else
$host = $config['host'] . (empty($config['port']) ? '' : ':'.$config['port']);
parent::__construct($config);
}
protected function connect()
{
$config = $this->config;
// some errors aren't handled. Must use $php_errormsg
if (function_exists('ini_set'))
$save = ini_set('track_errors', TRUE);
@@ -92,25 +100,24 @@ class DibiMySqlDriver extends DibiDriver
));
}
$obj = new self($config);
$obj->conn = $conn;
return $obj;
return $conn;
}
public function nativeQuery($sql)
{
$this->insertId = $this->affectedRows = FALSE;
$res = @mysql_query($sql, $this->conn);
$conn = $this->getResource();
$res = @mysql_query($sql, $conn);
if ($res === FALSE) return FALSE;
$this->affectedRows = mysql_affected_rows($this->conn);
$this->affectedRows = mysql_affected_rows($conn);
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
$this->insertId = mysql_insert_id($this->conn);
$this->insertId = mysql_insert_id($conn);
if ($this->insertId < 1) $this->insertId = FALSE;
if (is_resource($res))
@@ -134,36 +141,38 @@ class DibiMySqlDriver extends DibiDriver
public function begin()
{
return mysql_query('BEGIN', $this->conn);
return mysql_query('BEGIN', $this->getResource());
}
public function commit()
{
return mysql_query('COMMIT', $this->conn);
return mysql_query('COMMIT', $this->getResource());
}
public function rollback()
{
return mysql_query('ROLLBACK', $this->conn);
return mysql_query('ROLLBACK', $this->getResource());
}
public function errorInfo()
{
$conn = $this->getResource();
return array(
'message' => mysql_error($this->conn),
'code' => mysql_errno($this->conn),
'message' => mysql_error($conn),
'code' => mysql_errno($conn),
);
}
public function escape($value, $appendQuotes=TRUE)
{
$conn = $this->getResource();
return $appendQuotes
? "'" . mysql_real_escape_string($value, $this->conn) . "'"
: mysql_real_escape_string($value, $this->conn);
? "'" . mysql_real_escape_string($value, $conn) . "'"
: mysql_real_escape_string($value, $conn);
}
@@ -192,13 +201,6 @@ class DibiMySqlDriver extends DibiDriver
}
/* is this really needed?
public function getResource()
{
return $this->conn;
}
*/
} // DibiMySqlDriver

View File

@@ -21,7 +21,6 @@ if (!defined('DIBI')) die();
class DibiMySqliDriver extends DibiDriver
{
private
$conn,
$insertId = FALSE,
$affectedRows = FALSE;
@@ -35,7 +34,11 @@ class DibiMySqliDriver extends DibiDriver
public static function connect($config)
/**
* @param array connect configuration
* @throw DibiException
*/
public function __construct($config)
{
if (!extension_loaded('mysqli'))
throw new DibiException("PHP extension 'mysqli' is not loaded");
@@ -50,6 +53,15 @@ class DibiMySqliDriver extends DibiDriver
}
if (!isset($config['database'])) $config['database'] = NULL;
parent::__construct($config);
}
protected function connect()
{
$config = $this->config;
$conn = @mysqli_connect($config['host'], $config['username'], $config['password'], $config['database'], $config['port']);
if (!$conn)
@@ -61,9 +73,7 @@ class DibiMySqliDriver extends DibiDriver
if (!empty($config['charset']))
mysqli_query($conn, "SET NAMES '" . $config['charset'] . "'");
$obj = new self($config);
$obj->conn = $conn;
return $obj;
return $conn;
}
@@ -71,14 +81,15 @@ class DibiMySqliDriver extends DibiDriver
public function nativeQuery($sql)
{
$this->insertId = $this->affectedRows = FALSE;
$res = @mysqli_query($this->conn, $sql);
$conn = $this->getResource();
$res = @mysqli_query($conn, $sql);
if ($res === FALSE) return FALSE;
$this->affectedRows = mysqli_affected_rows($this->conn);
$this->affectedRows = mysqli_affected_rows($conn);
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
$this->insertId = mysqli_insert_id($this->conn);
$this->insertId = mysqli_insert_id($conn);
if ($this->insertId < 1) $this->insertId = FALSE;
if (is_object($res))
@@ -102,31 +113,34 @@ class DibiMySqliDriver extends DibiDriver
public function begin()
{
return mysqli_autocommit($this->conn, FALSE);
return mysqli_autocommit($this->getResource(), FALSE);
}
public function commit()
{
$ok = mysqli_commit($this->conn);
mysqli_autocommit($this->conn, TRUE);
$conn = $this->getResource();
$ok = mysqli_commit($conn);
mysqli_autocommit($conn, TRUE);
return $ok;
}
public function rollback()
{
$ok = mysqli_rollback($this->conn);
mysqli_autocommit($this->conn, TRUE);
$conn = $this->getResource();
$ok = mysqli_rollback($conn);
mysqli_autocommit($conn, TRUE);
return $ok;
}
public function errorInfo()
{
$conn = $this->getResource();
return array(
'message' => mysqli_error($this->conn),
'code' => mysqli_errno($this->conn),
'message' => mysqli_error($conn),
'code' => mysqli_errno($conn),
);
}
@@ -135,9 +149,10 @@ class DibiMySqliDriver extends DibiDriver
public function escape($value, $appendQuotes=TRUE)
{
$conn = $this->getResource();
return $appendQuotes
? "'" . mysqli_real_escape_string($this->conn, $value) . "'"
: mysqli_real_escape_string($this->conn, $value);
? "'" . mysqli_real_escape_string($conn, $value) . "'"
: mysqli_real_escape_string($conn, $value);
}

View File

@@ -21,7 +21,6 @@ if (!defined('DIBI')) die();
class DibiOdbcDriver extends DibiDriver
{
private
$conn,
$affectedRows = FALSE;
public
@@ -34,7 +33,11 @@ class DibiOdbcDriver extends DibiDriver
public static function connect($config)
/**
* @param array connect configuration
* @throw DibiException
*/
public function __construct($config)
{
if (!extension_loaded('odbc'))
throw new DibiException("PHP extension 'odbc' is not loaded");
@@ -53,6 +56,15 @@ class DibiOdbcDriver extends DibiDriver
if (empty($config['database']))
throw new DibiException("Database must be specified");
parent::__construct($config);
}
protected function connect()
{
$config = $this->config;
if (empty($config['persistent']))
$conn = @odbc_connect($config['database'], $config['username'], $config['password']);
else
@@ -64,9 +76,7 @@ class DibiOdbcDriver extends DibiDriver
'code' => odbc_error(),
));
$obj = new self($config);
$obj->conn = $conn;
return $obj;
return $conn;
}
@@ -75,11 +85,12 @@ class DibiOdbcDriver extends DibiDriver
{
$this->affectedRows = FALSE;
$res = @odbc_exec($this->conn, $sql);
$conn = $this->getResource();
$res = @odbc_exec($conn, $sql);
if ($res === FALSE) return FALSE;
$this->affectedRows = odbc_num_rows($this->conn);
$this->affectedRows = odbc_num_rows($conn);
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
if (is_resource($res))
@@ -103,31 +114,34 @@ class DibiOdbcDriver extends DibiDriver
public function begin()
{
return odbc_autocommit($this->conn, FALSE);
return odbc_autocommit($this->getResource(), FALSE);
}
public function commit()
{
$ok = odbc_commit($this->conn);
odbc_autocommit($this->conn, TRUE);
$conn = $this->getResource();
$ok = odbc_commit($conn);
odbc_autocommit($conn, TRUE);
return $ok;
}
public function rollback()
{
$ok = odbc_rollback($this->conn);
odbc_autocommit($this->conn, TRUE);
$conn = $this->getResource();
$ok = odbc_rollback($conn);
odbc_autocommit($conn, TRUE);
return $ok;
}
public function errorInfo()
{
$conn = $this->getResource();
return array(
'message' => odbc_errormsg($this->conn),
'code' => odbc_error($this->conn),
'message' => odbc_errormsg($conn),
'code' => odbc_error($conn),
);
}

View File

@@ -20,9 +20,6 @@ if (!defined('DIBI')) die();
*/
class DibiPdoDriver extends DibiDriver
{
/** @var PDO */
private $conn;
private $affectedRows = FALSE;
public
@@ -35,7 +32,11 @@ class DibiPdoDriver extends DibiDriver
public static function connect($config)
/**
* @param array connect configuration
* @throw DibiException
*/
public function __construct($config)
{
if (!extension_loaded('pdo'))
throw new DibiException("PHP extension 'pdo' is not loaded");
@@ -46,11 +47,15 @@ class DibiPdoDriver extends DibiDriver
if (empty($config['username'])) $config['username'] = NULL;
if (empty($config['password'])) $config['password'] = NULL;
$conn = new PDO($config['dsn'], $config['username'], $config['password']);
parent::__construct($config);
}
$obj = new self($config);
$obj->conn = $conn;
return $obj;
protected function connect()
{
$config = $this->config;
return new PDO($config['dsn'], $config['username'], $config['password']);
}
@@ -60,7 +65,7 @@ class DibiPdoDriver extends DibiDriver
$this->affectedRows = FALSE;
// TODO: or exec() ?
$res = $this->conn->query($sql);
$res = $this->getResource()->query($sql);
if ($res === FALSE) return FALSE;
@@ -79,31 +84,31 @@ class DibiPdoDriver extends DibiDriver
public function insertId()
{
return $this->conn->lastInsertId();
return $this->getResource()->lastInsertId();
}
public function begin()
{
return $this->conn->beginTransaction();
return $this->getResource()->beginTransaction();
}
public function commit()
{
return $this->conn->commit();
return $this->getResource()->commit();
}
public function rollback()
{
return $this->conn->rollBack();
return $this->getResource()->rollBack();
}
public function errorInfo()
{
$error = $this->conn->errorInfo();
$error = $this->getResource()->errorInfo();
return array(
'message' => $error[2],
'code' => $error[1],
@@ -118,7 +123,7 @@ class DibiPdoDriver extends DibiDriver
trigger_error('dibi: escaping without qoutes is not supported by PDO', E_USER_WARNING);
return NULL;
}
return $this->conn->quote($value);
return $this->getResource()->quote($value);
}

View File

@@ -21,7 +21,6 @@ if (!defined('DIBI')) die();
class DibiPostgreDriver extends DibiDriver
{
private
$conn,
$affectedRows = FALSE;
public
@@ -34,7 +33,11 @@ class DibiPostgreDriver extends DibiDriver
public static function connect($config)
/**
* @param array connect configuration
* @throw DibiException
*/
public function __construct($config)
{
if (!extension_loaded('pgsql'))
throw new DibiException("PHP extension 'pgsql' is not loaded");
@@ -44,6 +47,15 @@ class DibiPostgreDriver extends DibiDriver
if (empty($config['type'])) $config['type'] = NULL;
parent::__construct($config);
}
protected function connect()
{
$config = $this->config;
if (isset($config['persistent']))
$conn = @pg_connect($config['string'], $config['type']);
else
@@ -58,10 +70,7 @@ class DibiPostgreDriver extends DibiDriver
@pg_set_client_encoding($conn, $config['charset']);
// don't handle this error...
}
$obj = new self($config);
$obj->conn = $conn;
return $obj;
return $conn;
}
@@ -70,11 +79,12 @@ class DibiPostgreDriver extends DibiDriver
{
$this->affectedRows = FALSE;
$res = @pg_query($this->conn, $sql);
$conn = $this->getResource();
$res = @pg_query($conn, $sql);
if ($res === FALSE) return FALSE;
$this->affectedRows = pg_affected_rows($this->conn);
$this->affectedRows = pg_affected_rows($conn);
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
if (is_resource($res))
@@ -98,26 +108,26 @@ class DibiPostgreDriver extends DibiDriver
public function begin()
{
return pg_query($this->conn, 'BEGIN');
return pg_query($this->getResource(), 'BEGIN');
}
public function commit()
{
return pg_query($this->conn, 'COMMIT');
return pg_query($this->getResource(), 'COMMIT');
}
public function rollback()
{
return pg_query($this->conn, 'ROLLBACK');
return pg_query($this->getResource(), 'ROLLBACK');
}
public function errorInfo()
{
return array(
'message' => pg_last_error($this->conn),
'message' => pg_last_error($this->getResource()),
'code' => NULL,
);
}

View File

@@ -21,7 +21,6 @@ if (!defined('DIBI')) die();
class DibiSqliteDriver extends DibiDriver
{
private
$conn,
$insertId = FALSE,
$affectedRows = FALSE;
@@ -35,7 +34,11 @@ class DibiSqliteDriver extends DibiDriver
public static function connect($config)
/**
* @param array connect configuration
* @throw DibiException
*/
public function __construct($config)
{
if (!extension_loaded('sqlite'))
throw new DibiException("PHP extension 'sqlite' is not loaded");
@@ -46,6 +49,15 @@ class DibiSqliteDriver extends DibiDriver
if (!isset($config['mode']))
$config['mode'] = 0666;
parent::__construct($config);
}
protected function connect()
{
$config = $this->config;
$errorMsg = '';
if (empty($config['persistent']))
$conn = @sqlite_open($config['database'], $config['mode'], $errorMsg);
@@ -57,9 +69,7 @@ class DibiSqliteDriver extends DibiDriver
'message' => $errorMsg,
));
$obj = new self($config);
$obj->conn = $conn;
return $obj;
return $conn;
}
@@ -68,14 +78,15 @@ class DibiSqliteDriver extends DibiDriver
{
$this->insertId = $this->affectedRows = FALSE;
$res = @sqlite_query($this->conn, $sql, SQLITE_ASSOC);
$conn = $this->getResource();
$res = @sqlite_query($conn, $sql, SQLITE_ASSOC);
if ($res === FALSE) return FALSE;
$this->affectedRows = sqlite_changes($this->conn);
$this->affectedRows = sqlite_changes($conn);
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
$this->insertId = sqlite_last_insert_rowid($this->conn);
$this->insertId = sqlite_last_insert_rowid($conn);
if ($this->insertId < 1) $this->insertId = FALSE;
if (is_resource($res))
@@ -99,25 +110,25 @@ class DibiSqliteDriver extends DibiDriver
public function begin()
{
return sqlite_query($this->conn, 'BEGIN');
return sqlite_query($this->getResource(), 'BEGIN');
}
public function commit()
{
return sqlite_query($this->conn, 'COMMIT');
return sqlite_query($this->getResource(), 'COMMIT');
}
public function rollback()
{
return sqlite_query($this->conn, 'ROLLBACK');
return sqlite_query($this->getResource(), 'ROLLBACK');
}
public function errorInfo()
{
$code = sqlite_last_error($this->conn);
$code = sqlite_last_error($this->getResource());
return array(
'message' => sqlite_error_string($code),
'code' => $code,
@@ -135,7 +146,7 @@ class DibiSqliteDriver extends DibiDriver
public function delimite($value)
{
return '[' . $value . ']';
return '[' . str_replace('.', '].[', $value) . ']';
}

View File

@@ -27,6 +27,12 @@ abstract class DibiDriver
*/
protected $config;
/**
* Connection resource
* @var resource
*/
private $res;
/**
* Describes how convert some datatypes to SQL command
* @var array
@@ -39,31 +45,32 @@ abstract class DibiDriver
);
/**
* DibiDriver factory: creates object and connects to a database
*
* @param array connect configuration
* @return DibiDriver
* Creates object and (optionally) connects to a database
* @param array connect configuration
* @throw DibiException
*/
/*abstract disallowed since PHP 5.2*/ static public function connect($config) {}
/**
* Protected constructor. Must be initialized using the factory method.
* @see DibiDriver::connect()
* @param array connect configuration
*/
protected function __construct($config)
public function __construct($config)
{
$this->config = $config;
if (empty($config['lazy'])) $this->res = $this->connect();
}
/**
* Get the configuration descriptor used by connect() to connect to database.
* @see DibiDriver::connect()
* Connects to a database
* @throw DibiException
* @return resource
*/
abstract protected function connect();
/**
* Gets the configuration descriptor
* @see DibiDriver::__construct
* @return array
*/
public function getConfig()
@@ -73,6 +80,19 @@ abstract class DibiDriver
/**
* Returns the connection resource
* @return resource
*/
public function getResource()
{
if (!$this->res) $this->res = $this->connect();
return $this->res;
}
/**
* Generates and executes SQL query
*

View File

@@ -1,4 +1,4 @@
Dibi version 0.7h
Dibi version 0.8
Revision: $WCREV$
Date: $WCDATE$