]>
Commit | Line | Data |
---|---|---|
95947917 AD |
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 | ||
404e2e36 AD |
26 | $this->init(); |
27 | ||
95947917 AD |
28 | return $this->link; |
29 | } | |
30 | ||
31 | function escape_string($s, $strip_tags = true) { | |
32 | if ($strip_tags) $s = strip_tags($s); | |
33 | ||
a42c55f0 | 34 | return pg_escape_string($s); |
95947917 AD |
35 | } |
36 | ||
37 | function query($query, $die_on_error = true) { | |
38f43970 | 38 | $result = @pg_query($this->link, $query); |
95947917 AD |
39 | |
40 | if (!$result) { | |
38f43970 AD |
41 | $error = @pg_last_error($this->link); |
42 | ||
43 | @pg_query($this->link, "ROLLBACK"); | |
95947917 | 44 | $query = htmlspecialchars($query); // just in case |
38f43970 | 45 | user_error("Query $query failed: " . ($this->link ? $error : "No connection"), |
aca75cb5 | 46 | $die_on_error ? E_USER_ERROR : E_USER_WARNING); |
95947917 AD |
47 | } |
48 | return $result; | |
49 | } | |
50 | ||
51 | function fetch_assoc($result) { | |
52 | return pg_fetch_assoc($result); | |
53 | } | |
54 | ||
55 | ||
56 | function num_rows($result) { | |
57 | return pg_num_rows($result); | |
58 | } | |
59 | ||
60 | function fetch_result($result, $row, $param) { | |
61 | return pg_fetch_result($result, $row, $param); | |
62 | } | |
63 | ||
64 | function close() { | |
65 | return pg_close($this->link); | |
66 | } | |
67 | ||
68 | function affected_rows($result) { | |
69 | return pg_affected_rows($result); | |
70 | } | |
71 | ||
72 | function last_error() { | |
73 | return pg_last_error($this->link); | |
74 | } | |
75 | ||
ba68b681 AD |
76 | function init() { |
77 | $this->query("set client_encoding = 'UTF-8'"); | |
78 | pg_set_client_encoding("UNICODE"); | |
79 | $this->query("set datestyle = 'ISO, european'"); | |
80 | $this->query("set TIME ZONE 0"); | |
81 | ||
82 | return true; | |
83 | } | |
95947917 AD |
84 | } |
85 | ?> |