summaryrefslogtreecommitdiff
path: root/program/lib
diff options
context:
space:
mode:
authorAleksander Machniak <alec@alec.pl>2014-09-17 20:39:16 +0200
committerAleksander Machniak <alec@alec.pl>2014-09-17 20:39:16 +0200
commit84515776597960a8f33172ae2a38b418c944fdbb (patch)
tree8d5c24d78a0de2fce63aca1f650f5f9e53834113 /program/lib
parentd7c91c14f8ebacc6fde60b9a6cb16cab523102f1 (diff)
Added transactions support
Diffstat (limited to 'program/lib')
-rw-r--r--program/lib/Roundcube/rcube_db.php67
-rw-r--r--program/lib/Roundcube/rcube_db_oci8.php74
2 files changed, 137 insertions, 4 deletions
diff --git a/program/lib/Roundcube/rcube_db.php b/program/lib/Roundcube/rcube_db.php
index 72cad0119..5a2ad038c 100644
--- a/program/lib/Roundcube/rcube_db.php
+++ b/program/lib/Roundcube/rcube_db.php
@@ -555,7 +555,9 @@ class rcube_db
public function affected_rows($result = null)
{
if ($result || ($result === null && ($result = $this->last_result))) {
- return $result->rowCount();
+ if ($result !== true) {
+ return $result->rowCount();
+ }
}
return 0;
@@ -571,7 +573,7 @@ class rcube_db
*/
public function num_rows($result = null)
{
- if ($result || ($result === null && ($result = $this->last_result))) {
+ if (($result || ($result === null && ($result = $this->last_result))) && $result !== true) {
// repeat query with SELECT COUNT(*) ...
if (preg_match('/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/ims', $result->queryString, $m)) {
$query = $this->dbh->query('SELECT COUNT(*) FROM ' . $m[1], PDO::FETCH_NUM);
@@ -647,7 +649,9 @@ class rcube_db
protected function _fetch_row($result, $mode)
{
if ($result || ($result === null && ($result = $this->last_result))) {
- return $result->fetch($mode);
+ if ($result !== true) {
+ return $result->fetch($mode);
+ }
}
return false;
@@ -717,6 +721,63 @@ class rcube_db
}
/**
+ * Start transaction
+ *
+ * @return bool True on success, False on failure
+ */
+ public function startTransaction()
+ {
+ $this->db_connect('w', true);
+
+ // check connection before proceeding
+ if (!$this->is_connected()) {
+ return $this->last_result = false;
+ }
+
+ $this->debug('BEGIN TRANSACTION');
+
+ return $this->last_result = $this->dbh->beginTransaction();
+ }
+
+ /**
+ * Commit transaction
+ *
+ * @return bool True on success, False on failure
+ */
+ public function endTransaction()
+ {
+ $this->db_connect('w', true);
+
+ // check connection before proceeding
+ if (!$this->is_connected()) {
+ return $this->last_result = false;
+ }
+
+ $this->debug('COMMIT TRANSACTION');
+
+ return $this->last_result = $this->dbh->commit();
+ }
+
+ /**
+ * Rollback transaction
+ *
+ * @return bool True on success, False on failure
+ */
+ public function rollbackTransaction()
+ {
+ $this->db_connect('w', true);
+
+ // check connection before proceeding
+ if (!$this->is_connected()) {
+ return $this->last_result = false;
+ }
+
+ $this->debug('ROLLBACK TRANSACTION');
+
+ return $this->last_result = $this->dbh->rollBack();
+ }
+
+ /**
* Formats input so it can be safely used in a query
*
* @param mixed $input Value to quote
diff --git a/program/lib/Roundcube/rcube_db_oci8.php b/program/lib/Roundcube/rcube_db_oci8.php
index b9f63f38a..a55ba0459 100644
--- a/program/lib/Roundcube/rcube_db_oci8.php
+++ b/program/lib/Roundcube/rcube_db_oci8.php
@@ -110,8 +110,9 @@ class rcube_db_oci8 extends rcube_db
// prepare query
$result = oci_parse($this->dbh, $query);
+ $mode = $this->in_transaction ? OCI_NO_AUTO_COMMIT : OCI_COMMIT_ON_SUCCESS;
- if (!@oci_execute($result, OCI_COMMIT_ON_SUCCESS)) { // OCI_NO_AUTO_COMMIT
+ if (!@oci_execute($result, $mode)) {
$result = $this->handle_error($query, $result);
}
@@ -459,4 +460,75 @@ class rcube_db_oci8 extends rcube_db
return !$this->db_error;
}
+
+ /**
+ * Start transaction
+ *
+ * @return bool True on success, False on failure
+ */
+ public function startTransaction()
+ {
+ $this->db_connect('w', true);
+
+ // check connection before proceeding
+ if (!$this->is_connected()) {
+ return $this->last_result = false;
+ }
+
+ $this->debug('BEGIN TRANSACTION');
+
+ return $this->last_result = $this->in_transaction = true;
+ }
+
+ /**
+ * Commit transaction
+ *
+ * @return bool True on success, False on failure
+ */
+ public function endTransaction()
+ {
+ $this->db_connect('w', true);
+
+ // check connection before proceeding
+ if (!$this->is_connected()) {
+ return $this->last_result = false;
+ }
+
+ $this->debug('COMMIT TRANSACTION');
+
+ if ($result = @oci_commit($this->dbh)) {
+ $this->in_transaction = true;
+ }
+ else {
+ $this->handle_error('COMMIT');
+ }
+
+ return $this->last_result = $result;
+ }
+
+ /**
+ * Rollback transaction
+ *
+ * @return bool True on success, False on failure
+ */
+ public function rollbackTransaction()
+ {
+ $this->db_connect('w', true);
+
+ // check connection before proceeding
+ if (!$this->is_connected()) {
+ return $this->last_result = false;
+ }
+
+ $this->debug('ROLLBACK TRANSACTION');
+
+ if ($result = @oci_rollback($this->dbh)) {
+ $this->in_transaction = false;
+ }
+ else {
+ $this->handle_error('ROLLBACK');
+ }
+
+ return $this->last_result = $this->dbh->rollBack();
+ }
}