From fb2621eb04b4f4deb549ed864fb9e7980c6a89d3 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 17 Nov 2008 16:17:16 +0000 Subject: [PATCH] - implemented savepoints support --- dibi/dibi.php | 19 +++++++++------- dibi/drivers/mssql.php | 11 ++++++---- dibi/drivers/mysql.php | 15 +++++++------ dibi/drivers/mysqli.php | 15 +++++++------ dibi/drivers/odbc.php | 9 +++++--- dibi/drivers/oracle.php | 9 +++++--- dibi/drivers/pdo.php | 9 +++++--- dibi/drivers/postgre.php | 15 +++++++------ dibi/drivers/sqlite.php | 9 +++++--- dibi/libs/DibiConnection.php | 42 +++++++++++++++++++++++++----------- dibi/libs/DibiException.php | 4 ++-- dibi/libs/interfaces.php | 9 +++++--- 12 files changed, 107 insertions(+), 59 deletions(-) diff --git a/dibi/dibi.php b/dibi/dibi.php index 6da6d835..5db46c2e 100644 --- a/dibi/dibi.php +++ b/dibi/dibi.php @@ -56,7 +56,7 @@ if (!class_exists('FileNotFoundException', FALSE)) { class FileNotFoundException extends IOException {} } -if (!interface_exists(/*Nette::*/'IDebuggable', FALSE)) { +if (!interface_exists(/*Nette\*/'IDebuggable', FALSE)) { require_once dirname(__FILE__) . '/Nette/IDebuggable.php'; } @@ -380,36 +380,39 @@ class dibi /** * Begins a transaction - Monostate for DibiConnection::begin(). + * @param string optinal savepoint name * @return void * @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 * @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(). + * @param string optinal savepoint name * @return void * @throws DibiException */ - public static function rollback() + public static function rollback($savepoint = NULL) { - self::getConnection()->rollback(); + self::getConnection()->rollback($savepoint); } diff --git a/dibi/drivers/mssql.php b/dibi/drivers/mssql.php index 2e825677..8388f8fc 100644 --- a/dibi/drivers/mssql.php +++ b/dibi/drivers/mssql.php @@ -137,7 +137,7 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver if (is_resource($res)) { $row = mssql_fetch_row($res); return $row[0]; - } + } return FALSE; } @@ -145,10 +145,11 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver /** * Begins a transaction (if supported). + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function begin() + public function begin($savepoint = NULL) { $this->query('BEGIN TRANSACTION'); } @@ -157,10 +158,11 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver /** * Commits statements in a transaction. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function commit() + public function commit($savepoint = NULL) { $this->query('COMMIT'); } @@ -169,10 +171,11 @@ class DibiMsSqlDriver extends DibiObject implements IDibiDriver /** * Rollback changes in a transaction. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function rollback() + public function rollback($savepoint = NULL) { $this->query('ROLLBACK'); } diff --git a/dibi/drivers/mysql.php b/dibi/drivers/mysql.php index 4cf9b1c9..da7bb194 100644 --- a/dibi/drivers/mysql.php +++ b/dibi/drivers/mysql.php @@ -199,36 +199,39 @@ class DibiMySqlDriver extends DibiObject implements IDibiDriver /** * Begins a transaction (if supported). + * @param string optinal savepoint name * @return void * @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. + * @param string optinal savepoint name * @return void * @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. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function rollback() + public function rollback($savepoint = NULL) { - $this->query('ROLLBACK'); + $this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK'); } diff --git a/dibi/drivers/mysqli.php b/dibi/drivers/mysqli.php index 47e04cac..fcf05813 100644 --- a/dibi/drivers/mysqli.php +++ b/dibi/drivers/mysqli.php @@ -183,36 +183,39 @@ class DibiMySqliDriver extends DibiObject implements IDibiDriver /** * Begins a transaction (if supported). + * @param string optinal savepoint name * @return void * @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. + * @param string optinal savepoint name * @return void * @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. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function rollback() + public function rollback($savepoint = NULL) { - $this->query('ROLLBACK'); + $this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK'); } diff --git a/dibi/drivers/odbc.php b/dibi/drivers/odbc.php index cf392186..fa5c6f18 100644 --- a/dibi/drivers/odbc.php +++ b/dibi/drivers/odbc.php @@ -143,10 +143,11 @@ class DibiOdbcDriver extends DibiObject implements IDibiDriver /** * Begins a transaction (if supported). + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function begin() + public function begin($savepoint = NULL) { if (!odbc_autocommit($this->connection, FALSE)) { 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. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function commit() + public function commit($savepoint = NULL) { if (!odbc_commit($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. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function rollback() + public function rollback($savepoint = NULL) { if (!odbc_rollback($this->connection)) { throw new DibiDriverException(odbc_errormsg($this->connection) . ' ' . odbc_error($this->connection)); diff --git a/dibi/drivers/oracle.php b/dibi/drivers/oracle.php index 302448ec..65add54f 100644 --- a/dibi/drivers/oracle.php +++ b/dibi/drivers/oracle.php @@ -144,10 +144,11 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver /** * Begins a transaction (if supported). + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function begin() + public function begin($savepoint = NULL) { $this->autocommit = FALSE; } @@ -156,10 +157,11 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver /** * Commits statements in a transaction. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function commit() + public function commit($savepoint = NULL) { if (!oci_commit($this->connection)) { $err = oci_error($this->connection); @@ -172,10 +174,11 @@ class DibiOracleDriver extends DibiObject implements IDibiDriver /** * Rollback changes in a transaction. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function rollback() + public function rollback($savepoint = NULL) { if (!oci_rollback($this->connection)) { $err = oci_error($this->connection); diff --git a/dibi/drivers/pdo.php b/dibi/drivers/pdo.php index 2e60265c..6ff8d3c2 100644 --- a/dibi/drivers/pdo.php +++ b/dibi/drivers/pdo.php @@ -165,10 +165,11 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver /** * Begins a transaction (if supported). + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function begin() + public function begin($savepoint = NULL) { if (!$this->connection->beginTransaction()) { $err = $this->connection->errorInfo(); @@ -180,10 +181,11 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver /** * Commits statements in a transaction. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function commit() + public function commit($savepoint = NULL) { if (!$this->connection->commit()) { $err = $this->connection->errorInfo(); @@ -195,10 +197,11 @@ class DibiPdoDriver extends DibiObject implements IDibiDriver /** * Rollback changes in a transaction. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function rollback() + public function rollback($savepoint = NULL) { if (!$this->connection->rollBack()) { $err = $this->connection->errorInfo(); diff --git a/dibi/drivers/postgre.php b/dibi/drivers/postgre.php index ef4e31bb..591977ed 100644 --- a/dibi/drivers/postgre.php +++ b/dibi/drivers/postgre.php @@ -175,36 +175,39 @@ class DibiPostgreDriver extends DibiObject implements IDibiDriver /** * Begins a transaction (if supported). + * @param string optinal savepoint name * @return void * @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. + * @param string optinal savepoint name * @return void * @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. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function rollback() + public function rollback($savepoint = NULL) { - $this->query('ROLLBACK'); + $this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK'); } diff --git a/dibi/drivers/sqlite.php b/dibi/drivers/sqlite.php index c2e859ab..6a927e32 100644 --- a/dibi/drivers/sqlite.php +++ b/dibi/drivers/sqlite.php @@ -151,10 +151,11 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver /** * Begins a transaction (if supported). + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function begin() + public function begin($savepoint = NULL) { $this->query('BEGIN'); } @@ -163,10 +164,11 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver /** * Commits statements in a transaction. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function commit() + public function commit($savepoint = NULL) { $this->query('COMMIT'); } @@ -175,10 +177,11 @@ class DibiSqliteDriver extends DibiObject implements IDibiDriver /** * Rollback changes in a transaction. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - public function rollback() + public function rollback($savepoint = NULL) { $this->query('ROLLBACK'); } diff --git a/dibi/libs/DibiConnection.php b/dibi/libs/DibiConnection.php index 5228753f..25a19a6e 100644 --- a/dibi/libs/DibiConnection.php +++ b/dibi/libs/DibiConnection.php @@ -330,18 +330,23 @@ class DibiConnection extends DibiObject /** * Begins a transaction (if supported). + * @param string optinal savepoint name * @return void */ - public function begin() + public function begin($savepoint = NULL) { $this->connect(); - if ($this->inTxn) { + if (!$savepoint && $this->inTxn) { throw new DibiException('There is already an active transaction.'); } 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; if (isset($ticket)) { $this->profiler->after($ticket); @@ -352,18 +357,19 @@ class DibiConnection extends DibiObject /** * Commits statements in a transaction. + * @param string optinal savepoint name * @return void */ - public function commit() + public function commit($savepoint = NULL) { if (!$this->inTxn) { throw new DibiException('There is no active transaction.'); } if ($this->profiler !== NULL) { - $ticket = $this->profiler->before($this, IDibiProfiler::COMMIT); + $ticket = $this->profiler->before($this, IDibiProfiler::COMMIT, $savepoint); } - $this->driver->commit(); - $this->inTxn = FALSE; + $this->driver->commit($savepoint); + $this->inTxn = (bool) $savepoint; if (isset($ticket)) { $this->profiler->after($ticket); } @@ -373,18 +379,19 @@ class DibiConnection extends DibiObject /** * Rollback changes in a transaction. + * @param string optinal savepoint name * @return void */ - public function rollback() + public function rollback($savepoint = NULL) { if (!$this->inTxn) { throw new DibiException('There is no active transaction.'); } if ($this->profiler !== NULL) { - $ticket = $this->profiler->before($this, IDibiProfiler::ROLLBACK); + $ticket = $this->profiler->before($this, IDibiProfiler::ROLLBACK, $savepoint); } - $this->driver->rollback(); - $this->inTxn = FALSE; + $this->driver->rollback($savepoint); + $this->inTxn = (bool) $savepoint; if (isset($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. * @param string unescaped string diff --git a/dibi/libs/DibiException.php b/dibi/libs/DibiException.php index 42569573..b87bc8d6 100644 --- a/dibi/libs/DibiException.php +++ b/dibi/libs/DibiException.php @@ -41,7 +41,7 @@ class DibiException extends Exception * @copyright Copyright (c) 2005, 2008 David Grudl * @package dibi */ -class DibiDriverException extends DibiException implements /*Nette::*/IDebuggable +class DibiDriverException extends DibiException implements /*Nette\*/IDebuggable { /** @var string */ private static $errorMsg; @@ -85,7 +85,7 @@ class DibiDriverException extends DibiException implements /*Nette::*/IDebuggabl - /********************* interface Nette::IDebuggable ****************d*g**/ + /********************* interface Nette\IDebuggable ****************d*g**/ /** diff --git a/dibi/libs/interfaces.php b/dibi/libs/interfaces.php index 12436fd5..b834d89a 100644 --- a/dibi/libs/interfaces.php +++ b/dibi/libs/interfaces.php @@ -161,28 +161,31 @@ interface IDibiDriver /** * Begins a transaction (if supported). + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - function begin(); + function begin($savepoint = NULL); /** * Commits statements in a transaction. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - function commit(); + function commit($savepoint = NULL); /** * Rollback changes in a transaction. + * @param string optinal savepoint name * @return void * @throws DibiDriverException */ - function rollback(); + function rollback($savepoint = NULL);