]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | class Db_Pgsql implements IDb { | |
3 | private $link; | |
4 | private $last_error; | |
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) { | |
24 | print("Unable to connect to database (as $user to $host, database $db):" . pg_last_error()); | |
25 | exit(102); | |
26 | } | |
27 | ||
28 | $this->init(); | |
29 | ||
30 | return $this->link; | |
31 | } | |
32 | ||
33 | function escape_string($s, $strip_tags = true) { | |
34 | if ($strip_tags) $s = strip_tags($s); | |
35 | ||
36 | return pg_escape_string($s); | |
37 | } | |
38 | ||
39 | function query($query, $die_on_error = true) { | |
40 | $result = @pg_query($this->link, $query); | |
41 | ||
42 | if (!$result) { | |
43 | $this->last_error = @pg_last_error($this->link); | |
44 | ||
45 | @pg_query($this->link, "ROLLBACK"); | |
46 | $query = htmlspecialchars($query); // just in case | |
47 | user_error("Query $query failed: " . ($this->link ? $this->last_error : "No connection"), | |
48 | $die_on_error ? E_USER_ERROR : E_USER_WARNING); | |
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 | ||
78 | function last_query_error() { | |
79 | return $this->last_error; | |
80 | } | |
81 | ||
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"); | |
87 | $this->query("set cpu_tuple_cost = 0.5"); | |
88 | ||
89 | return true; | |
90 | } | |
91 | } |