commit | author | age
|
0d94fd
|
1 |
<?php |
AM |
2 |
|
c389a8
|
3 |
/** |
0d94fd
|
4 |
+-----------------------------------------------------------------------+ |
AM |
5 |
| This file is part of the Roundcube Webmail client | |
|
6 |
| Copyright (C) 2005-2012, The Roundcube Dev Team | |
|
7 |
| | |
|
8 |
| Licensed under the GNU General Public License version 3 or | |
|
9 |
| any later version with exceptions for skins & plugins. | |
|
10 |
| See the README file for a full license statement. | |
|
11 |
| | |
|
12 |
| PURPOSE: | |
|
13 |
| Database wrapper class that implements PHP PDO functions | |
|
14 |
| for PostgreSQL database | |
|
15 |
+-----------------------------------------------------------------------+ |
|
16 |
| Author: Aleksander Machniak <alec@alec.pl> | |
|
17 |
+-----------------------------------------------------------------------+ |
|
18 |
*/ |
|
19 |
|
|
20 |
/** |
|
21 |
* Database independent query interface |
|
22 |
* This is a wrapper for the PHP PDO |
|
23 |
* |
9ab346
|
24 |
* @package Framework |
AM |
25 |
* @subpackage Database |
0d94fd
|
26 |
*/ |
AM |
27 |
class rcube_db_pgsql extends rcube_db |
|
28 |
{ |
88107d
|
29 |
public $db_provider = 'postgres'; |
TB |
30 |
|
0d94fd
|
31 |
/** |
d18640
|
32 |
* Driver-specific configuration of database connection |
AM |
33 |
* |
|
34 |
* @param array $dsn DSN for DB connections |
|
35 |
* @param PDO $dbh Connection handler |
|
36 |
*/ |
|
37 |
protected function conn_configure($dsn, $dbh) |
|
38 |
{ |
120db6
|
39 |
$dbh->query("SET NAMES 'utf8'"); |
d18640
|
40 |
} |
AM |
41 |
|
|
42 |
/** |
0d94fd
|
43 |
* Get last inserted record ID |
AM |
44 |
* |
3d231c
|
45 |
* @param string $table Table name (to find the incremented sequence) |
0d94fd
|
46 |
* |
3d231c
|
47 |
* @return mixed ID or false on failure |
0d94fd
|
48 |
*/ |
8e926e
|
49 |
public function insert_id($table = null) |
0d94fd
|
50 |
{ |
AM |
51 |
if (!$this->db_connected || $this->db_mode == 'r') { |
|
52 |
return false; |
|
53 |
} |
|
54 |
|
|
55 |
if ($table) { |
|
56 |
$table = $this->sequence_name($table); |
|
57 |
} |
|
58 |
|
|
59 |
$id = $this->dbh->lastInsertId($table); |
|
60 |
|
|
61 |
return $id; |
|
62 |
} |
|
63 |
|
|
64 |
/** |
8e926e
|
65 |
* Return correct name for a specific database sequence |
AM |
66 |
* |
399db1
|
67 |
* @param string $table Table name |
8e926e
|
68 |
* |
AM |
69 |
* @return string Translated sequence name |
|
70 |
*/ |
399db1
|
71 |
protected function sequence_name($table) |
8e926e
|
72 |
{ |
399db1
|
73 |
// Note: we support only one sequence per table |
AM |
74 |
// Note: The sequence name must be <table_name>_seq |
|
75 |
$sequence = $table . '_seq'; |
|
76 |
$rcube = rcube::get_instance(); |
8e926e
|
77 |
|
AM |
78 |
// return sequence name if configured |
399db1
|
79 |
if ($prefix = $rcube->config->get('db_prefix')) { |
AM |
80 |
return $prefix . $sequence; |
8e926e
|
81 |
} |
AM |
82 |
|
|
83 |
return $sequence; |
|
84 |
} |
|
85 |
|
|
86 |
/** |
0d94fd
|
87 |
* Return SQL statement to convert a field value into a unix timestamp |
AM |
88 |
* |
3d231c
|
89 |
* @param string $field Field name |
0d94fd
|
90 |
* |
3d231c
|
91 |
* @return string SQL statement to use in query |
0d94fd
|
92 |
* @deprecated |
AM |
93 |
*/ |
|
94 |
public function unixtimestamp($field) |
|
95 |
{ |
|
96 |
return "EXTRACT (EPOCH FROM $field)"; |
|
97 |
} |
|
98 |
|
|
99 |
/** |
aa44ce
|
100 |
* Return SQL function for current time and date |
AM |
101 |
* |
|
102 |
* @param int $interval Optional interval (in seconds) to add/subtract |
|
103 |
* |
|
104 |
* @return string SQL function to use in query |
|
105 |
*/ |
|
106 |
public function now($interval = 0) |
|
107 |
{ |
|
108 |
if ($interval) { |
|
109 |
$add = ' ' . ($interval > 0 ? '+' : '-') . " interval '"; |
|
110 |
$add .= $interval > 0 ? intval($interval) : intval($interval) * -1; |
|
111 |
$add .= " seconds'"; |
|
112 |
} |
|
113 |
|
|
114 |
return "now()" . $add; |
|
115 |
} |
|
116 |
|
|
117 |
/** |
0d94fd
|
118 |
* Return SQL statement for case insensitive LIKE |
AM |
119 |
* |
3d231c
|
120 |
* @param string $column Field name |
AM |
121 |
* @param string $value Search value |
0d94fd
|
122 |
* |
3d231c
|
123 |
* @return string SQL statement to use in query |
0d94fd
|
124 |
*/ |
AM |
125 |
public function ilike($column, $value) |
|
126 |
{ |
c389a8
|
127 |
return $this->quote_identifier($column) . ' ILIKE ' . $this->quote($value); |
AM |
128 |
} |
|
129 |
|
|
130 |
/** |
|
131 |
* Get database runtime variables |
|
132 |
* |
3d231c
|
133 |
* @param string $varname Variable name |
AM |
134 |
* @param mixed $default Default value if variable is not set |
c389a8
|
135 |
* |
AM |
136 |
* @return mixed Variable value or default |
|
137 |
*/ |
|
138 |
public function get_variable($varname, $default = null) |
|
139 |
{ |
|
140 |
// There's a known case when max_allowed_packet is queried |
|
141 |
// PostgreSQL doesn't have such limit, return immediately |
|
142 |
if ($varname == 'max_allowed_packet') { |
|
143 |
return $default; |
|
144 |
} |
|
145 |
|
|
146 |
if (!isset($this->variables)) { |
|
147 |
$this->variables = array(); |
|
148 |
|
|
149 |
$result = $this->query('SHOW ALL'); |
|
150 |
|
|
151 |
while ($row = $this->fetch_array($result)) { |
|
152 |
$this->variables[$row[0]] = $row[1]; |
|
153 |
} |
|
154 |
} |
|
155 |
|
|
156 |
return isset($this->variables[$varname]) ? $this->variables[$varname] : $default; |
0d94fd
|
157 |
} |
AM |
158 |
|
d09621
|
159 |
/** |
AM |
160 |
* Returns PDO DSN string from DSN array |
|
161 |
* |
|
162 |
* @param array $dsn DSN parameters |
|
163 |
* |
|
164 |
* @return string DSN string |
|
165 |
*/ |
|
166 |
protected function dsn_string($dsn) |
|
167 |
{ |
|
168 |
$params = array(); |
|
169 |
$result = 'pgsql:'; |
|
170 |
|
|
171 |
if ($dsn['hostspec']) { |
|
172 |
$params[] = 'host=' . $dsn['hostspec']; |
|
173 |
} |
|
174 |
else if ($dsn['socket']) { |
|
175 |
$params[] = 'host=' . $dsn['socket']; |
|
176 |
} |
|
177 |
|
|
178 |
if ($dsn['port']) { |
|
179 |
$params[] = 'port=' . $dsn['port']; |
|
180 |
} |
|
181 |
|
|
182 |
if ($dsn['database']) { |
|
183 |
$params[] = 'dbname=' . $dsn['database']; |
|
184 |
} |
|
185 |
|
|
186 |
if (!empty($params)) { |
|
187 |
$result .= implode(';', $params); |
|
188 |
} |
|
189 |
|
|
190 |
return $result; |
|
191 |
} |
|
192 |
|
0d94fd
|
193 |
} |