]>
Commit | Line | Data |
---|---|---|
95947917 AD |
1 | <?php |
2 | class Db_Pgsql implements IDb { | |
3 | private $link; | |
977cea14 | 4 | private $last_error; |
95947917 AD |
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) { | |
3c111597 AD |
24 | print("Unable to connect to database (as $user to $host, database $db):" . pg_last_error()); |
25 | exit(102); | |
95947917 AD |
26 | } |
27 | ||
404e2e36 AD |
28 | $this->init(); |
29 | ||
95947917 AD |
30 | return $this->link; |
31 | } | |
32 | ||
33 | function escape_string($s, $strip_tags = true) { | |
34 | if ($strip_tags) $s = strip_tags($s); | |
35 | ||
a42c55f0 | 36 | return pg_escape_string($s); |
95947917 AD |
37 | } |
38 | ||
39 | function query($query, $die_on_error = true) { | |
38f43970 | 40 | $result = @pg_query($this->link, $query); |
95947917 AD |
41 | |
42 | if (!$result) { | |
977cea14 | 43 | $this->last_error = @pg_last_error($this->link); |
38f43970 AD |
44 | |
45 | @pg_query($this->link, "ROLLBACK"); | |
95947917 | 46 | $query = htmlspecialchars($query); // just in case |
977cea14 | 47 | user_error("Query $query failed: " . ($this->link ? $this->last_error : "No connection"), |
aca75cb5 | 48 | $die_on_error ? E_USER_ERROR : E_USER_WARNING); |
95947917 AD |
49 | } |
50 | return $result; | |
51 | } | |
52 | ||
53 | function fetch_assoc($result) { | |
54 | return pg_fetch_assoc($result); | |
55 | } | |
56 | ||
57 | ||
58 | function num_rows($result) { | |
59 | return pg_num_rows($result); | |
60 | } | |
61 | ||
62 | function fetch_result($result, $row, $param) { | |
63 | return pg_fetch_result($result, $row, $param); | |
64 | } | |
65 | ||
66 | function close() { | |
67 | return pg_close($this->link); | |
68 | } | |
69 | ||
70 | function affected_rows($result) { | |
71 | return pg_affected_rows($result); | |
72 | } | |
73 | ||
74 | function last_error() { | |
75 | return pg_last_error($this->link); | |
76 | } | |
77 | ||
977cea14 AD |
78 | function last_query_error() { |
79 | return $this->last_error; | |
80 | } | |
81 | ||
ba68b681 AD |
82 | function init() { |
83 | $this->query("set client_encoding = 'UTF-8'"); | |
84 | pg_set_client_encoding("UNICODE"); | |
85 | $this->query("set datestyle = 'ISO, european'"); | |
86 | $this->query("set TIME ZONE 0"); | |
ad9928a5 | 87 | $this->query("set cpu_tuple_cost = 0.5"); |
ba68b681 AD |
88 | |
89 | return true; | |
90 | } | |
ea79a0e0 | 91 | } |