Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implementing basic connection to postgres using PDO
  • Loading branch information
lorenzo committed Oct 18, 2010
1 parent 28685dc commit f3d3ee9
Showing 1 changed file with 21 additions and 40 deletions.
61 changes: 21 additions & 40 deletions cake/libs/model/datasources/dbo/dbo_postgres.php
Expand Up @@ -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 comment has been minimized.

Copy link
@tarcisio

tarcisio Oct 18, 2010

Contributor
if (!empty($config['schema']))

Maybe?

}
$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;
}

Expand All @@ -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());
}

/**
Expand Down

0 comments on commit f3d3ee9

Please sign in to comment.