ssb Mon Feb 19 04:22:31 2001 EDT
Added files:
/php4/pear/DB/tests transactions.inc
/php4/pear/DB/tests/pgsql 011.phpt
Modified files:
/php4/pear/DB common.php ibase.php msql.php mssql.php mysql.php
oci8.php odbc.php pgsql.php sybase.php
Log:
@Fixed pgsql transaction support (Stig, PEAR/DB)
also added "dsn" property in all backends
Index: php4/pear/DB/common.php
diff -u php4/pear/DB/common.php:1.37 php4/pear/DB/common.php:1.38
--- php4/pear/DB/common.php:1.37 Sun Feb 18 08:23:03 2001
+++ php4/pear/DB/common.php Mon Feb 19 04:22:25 2001
@@ -44,6 +44,7 @@
'persistent' => false, // persistent connection?
'optimize' => 'performance', // 'performance' or 'portability'
);
+ var $dbh;
// }}}
// {{{ toString()
Index: php4/pear/DB/ibase.php
diff -u php4/pear/DB/ibase.php:1.16 php4/pear/DB/ibase.php:1.17
--- php4/pear/DB/ibase.php:1.16 Fri Feb 16 09:14:39 2001
+++ php4/pear/DB/ibase.php Mon Feb 19 04:22:26 2001
@@ -16,7 +16,7 @@
// | Authors: Sterling Hughes <[EMAIL PROTECTED]> |
// +----------------------------------------------------------------------+
//
-// $Id: ibase.php,v 1.16 2001/02/16 17:14:39 chagenbu Exp $
+// $Id: ibase.php,v 1.17 2001/02/19 12:22:26 ssb Exp $
//
// Database independent query interface definition for PHP's Interbase
// extension.
@@ -53,6 +53,7 @@
if (!$dsninfo || !$dsninfo['phptype']) {
return $this->raiseError("invalid data source name");
}
+ $this->dsn = $dsninfo;
$user = $dsninfo['username'];
$pw = $dsninfo['password'];
$dbhost = $dsninfo['hostspec'] ?
Index: php4/pear/DB/msql.php
diff -u php4/pear/DB/msql.php:1.15 php4/pear/DB/msql.php:1.16
--- php4/pear/DB/msql.php:1.15 Fri Feb 16 09:14:39 2001
+++ php4/pear/DB/msql.php Mon Feb 19 04:22:26 2001
@@ -16,7 +16,7 @@
// | Authors: Sterling Hughes <[EMAIL PROTECTED]> |
// +----------------------------------------------------------------------+
//
-// $Id: msql.php,v 1.15 2001/02/16 17:14:39 chagenbu Exp $
+// $Id: msql.php,v 1.16 2001/02/19 12:22:26 ssb Exp $
//
// Database independent query interface definition for PHP's Mini-SQL
// extension.
@@ -53,7 +53,7 @@
if (!$dsninfo || !$dsninfo['phptype']) {
return $this->raiseError();
}
-
+ $this->dsn = $dsninfo;
$user = $dsninfo['username'];
$pw = $dsninfo['password'];
$dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
Index: php4/pear/DB/mssql.php
diff -u php4/pear/DB/mssql.php:1.18 php4/pear/DB/mssql.php:1.19
--- php4/pear/DB/mssql.php:1.18 Fri Feb 16 09:14:39 2001
+++ php4/pear/DB/mssql.php Mon Feb 19 04:22:26 2001
@@ -16,7 +16,7 @@
// | Authors: Sterling Hughes <[EMAIL PROTECTED]> |
// +----------------------------------------------------------------------+
//
-// $Id: mssql.php,v 1.18 2001/02/16 17:14:39 chagenbu Exp $
+// $Id: mssql.php,v 1.19 2001/02/19 12:22:26 ssb Exp $
//
// Database independent query interface definition for PHP's Microsoft SQL Server
// extension.
@@ -53,7 +53,7 @@
if (!$dsninfo || !$dsninfo['phptype']) {
return $this->raiseError(mssql_get_last_message());
}
-
+ $this->dsn = $dsninfo;
$user = $dsninfo['username'];
$pw = $dsninfo['password'];
$dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
Index: php4/pear/DB/mysql.php
diff -u php4/pear/DB/mysql.php:1.48 php4/pear/DB/mysql.php:1.49
--- php4/pear/DB/mysql.php:1.48 Sat Feb 17 08:45:01 2001
+++ php4/pear/DB/mysql.php Mon Feb 19 04:22:26 2001
@@ -102,6 +102,8 @@
return $this->raiseError(); // XXX ERRORMSG
}
+ $this->dsn = $dsninfo;
+
$dbhost = $dsninfo["hostspec"] ? $dsninfo["hostspec"] : "localhost";
$user = $dsninfo["username"];
$pw = $dsninfo["password"];
Index: php4/pear/DB/oci8.php
diff -u php4/pear/DB/oci8.php:1.18 php4/pear/DB/oci8.php:1.19
--- php4/pear/DB/oci8.php:1.18 Sun Feb 18 08:24:00 2001
+++ php4/pear/DB/oci8.php Mon Feb 19 04:22:26 2001
@@ -79,6 +79,7 @@
if (!$dsninfo || !$dsninfo['phptype']) {
return $this->raiseError();
}
+ $this->dsn = $dsninfo;
$user = $dsninfo['username'];
$pw = $dsninfo['password'];
$hostspec = $dsninfo['hostspec'];
Index: php4/pear/DB/odbc.php
diff -u php4/pear/DB/odbc.php:1.27 php4/pear/DB/odbc.php:1.28
--- php4/pear/DB/odbc.php:1.27 Sun Feb 18 16:14:27 2001
+++ php4/pear/DB/odbc.php Mon Feb 19 04:22:26 2001
@@ -97,6 +97,7 @@
if (!$dsninfo || !$dsninfo['phptype']) {
return $this->raiseError(); // XXX ERRORMSG
}
+ $this->dsn = $dsninfo;
$this->dbsyntax = $dsninfo['dbsyntax'];
switch ($this->dbsyntax) {
case 'solid':
Index: php4/pear/DB/pgsql.php
diff -u php4/pear/DB/pgsql.php:1.29 php4/pear/DB/pgsql.php:1.30
--- php4/pear/DB/pgsql.php:1.29 Sun Feb 18 16:14:27 2001
+++ php4/pear/DB/pgsql.php Mon Feb 19 04:22:26 2001
@@ -14,7 +14,7 @@
// | [EMAIL PROTECTED] so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Rui Hirokawa <[EMAIL PROTECTED]> |
-// | |
+// | Stig Bakken <[EMAIL PROTECTED]> |
// +----------------------------------------------------------------------+
//
// Database independent query interface definition for PHP's PostgreSQL
@@ -38,10 +38,12 @@
var $phptype, $dbsyntax;
var $prepare_tokens = array();
var $prepare_types = array();
+ var $transaction_opcount = 0;
var $numrows;
var $row;
var $affected;
- var $autocommit = 1;
+ var $autocommit = true;
+ var $dsn;
// }}}
// {{{ constructor
@@ -85,6 +87,7 @@
if (!$dsninfo || !$dsninfo['phptype']) {
return $this->raiseError(); // XXX ERRORMSG
}
+ $this->dsn = $dsninfo;
$dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'unix';
if ($dbhost == 'unix') {
$protocol = 'unix';
@@ -154,18 +157,25 @@
*/
function simpleQuery($query)
{
+ $ismanip = DB::isManip($query);
$this->last_query = $query;
$query = $this->modifyQuery($query);
+ if (!$this->autocommit && $ismanip) {
+ if ($this->transaction_opcount == 0) {
+ $result = @pg_exec($this->connection, "begin;");
+ if (!$result) {
+ return $this->pgsqlRaiseError();
+ }
+ }
+ $this->transaction_opcount++;
+ }
$result = @pg_exec($this->connection, $query);
if (!$result) {
return $this->pgsqlRaiseError();
}
- if ($this->autocommit && DB::isManip($query)) {
- $this->commit();
- }
// Determine which queries that should return data, and which
// should return an error code only.
- if (DB::isManip($query)) {
+ if ($ismanip) {
$this->affected = @pg_cmdtuples($result);
return DB_OK;
} elseif (preg_match('/^\s*(SELECT)\s/i', $query) &&
@@ -413,9 +423,9 @@
// {{{ execute()
/**
- * @return int returns a PostgreSQL result resource for successful
- * SELECT queries, DB_OK for other successful queries. A DB error
- * code is returned on failure.
+ * @return mixed returns a DB result object for successful SELECT
+ * queries, DB_OK for other successful queries. A DB
+ * error is returned on failure.
*/
function execute($stmt, $data = false)
{
@@ -436,6 +446,8 @@
*/
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;
}
@@ -444,15 +456,18 @@
// {{{ commit()
/**
- * Commit transactions on the current connection
+ * Commit the current transaction.
*/
function commit()
{
- // hack to shut up error messages from ext/pgsql or libpq.a
- @fclose(@fopen("php://stderr", "w"));
- $result = @pg_exec($this->connection, "end;");
- if (!$result) {
- return $this->pgsqlRaiseError();
+ if ($this->transaction_opcount > 0) {
+ // (disabled) hack to shut up error messages from libpq.a
+ //@fclose(@fopen("php://stderr", "w"));
+ $result = @pg_exec($this->connection, "end;");
+ $this->transaction_opcount = 0;
+ if (!$result) {
+ return $this->pgsqlRaiseError();
+ }
}
return DB_OK;
}
@@ -461,13 +476,16 @@
// {{{ rollback()
/**
- * Roll back all uncommitted transactions on the current connection.
+ * Roll back (undo) the current transaction.
*/
function rollback()
{
- $result = @pg_exec($this->connection, "abort;");
- if (!$result) {
- return $this->pgsqlRaiseError();
+ if ($this->transaction_opcount > 0) {
+ $result = @pg_exec($this->connection, "abort;");
+ $this->transaction_opcount = 0;
+ if (!$result) {
+ return $this->pgsqlRaiseError();
+ }
}
return DB_OK;
}
Index: php4/pear/DB/sybase.php
diff -u php4/pear/DB/sybase.php:1.14 php4/pear/DB/sybase.php:1.15
--- php4/pear/DB/sybase.php:1.14 Fri Feb 16 09:14:40 2001
+++ php4/pear/DB/sybase.php Mon Feb 19 04:22:26 2001
@@ -16,7 +16,7 @@
// | Authors: Sterling Hughes <[EMAIL PROTECTED]> |
// +----------------------------------------------------------------------+
//
-// $Id: sybase.php,v 1.14 2001/02/16 17:14:40 chagenbu Exp $
+// $Id: sybase.php,v 1.15 2001/02/19 12:22:26 ssb Exp $
//
// Database independent query interface definition for PHP's Sybase
// extension.
@@ -61,6 +61,7 @@
if (!$dsninfo || !$dsninfo['phptype']) {
return $this->raiseError();
}
+ $this->dsn = $dsninfo;
$dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
$connect_function = $persistent ? 'sybase_pconnect' : 'sybase_connect';
$conn = $dbhost ? $connect_function($dbhost) : false;
Index: php4/pear/DB/tests/transactions.inc
+++ php4/pear/DB/tests/transactions.inc
<?php
// View the table from a separate connection so we don't disturb
// the transaction.
$dbh2 = DB::connect($dbh->dsn);
function dumptable() {
global $dbh, $dbh2;
printf("%s ops=%d\n", implode(" ", $dbh->getCol("SELECT b FROM phptest")),
$dbh->transaction_opcount);
}
$dbh->setErrorHandling(PEAR_ERROR_DIE);
$dbh->autoCommit(true);
$dbh->query("INSERT INTO phptest VALUES(1, 'one', 'One', '2001-02-19')");
print "after autocommit: ";
dumptable();
$dbh->autoCommit(false);
$dbh->query("INSERT INTO phptest VALUES(2, 'two', 'Two', '2001-02-20')");
$dbh->query("INSERT INTO phptest VALUES(3, 'three', 'Three', '2001-02-21')");
print "before commit: ";
dumptable();
$dbh->commit();
print "after commit: ";
dumptable();
$dbh->query("INSERT INTO phptest VALUES(4, 'four', 'Four', '2001-02-22')");
$dbh->query("INSERT INTO phptest VALUES(5, 'five', 'Five', '2001-02-23')");
print "before rollback: ";
dumptable();
$dbh->rollback();
print "after rollback: ";
dumptable();
$dbh->autoCommit(true);
$dbh->query("INSERT INTO phptest VALUES(6, 'six', 'Six', '2001-02-24')");
$dbh->query("INSERT INTO phptest VALUES(7, 'seven', 'Seven', '2001-02-25')");
print "before autocommit+rollback: ";
dumptable();
$dbh->rollback();
print "after autocommit+rollback: ";
dumptable();
print "testing that select doesn't disturbe opcount: ";
$dbh->autoCommit(false);
$dbh->simpleQuery("SELECT * FROM phptest");
$dbh->simpleQuery("SELECT a,c FROM phptest");
$dbh->simpleQuery("SELECT b,d FROM phptest");
if ($dbh->transaction_opcount == 0) {
print "ok\n";
} else {
print "failed (count=$dbh->transaction_opcount)\n";
}
?>
Index: php4/pear/DB/tests/pgsql/011.phpt
+++ php4/pear/DB/tests/pgsql/011.phpt
--TEST--
DB_pgsql transaction test
--SKIPIF--
<?php require "skipif.inc"; ?>
--FILE--
<?php
require "connect.inc";
require "mktable.inc";
require "../transactions.inc";
?>
--EXPECT--
after autocommit: bing one ops=0
before commit: bing one two three ops=2
after commit: bing one two three ops=0
before rollback: bing one two three four five ops=2
after rollback: bing one two three ops=0
before autocommit+rollback: bing one two three six seven ops=0
after autocommit+rollback: bing one two three six seven ops=0
testing that select doesn't disturbe opcount: ok
--
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]