1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 05:37:39 +02:00

- implemented savepoints support

This commit is contained in:
David Grudl
2008-11-17 16:17:16 +00:00
parent 2f690b63f9
commit fb2621eb04
12 changed files with 107 additions and 59 deletions

View File

@@ -56,7 +56,7 @@ if (!class_exists('FileNotFoundException', FALSE)) {
class FileNotFoundException extends IOException {} class FileNotFoundException extends IOException {}
} }
if (!interface_exists(/*Nette::*/'IDebuggable', FALSE)) { if (!interface_exists(/*Nette\*/'IDebuggable', FALSE)) {
require_once dirname(__FILE__) . '/Nette/IDebuggable.php'; require_once dirname(__FILE__) . '/Nette/IDebuggable.php';
} }
@@ -380,36 +380,39 @@ class dibi
/** /**
* Begins a transaction - Monostate for DibiConnection::begin(). * Begins a transaction - Monostate for DibiConnection::begin().
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiException * @throws DibiException
*/ */
public static function begin() public static function begin($savepoint = NULL)
{ {
self::getConnection()->begin(); self::getConnection()->begin($savepoint);
} }
/** /**
* Commits statements in a transaction - Monostate for DibiConnection::commit(). * Commits statements in a transaction - Monostate for DibiConnection::commit($savepoint = NULL).
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiException * @throws DibiException
*/ */
public static function commit() public static function commit($savepoint = NULL)
{ {
self::getConnection()->commit(); self::getConnection()->commit($savepoint);
} }
/** /**
* Rollback changes in a transaction - Monostate for DibiConnection::rollback(). * Rollback changes in a transaction - Monostate for DibiConnection::rollback().
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiException * @throws DibiException
*/ */
public static function rollback() public static function rollback($savepoint = NULL)
{ {
self::getConnection()->rollback(); self::getConnection()->rollback($savepoint);
} }

View File

@@ -145,10 +145,11 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function begin() public function begin($savepoint = NULL)
{ {
$this->query('BEGIN TRANSACTION'); $this->query('BEGIN TRANSACTION');
} }
@@ -157,10 +158,11 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function commit() public function commit($savepoint = NULL)
{ {
$this->query('COMMIT'); $this->query('COMMIT');
} }
@@ -169,10 +171,11 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function rollback() public function rollback($savepoint = NULL)
{ {
$this->query('ROLLBACK'); $this->query('ROLLBACK');
} }

View File

@@ -199,36 +199,39 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function begin() public function begin($savepoint = NULL)
{ {
$this->query('START TRANSACTION'); $this->query($savepoint ? "SAVEPOINT $savepoint" : 'START TRANSACTION');
} }
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function commit() public function commit($savepoint = NULL)
{ {
$this->query('COMMIT'); $this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT');
} }
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function rollback() public function rollback($savepoint = NULL)
{ {
$this->query('ROLLBACK'); $this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK');
} }

View File

@@ -183,36 +183,39 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function begin() public function begin($savepoint = NULL)
{ {
$this->query('START TRANSACTION'); $this->query($savepoint ? "SAVEPOINT $savepoint" : 'START TRANSACTION');
} }
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function commit() public function commit($savepoint = NULL)
{ {
$this->query('COMMIT'); $this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT');
} }
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function rollback() public function rollback($savepoint = NULL)
{ {
$this->query('ROLLBACK'); $this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK');
} }

View File

@@ -143,10 +143,11 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function begin() public function begin($savepoint = NULL)
{ {
if (!odbc_autocommit($this->connection, FALSE)) { if (!odbc_autocommit($this->connection, FALSE)) {
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
@@ -157,10 +158,11 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function commit() public function commit($savepoint = NULL)
{ {
if (!odbc_commit($this->connection)) { if (!odbc_commit($this->connection)) {
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));
@@ -172,10 +174,11 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function rollback() public function rollback($savepoint = NULL)
{ {
if (!odbc_rollback($this->connection)) { if (!odbc_rollback($this->connection)) {
throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection));

View File

@@ -144,10 +144,11 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function begin() public function begin($savepoint = NULL)
{ {
$this->autocommit = FALSE; $this->autocommit = FALSE;
} }
@@ -156,10 +157,11 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function commit() public function commit($savepoint = NULL)
{ {
if (!oci_commit($this->connection)) { if (!oci_commit($this->connection)) {
$err = oci_error($this->connection); $err = oci_error($this->connection);
@@ -172,10 +174,11 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function rollback() public function rollback($savepoint = NULL)
{ {
if (!oci_rollback($this->connection)) { if (!oci_rollback($this->connection)) {
$err = oci_error($this->connection); $err = oci_error($this->connection);

View File

@@ -165,10 +165,11 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function begin() public function begin($savepoint = NULL)
{ {
if (!$this->connection->beginTransaction()) { if (!$this->connection->beginTransaction()) {
$err = $this->connection->errorInfo(); $err = $this->connection->errorInfo();
@@ -180,10 +181,11 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function commit() public function commit($savepoint = NULL)
{ {
if (!$this->connection->commit()) { if (!$this->connection->commit()) {
$err = $this->connection->errorInfo(); $err = $this->connection->errorInfo();
@@ -195,10 +197,11 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function rollback() public function rollback($savepoint = NULL)
{ {
if (!$this->connection->rollBack()) { if (!$this->connection->rollBack()) {
$err = $this->connection->errorInfo(); $err = $this->connection->errorInfo();

View File

@@ -175,36 +175,39 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function begin() public function begin($savepoint = NULL)
{ {
$this->query('START TRANSACTION'); $this->query($savepoint ? "SAVEPOINT $savepoint" : 'START TRANSACTION');
} }
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function commit() public function commit($savepoint = NULL)
{ {
$this->query('COMMIT'); $this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT');
} }
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function rollback() public function rollback($savepoint = NULL)
{ {
$this->query('ROLLBACK'); $this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK');
} }

View File

@@ -151,10 +151,11 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function begin() public function begin($savepoint = NULL)
{ {
$this->query('BEGIN'); $this->query('BEGIN');
} }
@@ -163,10 +164,11 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function commit() public function commit($savepoint = NULL)
{ {
$this->query('COMMIT'); $this->query('COMMIT');
} }
@@ -175,10 +177,11 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
public function rollback() public function rollback($savepoint = NULL)
{ {
$this->query('ROLLBACK'); $this->query('ROLLBACK');
} }

View File

@@ -330,18 +330,23 @@ class DibiConnection extends DibiObject
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optinal savepoint name
* @return void * @return void
*/ */
public function begin() public function begin($savepoint = NULL)
{ {
$this->connect(); $this->connect();
if ($this->inTxn) { if (!$savepoint && $this->inTxn) {
throw new DibiException('There is already an active transaction.'); throw new DibiException('There is already an active transaction.');
} }
if ($this->profiler !== NULL) { if ($this->profiler !== NULL) {
$ticket = $this->profiler->before($this, IDibiProfiler::BEGIN); $ticket = $this->profiler->before($this, IDibiProfiler::BEGIN, $savepoint);
} }
$this->driver->begin(); if ($savepoint && !$this->inTxn) {
$this->driver->begin();
}
$this->driver->begin($savepoint);
$this->inTxn = TRUE; $this->inTxn = TRUE;
if (isset($ticket)) { if (isset($ticket)) {
$this->profiler->after($ticket); $this->profiler->after($ticket);
@@ -352,18 +357,19 @@ class DibiConnection extends DibiObject
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optinal savepoint name
* @return void * @return void
*/ */
public function commit() public function commit($savepoint = NULL)
{ {
if (!$this->inTxn) { if (!$this->inTxn) {
throw new DibiException('There is no active transaction.'); throw new DibiException('There is no active transaction.');
} }
if ($this->profiler !== NULL) { if ($this->profiler !== NULL) {
$ticket = $this->profiler->before($this, IDibiProfiler::COMMIT); $ticket = $this->profiler->before($this, IDibiProfiler::COMMIT, $savepoint);
} }
$this->driver->commit(); $this->driver->commit($savepoint);
$this->inTxn = FALSE; $this->inTxn = (bool) $savepoint;
if (isset($ticket)) { if (isset($ticket)) {
$this->profiler->after($ticket); $this->profiler->after($ticket);
} }
@@ -373,18 +379,19 @@ class DibiConnection extends DibiObject
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optinal savepoint name
* @return void * @return void
*/ */
public function rollback() public function rollback($savepoint = NULL)
{ {
if (!$this->inTxn) { if (!$this->inTxn) {
throw new DibiException('There is no active transaction.'); throw new DibiException('There is no active transaction.');
} }
if ($this->profiler !== NULL) { if ($this->profiler !== NULL) {
$ticket = $this->profiler->before($this, IDibiProfiler::ROLLBACK); $ticket = $this->profiler->before($this, IDibiProfiler::ROLLBACK, $savepoint);
} }
$this->driver->rollback(); $this->driver->rollback($savepoint);
$this->inTxn = FALSE; $this->inTxn = (bool) $savepoint;
if (isset($ticket)) { if (isset($ticket)) {
$this->profiler->after($ticket); $this->profiler->after($ticket);
} }
@@ -392,6 +399,17 @@ class DibiConnection extends DibiObject
/**
* Is in transaction?
* @return bool
*/
public function inTransaction()
{
return $this->inTxn;
}
/** /**
* Encodes data for use in an SQL statement. * Encodes data for use in an SQL statement.
* @param string unescaped string * @param string unescaped string

View File

@@ -41,7 +41,7 @@ class DibiException extends Exception
* @copyright Copyright (c) 2005, 2008 David Grudl * @copyright Copyright (c) 2005, 2008 David Grudl
* @package dibi * @package dibi
*/ */
class DibiDriverException extends DibiException implements /*Nette::*/IDebuggable class DibiDriverException extends DibiException implements /*Nette\*/IDebuggable
{ {
/** @var string */ /** @var string */
private static $errorMsg; private static $errorMsg;
@@ -85,7 +85,7 @@ class DibiDriverException extends DibiException implements /*Nette::*/IDebuggabl
/********************* interface Nette::IDebuggable ****************d*g**/ /********************* interface Nette\IDebuggable ****************d*g**/
/** /**

View File

@@ -161,28 +161,31 @@ interface IDibiDriver
/** /**
* Begins a transaction (if supported). * Begins a transaction (if supported).
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
function begin(); function begin($savepoint = NULL);
/** /**
* Commits statements in a transaction. * Commits statements in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
function commit(); function commit($savepoint = NULL);
/** /**
* Rollback changes in a transaction. * Rollback changes in a transaction.
* @param string optinal savepoint name
* @return void * @return void
* @throws DibiDriverException * @throws DibiDriverException
*/ */
function rollback(); function rollback($savepoint = NULL);