]> git.wh0rd.org - tt-rss.git/blob - classes/db/pgsql.php
7bacfef60df28046e617d5ba85be46e7dcfd30dd
[tt-rss.git] / classes / db / pgsql.php
1 <?php
2 class Db_Pgsql implements IDb {
3 private $link;
4 private $last_error;
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
27 $this->init();
28
29 return $this->link;
30 }
31
32 function escape_string($s, $strip_tags = true) {
33 if ($strip_tags) $s = strip_tags($s);
34
35 return pg_escape_string($s);
36 }
37
38 function query($query, $die_on_error = true) {
39 $result = @pg_query($this->link, $query);
40
41 if (!$result) {
42 $this->last_error = @pg_last_error($this->link);
43
44 @pg_query($this->link, "ROLLBACK");
45 $query = htmlspecialchars($query); // just in case
46 user_error("Query $query failed: " . ($this->link ? $this->last_error : "No connection"),
47 $die_on_error ? E_USER_ERROR : E_USER_WARNING);
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
77 function last_query_error() {
78 return $this->last_error;
79 }
80
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");
86 $this->query("set cpu_tuple_cost = 0.5");
87
88 return true;
89 }
90 }
91 ?>