]> git.wh0rd.org - tt-rss.git/blame - classes/db/pgsql.php
logger: record last query before logged error
[tt-rss.git] / classes / db / pgsql.php
CommitLineData
95947917
AD
1<?php
2class Db_Pgsql implements IDb {
3 private $link;
4
5 function connect($host, $user, $pass, $db, $port) {
6 $string = "dbname=$db user=$user";
7
8 if ($pass) {
9 $string .= " password=$pass";
10 }
11
12 if ($host) {
13 $string .= " host=$host";
14 }
15
16 if (is_numeric($port) && $port > 0) {
17 $string = "$string port=" . $port;
18 }
19
20 $this->link = pg_connect($string);
21
22 if (!$this->link) {
23 die("Unable to connect to database (as $user to $host, database $db):" . pg_last_error());
24 }
25
404e2e36
AD
26 $this->init();
27
95947917
AD
28 return $this->link;
29 }
30
31 function escape_string($s, $strip_tags = true) {
32 if ($strip_tags) $s = strip_tags($s);
33
a42c55f0 34 return pg_escape_string($s);
95947917
AD
35 }
36
37 function query($query, $die_on_error = true) {
475d7628
AD
38 global $last_query;
39
40 if (strpos($query, "ttrss_error_log") === FALSE) $last_query = $query;
41
38f43970 42 $result = @pg_query($this->link, $query);
95947917
AD
43
44 if (!$result) {
38f43970
AD
45 $error = @pg_last_error($this->link);
46
47 @pg_query($this->link, "ROLLBACK");
95947917 48 $query = htmlspecialchars($query); // just in case
38f43970 49 user_error("Query $query failed: " . ($this->link ? $error : "No connection"),
aca75cb5 50 $die_on_error ? E_USER_ERROR : E_USER_WARNING);
95947917
AD
51 }
52 return $result;
53 }
54
55 function fetch_assoc($result) {
56 return pg_fetch_assoc($result);
57 }
58
59
60 function num_rows($result) {
61 return pg_num_rows($result);
62 }
63
64 function fetch_result($result, $row, $param) {
65 return pg_fetch_result($result, $row, $param);
66 }
67
68 function close() {
69 return pg_close($this->link);
70 }
71
72 function affected_rows($result) {
73 return pg_affected_rows($result);
74 }
75
76 function last_error() {
77 return pg_last_error($this->link);
78 }
79
ba68b681
AD
80 function init() {
81 $this->query("set client_encoding = 'UTF-8'");
82 pg_set_client_encoding("UNICODE");
83 $this->query("set datestyle = 'ISO, european'");
84 $this->query("set TIME ZONE 0");
ad9928a5 85 $this->query("set cpu_tuple_cost = 0.5");
ba68b681
AD
86
87 return true;
88 }
95947917
AD
89}
90?>