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