PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Database » DB » Bug #5655

Sqlite: missing transactions implementation

Details

Submitted2005-10-11 10:51 UTC
Fromdimitri at vinogradov dot de
StatusWont fix
PackageDB
PHP VersionIrrelevant
OSIrrelevant
Roadmaps(Not assigned)

Comments

[2005-10-11 10:51 UTC] dimitri at vinogradov dot de

Description:
------------
Sqlite: missing transactions implementation

Test script:
---------------
--- sqlite.orig.php 2005-10-11 12:28:40.000000000 +0200
+++ sqlite.new.php 2005-10-11 12:37:36.000000000 +0200
@@ -86,7 +86,7 @@
'pconnect' => true,
'prepare' => false,
'ssl' => false,
- 'transactions' => false,
+ 'transactions' => true,
);

/**
@@ -112,6 +112,24 @@
* @var array
*/
var $dsn = array();
+
+ /**
+ * Should data manipulation queries be committed automatically?
+ * @var bool
+ * @access private
+ */
+ var $autocommit = true;
+
+ /**
+ * The quantity of transactions begun
+ *
+ * {@internal While this is private, it can't actually be designated
+ * private in PHP 5 because it is directly accessed in the test suite.}}
+ *
+ * @var integer
+ * @access private
+ */
+ var $transaction_opcount = 0;


/**
@@ -283,6 +301,16 @@
$ismanip = DB::isManip($query);
$this->last_query = $query;
$query = $this->modifyQuery($query);
+
+ if (!$this->autocommit && $ismanip) {
+ if ($this->transaction_opcount == 0) {
+ $result = @sqlite_query('BEGIN TRANSACTION', $this->connection);
+ if (!$result) {
+ return $this->sqliteRaiseError();
+ }
+ }
+ $this->transaction_opcount++;
+ }

$php_errormsg = '';

@@ -930,6 +958,69 @@
}

// }}}
+ // {{{ autoCommit()
+
+ /**
+ * Enables or disables automatic commits
+ *
+ * @param bool $onoff true turns it on, false turns it off
+ *
+ * @return int DB_OK on success. A DB_Error object if the driver
+ * doesn't support auto-committing transactions.
+ */
+ function autoCommit($onoff = false)
+ {
+ // XXX if $this->transaction_opcount > 0, we should probably
+ // issue a warning here.
+ $this->autocommit = $onoff ? true : false;
+ return DB_OK;
+ }
+
+
+
+
+ // }}}
+ // {{{ commit()
+
+ /**
+ * Commits the current transaction
+ *
+ * @return int DB_OK on success. A DB_Error object on failure.
+ */
+ function commit()
+ {
+ if ($this->transaction_opcount > 0) {
+ $result = @sqlite_query('COMMIT', $this->connection);
+ $this->transaction_opcount = 0;
+ if (!$result) {
+ return $this->sqliteRaiseError();
+ }
+ }
+ return DB_OK;
+ }
+
+
+
+ // }}}
+ // {{{ rollback()
+
+ /**
+ * Reverts the current transaction
+ *
+ * @return int DB_OK on success. A DB_Error object on failure.
+ */
+ function rollback()
+ {
+ if ($this->transaction_opcount > 0) {
+ $result = @sqlite_query('ROLLBACK', $this->connection);
+ $this->transaction_opcount = 0;
+ if (!$result) {
+ return $this->sqliteRaiseError();
+ }
+ }
+ return DB_OK;
+ }
+ // }}}
}

/*