]> git.wh0rd.org - tt-rss.git/blob - classes/db/pgsql.php
db: use builtin classloader, lowercase class files
[tt-rss.git] / classes / db / pgsql.php
1 <?php
2
3 class Db_Pgsql extends Db_Abstract
4 {
5 public function connect($host, $user, $pass, $db)
6 {
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 (defined('DB_PORT')) {
18 $string = "$string port=" . DB_PORT;
19 }
20
21 $link = pg_connect($string);
22
23 if (!$link) {
24 die("Unable to connect to database (as $user to $host, database $db):" . pg_last_error());
25 }
26
27 $this->dbconn = $link;
28 return $link;
29 }
30
31 public function init()
32 {
33 pg_query($this->dbconn, "set client_encoding = 'UTF-8'");
34 pg_set_client_encoding("UNICODE");
35 pg_query($this->dbconn, "set datestyle = 'ISO, european'");
36 pg_query($this->dbconn, "set TIME ZONE 0");
37 }
38
39 public function escape_string($s, $strip_tags = true)
40 {
41 if ($strip_tags) $s = strip_tags($s);
42 return pg_escape_string($s);
43 }
44
45 public function query($query, $die_on_error = true)
46 {
47 $result = pg_query($this->dbconn, $query);
48 if (!$result) {
49 $query = htmlspecialchars($query); // just in case
50 if ($die_on_error) {
51 die("Query <i>$query</i> failed [$result]: " . ($this->dbconn ? pg_last_error($this->dbconn) : "No connection"));
52 }
53 }
54 return $result;
55 }
56
57 public function fetch_assoc($result) {
58 return pg_fetch_assoc($result);
59 }
60
61 public function num_rows($result) {
62 return pg_num_rows($result);
63 }
64
65 public function fetch_result($result, $row, $param) {
66 return pg_fetch_result($result, $row, $param);
67 }
68
69 public function close() {
70 return pg_close($this->dbconn);
71 }
72
73 public function affected_rows($result) {
74 return pg_affected_rows($result);
75 }
76
77 public function last_error() {
78 return pg_last_error($this->dbconn);
79 }
80 }