mirror of
https://github.com/dg/dibi.git
synced 2025-08-12 00:54:11 +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:
@@ -47,6 +47,12 @@ class DibiConnection extends NObject
|
||||
*/
|
||||
private $connected = FALSE;
|
||||
|
||||
/**
|
||||
* Is in transaction?
|
||||
* @var bool
|
||||
*/
|
||||
private $inTxn = FALSE;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@@ -122,6 +128,9 @@ class DibiConnection extends NObject
|
||||
final public function disconnect()
|
||||
{
|
||||
if ($this->connected) {
|
||||
if ($this->inTxn) {
|
||||
$this->rollback();
|
||||
}
|
||||
$this->driver->disconnect();
|
||||
$this->connected = FALSE;
|
||||
dibi::notify($this, 'disconnected');
|
||||
@@ -259,12 +268,14 @@ class DibiConnection extends NObject
|
||||
/**
|
||||
* 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 number of rows
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function affectedRows()
|
||||
{
|
||||
$rows = $this->driver->affectedRows();
|
||||
return $rows < 0 ? FALSE : $rows;
|
||||
if (!is_int($rows) || $rows < 0) throw new DibiException('Cannot retrieve number of affected rows');
|
||||
return $rows;
|
||||
}
|
||||
|
||||
|
||||
@@ -272,12 +283,15 @@ class DibiConnection extends NObject
|
||||
/**
|
||||
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query
|
||||
*
|
||||
* @return int|FALSE int on success or FALSE on failure
|
||||
* @param string optional sequence name
|
||||
* @return int
|
||||
* @throws DibiException
|
||||
*/
|
||||
public function insertId($sequence = NULL)
|
||||
{
|
||||
$id = $this->driver->insertId($sequence);
|
||||
return $id < 1 ? FALSE : $id;
|
||||
if ($id < 1) throw new DibiException('Cannot retrieve last generated ID');
|
||||
return $id;
|
||||
}
|
||||
|
||||
|
||||
@@ -289,8 +303,14 @@ class DibiConnection extends NObject
|
||||
public function begin()
|
||||
{
|
||||
$this->connect();
|
||||
if ($this->inTxn) {
|
||||
throw new DibiException('There is already an active transaction');
|
||||
}
|
||||
$this->driver->begin();
|
||||
$this->inTxn = TRUE;
|
||||
dibi::notify($this, 'begin');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
@@ -301,8 +321,11 @@ class DibiConnection extends NObject
|
||||
*/
|
||||
public function commit()
|
||||
{
|
||||
$this->connect();
|
||||
if (!$this->inTxn) {
|
||||
throw new DibiException('There is no active transaction');
|
||||
}
|
||||
$this->driver->commit();
|
||||
$this->inTxn = FALSE;
|
||||
dibi::notify($this, 'commit');
|
||||
}
|
||||
|
||||
@@ -314,8 +337,11 @@ class DibiConnection extends NObject
|
||||
*/
|
||||
public function rollback()
|
||||
{
|
||||
$this->connect();
|
||||
if (!$this->inTxn) {
|
||||
throw new DibiException('There is no active transaction');
|
||||
}
|
||||
$this->driver->rollback();
|
||||
$this->inTxn = FALSE;
|
||||
dibi::notify($this, 'rollback');
|
||||
}
|
||||
|
||||
@@ -375,6 +401,26 @@ class DibiConnection extends NObject
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Prevents unserialization
|
||||
*/
|
||||
public function __wakeup()
|
||||
{
|
||||
throw new DibiException('You cannot serialize or unserialize '.__CLASS__.' instances');
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Prevents serialization
|
||||
*/
|
||||
public function __sleep()
|
||||
{
|
||||
throw new DibiException('You cannot serialize or unserialize '.__CLASS__.' instances');
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns last error
|
||||
* @deprecated
|
||||
@@ -384,6 +430,4 @@ class DibiConnection extends NObject
|
||||
throw new BadMethodCallException(__METHOD__ . ' has been deprecated');
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@@ -65,7 +65,7 @@ interface 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
|
||||
*/
|
||||
function affectedRows();
|
||||
|
||||
@@ -83,6 +83,7 @@ interface DibiDriverInterface
|
||||
/**
|
||||
* Begins a transaction (if supported).
|
||||
* @return void
|
||||
* @throws DibiDriverException
|
||||
*/
|
||||
function begin();
|
||||
|
||||
@@ -91,6 +92,7 @@ interface DibiDriverInterface
|
||||
/**
|
||||
* Commits statements in a transaction.
|
||||
* @return void
|
||||
* @throws DibiDriverException
|
||||
*/
|
||||
function commit();
|
||||
|
||||
@@ -99,6 +101,7 @@ interface DibiDriverInterface
|
||||
/**
|
||||
* Rollback changes in a transaction.
|
||||
* @return void
|
||||
* @throws DibiDriverException
|
||||
*/
|
||||
function rollback();
|
||||
|
||||
@@ -165,6 +168,7 @@ interface DibiDriverInterface
|
||||
function free();
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns the connection resource
|
||||
*
|
||||
|
@@ -46,6 +46,13 @@ class DibiDriverException extends DibiException
|
||||
private $sql;
|
||||
|
||||
|
||||
/**
|
||||
* Construct an dibi driver exception
|
||||
*
|
||||
* @param string Message describing the exception
|
||||
* @param int Some code
|
||||
* @param string SQL command
|
||||
*/
|
||||
public function __construct($message = NULL, $code = 0, $sql = NULL)
|
||||
{
|
||||
parent::__construct($message, (int) $code);
|
||||
@@ -55,6 +62,9 @@ class DibiDriverException extends DibiException
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return string The SQL passed to the constructor
|
||||
*/
|
||||
final public function getSql()
|
||||
{
|
||||
return $this->sql;
|
||||
@@ -62,9 +72,22 @@ class DibiDriverException extends DibiException
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return string string represenation of exception with SQL command
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
return parent::__toString() . ($this->sql ? "\nSQL: " . $this->sql : '');
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @see NException::catchError (this is Late static binding fix
|
||||
*/
|
||||
public static function catchError($class = __CLASS__)
|
||||
{
|
||||
parent::catchError($class);
|
||||
}
|
||||
|
||||
}
|
@@ -74,7 +74,7 @@ class NException extends Exception
|
||||
/**
|
||||
* Returns string represenation of exception
|
||||
*
|
||||
* @return void
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
|
Reference in New Issue
Block a user