1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-16 02:54:25 +02:00

* added dibi::date & dibi::datetime

* DibiConnection::insertId && affectedRows throws exception on failure
* added protected throwException() to drivers
* DibiPostgreDriver - can build connection string
* DibiSqliteDriver - support for parameters 'format:date' & 'format:datetime'
* fixed query errors in DibiSqliteDriver
* DibiConnection prevents serialization and multiple transactions
This commit is contained in:
David Grudl
2007-11-23 23:27:14 +00:00
parent 3f42b2cf55
commit 7c6947a019
18 changed files with 419 additions and 176 deletions

View File

@@ -121,7 +121,7 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
* @return int|FALSE number of rows or FALSE on error
*/
public function affectedRows()
{
@@ -145,6 +145,7 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
/**
* Begins a transaction (if supported).
* @return void
* @throws DibiDriverException
*/
public function begin()
{
@@ -156,6 +157,7 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
/**
* Commits statements in a transaction.
* @return void
* @throws DibiDriverException
*/
public function commit()
{
@@ -167,6 +169,7 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
/**
* Rollback changes in a transaction.
* @return void
* @throws DibiDriverException
*/
public function rollback()
{
@@ -316,6 +319,19 @@ class DibiMsSqlDriver extends NObject implements DibiDriverInterface
}
/**
* Converts database error to DibiDriverException
*
* @throws DibiDriverException
*/
protected function throwException($sql=NULL)
{
throw new DibiDriverException();
}
/**
* Returns the connection resource
*

View File

@@ -29,7 +29,7 @@
* - 'password' (or 'pass')
* - 'persistent' - try to find a persistent link?
* - 'database' - the database name to select
* - 'charset' - sets the encoding
* - 'charset' - character encoding to set
* - 'unbuffered' - sends query without fetching and buffering the result rows automatically?
* - 'options' - driver specific constants (MYSQL_*)
* - 'lazy' - if TRUE, connection will be established only when required
@@ -115,7 +115,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
}
if (isset($config['database']) && !@mysql_select_db($config['database'], $this->connection)) {
throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection));
$this->throwException();
}
$this->buffered = empty($config['unbuffered']);
@@ -150,8 +150,8 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
$this->resultset = @mysql_unbuffered_query($sql, $this->connection);
}
if ($errno = mysql_errno($this->connection)) {
throw new DibiDriverException(mysql_error($this->connection), $errno, $sql);
if (mysql_errno($this->connection)) {
$this->throwException($sql);
}
return is_resource($this->resultset);
@@ -162,7 +162,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
* @return int|FALSE number of rows or FALSE on error
*/
public function affectedRows()
{
@@ -186,6 +186,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
/**
* Begins a transaction (if supported).
* @return void
* @throws DibiDriverException
*/
public function begin()
{
@@ -197,6 +198,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
/**
* Commits statements in a transaction.
* @return void
* @throws DibiDriverException
*/
public function commit()
{
@@ -208,6 +210,7 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
/**
* Rollback changes in a transaction.
* @return void
* @throws DibiDriverException
*/
public function rollback()
{
@@ -378,6 +381,19 @@ class DibiMySqlDriver extends NObject implements DibiDriverInterface
}
/**
* Converts database error to DibiDriverException
*
* @throws DibiDriverException
*/
protected function throwException($sql=NULL)
{
throw new DibiDriverException(mysql_error($this->connection), mysql_errno($this->connection), $sql);
}
/**
* Returns the connection resource
*

View File

@@ -29,7 +29,7 @@
* - 'password' (or 'pass')
* - 'persistent' - try to find a persistent link?
* - 'database' - the database name to select
* - 'charset' - sets the encoding
* - 'charset' - character encoding to set
* - 'unbuffered' - sends query without fetching and buffering the result rows automatically?
* - 'options' - driver specific constants (MYSQLI_*)
* - 'lazy' - if TRUE, connection will be established only when required
@@ -130,7 +130,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
$this->resultset = @mysqli_query($this->connection, $sql, $this->buffered ? MYSQLI_STORE_RESULT : MYSQLI_USE_RESULT);
if ($errno = mysqli_errno($this->connection)) {
throw new DibiDriverException(mysqli_error($this->connection), $errno, $sql);
$this->throwException($sql);
}
return is_object($this->resultset);
@@ -141,7 +141,7 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
* @return int|FALSE number of rows or FALSE on error
*/
public function affectedRows()
{
@@ -165,11 +165,12 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
/**
* Begins a transaction (if supported).
* @return void
* @throws DibiDriverException
*/
public function begin()
{
if (!mysqli_autocommit($this->connection, FALSE)) {
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
$this->throwException($sql);
}
}
@@ -178,11 +179,12 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
/**
* Commits statements in a transaction.
* @return void
* @throws DibiDriverException
*/
public function commit()
{
if (!mysqli_commit($this->connection)) {
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
$this->throwException($sql);
}
mysqli_autocommit($this->connection, TRUE);
}
@@ -192,11 +194,12 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
/**
* Rollback changes in a transaction.
* @return void
* @throws DibiDriverException
*/
public function rollback()
{
if (!mysqli_rollback($this->connection)) {
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection));
$this->throwException($sql);
}
mysqli_autocommit($this->connection, TRUE);
}
@@ -353,6 +356,19 @@ class DibiMySqliDriver extends NObject implements DibiDriverInterface
}
/**
* Converts database error to DibiDriverException
*
* @throws DibiDriverException
*/
protected function throwException($sql=NULL)
{
throw new DibiDriverException(mysqli_error($this->connection), mysqli_errno($this->connection), $sql);
}
/**
* Returns the connection resource
*

View File

@@ -116,7 +116,7 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
$this->resultset = @odbc_exec($this->connection, $sql);
if ($this->resultset === FALSE) {
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection), 0, $sql);
$this->throwException($sql);
}
return is_resource($this->resultset);
@@ -127,7 +127,7 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
* @return int|FALSE number of rows or FALSE on error
*/
public function affectedRows()
{
@@ -151,11 +151,12 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
/**
* Begins a transaction (if supported).
* @return void
* @throws DibiDriverException
*/
public function begin()
{
if (!odbc_autocommit($this->connection, FALSE)) {
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
$this->throwException();
}
}
@@ -164,11 +165,12 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
/**
* Commits statements in a transaction.
* @return void
* @throws DibiDriverException
*/
public function commit()
{
if (!odbc_commit($this->connection)) {
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
$this->throwException();
}
odbc_autocommit($this->connection, TRUE);
}
@@ -178,11 +180,12 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
/**
* Rollback changes in a transaction.
* @return void
* @throws DibiDriverException
*/
public function rollback()
{
if (!odbc_rollback($this->connection)) {
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
$this->throwException();
}
odbc_autocommit($this->connection, TRUE);
}
@@ -330,6 +333,19 @@ class DibiOdbcDriver extends NObject implements DibiDriverInterface
}
/**
* Converts database error to DibiDriverException
*
* @throws DibiDriverException
*/
protected function throwException($sql=NULL)
{
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection), 0, $sql);
}
/**
* Returns the connection resource
*

View File

@@ -25,7 +25,7 @@
* - 'database' (or 'db') - the name of the local Oracle instance or the name of the entry in tnsnames.ora
* - 'username' (or 'user')
* - 'password' (or 'pass')
* - 'charset' - sets the encoding
* - 'charset' - character encoding to set
* - 'lazy' - if TRUE, connection will be established only when required
*
* @author David Grudl
@@ -114,8 +114,7 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
throw new DibiDriverException($err['message'], $err['code'], $sql);
}
} else {
$err = oci_error($this->connection);
throw new DibiDriverException($err['message'], $err['code'], $sql);
$this->throwException($sql);
}
return is_resource($this->resultset);
@@ -126,7 +125,7 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
* @return int|FALSE number of rows or FALSE on error
*/
public function affectedRows()
{
@@ -150,6 +149,7 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
/**
* Begins a transaction (if supported).
* @return void
* @throws DibiDriverException
*/
public function begin()
{
@@ -161,12 +161,12 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
/**
* Commits statements in a transaction.
* @return void
* @throws DibiDriverException
*/
public function commit()
{
if (!oci_commit($this->connection)) {
$err = oci_error($this->connection);
throw new DibiDriverException($err['message'], $err['code']);
$this->throwException();
}
$this->autocommit = TRUE;
}
@@ -176,12 +176,12 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
/**
* Rollback changes in a transaction.
* @return void
* @throws DibiDriverException
*/
public function rollback()
{
if (!oci_rollback($this->connection)) {
$err = oci_error($this->connection);
throw new DibiDriverException($err['message'], $err['code']);
$this->throwException();
}
$this->autocommit = TRUE;
}
@@ -290,6 +290,20 @@ class DibiOracleDriver extends NObject implements DibiDriverInterface
}
/**
* Converts database error to DibiDriverException
*
* @throws DibiDriverException
*/
protected function throwException($sql=NULL)
{
$err = oci_error($this->connection);
throw new DibiDriverException($err['message'], $err['code'], $sql);
}
/**
* Returns the connection resource
*

View File

@@ -77,8 +77,6 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
if (!$this->connection) {
throw new DibiDriverException('Connecting error');
}
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
@@ -103,11 +101,12 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
*/
public function query($sql)
{
try {
$this->resultset = $this->connection->query($sql);
} catch (PDOException $e) {
throw new DibiDriverException($e->getMessage(), $e->getCode(), $sql);
$this->resultset = $this->connection->query($sql);
if ($this->resultset === FALSE) {
$this->throwException($sql);
}
return $this->resultset instanceof PDOStatement;
}
@@ -116,7 +115,7 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
* @return int|FALSE number of rows or FALSE on error
*/
public function affectedRows()
{
@@ -140,13 +139,12 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
/**
* Begins a transaction (if supported).
* @return void
* @throws DibiDriverException
*/
public function begin()
{
try {
$this->connection->beginTransaction();
} catch (PDOException $e) {
throw new DibiDriverException($e->getMessage(), $e->getCode());
if (!$this->connection->beginTransaction()) {
$this->throwException();
}
}
@@ -155,13 +153,12 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
/**
* Commits statements in a transaction.
* @return void
* @throws DibiDriverException
*/
public function commit()
{
try {
$this->connection->commit();
} catch (PDOException $e) {
throw new DibiDriverException($e->getMessage(), $e->getCode());
if (!$this->connection->commit()) {
$this->throwException();
}
}
@@ -170,13 +167,12 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
/**
* Rollback changes in a transaction.
* @return void
* @throws DibiDriverException
*/
public function rollback()
{
try {
$this->connection->rollBack();
} catch (PDOException $e) {
throw new DibiDriverException($e->getMessage(), $e->getCode());
if (!$this->connection->rollBack()) {
$this->throwException();
}
}
@@ -284,6 +280,20 @@ class DibiPdoDriver extends NObject implements DibiDriverInterface
}
/**
* Converts database error to DibiDriverException
*
* @throws DibiDriverException
*/
protected function throwException($sql=NULL)
{
$err = $this->connection->errorInfo();
throw new DibiDriverException("SQLSTATE[$err[0]]: $err[2]", $err[1], $sql);
}
/**
* Returns the connection resource
*

View File

@@ -22,9 +22,10 @@
* The dibi driver for PostgreSQL database
*
* Connection options:
* - 'database' (or 'string') - connection string
* - 'host','hostaddr','port','dbname','user','password','connect_timeout','options','sslmode','service' - see PostgreSQL API
* - 'string' - or use connection string
* - 'persistent' - try to find a persistent link?
* - 'charset' - sets the encoding
* - 'charset' - character encoding to set
* - 'lazy' - if TRUE, connection will be established only when required
*
* @author David Grudl
@@ -58,21 +59,26 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
*/
public function connect(array &$config)
{
DibiConnection::alias($config, 'database', 'string');
DibiConnection::alias($config, 'type');
if (!extension_loaded('pgsql')) {
throw new DibiException("PHP extension 'pgsql' is not loaded");
}
NException::catchError('DibiDriverException');
if (isset($config['persistent'])) {
$this->connection = @pg_connect($config['database'], PGSQL_CONNECT_FORCE_NEW);
if (isset($config['string'])) {
$string = $config['string'];
} else {
$this->connection = @pg_pconnect($config['database'], PGSQL_CONNECT_FORCE_NEW);
$string = '';
foreach (array('host','hostaddr','port','dbname','user','password','connect_timeout','options','sslmode','service') as $key) {
if (isset($config[$key])) $string .= $key . '=' . $config[$key] . ' ';
}
}
NException::restore();
DibiDriverException::catchError();
if (isset($config['persistent'])) {
$this->connection = @pg_connect($string, PGSQL_CONNECT_FORCE_NEW);
} else {
$this->connection = @pg_pconnect($string, PGSQL_CONNECT_FORCE_NEW);
}
DibiDriverException::restore();
if (!is_resource($this->connection)) {
throw new DibiDriverException('Connecting error');
@@ -111,7 +117,7 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
$this->resultset = @pg_query($this->connection, $sql);
if ($this->resultset === FALSE) {
throw new DibiDriverException(pg_last_error($this->connection), 0, $sql);
$this->throwException($sql);
}
return is_resource($this->resultset);
@@ -122,7 +128,7 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
* @return int|FALSE number of rows or FALSE on error
*/
public function affectedRows()
{
@@ -145,13 +151,11 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
$has = $this->query("SELECT CURRVAL('$sequence') AS seq");
}
if ($has) {
$row = $this->fetch();
$this->free();
return $row['seq'];
}
if (!$has) return FALSE;
return FALSE;
$row = $this->fetch();
$this->free();
return isset($row['seq']) ? $row['seq'] : FALSE;
}
@@ -159,6 +163,7 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
/**
* Begins a transaction (if supported).
* @return void
* @throws DibiDriverException
*/
public function begin()
{
@@ -170,6 +175,7 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
/**
* Commits statements in a transaction.
* @return void
* @throws DibiDriverException
*/
public function commit()
{
@@ -181,6 +187,7 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
/**
* Rollback changes in a transaction.
* @return void
* @throws DibiDriverException
*/
public function rollback()
{
@@ -318,6 +325,19 @@ class DibiPostgreDriver extends NObject implements DibiDriverInterface
}
/**
* Converts database error to DibiDriverException
*
* @throws DibiDriverException
*/
protected function throwException($sql=NULL)
{
throw new DibiDriverException(pg_last_error($this->connection), 0, $sql);
}
/**
* Returns the connection resource
*

View File

@@ -26,6 +26,8 @@
* - 'persistent' - try to find a persistent link?
* - 'unbuffered' - sends query without fetching and buffering the result rows automatically?
* - 'lazy' - if TRUE, connection will be established only when required
* - 'format:date' - how to format date in SQL (@see date)
* - 'format:datetime' - how to format datetime in SQL (@see date)
*
* @author David Grudl
* @copyright Copyright (c) 2005, 2007 David Grudl
@@ -55,6 +57,12 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
*/
private $buffered;
/**
* Date and datetime format
* @var string
*/
private $fmtDate, $fmtDateTime;
/**
* Connects to a database
@@ -65,6 +73,8 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
public function connect(array &$config)
{
DibiConnection::alias($config, 'database', 'file');
$this->fmtDate = isset($config['format:date']) ? $config['format:date'] : 'U';
$this->fmtDateTime = isset($config['format:datetime']) ? $config['format:datetime'] : 'U';
if (!extension_loaded('sqlite')) {
throw new DibiException("PHP extension 'sqlite' is not loaded");
@@ -108,14 +118,16 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
{
$errorMsg = NULL;
DibiDriverException::catchError();
if ($this->buffered) {
$this->resultset = @sqlite_query($this->connection, $sql, SQLITE_ASSOC, $errorMsg);
$this->resultset = sqlite_query($this->connection, $sql, SQLITE_ASSOC);
} else {
$this->resultset = @sqlite_unbuffered_query($this->connection, $sql, SQLITE_ASSOC, $errorMsg);
$this->resultset = sqlite_unbuffered_query($this->connection, $sql, SQLITE_ASSOC);
}
DibiDriverException::restore();
if ($errorMsg !== NULL) {
throw new DibiDriverException($errorMsg, sqlite_last_error($this->connection), $sql);
if (sqlite_last_error($this->connection)) {
$this->throwException();
}
return is_resource($this->resultset);
@@ -126,7 +138,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query
*
* @return int number of rows or FALSE on error
* @return int|FALSE number of rows or FALSE on error
*/
public function affectedRows()
{
@@ -150,6 +162,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
/**
* Begins a transaction (if supported).
* @return void
* @throws DibiDriverException
*/
public function begin()
{
@@ -161,6 +174,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
/**
* Commits statements in a transaction.
* @return void
* @throws DibiDriverException
*/
public function commit()
{
@@ -172,6 +186,7 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
/**
* Rollback changes in a transaction.
* @return void
* @throws DibiDriverException
*/
public function rollback()
{
@@ -193,8 +208,8 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
if ($type === dibi::FIELD_TEXT) return "'" . sqlite_escape_string($value) . "'";
if ($type === dibi::IDENTIFIER) return '[' . str_replace('.', '].[', $value) . ']';
if ($type === dibi::FIELD_BOOL) return $value ? 1 : 0;
if ($type === dibi::FIELD_DATE) return date("U", $value);
if ($type === dibi::FIELD_DATETIME) return date("U", $value);
if ($type === dibi::FIELD_DATE) return date($this->fmtDate, $value);
if ($type === dibi::FIELD_DATETIME) return date($this->fmtDateTime, $value);
throw new InvalidArgumentException('Unsupported formatting type');
}
@@ -287,6 +302,20 @@ class DibiSqliteDriver extends NObject implements DibiDriverInterface
}
/**
* Converts database error to DibiDriverException
*
* @throws DibiDriverException
*/
protected function throwException($sql=NULL)
{
$errno = sqlite_last_error($this->connection);
throw new DibiDriverException(sqlite_error_string($errno), $errno, $sql);
}
/**
* Returns the connection resource
*