cvs: php4 /pear/DB mysql.php odbc.php pgsql.php
| From: | Stig Bakken | Date: | Fri, 15 Dec 2000 02:37:10 +0000 |
| Subject: | cvs: php4 /pear/DB mysql.php odbc.php pgsql.php | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-3173@lists.php.net to get a copy of this message | ||
ssb Thu Dec 14 18:37:10 2000 EDT
Modified files:
/php4/pear/DB mysql.php odbc.php pgsql.php
Log:
* implemented error mapping for postgres (very early version) and tests
for mysql and pgsql
# NB! suddenly two tests are broken for some reason: DB/tests/db_error2.phpt
# and tests/pear_error.phpt. The symptom is that objects created for
# testing method error handlers are empty (no methods, no properties).
# No time to investigate them right now.
Index: php4/pear/DB/mysql.php
diff -u php4/pear/DB/mysql.php:1.41 php4/pear/DB/mysql.php:1.42
--- php4/pear/DB/mysql.php:1.41 Wed Dec 13 06:41:04 2000
+++ php4/pear/DB/mysql.php Thu Dec 14 18:37:09 2000
@@ -66,13 +66,14 @@
1007 => DB_ERROR_ALREADY_EXISTS,
1008 => DB_ERROR_CANNOT_DROP,
1046 => DB_ERROR_NODBSELECTED,
- 1146 => DB_ERROR_NOSUCHTABLE,
- 1064 => DB_ERROR_SYNTAX,
+ 1050 => DB_ERROR_ALREADY_EXISTS,
+ 1051 => DB_ERROR_NOSUCHTABLE,
1054 => DB_ERROR_NOSUCHFIELD,
1062 => DB_ERROR_ALREADY_EXISTS,
- 1051 => DB_ERROR_NOSUCHTABLE,
+ 1064 => DB_ERROR_SYNTAX,
1100 => DB_ERROR_NOT_LOCKED,
1136 => DB_ERROR_VALUE_COUNT_ON_ROW,
+ 1146 => DB_ERROR_NOSUCHTABLE,
);
}
@@ -382,7 +383,7 @@
$sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name);
$repeat = 0;
do {
- $result = $this->query("INSERT INTO db_seq_${sqn} VALUES(NULL)");
+ $result = $this->query("INSERT INTO ${sqn}_seq VALUES(NULL)");
if ($ondemand && DB::isError($result) &&
$result->getCode() == DB_ERROR_NOSUCHTABLE) {
$repeat = 1;
@@ -406,7 +407,7 @@
function createSequence($seq_name)
{
$sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name);
- return $this->query("CREATE TABLE db_seq_${sqn} ".
+ return $this->query("CREATE TABLE ${sqn}_seq ".
"(id INTEGER AUTO_INCREMENT, PRIMARY KEY(id))");
}
@@ -416,7 +417,7 @@
function dropSequence($seq_name)
{
$sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name);
- return $this->query("DROP TABLE db_seq_${sqn}");
+ return $this->query("DROP TABLE ${sqn}_seq");
}
// }}}
Index: php4/pear/DB/odbc.php
diff -u php4/pear/DB/odbc.php:1.21 php4/pear/DB/odbc.php:1.22
--- php4/pear/DB/odbc.php:1.21 Sat Dec 9 19:18:56 2000
+++ php4/pear/DB/odbc.php Thu Dec 14 18:37:09 2000
@@ -44,12 +44,9 @@
{
$this->DB_common();
$this->phptype = 'odbc';
- $this->dbsyntax = 'unknown';
- // Let's be very pessimistic about the features of an odbc
- // backend. The user has to specify the dbsyntax to access
- // other features.
+ $this->dbsyntax = 'sql92';
$this->features = array(
- 'prepare' => false,
+ 'prepare' => true,
'pconnect' => true,
'transactions' => false
);
Index: php4/pear/DB/pgsql.php
diff -u php4/pear/DB/pgsql.php:1.24 php4/pear/DB/pgsql.php:1.25
--- php4/pear/DB/pgsql.php:1.24 Wed Dec 13 06:41:04 2000
+++ php4/pear/DB/pgsql.php Thu Dec 14 18:37:09 2000
@@ -56,7 +56,8 @@
'pconnect' => true,
'transactions' => true
);
- $this->errorcode_map = array();
+ $this->errorcode_map = array(
+ );
$this->numrows = array();
$this->row = array();
$this->affected = 0;
@@ -183,6 +184,45 @@
}
// }}}
+ // {{{ errorCode()
+
+ /**
+ * Map native error codes to DB's portable ones. Requires that
+ * the DB implementation's constructor fills in the $errorcode_map
+ * property.
+ *
+ * @param $nativecode the native error code, as returned by the backend
+ * database extension (string or integer)
+ *
+ * @return int a portable DB error code, or FALSE if this DB
+ * implementation has no mapping for the given error code.
+ */
+
+ function errorCode($errormsg)
+ {
+ static $error_regexps;
+ if (empty($error_regexps)) {
+ $error_regexps = array(
+ '/(Table does not exist\.|Relation \'.*\' does not exist|sequence
does not exist)$/' => DB_ERROR_NOSUCHTABLE,
+ '/Relation \'.*\' already exists/' =>
DB_ERROR_ALREADY_EXISTS,
+ '/divide by zero$/' => DB_ERROR_DIVZERO,
+ '/pg_atoi: error in .*: can\'t parse /' =>
DB_ERROR_INVALID_NUMBER,
+ '/attribute \'.*\' not found$/' => DB_ERROR_NOSUCHFIELD,
+ '/parser: parse error at or near \"/' => DB_ERROR_SYNTAX
+ );
+ }
+ foreach ($error_regexps as $regexp => $code) {
+ if (preg_match($regexp, $errormsg)) {
+ return $code;
+ }
+ }
+ //php_error(E_WARNING, get_class($this)."::errorCode: no mapping for
$nativecode");
+ // Fall back to DB_ERROR if there was no mapping.
+ return DB_ERROR;
+ //return $errormsg;
+ }
+
+ // }}}
// {{{ fetchRow()
/**
@@ -323,14 +363,6 @@
}
// }}}
- // {{{ errorCode()
-
- function errorCode($msg)
- {
- return DB_ERROR;
- }
-
- // }}}
// {{{ errorNative()
/**
@@ -472,9 +504,9 @@
$sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name);
$repeat = 0;
do {
- $result = $this->query("SELECT NEXTVAL('db_seq_${sqn}')");
+ $result = $this->query("SELECT NEXTVAL('${sqn}_seq')");
if ($ondemand && DB::isError($result) &&
- $result->getMessage() == "DB Error: ERROR: db_seq_test.nextval: sequence
does not exist\n") {
+ $result->getCode() == DB_ERROR_NOSUCHTABLE) {
$repeat = 1;
$result = $this->createSequence($seq_name);
if (DB::isError($result)) {
@@ -505,7 +537,7 @@
function createSequence($seq_name)
{
$sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name);
- return $this->query("CREATE SEQUENCE db_seq_${sqn}");
+ return $this->query("CREATE SEQUENCE ${sqn}_seq");
}
// }}}
@@ -521,7 +553,7 @@
function dropSequence($seq_name)
{
$sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name);
- return $this->query("DROP SEQUENCE db_seq_${sqn}");
+ return $this->query("DROP SEQUENCE ${sqn}_seq");
}
// }}}
@@ -529,8 +561,8 @@
function pgsqlRaiseError($errno = null)
{
- if ($errno === null && pg_errormessage($this->connection)) {
- return $this->raiseError(pg_errormessage($this->connection));
+ if ($errno === null) {
+ return
$this->raiseError($this->errorCode(pg_errormessage($this->connection)));
}
return $this->raiseError($this->errorCode($errno));
}