]> git.wh0rd.org - tt-rss.git/blob - classes/db/pgsql.php
experimental singleton-based Db connection
[tt-rss.git] / classes / db / pgsql.php
1 <?php
2 class 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
26 return $this->link;
27 }
28
29 function escape_string($s, $strip_tags = true) {
30 if ($strip_tags) $s = strip_tags($s);
31
32 return pg_escape_string($this->link, $s);
33 }
34
35 function query($query, $die_on_error = true) {
36 $result = pg_query($this->link, $query);
37
38 if (!$result) {
39 $query = htmlspecialchars($query); // just in case
40 if ($die_on_error) {
41 die("Query <i>$query</i> failed [$result]: " . ($this->link ? pg_last_error($this->link) : "No connection"));
42 }
43 }
44 return $result;
45 }
46
47 function fetch_assoc($result) {
48 return pg_fetch_assoc($result);
49 }
50
51
52 function num_rows($result) {
53 return pg_num_rows($result);
54 }
55
56 function fetch_result($result, $row, $param) {
57 return pg_fetch_result($result, $row, $param);
58 }
59
60 function close() {
61 return pg_close($this->link);
62 }
63
64 function affected_rows($result) {
65 return pg_affected_rows($result);
66 }
67
68 function last_error() {
69 return pg_last_error($this->link);
70 }
71
72 }
73 ?>