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