From 3c330dde766c82eed2c09bf61272e7922e74b366 Mon Sep 17 00:00:00 2001 From: dk Date: Mon, 12 Apr 2010 20:30:14 +0000 Subject: [PATCH] 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 --- lib/DbSimple/Mysql.php | 3 ++ lib/DbSimple/Postgresql.php | 4 +-- t/DbSimple/Mysql/020_charset.phpt | 28 ++++++++++++++++++ t/DbSimple/Mysql/dsn.txt | 2 +- t/DbSimple/Postgresql/010_native_pholder.phpt | 2 +- .../Postgresql/020_cell_placeholder.phpt | 5 ++-- t/DbSimple/Postgresql/050_escaping.phpt | 29 +++++++++++++++++++ t/DbSimple/Postgresql/dsn.txt | 2 +- 8 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 t/DbSimple/Mysql/020_charset.phpt create mode 100644 t/DbSimple/Postgresql/050_escaping.phpt diff --git a/lib/DbSimple/Mysql.php b/lib/DbSimple/Mysql.php index 8a09b63..38c2553 100644 --- a/lib/DbSimple/Mysql.php +++ b/lib/DbSimple/Mysql.php @@ -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"]); + } } diff --git a/lib/DbSimple/Postgresql.php b/lib/DbSimple/Postgresql.php index f08591a..7f5f87e 100644 --- a/lib/DbSimple/Postgresql.php +++ b/lib/DbSimple/Postgresql.php @@ -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() diff --git a/t/DbSimple/Mysql/020_charset.phpt b/t/DbSimple/Mysql/020_charset.phpt new file mode 100644 index 0000000..f7ff034 --- /dev/null +++ b/t/DbSimple/Mysql/020_charset.phpt @@ -0,0 +1,28 @@ +--TEST-- +Mysql: ?# placeholder usage +--FILE-- + 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', + ), +) + diff --git a/t/DbSimple/Mysql/dsn.txt b/t/DbSimple/Mysql/dsn.txt index 1fc986a..11efa0b 100644 --- a/t/DbSimple/Mysql/dsn.txt +++ b/t/DbSimple/Mysql/dsn.txt @@ -1 +1 @@ -mysql://test:test@localhost/test +mysql://test:test@localhost/test?charset=utf8 diff --git a/t/DbSimple/Postgresql/010_native_pholder.phpt b/t/DbSimple/Postgresql/010_native_pholder.phpt index 06d7066..085fa2b 100644 --- a/t/DbSimple/Postgresql/010_native_pholder.phpt +++ b/t/DbSimple/Postgresql/010_native_pholder.phpt @@ -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\')', ) diff --git a/t/DbSimple/Postgresql/020_cell_placeholder.phpt b/t/DbSimple/Postgresql/020_cell_placeholder.phpt index 6584e06..fcb6c51 100644 --- a/t/DbSimple/Postgresql/020_cell_placeholder.phpt +++ b/t/DbSimple/Postgresql/020_cell_placeholder.phpt @@ -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', -) \ No newline at end of file +) + diff --git a/t/DbSimple/Postgresql/050_escaping.phpt b/t/DbSimple/Postgresql/050_escaping.phpt new file mode 100644 index 0000000..46936cd --- /dev/null +++ b/t/DbSimple/Postgresql/050_escaping.phpt @@ -0,0 +1,29 @@ +--TEST-- +PostgreSQL: excaping test + +--FILE-- +DbSimple_Postgresql_USE_NATIVE_PHOLDERS = false; + printr($DB->query("select ? as a", "aaa\\"), "Result"); +} +?> + + +--SKIPIF-- + + + +--EXPECT-- +Query: 'select E\'aaa\\\\\' as a' +Result: array ( + 0 => + array ( + 'a' => 'aaa\\', + ), +) diff --git a/t/DbSimple/Postgresql/dsn.txt b/t/DbSimple/Postgresql/dsn.txt index 3ce9f4a..dc0ea40 100644 --- a/t/DbSimple/Postgresql/dsn.txt +++ b/t/DbSimple/Postgresql/dsn.txt @@ -1 +1 @@ -postgresql://test:test@localhost/test +postgresql://test:test@127.0.0.1/test