mirror of
https://github.com/dg/dibi.git
synced 2025-08-07 22:56:35 +02:00
* removed static factory DibiDriver::connect()
* added support for lazy connections
This commit is contained in:
@@ -12,7 +12,7 @@
|
|||||||
* @license GNU GENERAL PUBLIC LICENSE version 2
|
* @license GNU GENERAL PUBLIC LICENSE version 2
|
||||||
* @package dibi
|
* @package dibi
|
||||||
* @category Database
|
* @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', '<'))
|
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'.");
|
throw new DibiException("Unable to create instance of dibi driver class '$className'.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// create connection object and store in list
|
// create connection object and store in list
|
||||||
/** like $conn = $className::connect($config); */
|
/** 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]'");
|
if (dibi::$logAll) dibi::log("OK: connected to DB '$config[driver]'");
|
||||||
}
|
}
|
||||||
|
@@ -21,7 +21,6 @@ if (!defined('DIBI')) die();
|
|||||||
class DibiMySqlDriver extends DibiDriver
|
class DibiMySqlDriver extends DibiDriver
|
||||||
{
|
{
|
||||||
private
|
private
|
||||||
$conn,
|
|
||||||
$insertId = FALSE,
|
$insertId = FALSE,
|
||||||
$affectedRows = FALSE;
|
$affectedRows = FALSE;
|
||||||
|
|
||||||
@@ -35,10 +34,10 @@ class DibiMySqlDriver extends DibiDriver
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Driver factory
|
* @param array connect configuration
|
||||||
* @throw DibiException
|
* @throw DibiException
|
||||||
*/
|
*/
|
||||||
public static function connect($config)
|
public function __construct($config)
|
||||||
{
|
{
|
||||||
if (!extension_loaded('mysql'))
|
if (!extension_loaded('mysql'))
|
||||||
throw new DibiException("PHP extension 'mysql' is not loaded");
|
throw new DibiException("PHP extension 'mysql' is not loaded");
|
||||||
@@ -57,6 +56,15 @@ class DibiMySqlDriver extends DibiDriver
|
|||||||
else
|
else
|
||||||
$host = $config['host'] . (empty($config['port']) ? '' : ':'.$config['port']);
|
$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
|
// some errors aren't handled. Must use $php_errormsg
|
||||||
if (function_exists('ini_set'))
|
if (function_exists('ini_set'))
|
||||||
$save = ini_set('track_errors', TRUE);
|
$save = ini_set('track_errors', TRUE);
|
||||||
@@ -92,25 +100,24 @@ class DibiMySqlDriver extends DibiDriver
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $conn;
|
||||||
$obj = new self($config);
|
|
||||||
$obj->conn = $conn;
|
|
||||||
return $obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public function nativeQuery($sql)
|
public function nativeQuery($sql)
|
||||||
{
|
{
|
||||||
$this->insertId = $this->affectedRows = FALSE;
|
$this->insertId = $this->affectedRows = FALSE;
|
||||||
$res = @mysql_query($sql, $this->conn);
|
$conn = $this->getResource();
|
||||||
|
$res = @mysql_query($sql, $conn);
|
||||||
|
|
||||||
if ($res === FALSE) return FALSE;
|
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;
|
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 ($this->insertId < 1) $this->insertId = FALSE;
|
||||||
|
|
||||||
if (is_resource($res))
|
if (is_resource($res))
|
||||||
@@ -134,36 +141,38 @@ class DibiMySqlDriver extends DibiDriver
|
|||||||
|
|
||||||
public function begin()
|
public function begin()
|
||||||
{
|
{
|
||||||
return mysql_query('BEGIN', $this->conn);
|
return mysql_query('BEGIN', $this->getResource());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function commit()
|
public function commit()
|
||||||
{
|
{
|
||||||
return mysql_query('COMMIT', $this->conn);
|
return mysql_query('COMMIT', $this->getResource());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function rollback()
|
public function rollback()
|
||||||
{
|
{
|
||||||
return mysql_query('ROLLBACK', $this->conn);
|
return mysql_query('ROLLBACK', $this->getResource());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function errorInfo()
|
public function errorInfo()
|
||||||
{
|
{
|
||||||
|
$conn = $this->getResource();
|
||||||
return array(
|
return array(
|
||||||
'message' => mysql_error($this->conn),
|
'message' => mysql_error($conn),
|
||||||
'code' => mysql_errno($this->conn),
|
'code' => mysql_errno($conn),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function escape($value, $appendQuotes=TRUE)
|
public function escape($value, $appendQuotes=TRUE)
|
||||||
{
|
{
|
||||||
|
$conn = $this->getResource();
|
||||||
return $appendQuotes
|
return $appendQuotes
|
||||||
? "'" . mysql_real_escape_string($value, $this->conn) . "'"
|
? "'" . mysql_real_escape_string($value, $conn) . "'"
|
||||||
: mysql_real_escape_string($value, $this->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
|
} // DibiMySqlDriver
|
||||||
|
|
||||||
|
|
||||||
|
@@ -21,7 +21,6 @@ if (!defined('DIBI')) die();
|
|||||||
class DibiMySqliDriver extends DibiDriver
|
class DibiMySqliDriver extends DibiDriver
|
||||||
{
|
{
|
||||||
private
|
private
|
||||||
$conn,
|
|
||||||
$insertId = FALSE,
|
$insertId = FALSE,
|
||||||
$affectedRows = 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'))
|
if (!extension_loaded('mysqli'))
|
||||||
throw new DibiException("PHP extension 'mysqli' is not loaded");
|
throw new DibiException("PHP extension 'mysqli' is not loaded");
|
||||||
@@ -50,6 +53,15 @@ class DibiMySqliDriver extends DibiDriver
|
|||||||
}
|
}
|
||||||
if (!isset($config['database'])) $config['database'] = NULL;
|
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']);
|
$conn = @mysqli_connect($config['host'], $config['username'], $config['password'], $config['database'], $config['port']);
|
||||||
|
|
||||||
if (!$conn)
|
if (!$conn)
|
||||||
@@ -61,9 +73,7 @@ class DibiMySqliDriver extends DibiDriver
|
|||||||
if (!empty($config['charset']))
|
if (!empty($config['charset']))
|
||||||
mysqli_query($conn, "SET NAMES '" . $config['charset'] . "'");
|
mysqli_query($conn, "SET NAMES '" . $config['charset'] . "'");
|
||||||
|
|
||||||
$obj = new self($config);
|
return $conn;
|
||||||
$obj->conn = $conn;
|
|
||||||
return $obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -71,14 +81,15 @@ class DibiMySqliDriver extends DibiDriver
|
|||||||
public function nativeQuery($sql)
|
public function nativeQuery($sql)
|
||||||
{
|
{
|
||||||
$this->insertId = $this->affectedRows = FALSE;
|
$this->insertId = $this->affectedRows = FALSE;
|
||||||
$res = @mysqli_query($this->conn, $sql);
|
$conn = $this->getResource();
|
||||||
|
$res = @mysqli_query($conn, $sql);
|
||||||
|
|
||||||
if ($res === FALSE) return FALSE;
|
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;
|
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 ($this->insertId < 1) $this->insertId = FALSE;
|
||||||
|
|
||||||
if (is_object($res))
|
if (is_object($res))
|
||||||
@@ -102,31 +113,34 @@ class DibiMySqliDriver extends DibiDriver
|
|||||||
|
|
||||||
public function begin()
|
public function begin()
|
||||||
{
|
{
|
||||||
return mysqli_autocommit($this->conn, FALSE);
|
return mysqli_autocommit($this->getResource(), FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function commit()
|
public function commit()
|
||||||
{
|
{
|
||||||
$ok = mysqli_commit($this->conn);
|
$conn = $this->getResource();
|
||||||
mysqli_autocommit($this->conn, TRUE);
|
$ok = mysqli_commit($conn);
|
||||||
|
mysqli_autocommit($conn, TRUE);
|
||||||
return $ok;
|
return $ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function rollback()
|
public function rollback()
|
||||||
{
|
{
|
||||||
$ok = mysqli_rollback($this->conn);
|
$conn = $this->getResource();
|
||||||
mysqli_autocommit($this->conn, TRUE);
|
$ok = mysqli_rollback($conn);
|
||||||
|
mysqli_autocommit($conn, TRUE);
|
||||||
return $ok;
|
return $ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function errorInfo()
|
public function errorInfo()
|
||||||
{
|
{
|
||||||
|
$conn = $this->getResource();
|
||||||
return array(
|
return array(
|
||||||
'message' => mysqli_error($this->conn),
|
'message' => mysqli_error($conn),
|
||||||
'code' => mysqli_errno($this->conn),
|
'code' => mysqli_errno($conn),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,9 +149,10 @@ class DibiMySqliDriver extends DibiDriver
|
|||||||
|
|
||||||
public function escape($value, $appendQuotes=TRUE)
|
public function escape($value, $appendQuotes=TRUE)
|
||||||
{
|
{
|
||||||
|
$conn = $this->getResource();
|
||||||
return $appendQuotes
|
return $appendQuotes
|
||||||
? "'" . mysqli_real_escape_string($this->conn, $value) . "'"
|
? "'" . mysqli_real_escape_string($conn, $value) . "'"
|
||||||
: mysqli_real_escape_string($this->conn, $value);
|
: mysqli_real_escape_string($conn, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -21,7 +21,6 @@ if (!defined('DIBI')) die();
|
|||||||
class DibiOdbcDriver extends DibiDriver
|
class DibiOdbcDriver extends DibiDriver
|
||||||
{
|
{
|
||||||
private
|
private
|
||||||
$conn,
|
|
||||||
$affectedRows = FALSE;
|
$affectedRows = FALSE;
|
||||||
|
|
||||||
public
|
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'))
|
if (!extension_loaded('odbc'))
|
||||||
throw new DibiException("PHP extension 'odbc' is not loaded");
|
throw new DibiException("PHP extension 'odbc' is not loaded");
|
||||||
@@ -53,6 +56,15 @@ class DibiOdbcDriver extends DibiDriver
|
|||||||
if (empty($config['database']))
|
if (empty($config['database']))
|
||||||
throw new DibiException("Database must be specified");
|
throw new DibiException("Database must be specified");
|
||||||
|
|
||||||
|
parent::__construct($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
protected function connect()
|
||||||
|
{
|
||||||
|
$config = $this->config;
|
||||||
|
|
||||||
if (empty($config['persistent']))
|
if (empty($config['persistent']))
|
||||||
$conn = @odbc_connect($config['database'], $config['username'], $config['password']);
|
$conn = @odbc_connect($config['database'], $config['username'], $config['password']);
|
||||||
else
|
else
|
||||||
@@ -64,9 +76,7 @@ class DibiOdbcDriver extends DibiDriver
|
|||||||
'code' => odbc_error(),
|
'code' => odbc_error(),
|
||||||
));
|
));
|
||||||
|
|
||||||
$obj = new self($config);
|
return $conn;
|
||||||
$obj->conn = $conn;
|
|
||||||
return $obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -75,11 +85,12 @@ class DibiOdbcDriver extends DibiDriver
|
|||||||
{
|
{
|
||||||
$this->affectedRows = FALSE;
|
$this->affectedRows = FALSE;
|
||||||
|
|
||||||
$res = @odbc_exec($this->conn, $sql);
|
$conn = $this->getResource();
|
||||||
|
$res = @odbc_exec($conn, $sql);
|
||||||
|
|
||||||
if ($res === FALSE) return FALSE;
|
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 ($this->affectedRows < 0) $this->affectedRows = FALSE;
|
||||||
|
|
||||||
if (is_resource($res))
|
if (is_resource($res))
|
||||||
@@ -103,31 +114,34 @@ class DibiOdbcDriver extends DibiDriver
|
|||||||
|
|
||||||
public function begin()
|
public function begin()
|
||||||
{
|
{
|
||||||
return odbc_autocommit($this->conn, FALSE);
|
return odbc_autocommit($this->getResource(), FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function commit()
|
public function commit()
|
||||||
{
|
{
|
||||||
$ok = odbc_commit($this->conn);
|
$conn = $this->getResource();
|
||||||
odbc_autocommit($this->conn, TRUE);
|
$ok = odbc_commit($conn);
|
||||||
|
odbc_autocommit($conn, TRUE);
|
||||||
return $ok;
|
return $ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function rollback()
|
public function rollback()
|
||||||
{
|
{
|
||||||
$ok = odbc_rollback($this->conn);
|
$conn = $this->getResource();
|
||||||
odbc_autocommit($this->conn, TRUE);
|
$ok = odbc_rollback($conn);
|
||||||
|
odbc_autocommit($conn, TRUE);
|
||||||
return $ok;
|
return $ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function errorInfo()
|
public function errorInfo()
|
||||||
{
|
{
|
||||||
|
$conn = $this->getResource();
|
||||||
return array(
|
return array(
|
||||||
'message' => odbc_errormsg($this->conn),
|
'message' => odbc_errormsg($conn),
|
||||||
'code' => odbc_error($this->conn),
|
'code' => odbc_error($conn),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -20,9 +20,6 @@ if (!defined('DIBI')) die();
|
|||||||
*/
|
*/
|
||||||
class DibiPdoDriver extends DibiDriver
|
class DibiPdoDriver extends DibiDriver
|
||||||
{
|
{
|
||||||
/** @var PDO */
|
|
||||||
private $conn;
|
|
||||||
|
|
||||||
private $affectedRows = FALSE;
|
private $affectedRows = FALSE;
|
||||||
|
|
||||||
public
|
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'))
|
if (!extension_loaded('pdo'))
|
||||||
throw new DibiException("PHP extension 'pdo' is not loaded");
|
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['username'])) $config['username'] = NULL;
|
||||||
if (empty($config['password'])) $config['password'] = 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;
|
$this->affectedRows = FALSE;
|
||||||
|
|
||||||
// TODO: or exec() ?
|
// TODO: or exec() ?
|
||||||
$res = $this->conn->query($sql);
|
$res = $this->getResource()->query($sql);
|
||||||
|
|
||||||
if ($res === FALSE) return FALSE;
|
if ($res === FALSE) return FALSE;
|
||||||
|
|
||||||
@@ -79,31 +84,31 @@ class DibiPdoDriver extends DibiDriver
|
|||||||
|
|
||||||
public function insertId()
|
public function insertId()
|
||||||
{
|
{
|
||||||
return $this->conn->lastInsertId();
|
return $this->getResource()->lastInsertId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function begin()
|
public function begin()
|
||||||
{
|
{
|
||||||
return $this->conn->beginTransaction();
|
return $this->getResource()->beginTransaction();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function commit()
|
public function commit()
|
||||||
{
|
{
|
||||||
return $this->conn->commit();
|
return $this->getResource()->commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function rollback()
|
public function rollback()
|
||||||
{
|
{
|
||||||
return $this->conn->rollBack();
|
return $this->getResource()->rollBack();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function errorInfo()
|
public function errorInfo()
|
||||||
{
|
{
|
||||||
$error = $this->conn->errorInfo();
|
$error = $this->getResource()->errorInfo();
|
||||||
return array(
|
return array(
|
||||||
'message' => $error[2],
|
'message' => $error[2],
|
||||||
'code' => $error[1],
|
'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);
|
trigger_error('dibi: escaping without qoutes is not supported by PDO', E_USER_WARNING);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return $this->conn->quote($value);
|
return $this->getResource()->quote($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -21,7 +21,6 @@ if (!defined('DIBI')) die();
|
|||||||
class DibiPostgreDriver extends DibiDriver
|
class DibiPostgreDriver extends DibiDriver
|
||||||
{
|
{
|
||||||
private
|
private
|
||||||
$conn,
|
|
||||||
$affectedRows = FALSE;
|
$affectedRows = FALSE;
|
||||||
|
|
||||||
public
|
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'))
|
if (!extension_loaded('pgsql'))
|
||||||
throw new DibiException("PHP extension 'pgsql' is not loaded");
|
throw new DibiException("PHP extension 'pgsql' is not loaded");
|
||||||
@@ -44,6 +47,15 @@ class DibiPostgreDriver extends DibiDriver
|
|||||||
|
|
||||||
if (empty($config['type'])) $config['type'] = NULL;
|
if (empty($config['type'])) $config['type'] = NULL;
|
||||||
|
|
||||||
|
parent::__construct($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
protected function connect()
|
||||||
|
{
|
||||||
|
$config = $this->config;
|
||||||
|
|
||||||
if (isset($config['persistent']))
|
if (isset($config['persistent']))
|
||||||
$conn = @pg_connect($config['string'], $config['type']);
|
$conn = @pg_connect($config['string'], $config['type']);
|
||||||
else
|
else
|
||||||
@@ -58,10 +70,7 @@ class DibiPostgreDriver extends DibiDriver
|
|||||||
@pg_set_client_encoding($conn, $config['charset']);
|
@pg_set_client_encoding($conn, $config['charset']);
|
||||||
// don't handle this error...
|
// don't handle this error...
|
||||||
}
|
}
|
||||||
|
return $conn;
|
||||||
$obj = new self($config);
|
|
||||||
$obj->conn = $conn;
|
|
||||||
return $obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -70,11 +79,12 @@ class DibiPostgreDriver extends DibiDriver
|
|||||||
{
|
{
|
||||||
$this->affectedRows = FALSE;
|
$this->affectedRows = FALSE;
|
||||||
|
|
||||||
$res = @pg_query($this->conn, $sql);
|
$conn = $this->getResource();
|
||||||
|
$res = @pg_query($conn, $sql);
|
||||||
|
|
||||||
if ($res === FALSE) return FALSE;
|
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 ($this->affectedRows < 0) $this->affectedRows = FALSE;
|
||||||
|
|
||||||
if (is_resource($res))
|
if (is_resource($res))
|
||||||
@@ -98,26 +108,26 @@ class DibiPostgreDriver extends DibiDriver
|
|||||||
|
|
||||||
public function begin()
|
public function begin()
|
||||||
{
|
{
|
||||||
return pg_query($this->conn, 'BEGIN');
|
return pg_query($this->getResource(), 'BEGIN');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function commit()
|
public function commit()
|
||||||
{
|
{
|
||||||
return pg_query($this->conn, 'COMMIT');
|
return pg_query($this->getResource(), 'COMMIT');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function rollback()
|
public function rollback()
|
||||||
{
|
{
|
||||||
return pg_query($this->conn, 'ROLLBACK');
|
return pg_query($this->getResource(), 'ROLLBACK');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function errorInfo()
|
public function errorInfo()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
'message' => pg_last_error($this->conn),
|
'message' => pg_last_error($this->getResource()),
|
||||||
'code' => NULL,
|
'code' => NULL,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@@ -21,7 +21,6 @@ if (!defined('DIBI')) die();
|
|||||||
class DibiSqliteDriver extends DibiDriver
|
class DibiSqliteDriver extends DibiDriver
|
||||||
{
|
{
|
||||||
private
|
private
|
||||||
$conn,
|
|
||||||
$insertId = FALSE,
|
$insertId = FALSE,
|
||||||
$affectedRows = 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'))
|
if (!extension_loaded('sqlite'))
|
||||||
throw new DibiException("PHP extension 'sqlite' is not loaded");
|
throw new DibiException("PHP extension 'sqlite' is not loaded");
|
||||||
@@ -46,6 +49,15 @@ class DibiSqliteDriver extends DibiDriver
|
|||||||
if (!isset($config['mode']))
|
if (!isset($config['mode']))
|
||||||
$config['mode'] = 0666;
|
$config['mode'] = 0666;
|
||||||
|
|
||||||
|
parent::__construct($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
protected function connect()
|
||||||
|
{
|
||||||
|
$config = $this->config;
|
||||||
|
|
||||||
$errorMsg = '';
|
$errorMsg = '';
|
||||||
if (empty($config['persistent']))
|
if (empty($config['persistent']))
|
||||||
$conn = @sqlite_open($config['database'], $config['mode'], $errorMsg);
|
$conn = @sqlite_open($config['database'], $config['mode'], $errorMsg);
|
||||||
@@ -57,9 +69,7 @@ class DibiSqliteDriver extends DibiDriver
|
|||||||
'message' => $errorMsg,
|
'message' => $errorMsg,
|
||||||
));
|
));
|
||||||
|
|
||||||
$obj = new self($config);
|
return $conn;
|
||||||
$obj->conn = $conn;
|
|
||||||
return $obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -68,14 +78,15 @@ class DibiSqliteDriver extends DibiDriver
|
|||||||
{
|
{
|
||||||
$this->insertId = $this->affectedRows = FALSE;
|
$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;
|
if ($res === FALSE) return FALSE;
|
||||||
|
|
||||||
$this->affectedRows = sqlite_changes($this->conn);
|
$this->affectedRows = sqlite_changes($conn);
|
||||||
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
|
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 ($this->insertId < 1) $this->insertId = FALSE;
|
||||||
|
|
||||||
if (is_resource($res))
|
if (is_resource($res))
|
||||||
@@ -99,25 +110,25 @@ class DibiSqliteDriver extends DibiDriver
|
|||||||
|
|
||||||
public function begin()
|
public function begin()
|
||||||
{
|
{
|
||||||
return sqlite_query($this->conn, 'BEGIN');
|
return sqlite_query($this->getResource(), 'BEGIN');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function commit()
|
public function commit()
|
||||||
{
|
{
|
||||||
return sqlite_query($this->conn, 'COMMIT');
|
return sqlite_query($this->getResource(), 'COMMIT');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function rollback()
|
public function rollback()
|
||||||
{
|
{
|
||||||
return sqlite_query($this->conn, 'ROLLBACK');
|
return sqlite_query($this->getResource(), 'ROLLBACK');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function errorInfo()
|
public function errorInfo()
|
||||||
{
|
{
|
||||||
$code = sqlite_last_error($this->conn);
|
$code = sqlite_last_error($this->getResource());
|
||||||
return array(
|
return array(
|
||||||
'message' => sqlite_error_string($code),
|
'message' => sqlite_error_string($code),
|
||||||
'code' => $code,
|
'code' => $code,
|
||||||
@@ -135,7 +146,7 @@ class DibiSqliteDriver extends DibiDriver
|
|||||||
|
|
||||||
public function delimite($value)
|
public function delimite($value)
|
||||||
{
|
{
|
||||||
return '[' . $value . ']';
|
return '[' . str_replace('.', '].[', $value) . ']';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -27,6 +27,12 @@ abstract class DibiDriver
|
|||||||
*/
|
*/
|
||||||
protected $config;
|
protected $config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connection resource
|
||||||
|
* @var resource
|
||||||
|
*/
|
||||||
|
private $res;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Describes how convert some datatypes to SQL command
|
* Describes how convert some datatypes to SQL command
|
||||||
* @var array
|
* @var array
|
||||||
@@ -39,31 +45,32 @@ abstract class DibiDriver
|
|||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DibiDriver factory: creates object and connects to a database
|
* Creates object and (optionally) connects to a database
|
||||||
*
|
|
||||||
* @param array connect configuration
|
* @param array connect configuration
|
||||||
* @return DibiDriver
|
|
||||||
* @throw DibiException
|
* @throw DibiException
|
||||||
*/
|
*/
|
||||||
/*abstract disallowed since PHP 5.2*/ static public function connect($config) {}
|
public function __construct($config)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Protected constructor. Must be initialized using the factory method.
|
|
||||||
* @see DibiDriver::connect()
|
|
||||||
* @param array connect configuration
|
|
||||||
*/
|
|
||||||
protected function __construct($config)
|
|
||||||
{
|
{
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
|
if (empty($config['lazy'])) $this->res = $this->connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the configuration descriptor used by connect() to connect to database.
|
* Connects to a database
|
||||||
* @see DibiDriver::connect()
|
* @throw DibiException
|
||||||
|
* @return resource
|
||||||
|
*/
|
||||||
|
abstract protected function connect();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the configuration descriptor
|
||||||
|
* @see DibiDriver::__construct
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getConfig()
|
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
|
* Generates and executes SQL query
|
||||||
*
|
*
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
Dibi version 0.7h
|
Dibi version 0.8
|
||||||
|
|
||||||
Revision: $WCREV$
|
Revision: $WCREV$
|
||||||
Date: $WCDATE$
|
Date: $WCDATE$
|
||||||
|
Reference in New Issue
Block a user