From 549bccdd3ec6d9616dd6797ca4e36e2617285f12 Mon Sep 17 00:00:00 2001 From: daybreaker Date: Sun, 20 Jul 2008 12:39:59 +0000 Subject: [PATCH] =?UTF-8?q?#1052:=20=EC=9E=91=EC=97=85=20=EC=A4=91...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/data/mysql/Adapter.php | 50 +++++++++++------- framework/data/postgresql/Adapter.php | 75 +++++++++++++++++++++++++++ 2 files changed, 105 insertions(+), 20 deletions(-) create mode 100644 framework/data/postgresql/Adapter.php diff --git a/framework/data/mysql/Adapter.php b/framework/data/mysql/Adapter.php index 4e4f7a1a9..be30a3522 100644 --- a/framework/data/mysql/Adapter.php +++ b/framework/data/mysql/Adapter.php @@ -3,9 +3,7 @@ /// All rights reserved. Licensed under the GPL. /// See the GNU General Public License for more details. (/doc/LICENSE, /doc/COPYRIGHT) -// TODO: process errors and exceptions - -class Adapter implements IAdapter +class MySQLAdapter implements IAdapter { function __construct() { @@ -19,54 +17,66 @@ function __destruct() public function connect($server, $dbname, $userid, $password, array $options) { + if ($options != NULL) { + foreach ($options as $key => $value) { + switch ($key) { + default: + // Do something with options + break; + } + } + } $this->conn = mysql_connect($server, $userid, $password); - if ($this->conn === false) - throw new ConnectionError(); - @mysql_query("SET NAMES UTF8"); - @mysql_query("SET CHARACTER SET UTF8"); - mysql_query("USE $dbname"); + if ($this->conn === FALSE) { + $this->conn = NULL; + throw new ConnectionError(mysql_error()); + } + @mysql_query("SET NAMES UTF8", $this->conn); + @mysql_query("SET CHARACTER SET UTF8", $this->conn); + if (mysql_select_db($dbname, $this->conn) === FALSE) + throw new DBError("No such database: $dbname"); } public function disconnect() { - if ($this->conn) + if ($this->conn) { mysql_close($this->conn); + $this->conn = NULL; + } } public function beginTransaction() { - mysql_query("BEGIN"); + mysql_query("BEGIN", $this->conn); } - public function endTransaction($apply = true) + public function endTransaction($apply = TRUE) { if ($apply) - mysql_query("COMMIT"); + mysql_query("COMMIT", $this->conn); else - mysql_query("ROLLBACK"); + mysql_query("ROLLBACK", $this->conn); } public function query($query) { - return mysql_query($query); + return mysql_query($query, $this->conn); } public static function escapeString($var) { - if (is_array($var)) { + if (is_array($var)) return array_map(escapeString, $var); - } else { + else return mysql_escape_string($var); - } } public static function escapeFieldName($var) { - if (is_array($var)) { + if (is_array($var)) return array_map(escapeFieldName, $var); - } else { + else return '`'.$var.'`'; - } } } ?> diff --git a/framework/data/postgresql/Adapter.php b/framework/data/postgresql/Adapter.php new file mode 100644 index 000000000..09427cf8c --- /dev/null +++ b/framework/data/postgresql/Adapter.php @@ -0,0 +1,75 @@ +conn = NULL; + } + + function __destruct() + { + $this->disconnect(); + } + + public function connect($server, $dbname, $userid, $password, array $options) + { + $connection_str = "host=$server dbname=$dbname user=$userid password=$password"; + if ($options != NULL) { + foreach ($options as $key => $value) { + switch ($key) { + default: + // Do something with options + break; + } + } + } + $this->conn = pg_connect($connection_str); + if (pg_connection_status($this->conn) === PSQL_CONNECTION_OK) { + pg_set_client_encoding($this->conn, 'UTF8'); + } else { + $this->conn = NULL; + throw new ConnectionError(pg_last_error()); + } + } + + public function disconnect() + { + if ($this->conn) { + pg_close($this->conn); + $this->conn = NULL; + } + } + + public function beginTransaction() + { + pg_query($this->conn, "BEGIN"); + } + + public function endTransaction($apply = TRUE) + { + if ($apply) + pg_query($this->conn, "COMMIT"); + else + pg_query($this->conn, "ROLLBACK"); + } + + public static function escapeString($var) + { + if (is_array($var)) + return array_map(escapeString, $var); + else + return pg_escape_string($var); + } + + public static function escapeFieldName($var) + { + // TODO: how to do this in psql? + return $var; + } +} + +?>