From f3d3ee92f2c637868dba6eeaca61c0407f18580f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Lorenzo=20Rodr=C3=ADguez?= Date: Sun, 17 Oct 2010 23:36:34 -0430 Subject: [PATCH] Implementing basic connection to postgres using PDO --- .../model/datasources/dbo/dbo_postgres.php | 61 +++++++------------ 1 file changed, 21 insertions(+), 40 deletions(-) diff --git a/cake/libs/model/datasources/dbo/dbo_postgres.php b/cake/libs/model/datasources/dbo/dbo_postgres.php index 7b37f5412ba..4fa0749fa70 100644 --- a/cake/libs/model/datasources/dbo/dbo_postgres.php +++ b/cake/libs/model/datasources/dbo/dbo_postgres.php @@ -112,23 +112,30 @@ class DboPostgres extends DboSource { */ function connect() { $config = $this->config; - $conn = "host='{$config['host']}' port='{$config['port']}' dbname='{$config['database']}' "; - $conn .= "user='{$config['login']}' password='{$config['password']}'"; - - if (!$config['persistent']) { - $this->connection = pg_connect($conn, PGSQL_CONNECT_FORCE_NEW); - } else { - $this->connection = pg_pconnect($conn); - } $this->connected = false; + try { + $flags = array( + PDO::ATTR_PERSISTENT => $config['persistent'] + ); + if (!empty($config['encoding'])) { + $flags[PDO::MYSQL_ATTR_INIT_COMMAND] = 'SET search_path TO ' . $config['schema']; + } + $this->_connection = new PDO( + "pgsql:host={$config['host']};port={$config['port']};dbname={$config['database']}", + $config['login'], + $config['password'], + $flags + ); + + if (!empty($config['encoding'])) { + $this->setEncoding($config['encoding']); + } - if ($this->connection) { $this->connected = true; - $this->_execute("SET search_path TO " . $config['schema']); - } - if (!empty($config['encoding'])) { - $this->setEncoding($config['encoding']); + } catch (PDOException $e) { + $this->errors[] = $e->getMessage(); } + return $this->connected; } @@ -138,33 +145,7 @@ function connect() { * @return boolean */ function enabled() { - return extension_loaded('pgsql'); - } -/** - * Disconnects from database. - * - * @return boolean True if the database could be disconnected, else false - */ - function disconnect() { - if ($this->hasResult()) { - pg_free_result($this->_result); - } - if (is_resource($this->connection)) { - $this->connected = !pg_close($this->connection); - } else { - $this->connected = false; - } - return !$this->connected; - } - -/** - * Executes given SQL statement. - * - * @param string $sql SQL statement - * @return resource Result resource identifier - */ - function _execute($sql) { - return pg_query($this->connection, $sql); + return in_array('pgsql', PDO::getAvailableDrivers()); } /**