]> git.wh0rd.org - tt-rss.git/blob - classes/db/pgsql.php
more work on singleton-based DB
[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 $this->init();
27
28 return $this->link;
29 }
30
31 function escape_string($s, $strip_tags = true) {
32 if ($strip_tags) $s = strip_tags($s);
33
34 return pg_escape_string($this->link, $s);
35 }
36
37 function query($query, $die_on_error = true) {
38 $result = pg_query($this->link, $query);
39
40 if (!$result) {
41 $query = htmlspecialchars($query); // just in case
42 if ($die_on_error) {
43 die("Query <i>$query</i> failed [$result]: " . ($this->link ? pg_last_error($this->link) : "No connection"));
44 }
45 }
46 return $result;
47 }
48
49 function fetch_assoc($result) {
50 return pg_fetch_assoc($result);
51 }
52
53
54 function num_rows($result) {
55 return pg_num_rows($result);
56 }
57
58 function fetch_result($result, $row, $param) {
59 return pg_fetch_result($result, $row, $param);
60 }
61
62 function close() {
63 return pg_close($this->link);
64 }
65
66 function affected_rows($result) {
67 return pg_affected_rows($result);
68 }
69
70 function last_error() {
71 return pg_last_error($this->link);
72 }
73
74 function init() {
75 $this->query("set client_encoding = 'UTF-8'");
76 pg_set_client_encoding("UNICODE");
77 $this->query("set datestyle = 'ISO, european'");
78 $this->query("set TIME ZONE 0");
79
80 return true;
81 }
82 }
83 ?>