|
1
|
|
|
<?php |
|
2
|
|
|
class Db_Pgsql implements IDb { |
|
3
|
|
|
private $link; |
|
4
|
|
|
private $last_error; |
|
5
|
|
|
|
|
6
|
|
|
public 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
|
|
|
public function escape_string($s, $strip_tags = true) { |
|
34
|
|
|
if ($strip_tags) { |
|
35
|
|
|
$s = strip_tags($s); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return pg_escape_string($s); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function query($query, $die_on_error = true) { |
|
42
|
|
|
$result = @pg_query($this->link, $query); |
|
43
|
|
|
|
|
44
|
|
|
if (!$result) { |
|
|
|
|
|
|
45
|
|
|
$this->last_error = @pg_last_error($this->link); |
|
46
|
|
|
|
|
47
|
|
|
@pg_query($this->link, "ROLLBACK"); |
|
|
|
|
|
|
48
|
|
|
$query = htmlspecialchars($query); // just in case |
|
49
|
|
|
user_error("query $query failed: ".($this->link ? $this->last_error : "No connection"), |
|
50
|
|
|
$die_on_error ? E_USER_ERROR : E_USER_WARNING); |
|
51
|
|
|
} |
|
52
|
|
|
return $result; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function fetch_assoc($result) { |
|
56
|
|
|
return pg_fetch_assoc($result); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
public function num_rows($result) { |
|
61
|
|
|
return pg_num_rows($result); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function fetch_result($result, $row, $param) { |
|
65
|
|
|
return pg_fetch_result($result, $row, $param); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function close() { |
|
69
|
|
|
return pg_close($this->link); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function affected_rows($result) { |
|
73
|
|
|
return pg_affected_rows($result); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function last_error() { |
|
77
|
|
|
return pg_last_error($this->link); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function last_query_error() { |
|
81
|
|
|
return $this->last_error; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function init() { |
|
85
|
|
|
$this->query("set client_encoding = 'UTF-8'"); |
|
86
|
|
|
pg_set_client_encoding("UNICODE"); |
|
|
|
|
|
|
87
|
|
|
$this->query("set datestyle = 'ISO, european'"); |
|
88
|
|
|
$this->query("set TIME ZONE 0"); |
|
89
|
|
|
$this->query("set cpu_tuple_cost = 0.5"); |
|
90
|
|
|
|
|
91
|
|
|
return true; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.