mirror of
https://github.com/dg/dibi.git
synced 2025-08-11 00:24:19 +02:00
* removed $throwExceptions (always throws)
* added DibiLogger, dibi::notify(), dibi::startLogger() * miniprofiler dibi::$numOfQueries, $totalTime, $elapsedTime * simplified DibiException, added DibiDatabaseException * Dibi::nativeQuery splitted into DibiDriver::doQuery & nativeQuery() * moved dibi::dumpResult -> DibiResult::dump() * moved dibi::test() -> DibiDriver::test() * DibiTranslator generates $mask
This commit is contained in:
@@ -12,35 +12,25 @@
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!class_exists('dibi', FALSE)) die();
|
||||
|
||||
|
||||
/**
|
||||
* The dibi driver for MS SQL database
|
||||
*
|
||||
*/
|
||||
class DibiMSSqlDriver extends DibiDriver
|
||||
class DibiMsSqlDriver extends DibiDriver
|
||||
{
|
||||
public
|
||||
$formats = array(
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "'Y-m-d'",
|
||||
'datetime' => "'Y-m-d H:i:s'",
|
||||
);
|
||||
public $formats = array(
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "'Y-m-d'",
|
||||
'datetime' => "'Y-m-d H:i:s'",
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* @param array connect configuration
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
if (!extension_loaded('mssql')) {
|
||||
throw new DibiException("PHP extension 'mssql' is not loaded");
|
||||
}
|
||||
|
||||
if (!isset($config['host'])) $config['host'] = NULL;
|
||||
if (!isset($config['username'])) $config['username'] = NULL;
|
||||
if (!isset($config['password'])) $config['password'] = NULL;
|
||||
@@ -52,7 +42,11 @@ class DibiMSSqlDriver extends DibiDriver
|
||||
|
||||
protected function connect()
|
||||
{
|
||||
$config = $this->config;
|
||||
if (!extension_loaded('mssql')) {
|
||||
throw new DibiException("PHP extension 'mssql' is not loaded");
|
||||
}
|
||||
|
||||
$config = $this->getConfig();
|
||||
|
||||
if (empty($config['persistent'])) {
|
||||
$connection = @mssql_connect($config['host'], $config['username'], $config['password'], TRUE);
|
||||
@@ -61,30 +55,28 @@ class DibiMSSqlDriver extends DibiDriver
|
||||
}
|
||||
|
||||
if (!is_resource($connection)) {
|
||||
throw new DibiException("Connecting error (driver mssql)'");
|
||||
throw new DibiDatabaseException("Can't connect to DB");
|
||||
}
|
||||
|
||||
if (!empty($config['database']) && !@mssql_select_db($config['database'], $connection)) {
|
||||
throw new DibiException("Connecting error (driver mssql)");
|
||||
throw new DibiDatabaseException("Can't select DB '$config[database]'");
|
||||
}
|
||||
|
||||
dibi::notify('connected', $this);
|
||||
return $connection;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function nativeQuery($sql)
|
||||
protected function doQuery($sql)
|
||||
{
|
||||
$res = @mssql_query($sql, $this->getConnection());
|
||||
|
||||
if ($res === FALSE) {
|
||||
return FALSE;
|
||||
throw new DibiDatabaseException('Query error', 0, $sql);
|
||||
|
||||
} elseif (is_resource($res)) {
|
||||
return new DibiMSSqlResult($res);
|
||||
|
||||
} else {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,21 +99,24 @@ class DibiMSSqlDriver extends DibiDriver
|
||||
|
||||
public function begin()
|
||||
{
|
||||
return mssql_query('BEGIN TRANSACTION', $this->getConnection());
|
||||
$this->doQuery('BEGIN TRANSACTION');
|
||||
dibi::notify('begin', $this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function commit()
|
||||
{
|
||||
return mssql_query('COMMIT', $this->getConnection());
|
||||
$this->doQuery('COMMIT');
|
||||
dibi::notify('commit', $this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function rollback()
|
||||
{
|
||||
return mssql_query('ROLLBACK', $this->getConnection());
|
||||
$this->doQuery('ROLLBACK');
|
||||
dibi::notify('rollback', $this);
|
||||
}
|
||||
|
||||
|
||||
@@ -171,12 +166,12 @@ class DibiMSSqlDriver extends DibiDriver
|
||||
}
|
||||
|
||||
if ($offset) {
|
||||
throw new DibiException('Offset is not implemented in driver odbc');
|
||||
throw new DibiException('Offset is not implemented');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // DibiMSSqlDriver
|
||||
} // DibiMsSqlDriver
|
||||
|
||||
|
||||
|
||||
@@ -188,15 +183,6 @@ class DibiMSSqlDriver extends DibiDriver
|
||||
|
||||
class DibiMSSqlResult extends DibiResult
|
||||
{
|
||||
private $resource;
|
||||
|
||||
|
||||
public function __construct($resource)
|
||||
{
|
||||
$this->resource = $resource;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function rowCount()
|
||||
{
|
||||
|
@@ -12,23 +12,18 @@
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!class_exists('dibi', FALSE)) die();
|
||||
|
||||
|
||||
/**
|
||||
* The dibi driver for MySQL database
|
||||
*
|
||||
*/
|
||||
class DibiMySqlDriver extends DibiDriver
|
||||
{
|
||||
public
|
||||
$formats = array(
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "'Y-m-d'",
|
||||
'datetime' => "'Y-m-d H:i:s'",
|
||||
);
|
||||
public $formats = array(
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "'Y-m-d'",
|
||||
'datetime' => "'Y-m-d H:i:s'",
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
@@ -37,10 +32,6 @@ class DibiMySqlDriver extends DibiDriver
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
if (!extension_loaded('mysql')) {
|
||||
throw new DibiException("PHP extension 'mysql' is not loaded");
|
||||
}
|
||||
|
||||
// default values
|
||||
if (empty($config['username'])) $config['username'] = ini_get('mysql.default_user');
|
||||
if (empty($config['password'])) $config['password'] = ini_get('mysql.default_password');
|
||||
@@ -57,7 +48,11 @@ class DibiMySqlDriver extends DibiDriver
|
||||
|
||||
protected function connect()
|
||||
{
|
||||
$config = $this->config;
|
||||
if (!extension_loaded('mysql')) {
|
||||
throw new DibiException("PHP extension 'mysql' is not loaded");
|
||||
}
|
||||
|
||||
$config = $this->getConfig();
|
||||
|
||||
if (isset($config['protocol']) && $config['protocol'] === 'unix') { // host can be socket
|
||||
$host = ':' . $config['host'];
|
||||
@@ -82,45 +77,37 @@ class DibiMySqlDriver extends DibiDriver
|
||||
ini_set('track_errors', $save);
|
||||
}
|
||||
|
||||
|
||||
if (!is_resource($connection)) {
|
||||
throw new DibiException("Connecting error (driver mysql)'", array(
|
||||
'message' => mysql_error() ? mysql_error() : $php_errormsg,
|
||||
'code' => mysql_errno(),
|
||||
));
|
||||
$msg = mysql_error();
|
||||
if (!$msg) $msg = $php_errormsg;
|
||||
throw new DibiDatabaseException($msg, mysql_errno());
|
||||
}
|
||||
|
||||
|
||||
if (!empty($config['charset'])) {
|
||||
@mysql_query("SET NAMES '" . $config['charset'] . "'", $connection);
|
||||
// don't handle this error...
|
||||
}
|
||||
|
||||
|
||||
if (!empty($config['database']) && !@mysql_select_db($config['database'], $connection)) {
|
||||
throw new DibiException("Connecting error (driver mysql)", array(
|
||||
'message' => mysql_error($connection),
|
||||
'code' => mysql_errno($connection),
|
||||
));
|
||||
throw new DibiDatabaseException(mysql_error($connection), mysql_errno($connection));
|
||||
}
|
||||
|
||||
dibi::notify('connected', $this);
|
||||
return $connection;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function nativeQuery($sql)
|
||||
protected function doQuery($sql)
|
||||
{
|
||||
$res = @mysql_query($sql, $this->getConnection());
|
||||
$connection = $this->getConnection();
|
||||
$res = @mysql_query($sql, $connection);
|
||||
|
||||
if ($res === FALSE) {
|
||||
return FALSE;
|
||||
throw new DibiDatabaseException(mysql_error($connection), mysql_errno($connection), $sql);
|
||||
|
||||
} elseif (is_resource($res)) {
|
||||
return new DibiMySqlResult($res);
|
||||
|
||||
} else {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,21 +131,24 @@ class DibiMySqlDriver extends DibiDriver
|
||||
|
||||
public function begin()
|
||||
{
|
||||
return mysql_query('BEGIN', $this->getConnection());
|
||||
$this->doQuery('BEGIN');
|
||||
dibi::notify('begin', $this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function commit()
|
||||
{
|
||||
return mysql_query('COMMIT', $this->getConnection());
|
||||
$this->doQuery('COMMIT');
|
||||
dibi::notify('commit', $this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function rollback()
|
||||
{
|
||||
return mysql_query('ROLLBACK', $this->getConnection());
|
||||
$this->doQuery('ROLLBACK');
|
||||
dibi::notify('rollback', $this);
|
||||
}
|
||||
|
||||
|
||||
@@ -223,15 +213,6 @@ class DibiMySqlDriver extends DibiDriver
|
||||
|
||||
class DibiMySqlResult extends DibiResult
|
||||
{
|
||||
private $resource;
|
||||
|
||||
|
||||
public function __construct($resource)
|
||||
{
|
||||
$this->resource = $resource;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function rowCount()
|
||||
{
|
||||
|
@@ -12,23 +12,18 @@
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!class_exists('dibi', FALSE)) die();
|
||||
|
||||
|
||||
/**
|
||||
* The dibi driver for MySQLi database
|
||||
*
|
||||
*/
|
||||
class DibiMySqliDriver extends DibiDriver
|
||||
{
|
||||
public
|
||||
$formats = array(
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "'Y-m-d'",
|
||||
'datetime' => "'Y-m-d H:i:s'",
|
||||
);
|
||||
public $formats = array(
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "'Y-m-d'",
|
||||
'datetime' => "'Y-m-d H:i:s'",
|
||||
);
|
||||
|
||||
|
||||
|
||||
@@ -38,10 +33,6 @@ class DibiMySqliDriver extends DibiDriver
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
if (!extension_loaded('mysqli')) {
|
||||
throw new DibiException("PHP extension 'mysqli' is not loaded");
|
||||
}
|
||||
|
||||
// default values
|
||||
if (empty($config['username'])) $config['username'] = ini_get('mysqli.default_user');
|
||||
if (empty($config['password'])) $config['password'] = ini_get('mysqli.default_password');
|
||||
@@ -59,38 +50,38 @@ class DibiMySqliDriver extends DibiDriver
|
||||
|
||||
protected function connect()
|
||||
{
|
||||
$config = $this->config;
|
||||
if (!extension_loaded('mysqli')) {
|
||||
throw new DibiException("PHP extension 'mysqli' is not loaded");
|
||||
}
|
||||
|
||||
$config = $this->getConfig();
|
||||
|
||||
$connection = @mysqli_connect($config['host'], $config['username'], $config['password'], $config['database'], $config['port']);
|
||||
|
||||
if (!$connection) {
|
||||
throw new DibiException("Connecting error (driver mysqli)", array(
|
||||
'message' => mysqli_connect_error(),
|
||||
'code' => mysqli_connect_errno(),
|
||||
));
|
||||
throw new DibiDatabaseException(mysqli_connect_error(), mysqli_connect_errno());
|
||||
}
|
||||
|
||||
if (!empty($config['charset'])) {
|
||||
mysqli_query($connection, "SET NAMES '" . $config['charset'] . "'");
|
||||
}
|
||||
|
||||
dibi::notify('connected', $this);
|
||||
return $connection;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function nativeQuery($sql)
|
||||
protected function doQuery($sql)
|
||||
{
|
||||
$res = @mysqli_query($this->getConnection(), $sql);
|
||||
$connection = $this->getConnection();
|
||||
$res = @mysqli_query($connection, $sql);
|
||||
|
||||
if ($res === FALSE) {
|
||||
return FALSE;
|
||||
throw new DibiDatabaseException(mysqli_error($connection), mysqli_errno($connection), $sql);
|
||||
|
||||
} elseif (is_object($res)) {
|
||||
return new DibiMySqliResult($res);
|
||||
|
||||
} else {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,7 +105,11 @@ class DibiMySqliDriver extends DibiDriver
|
||||
|
||||
public function begin()
|
||||
{
|
||||
return mysqli_autocommit($this->getConnection(), FALSE);
|
||||
$connection = $this->getConnection();
|
||||
if (!mysqli_autocommit($connection, FALSE)) {
|
||||
throw new DibiDatabaseException(mysqli_error($connection), mysqli_errno($connection));
|
||||
}
|
||||
dibi::notify('begin', $this);
|
||||
}
|
||||
|
||||
|
||||
@@ -122,9 +117,11 @@ class DibiMySqliDriver extends DibiDriver
|
||||
public function commit()
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
$ok = mysqli_commit($connection);
|
||||
if (!mysqli_commit($connection)) {
|
||||
throw new DibiDatabaseException(mysqli_error($connection), mysqli_errno($connection));
|
||||
}
|
||||
mysqli_autocommit($connection, TRUE);
|
||||
return $ok;
|
||||
dibi::notify('commit', $this);
|
||||
}
|
||||
|
||||
|
||||
@@ -132,9 +129,11 @@ class DibiMySqliDriver extends DibiDriver
|
||||
public function rollback()
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
$ok = mysqli_rollback($connection);
|
||||
if (!mysqli_rollback($connection)) {
|
||||
throw new DibiDatabaseException(mysqli_error($connection), mysqli_errno($connection));
|
||||
}
|
||||
mysqli_autocommit($connection, TRUE);
|
||||
return $ok;
|
||||
dibi::notify('rollback', $this);
|
||||
}
|
||||
|
||||
|
||||
@@ -199,15 +198,6 @@ class DibiMySqliDriver extends DibiDriver
|
||||
|
||||
class DibiMySqliResult extends DibiResult
|
||||
{
|
||||
private $resource;
|
||||
|
||||
|
||||
public function __construct($resource)
|
||||
{
|
||||
$this->resource = $resource;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function rowCount()
|
||||
{
|
||||
|
@@ -12,26 +12,24 @@
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!class_exists('dibi', FALSE)) die();
|
||||
|
||||
|
||||
/**
|
||||
* The dibi driver interacting with databases via ODBC connections
|
||||
*
|
||||
*/
|
||||
class DibiOdbcDriver extends DibiDriver
|
||||
{
|
||||
private
|
||||
$affectedRows = FALSE;
|
||||
public $formats = array(
|
||||
'TRUE' => "-1",
|
||||
'FALSE' => "0",
|
||||
'date' => "#m/d/Y#",
|
||||
'datetime' => "#m/d/Y H:i:s#",
|
||||
);
|
||||
|
||||
public
|
||||
$formats = array(
|
||||
'TRUE' => "-1",
|
||||
'FALSE' => "0",
|
||||
'date' => "#m/d/Y#",
|
||||
'datetime' => "#m/d/Y H:i:s#",
|
||||
);
|
||||
/**
|
||||
* Affected rows
|
||||
* @var mixed
|
||||
*/
|
||||
private $affectedRows = FALSE;
|
||||
|
||||
|
||||
|
||||
@@ -41,9 +39,6 @@ class DibiOdbcDriver extends DibiDriver
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
if (!extension_loaded('odbc'))
|
||||
throw new DibiException("PHP extension 'odbc' is not loaded");
|
||||
|
||||
// default values
|
||||
if (empty($config['username'])) $config['username'] = ini_get('odbc.default_user');
|
||||
if (empty($config['password'])) $config['password'] = ini_get('odbc.default_pw');
|
||||
@@ -68,7 +63,11 @@ class DibiOdbcDriver extends DibiDriver
|
||||
|
||||
protected function connect()
|
||||
{
|
||||
$config = $this->config;
|
||||
if (!extension_loaded('odbc')) {
|
||||
throw new DibiException("PHP extension 'odbc' is not loaded");
|
||||
}
|
||||
|
||||
$config = $this->getConfig();
|
||||
|
||||
if (empty($config['persistent'])) {
|
||||
$connection = @odbc_connect($config['database'], $config['username'], $config['password']);
|
||||
@@ -77,12 +76,10 @@ class DibiOdbcDriver extends DibiDriver
|
||||
}
|
||||
|
||||
if (!is_resource($connection)) {
|
||||
throw new DibiException("Connecting error (driver odbc)", array(
|
||||
'message' => odbc_errormsg(),
|
||||
'code' => odbc_error(),
|
||||
));
|
||||
throw new DibiDatabaseException(odbc_errormsg(), odbc_error());
|
||||
}
|
||||
|
||||
dibi::notify('connected', $this);
|
||||
return $connection;
|
||||
}
|
||||
|
||||
@@ -91,19 +88,26 @@ class DibiOdbcDriver extends DibiDriver
|
||||
public function nativeQuery($sql)
|
||||
{
|
||||
$this->affectedRows = FALSE;
|
||||
$res = @odbc_exec($this->getConnection(), $sql);
|
||||
$res = parent::nativeQuery($sql);
|
||||
if ($res instanceof DibiResult) {
|
||||
$this->affectedRows = odbc_num_rows($res->getResource());
|
||||
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
protected function doQuery($sql)
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
$res = @odbc_exec($connection, $sql);
|
||||
|
||||
if ($res === FALSE) {
|
||||
return FALSE;
|
||||
throw new DibiDatabaseException(odbc_errormsg($connection), odbc_error($connection), $sql);
|
||||
|
||||
} elseif (is_resource($res)) {
|
||||
$this->affectedRows = odbc_num_rows($res);
|
||||
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
|
||||
|
||||
return new DibiOdbcResult($res);
|
||||
|
||||
} else {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,7 +129,11 @@ class DibiOdbcDriver extends DibiDriver
|
||||
|
||||
public function begin()
|
||||
{
|
||||
return odbc_autocommit($this->getConnection(), FALSE);
|
||||
$connection = $this->getConnection();
|
||||
if (!odbc_autocommit($connection, FALSE)) {
|
||||
throw new DibiDatabaseException(odbc_errormsg($connection), odbc_error($connection));
|
||||
}
|
||||
dibi::notify('begin', $this);
|
||||
}
|
||||
|
||||
|
||||
@@ -133,9 +141,11 @@ class DibiOdbcDriver extends DibiDriver
|
||||
public function commit()
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
$ok = odbc_commit($connection);
|
||||
if (!odbc_commit($connection)) {
|
||||
throw new DibiDatabaseException(odbc_errormsg($connection), odbc_error($connection));
|
||||
}
|
||||
odbc_autocommit($connection, TRUE);
|
||||
return $ok;
|
||||
dibi::notify('commit', $this);
|
||||
}
|
||||
|
||||
|
||||
@@ -143,9 +153,11 @@ class DibiOdbcDriver extends DibiDriver
|
||||
public function rollback()
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
$ok = odbc_rollback($connection);
|
||||
if (!odbc_rollback($connection)) {
|
||||
throw new DibiDatabaseException(odbc_errormsg($connection), odbc_error($connection));
|
||||
}
|
||||
odbc_autocommit($connection, TRUE);
|
||||
return $ok;
|
||||
dibi::notify('rollback', $this);
|
||||
}
|
||||
|
||||
|
||||
@@ -209,16 +221,9 @@ class DibiOdbcDriver extends DibiDriver
|
||||
|
||||
class DibiOdbcResult extends DibiResult
|
||||
{
|
||||
private $resource;
|
||||
private $row = 0;
|
||||
|
||||
|
||||
public function __construct($resource)
|
||||
{
|
||||
$this->resource = $resource;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function rowCount()
|
||||
{
|
||||
|
@@ -12,23 +12,18 @@
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!class_exists('dibi', FALSE)) die();
|
||||
|
||||
|
||||
/**
|
||||
* The dibi driver for PDO
|
||||
*
|
||||
*/
|
||||
class DibiPdoDriver extends DibiDriver
|
||||
{
|
||||
public
|
||||
$formats = array(
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "'Y-m-d'",
|
||||
'datetime' => "'Y-m-d H:i:s'",
|
||||
);
|
||||
public $formats = array(
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "'Y-m-d'",
|
||||
'datetime' => "'Y-m-d H:i:s'",
|
||||
);
|
||||
|
||||
|
||||
|
||||
@@ -38,10 +33,6 @@ class DibiPdoDriver extends DibiDriver
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
if (!extension_loaded('pdo')) {
|
||||
throw new DibiException("PHP extension 'pdo' is not loaded");
|
||||
}
|
||||
|
||||
if (empty($config['dsn'])) {
|
||||
throw new DibiException("DSN must be specified (driver odbc)");
|
||||
}
|
||||
@@ -56,24 +47,25 @@ class DibiPdoDriver extends DibiDriver
|
||||
|
||||
protected function connect()
|
||||
{
|
||||
return new PDO($this->config['dsn'], $this->config['username'], $this->config['password']);
|
||||
if (!extension_loaded('pdo')) {
|
||||
throw new DibiException("PHP extension 'pdo' is not loaded");
|
||||
}
|
||||
|
||||
$config = $this->getConfig();
|
||||
$connection = new PDO($config['dsn'], $config['username'], $config['password']);
|
||||
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
dibi::notify('connected', $this);
|
||||
return $connection;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function nativeQuery($sql)
|
||||
protected function doQuery($sql)
|
||||
{
|
||||
// TODO: or exec() ?
|
||||
$res = $this->getConnection()->query($sql);
|
||||
|
||||
if ($res === FALSE) {
|
||||
return FALSE;
|
||||
|
||||
} elseif ($res instanceof PDOStatement) {
|
||||
if ($res instanceof PDOStatement) {
|
||||
return new DibiPdoResult($res);
|
||||
|
||||
} else {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,21 +87,24 @@ class DibiPdoDriver extends DibiDriver
|
||||
|
||||
public function begin()
|
||||
{
|
||||
return $this->getConnection()->beginTransaction();
|
||||
$this->getConnection()->beginTransaction();
|
||||
dibi::notify('begin', $this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function commit()
|
||||
{
|
||||
return $this->getConnection()->commit();
|
||||
$this->getConnection()->commit();
|
||||
dibi::notify('commit', $this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function rollback()
|
||||
{
|
||||
return $this->getConnection()->rollBack();
|
||||
$this->getConnection()->rollBack();
|
||||
dibi::notify('rollback', $this);
|
||||
}
|
||||
|
||||
|
||||
@@ -171,19 +166,9 @@ class DibiPdoDriver extends DibiDriver
|
||||
|
||||
class DibiPdoResult extends DibiResult
|
||||
{
|
||||
/** @var PDOStatement */
|
||||
private $resource;
|
||||
|
||||
private $row = 0;
|
||||
|
||||
|
||||
public function __construct($resource)
|
||||
{
|
||||
$this->resource = $resource;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function rowCount()
|
||||
{
|
||||
return $this->resource->rowCount();
|
||||
|
@@ -12,26 +12,24 @@
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!class_exists('dibi', FALSE)) die();
|
||||
|
||||
|
||||
/**
|
||||
* The dibi driver for PostgreSql database
|
||||
*
|
||||
*/
|
||||
class DibiPostgreDriver extends DibiDriver
|
||||
{
|
||||
private
|
||||
$affectedRows = FALSE;
|
||||
public $formats = array(
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "'Y-m-d'",
|
||||
'datetime' => "'Y-m-d H:i:s'",
|
||||
);
|
||||
|
||||
public
|
||||
$formats = array(
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "'Y-m-d'",
|
||||
'datetime' => "'Y-m-d H:i:s'",
|
||||
);
|
||||
/**
|
||||
* Affected rows
|
||||
* @var mixed
|
||||
*/
|
||||
private $affectedRows = FALSE;
|
||||
|
||||
|
||||
|
||||
@@ -41,10 +39,6 @@ class DibiPostgreDriver extends DibiDriver
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
if (!extension_loaded('pgsql')) {
|
||||
throw new DibiException("PHP extension 'pgsql' is not loaded");
|
||||
}
|
||||
|
||||
if (empty($config['string'])) {
|
||||
throw new DibiException("Connection string must be specified (driver postgre)");
|
||||
}
|
||||
@@ -58,7 +52,11 @@ class DibiPostgreDriver extends DibiDriver
|
||||
|
||||
protected function connect()
|
||||
{
|
||||
$config = $this->config;
|
||||
if (!extension_loaded('pgsql')) {
|
||||
throw new DibiException("PHP extension 'pgsql' is not loaded");
|
||||
}
|
||||
|
||||
$config = $this->getConfig();
|
||||
|
||||
if (isset($config['persistent'])) {
|
||||
$connection = @pg_connect($config['string'], $config['type']);
|
||||
@@ -67,15 +65,15 @@ class DibiPostgreDriver extends DibiDriver
|
||||
}
|
||||
|
||||
if (!is_resource($connection)) {
|
||||
throw new DibiException("Connecting error (driver postgre)", array(
|
||||
'message' => pg_last_error(),
|
||||
));
|
||||
throw new DibiDatabaseException(pg_last_error());
|
||||
}
|
||||
|
||||
if (!empty($config['charset'])) {
|
||||
@pg_set_client_encoding($connection, $config['charset']);
|
||||
// don't handle this error...
|
||||
}
|
||||
|
||||
dibi::notify('connected', $this);
|
||||
return $connection;
|
||||
}
|
||||
|
||||
@@ -84,20 +82,26 @@ class DibiPostgreDriver extends DibiDriver
|
||||
public function nativeQuery($sql)
|
||||
{
|
||||
$this->affectedRows = FALSE;
|
||||
$res = parent::nativeQuery($sql);
|
||||
if ($res instanceof DibiResult) {
|
||||
$this->affectedRows = pg_affected_rows($res->getResource());
|
||||
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
$res = @pg_query($this->getConnection(), $sql);
|
||||
|
||||
|
||||
protected function doQuery($sql)
|
||||
{
|
||||
$connection = $this->getConnection();
|
||||
$res = @pg_query($connection, $sql);
|
||||
|
||||
if ($res === FALSE) {
|
||||
return FALSE;
|
||||
throw new DibiDatabaseException(pg_last_error($connection), 0, $sql);
|
||||
|
||||
} elseif (is_resource($res)) {
|
||||
$this->affectedRows = pg_affected_rows($res);
|
||||
if ($this->affectedRows < 0) $this->affectedRows = FALSE;
|
||||
|
||||
return new DibiPostgreResult($res);
|
||||
|
||||
} else {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,9 +118,9 @@ class DibiPostgreDriver extends DibiDriver
|
||||
{
|
||||
if (empty($sequence)) {
|
||||
// PostgreSQL 8.1 is needed
|
||||
$res = pg_query($this->getConnection(), "SELECT LASTVAL() AS seq");
|
||||
$res = $this->doQuery("SELECT LASTVAL() AS seq");
|
||||
} else {
|
||||
$res = pg_query($this->getConnection(), "SELECT CURRVAL('$sequence') AS seq");
|
||||
$res = $this->doQuery("SELECT CURRVAL('$sequence') AS seq");
|
||||
}
|
||||
|
||||
if (is_resource($res)) {
|
||||
@@ -132,21 +136,24 @@ class DibiPostgreDriver extends DibiDriver
|
||||
|
||||
public function begin()
|
||||
{
|
||||
return pg_query($this->getConnection(), 'BEGIN');
|
||||
$this->doQuery('BEGIN');
|
||||
dibi::notify('begin', $this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function commit()
|
||||
{
|
||||
return pg_query($this->getConnection(), 'COMMIT');
|
||||
$this->doQuery('COMMIT');
|
||||
dibi::notify('commit', $this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function rollback()
|
||||
{
|
||||
return pg_query($this->getConnection(), 'ROLLBACK');
|
||||
$this->doQuery('ROLLBACK');
|
||||
dibi::notify('rollback', $this);
|
||||
}
|
||||
|
||||
|
||||
@@ -210,15 +217,6 @@ class DibiPostgreDriver extends DibiDriver
|
||||
|
||||
class DibiPostgreResult extends DibiResult
|
||||
{
|
||||
private $resource;
|
||||
|
||||
|
||||
public function __construct($resource)
|
||||
{
|
||||
$this->resource = $resource;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function rowCount()
|
||||
{
|
||||
|
@@ -12,23 +12,18 @@
|
||||
*/
|
||||
|
||||
|
||||
// security - include dibi.php, not this file
|
||||
if (!class_exists('dibi', FALSE)) die();
|
||||
|
||||
|
||||
/**
|
||||
* The dibi driver for SQlite database
|
||||
*
|
||||
*/
|
||||
class DibiSqliteDriver extends DibiDriver
|
||||
{
|
||||
public
|
||||
$formats = array(
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "U",
|
||||
'datetime' => "U",
|
||||
);
|
||||
public $formats = array(
|
||||
'TRUE' => "1",
|
||||
'FALSE' => "0",
|
||||
'date' => "U",
|
||||
'datetime' => "U",
|
||||
);
|
||||
|
||||
|
||||
|
||||
@@ -38,10 +33,6 @@ class DibiSqliteDriver extends DibiDriver
|
||||
*/
|
||||
public function __construct($config)
|
||||
{
|
||||
if (!extension_loaded('sqlite')) {
|
||||
throw new DibiException("PHP extension 'sqlite' is not loaded");
|
||||
}
|
||||
|
||||
if (empty($config['database'])) {
|
||||
throw new DibiException("Database must be specified (driver sqlite)");
|
||||
}
|
||||
@@ -55,7 +46,11 @@ class DibiSqliteDriver extends DibiDriver
|
||||
|
||||
protected function connect()
|
||||
{
|
||||
$config = $this->config;
|
||||
if (!extension_loaded('sqlite')) {
|
||||
throw new DibiException("PHP extension 'sqlite' is not loaded");
|
||||
}
|
||||
|
||||
$config = $this->getConfig();
|
||||
|
||||
$errorMsg = '';
|
||||
if (empty($config['persistent'])) {
|
||||
@@ -65,28 +60,26 @@ class DibiSqliteDriver extends DibiDriver
|
||||
}
|
||||
|
||||
if (!$connection) {
|
||||
throw new DibiException("Connecting error (driver sqlite)", array(
|
||||
'message' => $errorMsg,
|
||||
));
|
||||
throw new DibiDatabaseException($errorMsg);
|
||||
}
|
||||
|
||||
dibi::notify('connected', $this);
|
||||
return $connection;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function nativeQuery($sql)
|
||||
protected function doQuery($sql)
|
||||
{
|
||||
$res = @sqlite_query($this->getConnection(), $sql, SQLITE_ASSOC);
|
||||
$connection = $this->getConnection();
|
||||
$res = @sqlite_query($connection, $sql, SQLITE_ASSOC);
|
||||
|
||||
if ($res === FALSE) {
|
||||
return FALSE;
|
||||
$code = sqlite_last_error($connection);
|
||||
throw new DibiDatabaseException(sqlite_error_string($code), $code, $sql);
|
||||
|
||||
} elseif (is_resource($res)) {
|
||||
return new DibiSqliteResult($res);
|
||||
|
||||
} else {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,21 +103,24 @@ class DibiSqliteDriver extends DibiDriver
|
||||
|
||||
public function begin()
|
||||
{
|
||||
return sqlite_query($this->getConnection(), 'BEGIN');
|
||||
$this->doQuery('BEGIN');
|
||||
dibi::notify('begin', $this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function commit()
|
||||
{
|
||||
return sqlite_query($this->getConnection(), 'COMMIT');
|
||||
$this->doQuery('COMMIT');
|
||||
dibi::notify('commit', $this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function rollback()
|
||||
{
|
||||
return sqlite_query($this->getConnection(), 'ROLLBACK');
|
||||
$this->doQuery('ROLLBACK');
|
||||
dibi::notify('rollback', $this);
|
||||
}
|
||||
|
||||
|
||||
@@ -184,15 +180,6 @@ class DibiSqliteDriver extends DibiDriver
|
||||
|
||||
class DibiSqliteResult extends DibiResult
|
||||
{
|
||||
private $resource;
|
||||
|
||||
|
||||
public function __construct($resource)
|
||||
{
|
||||
$this->resource = $resource;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function rowCount()
|
||||
{
|
||||
|
Reference in New Issue
Block a user