PATCH: DB::ifx.php
| From: | James Kaufman | Date: | Fri, 27 Jun 2003 02:36:22 +0000 |
| Subject: | PATCH: DB::ifx.php | ||
| Groups: | php.pear.dev | ||
| Request: | Send a blank email to pear-dev+get-17791@lists.php.net to get a copy of this message | ||
Attached is a patch for ifx.php for PEAR's DB package. The patch adds
transaction capability in much the same way that it exists for mysql. The
commit flag defaults to true, but if the programmer wants to start a
transaction, s/he can set the commit flag to false, and all further queries
that change the database will be part of the transaction. Here is the basic
usage:
$dbh->autoCommit(false); // start the transaction
$res=$dbh->query('update database...');
if (!$res) {
$dbh->rollback();
die...
}
$res=$dbh->query('update more...);
if (!$res) {
$dbh->rollback();
die...
}
$dbh->commit(); // commit the transaction
--
Jim Kaufman mailto:jmk@linuxforbusiness.net
Linux Evangelist cell: 612-481-9778
public key 0x6D802619 fax: 952-937-9832
http://www.linuxforbusiness.net
? ifx.diff Index: ifx.php =================================================================== RCS file: /repository/pear/DB/DB/ifx.php,v retrieving revision 1.10 diff -u -u -r1.10 ifx.php --- ifx.php 10 Jun 2003 20:16:39 -0000 1.10 +++ ifx.php 27 Jun 2003 02:27:04 -0000 @@ -39,6 +39,8 @@ var $connection; var $affected = 0; var $dsn = array(); + var $transaction_opcount = 0; + var $autocommit = true; var $fetchmode = DB_FETCHMODE_ORDERED; /* Default fetch mode */ // }}} @@ -127,12 +129,22 @@ */ function simpleQuery($query) { + $ismanip = DB::isManip($query); $this->last_query = $query; if (preg_match('/(SELECT)/i', $query)) { //TESTME: Use !DB::isManip()? // the scroll is needed for fetching absolute row numbers // in a select query result $result = @ifx_query($query, $this->connection, IFX_SCROLL); } else { + if (!$this->autocommit && $ismanip) { + if ($this->transaction_opcount == 0) { + $result = @ifx_query('BEGIN WORK', $this->connection); + if (!$result) { + return $this->ifxraiseError(); + } + } + $this->transaction_opcount++; + } $result = @ifx_query($query, $this->connection); } if (!$result) { @@ -144,7 +156,7 @@ if (preg_match('/(SELECT)/i', $query)) { return $result; } - // Result has to be freeed even with a insert or update + // Result has to be freed even with a insert or update ifx_free_result($result); return DB_OK; @@ -261,6 +273,56 @@ unset($this->prepare_tokens[(int)$result]); unset($this->prepare_types[(int)$result]); return true; + } + + // }}} + // {{{ autoCommit() + + /** + * Enable/disable automatic commits + */ + function autoCommit($onoff = true) + { + // XXX if $this->transaction_opcount > 0, we should probably + // issue a warning here. + $this->autocommit = $onoff ? true : false; + return DB_OK; + } + + // }}} + // {{{ commit() + + /** + * Commit the current transaction. + */ + function commit() + { + if ($this->transaction_opcount > 0) { + $result = @ifx_query('COMMIT WORK', $this->connection); + $this->transaction_opcount = 0; + if (!$result) { + return $this->ifxRaiseError(); + } + } + return DB_OK; + } + + // }}} + // {{{ rollback() + + /** + * Roll back (undo) the current transaction. + */ + function rollback() + { + if ($this->transaction_opcount > 0) { + $result = @ifx_query('ROLLBACK WORK', $this->connection); + $this->transaction_opcount = 0; + if (!$result) { + return $this->ifxRaiseError(); + } + } + return DB_OK; } // }}}
? ifx.diff Index: ifx.php =================================================================== RCS file: /repository/pear/DB/DB/ifx.php,v retrieving revision 1.10 diff -u -u -r1.10 ifx.php --- ifx.php 10 Jun 2003 20:16:39 -0000 1.10 +++ ifx.php 27 Jun 2003 02:27:04 -0000 @@ -39,6 +39,8 @@ var $connection; var $affected = 0; var $dsn = array(); + var $transaction_opcount = 0; + var $autocommit = true; var $fetchmode = DB_FETCHMODE_ORDERED; /* Default fetch mode */ // }}} @@ -127,12 +129,22 @@ */ function simpleQuery($query) { + $ismanip = DB::isManip($query); $this->last_query = $query; if (preg_match('/(SELECT)/i', $query)) { //TESTME: Use !DB::isManip()? // the scroll is needed for fetching absolute row numbers // in a select query result $result = @ifx_query($query, $this->connection, IFX_SCROLL); } else { + if (!$this->autocommit && $ismanip) { + if ($this->transaction_opcount == 0) { + $result = @ifx_query('BEGIN WORK', $this->connection); + if (!$result) { + return $this->ifxraiseError(); + } + } + $this->transaction_opcount++; + } $result = @ifx_query($query, $this->connection); } if (!$result) { @@ -144,7 +156,7 @@ if (preg_match('/(SELECT)/i', $query)) { return $result; } - // Result has to be freeed even with a insert or update + // Result has to be freed even with a insert or update ifx_free_result($result); return DB_OK; @@ -261,6 +273,56 @@ unset($this->prepare_tokens[(int)$result]); unset($this->prepare_types[(int)$result]); return true; + } + + // }}} + // {{{ autoCommit() + + /** + * Enable/disable automatic commits + */ + function autoCommit($onoff = true) + { + // XXX if $this->transaction_opcount > 0, we should probably + // issue a warning here. + $this->autocommit = $onoff ? true : false; + return DB_OK; + } + + // }}} + // {{{ commit() + + /** + * Commit the current transaction. + */ + function commit() + { + if ($this->transaction_opcount > 0) { + $result = @ifx_query('COMMIT WORK', $this->connection); + $this->transaction_opcount = 0; + if (!$result) { + return $this->ifxRaiseError(); + } + } + return DB_OK; + } + + // }}} + // {{{ rollback() + + /** + * Roll back (undo) the current transaction. + */ + function rollback() + { + if ($this->transaction_opcount > 0) { + $result = @ifx_query('ROLLBACK WORK', $this->connection); + $this->transaction_opcount = 0; + if (!$result) { + return $this->ifxRaiseError(); + } + } + return DB_OK; } // }}}