2013-04-17 11:08:36 +02:00
|
|
|
<?php
|
|
|
|
class Db_Pgsql implements IDb {
|
|
|
|
private $link;
|
|
|
|
|
|
|
|
function connect($host, $user, $pass, $db, $port) {
|
|
|
|
$string = "dbname=$db user=$user";
|
|
|
|
|
|
|
|
if ($pass) {
|
|
|
|
$string .= " password=$pass";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($host) {
|
|
|
|
$string .= " host=$host";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_numeric($port) && $port > 0) {
|
|
|
|
$string = "$string port=" . $port;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->link = pg_connect($string);
|
|
|
|
|
|
|
|
if (!$this->link) {
|
|
|
|
die("Unable to connect to database (as $user to $host, database $db):" . pg_last_error());
|
|
|
|
}
|
|
|
|
|
2013-04-17 13:36:34 +02:00
|
|
|
$this->init();
|
|
|
|
|
2013-04-17 11:08:36 +02:00
|
|
|
return $this->link;
|
|
|
|
}
|
|
|
|
|
|
|
|
function escape_string($s, $strip_tags = true) {
|
|
|
|
if ($strip_tags) $s = strip_tags($s);
|
|
|
|
|
2013-04-17 16:34:18 +02:00
|
|
|
return pg_escape_string($s);
|
2013-04-17 11:08:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function query($query, $die_on_error = true) {
|
2015-12-03 14:15:28 +01:00
|
|
|
global $last_query;
|
|
|
|
|
|
|
|
if (strpos($query, "ttrss_error_log") === FALSE) $last_query = $query;
|
|
|
|
|
2013-08-02 14:20:39 +02:00
|
|
|
$result = @pg_query($this->link, $query);
|
2013-04-17 11:08:36 +02:00
|
|
|
|
|
|
|
if (!$result) {
|
2013-08-02 14:20:39 +02:00
|
|
|
$error = @pg_last_error($this->link);
|
|
|
|
|
|
|
|
@pg_query($this->link, "ROLLBACK");
|
2013-04-17 11:08:36 +02:00
|
|
|
$query = htmlspecialchars($query); // just in case
|
2013-08-02 14:20:39 +02:00
|
|
|
user_error("Query $query failed: " . ($this->link ? $error : "No connection"),
|
2013-04-17 14:05:52 +02:00
|
|
|
$die_on_error ? E_USER_ERROR : E_USER_WARNING);
|
2013-04-17 11:08:36 +02:00
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetch_assoc($result) {
|
|
|
|
return pg_fetch_assoc($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function num_rows($result) {
|
|
|
|
return pg_num_rows($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetch_result($result, $row, $param) {
|
|
|
|
return pg_fetch_result($result, $row, $param);
|
|
|
|
}
|
|
|
|
|
|
|
|
function close() {
|
|
|
|
return pg_close($this->link);
|
|
|
|
}
|
|
|
|
|
|
|
|
function affected_rows($result) {
|
|
|
|
return pg_affected_rows($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function last_error() {
|
|
|
|
return pg_last_error($this->link);
|
|
|
|
}
|
|
|
|
|
2013-04-17 12:23:35 +02:00
|
|
|
function init() {
|
|
|
|
$this->query("set client_encoding = 'UTF-8'");
|
|
|
|
pg_set_client_encoding("UNICODE");
|
|
|
|
$this->query("set datestyle = 'ISO, european'");
|
|
|
|
$this->query("set TIME ZONE 0");
|
2015-08-11 19:28:04 +02:00
|
|
|
$this->query("set cpu_tuple_cost = 0.5");
|
2013-04-17 12:23:35 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-04-17 11:08:36 +02:00
|
|
|
}
|
|
|
|
?>
|