Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

1. Debugging messages improvements on connection error.

2. Use PGSQL_CONNECT_FORCE_NEW in Postgresql driver.
3. Minor: do not save errorous cached plans in Postgresql driver.
  • Loading branch information...
commit b938a81ced1da17cbb5fb60de51b7945a2c61ef3 1 parent 71e2b43
@dmitrymin dmitrymin authored
View
10 lib/DbSimple/Mysql.php
@@ -37,13 +37,13 @@ function DbSimple_Mysql($dsn)
return $this->_setLastError("-1", "MySQL extension is not loaded", "mysql_connect");
}
$ok = $this->link = @mysql_connect(
- $p['host'] . (empty($p['port'])? "" : ":".$p['port']),
+ $str = $p['host'] . (empty($p['port'])? "" : ":" . $p['port']),
$p['user'],
$p['pass'],
true
);
$this->_resetLastError();
- if (!$ok) return $this->_setDbError('mysql_connect()');
+ if (!$ok) return $this->_setDbError('mysql_connect("' . $str . '", "' . $p['user'] . '")');
$ok = @mysql_select_db(preg_replace('{^/}s', '', $p['path']), $this->link);
if (!$ok) return $this->_setDbError('mysql_select_db()');
if (isset($p["charset"])) {
@@ -181,7 +181,11 @@ function _performFetch($result)
function _setDbError($query)
{
- return $this->_setLastError(mysql_errno($this->link), mysql_error($this->link), $query);
+ if ($this->link) {
+ return $this->_setLastError(mysql_errno($this->link), mysql_error($this->link), $query);
+ } else {
+ return $this->_setLastError(mysql_errno(), mysql_error(), $query);
+ }
}
View
25 lib/DbSimple/Postgresql.php
@@ -43,15 +43,24 @@ function DbSimple_Postgresql($dsn)
// Prepare+execute works only in PHP 5.1+.
$this->DbSimple_Postgresql_USE_NATIVE_PHOLDERS = function_exists('pg_prepare');
+ $dsnWithoutPass =
+ (!empty($p['host']) ? 'host='.$p['host'].' ' : '') .
+ (!empty($p['port']) ? 'port=' . $p['port'] . ' ' : '') .
+ 'dbname=' . preg_replace('{^/}s', '', $p['path']) .' '.
+ (!empty($p['user']) ? 'user='. $p['user'] : '');
$ok = $this->link = @pg_connect(
- $t = (!empty($p['host']) ? 'host='.$p['host'].' ' : '').
- (!empty($p['port']) ? 'port='.$p['port'].' ' : '').
- 'dbname='.preg_replace('{^/}s', '', $p['path']).' '.
- (!empty($p['user']) ? 'user='.$p['user'].' ' : '').
- (!empty($p['pass']) ? 'password='.$p['pass'].' ' : '')
+ $dsnWithoutPass . " " . (!empty($p['pass']) ? 'password=' . $p['pass'] . ' ' : ''),
+ PGSQL_CONNECT_FORCE_NEW
);
+ // We use PGSQL_CONNECT_FORCE_NEW, because in PHP 5.3 & PHPUnit
+ // $this->prepareCache may be cleaned, but $this->link is still
+ // not closed. So the next creation of DbSimple_Postgresql()
+ // would use exactly the same connection as the previous, but with
+ // empty $this->prepareCache, and it will generate "prepared statement
+ // xxx already exists" error each time we execute the same statement
+ // as in the previous calls.
$this->_resetLastError();
- if (!$ok) return $this->_setDbError('pg_connect()');
+ if (!$ok) return $this->_setDbError('pg_connect("' . $dsnWithoutPass . '") error');
}
@@ -170,15 +179,15 @@ function _performQuery($queryMain)
$this->_expandPlaceholders($queryMain, true);
$hash = md5($queryMain[0]);
if (!isset($this->prepareCache[$hash])) {
- $this->prepareCache[$hash] = true;
$prepared = @pg_prepare($this->link, $hash, $queryMain[0]);
if ($prepared === false) return $this->_setDbError($queryMain[0]);
+ else $this->prepareCache[$hash] = true;
} else {
// Prepare cache hit!
}
$result = pg_execute($this->link, $hash, array_slice($queryMain, 1));
} else {
- // No support for native placeholders or INSERT query.
+ // No support for native placeholders on INSERT query.
$this->_expandPlaceholders($queryMain, false);
$result = @pg_query($this->link, $queryMain[0]);
}
View
2  t/DbSimple/Mysql/dsn.txt
@@ -1 +1 @@
-mysql://test:test@localhost/test?charset=utf8
+mysql://test:test@127.0.0.1/test?charset=utf8
View
22 t/DbSimple/Postgresql/005_connect_error.phpt
@@ -0,0 +1,22 @@
+--TEST--
+Postgresql: connection error catching
+
+--FILE--
+<?php
+$DSN[] = "postgresql://user:pass@127.0.0.1:55555/test";
+require_once dirname(__FILE__) . '/../init.php';
+
+function main(&$DB)
+{
+}
+?>
+
+
+--SKIPIF--
+<?php
+if (!is_callable('pg_connect')) print('skip pgsql extension not loaded');
+?>
+
+
+--EXPECT--
+Error: 'pg_connect("host=127.0.0.1 port=55555 dbname=test user=user") error'
View
25 t/DbSimple/Postgresql/060_double_connections.phpt
@@ -0,0 +1,25 @@
+--TEST--
+PostgreSQL: connect twice even with same DSNs
+
+--FILE--
+<?php
+require_once dirname(__FILE__) . '/../init.php';
+
+function main(&$DB)
+{
+ global $DSN;
+ $db2 = DbSimple_Generic::connect($DSN[0]);
+ if ($DB->link != $db2->link) echo "OK\n";
+ else echo "Same link is established for same DSNs: {$DB->link}\n";
+}
+?>
+
+
+--SKIPIF--
+<?php
+if (!is_callable('pg_connect')) print('skip pgsql extension not loaded');
+?>
+
+
+--EXPECT--
+OK
View
40 t/DbSimple/Postgresql/070_no_save_error_prepare.phpt
@@ -0,0 +1,40 @@
+--TEST--
+PostgreSQL: do not save errorous prepared statements
+
+--FILE--
+<?php
+require_once dirname(__FILE__) . '/../init.php';
+
+$LAST_ERROR = null;
+function errorSaver($msg, $error)
+{
+ global $LAST_ERROR;
+ $LAST_ERROR = $error;
+}
+
+function main(&$DB)
+{
+ global $LAST_ERROR;
+ $query = "oshibka";
+ $DB->setErrorHandler("errorSaver");
+ $DB->query($query);
+ $msg1 = $LAST_ERROR["message"];
+ $DB->query($query);
+ $msg2 = $LAST_ERROR["message"];
+ if (false !== strpos($msg1, $query) && false !== strpos($msg2, $query)) echo "OK\n";
+ else echo "Different errors are responded from the same errorous queries:\n$msg1\n$msg2\n";
+}
+?>
+
+
+--SKIPIF--
+<?php
+if (!is_callable('pg_connect')) print('skip pgsql extension not loaded');
+?>
+
+
+--EXPECT--
+Query: 'oshibka'
+Query: 'oshibka'
+OK
+
View
33 t/DbSimple/init.php
@@ -7,23 +7,24 @@
ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.dirname(__FILE__).'/..'); // for Cache_Lite
include_once "DbSimple/Generic.php";
-$DSN = array();
-
-$dsnFile = "dsn.txt";
-$dsnOwn = trim(@join("", file($dsnFile)));
-if (!$dsnOwn) die("Current directory must contain $dsnFile file!");
-if ($dsnOwn == '*' || preg_match('/^\w+$/', $dsnOwn)) {
- $dir = dirname(__FILE__);
- $d = opendir($dir);
- while (false !== ($e = readdir($d))) {
- $full = realpath("$dir/$e");
- if ($e == "." || $e == ".." || !is_dir($full) || $full == realpath(getcwd())) continue;
- if ($dsnOwn != '*' && strtolower($e) != strtolower($dsnOwn)) continue;
- $dsn = trim(@join("", file("$full/$dsnFile")));
- if ($dsn) $DSN = array_merge($DSN, preg_split('/\s+/s', $dsn));
+if (!is_array(@$DSN)) {
+ $DSN = array();
+ $dsnFile = "dsn.txt";
+ $dsnOwn = trim(@join("", file($dsnFile)));
+ if (!$dsnOwn) die("Current directory must contain $dsnFile file!");
+ if ($dsnOwn == '*' || preg_match('/^\w+$/', $dsnOwn)) {
+ $dir = dirname(__FILE__);
+ $d = opendir($dir);
+ while (false !== ($e = readdir($d))) {
+ $full = realpath("$dir/$e");
+ if ($e == "." || $e == ".." || !is_dir($full) || $full == realpath(getcwd())) continue;
+ if ($dsnOwn != '*' && strtolower($e) != strtolower($dsnOwn)) continue;
+ $dsn = trim(@join("", file("$full/$dsnFile")));
+ if ($dsn) $DSN = array_merge($DSN, preg_split('/\s+/s', $dsn));
+ }
+ } else {
+ $DSN[] = $dsnOwn;
}
-} else {
- $DSN[] = $dsnOwn;
}
foreach ($DSN as $dsn) {
Please sign in to comment.
Something went wrong with that request. Please try again.