Permalink
Browse files

1. Allow to specify ?charset=xxx in MySQL DSN.

2. When you assign $this->DbSimple_Postgresql_USE_NATIVE_PHOLDERS = false manually for PostgreSQL, apostrophs were quoted incorrectly.


git-svn-id: svn://dklab.ru/lib/DbSimple/trunk@332 78bb956b-1e24-0410-b8d0-c528fdc9eae3
  • Loading branch information...
1 parent 88f089d commit 3c330dde766c82eed2c09bf61272e7922e74b366 dk committed Apr 12, 2010
View
@@ -46,6 +46,9 @@ function DbSimple_Mysql($dsn)
if (!$ok) return $this->_setDbError('mysql_connect()');
$ok = @mysql_select_db(preg_replace('{^/}s', '', $p['path']), $this->link);
if (!$ok) return $this->_setDbError('mysql_select_db()');
+ if (isset($p["charset"])) {
+ $this->query('SET NAMES ?', $p["charset"]);
+ }
}
@@ -58,7 +58,7 @@ function DbSimple_Postgresql($dsn)
function _performEscape($s, $isIdent=false)
{
if (!$isIdent)
- return "'" . str_replace("'", "''", $s) . "'";
+ return "E'" . pg_escape_string($this->link, $s) . "'";
else
return '"' . str_replace('"', '_', $s) . '"';
}
@@ -218,7 +218,7 @@ function _performFetch($result)
function _setDbError($query)
{
- return $this->_setLastError(null, pg_last_error($this->link), $query);
+ return $this->_setLastError(null, $this->link? pg_last_error($this->link) : (is_array($query)? "Connection is not established" : $query), $query);
}
function _getVersion()
@@ -0,0 +1,28 @@
+--TEST--
+Mysql: ?# placeholder usage
+--FILE--
+<?php
+require_once dirname(__FILE__) . '/../init.php';
+
+function main(&$DB)
+{
+ $row = array(
+ 'id' => 1,
+ 'str' => 'test'
+ );
+
+ printr($DB->select("SHOW VARIABLES LIKE 'character_set_client'"));
+}
+?>
+
+
+--EXPECT--
+Query: 'SHOW VARIABLES LIKE \'character_set_client\''
+array (
+ 0 =>
+ array (
+ 'Variable_name' => 'character_set_client',
+ 'Value' => 'utf8',
+ ),
+)
+
View
@@ -1 +1 @@
-mysql://test:test@localhost/test
+mysql://test:test@localhost/test?charset=utf8
@@ -32,6 +32,6 @@ With native placeholders: array (
3 => 'a',
)
Without native placeholders: array (
- 0 => 'INSERT INTO test(id, pid, str) VALUES(\'10\', \'101\', \'a\')',
+ 0 => 'INSERT INTO test(id, pid, str) VALUES(E\'10\', E\'101\', E\'a\')',
)
@@ -29,8 +29,9 @@ if (!is_callable('pg_connect')) print('skip pgsql extension not loaded');
--EXPECT--
Query: 'DROP TABLE test'
Query: 'CREATE TABLE test(id INTEGER, str VARCHAR(10))'
-Query: 'INSERT INTO test("id", "str") VALUES(\'1\', \'test\')'
+Query: 'INSERT INTO test("id", "str") VALUES(E\'1\', E\'test\')'
Query: 'SELECT "id" FROM test'
array (
0 => '1',
-)
+)
+
@@ -0,0 +1,29 @@
+--TEST--
+PostgreSQL: excaping test
+
+--FILE--
+<?php
+require_once dirname(__FILE__) . '/../init.php';
+
+function main(&$DB)
+{
+ $DB->DbSimple_Postgresql_USE_NATIVE_PHOLDERS = false;
+ printr($DB->query("select ? as a", "aaa\\"), "Result");
+}
+?>
+
+
+--SKIPIF--
+<?php
+if (!is_callable('pg_connect')) print('skip pgsql extension not loaded');
+?>
+
+
+--EXPECT--
+Query: 'select E\'aaa\\\\\' as a'
+Result: array (
+ 0 =>
+ array (
+ 'a' => 'aaa\\',
+ ),
+)
@@ -1 +1 @@
-postgresql://test:test@localhost/test
+postgresql://test:test@127.0.0.1/test

0 comments on commit 3c330dd

Please sign in to comment.