Verzeichnisstruktur phpBB-3.0.0
- Veröffentlicht
- 12.12.2007
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
postgres.php
001 <?php
002 /**
003 *
004 * @package dbal
005 * @version $Id$
006 * @copyright (c) 2005 phpBB Group
007 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
008 *
009 */
010
011 /**
012 * @ignore
013 */
014 if (!defined('IN_PHPBB'))
015 {
016 exit;
017 }
018
019 include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
020
021 /**
022 * PostgreSQL Database Abstraction Layer
023 * Minimum Requirement is Version 7.3+
024 * @package dbal
025 */
026 class dbal_postgres extends dbal
027 {
028 var $last_query_text = '';
029 var $pgsql_version;
030
031 /**
032 * Connect to server
033 */
034 function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
035 {
036 $connect_string = '';
037
038 if ($sqluser)
039 {
040 $connect_string .= "user=$sqluser ";
041 }
042
043 if ($sqlpassword)
044 {
045 $connect_string .= "password=$sqlpassword ";
046 }
047
048 if ($sqlserver)
049 {
050 if (strpos($sqlserver, ':') !== false)
051 {
052 list($sqlserver, $port) = explode(':', $sqlserver);
053 }
054
055 if ($sqlserver !== 'localhost')
056 {
057 $connect_string .= "host=$sqlserver ";
058 }
059
060 if ($port)
061 {
062 $connect_string .= "port=$port ";
063 }
064 }
065
066 $schema = '';
067
068 if ($database)
069 {
070 $this->dbname = $database;
071 if (strpos($database, '.') !== false)
072 {
073 list($database, $schema) = explode('.', $database);
074 }
075 $connect_string .= "dbname=$database";
076 }
077
078 $this->persistency = $persistency;
079
080 $this->db_connect_id = ($this->persistency) ? @pg_pconnect($connect_string, $new_link) : @pg_connect($connect_string, $new_link);
081
082 if ($this->db_connect_id)
083 {
084 // determine what version of PostgreSQL is running, we can be more efficient if they are running 8.2+
085 if (version_compare(PHP_VERSION, '5.0.0', '>='))
086 {
087 $this->pgsql_version = @pg_parameter_status($this->db_connect_id, 'server_version');
088 }
089 else
090 {
091 $query_id = @pg_query($this->db_connect_id, 'SELECT VERSION()');
092 $row = @pg_fetch_assoc($query_id, null);
093 @pg_free_result($query_id);
094
095 if (!empty($row['version']))
096 {
097 $this->pgsql_version = substr($row['version'], 10);
098 }
099 }
100
101 if (!empty($this->pgsql_version) && $this->pgsql_version[0] >= '8' && $this->pgsql_version[2] >= '2')
102 {
103 $this->multi_insert = true;
104 }
105
106 if ($schema !== '')
107 {
108 @pg_query($this->db_connect_id, 'SET search_path TO ' . $schema);
109 }
110 return $this->db_connect_id;
111 }
112
113 return $this->sql_error('');
114 }
115
116 /**
117 * Version information about used database
118 */
119 function sql_server_info()
120 {
121 return 'PostgreSQL ' . $this->pgsql_version;
122 }
123
124 /**
125 * SQL Transaction
126 * @access private
127 */
128 function _sql_transaction($status = 'begin')
129 {
130 switch ($status)
131 {
132 case 'begin':
133 return @pg_query($this->db_connect_id, 'BEGIN');
134 break;
135
136 case 'commit':
137 return @pg_query($this->db_connect_id, 'COMMIT');
138 break;
139
140 case 'rollback':
141 return @pg_query($this->db_connect_id, 'ROLLBACK');
142 break;
143 }
144
145 return true;
146 }
147
148 /**
149 * Base query method
150 *
151 * @param string $query Contains the SQL query which shall be executed
152 * @param int $cache_ttl Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
153 * @return mixed When casted to bool the returned value returns true on success and false on failure
154 *
155 * @access public
156 */
157 function sql_query($query = '', $cache_ttl = 0)
158 {
159 if ($query != '')
160 {
161 global $cache;
162
163 // EXPLAIN only in extra debug mode
164 if (defined('DEBUG_EXTRA'))
165 {
166 $this->sql_report('start', $query);
167 }
168
169 $this->last_query_text = $query;
170 $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
171 $this->sql_add_num_queries($this->query_result);
172
173 if ($this->query_result === false)
174 {
175 if (($this->query_result = @pg_query($this->db_connect_id, $query)) === false)
176 {
177 $this->sql_error($query);
178 }
179
180 if (defined('DEBUG_EXTRA'))
181 {
182 $this->sql_report('stop', $query);
183 }
184
185 if ($cache_ttl && method_exists($cache, 'sql_save'))
186 {
187 $this->open_queries[(int) $this->query_result] = $this->query_result;
188 $cache->sql_save($query, $this->query_result, $cache_ttl);
189 }
190 else if (strpos($query, 'SELECT') === 0 && $this->query_result)
191 {
192 $this->open_queries[(int) $this->query_result] = $this->query_result;
193 }
194 }
195 else if (defined('DEBUG_EXTRA'))
196 {
197 $this->sql_report('fromcache', $query);
198 }
199 }
200 else
201 {
202 return false;
203 }
204
205 return ($this->query_result) ? $this->query_result : false;
206 }
207
208 /**
209 * Build db-specific query data
210 * @access private
211 */
212 function _sql_custom_build($stage, $data)
213 {
214 return $data;
215 }
216
217 /**
218 * Build LIMIT query
219 */
220 function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
221 {
222 $this->query_result = false;
223
224 // if $total is set to 0 we do not want to limit the number of rows
225 if ($total == 0)
226 {
227 $total = -1;
228 }
229
230 $query .= "\n LIMIT $total OFFSET $offset";
231
232 return $this->sql_query($query, $cache_ttl);
233 }
234
235 /**
236 * Return number of affected rows
237 */
238 function sql_affectedrows()
239 {
240 return ($this->query_result) ? @pg_affected_rows($this->query_result) : false;
241 }
242
243 /**
244 * Fetch current row
245 */
246 function sql_fetchrow($query_id = false)
247 {
248 global $cache;
249
250 if ($query_id === false)
251 {
252 $query_id = $this->query_result;
253 }
254
255 if (isset($cache->sql_rowset[$query_id]))
256 {
257 return $cache->sql_fetchrow($query_id);
258 }
259
260 return ($query_id !== false) ? @pg_fetch_assoc($query_id, null) : false;
261 }
262
263 /**
264 * Seek to given row number
265 * rownum is zero-based
266 */
267 function sql_rowseek($rownum, &$query_id)
268 {
269 global $cache;
270
271 if ($query_id === false)
272 {
273 $query_id = $this->query_result;
274 }
275
276 if (isset($cache->sql_rowset[$query_id]))
277 {
278 return $cache->sql_rowseek($rownum, $query_id);
279 }
280
281 return ($query_id !== false) ? @pg_result_seek($query_id, $rownum) : false;
282 }
283
284 /**
285 * Get last inserted id after insert statement
286 */
287 function sql_nextid()
288 {
289 $query_id = $this->query_result;
290
291 if ($query_id !== false && $this->last_query_text != '')
292 {
293 if (preg_match("/^INSERT[\t\n ]+INTO[\t\n ]+([a-z0-9\_\-]+)/is", $this->last_query_text, $tablename))
294 {
295 $query = "SELECT currval('" . $tablename[1] . "_seq') AS last_value";
296 $temp_q_id = @pg_query($this->db_connect_id, $query);
297
298 if (!$temp_q_id)
299 {
300 return false;
301 }
302
303 $temp_result = @pg_fetch_assoc($temp_q_id, NULL);
304 @pg_free_result($query_id);
305
306 return ($temp_result) ? $temp_result['last_value'] : false;
307 }
308 }
309
310 return false;
311 }
312
313 /**
314 * Free sql result
315 */
316 function sql_freeresult($query_id = false)
317 {
318 global $cache;
319
320 if ($query_id === false)
321 {
322 $query_id = $this->query_result;
323 }
324
325 if (isset($cache->sql_rowset[$query_id]))
326 {
327 return $cache->sql_freeresult($query_id);
328 }
329
330 if (isset($this->open_queries[(int) $query_id]))
331 {
332 unset($this->open_queries[(int) $query_id]);
333 return @pg_free_result($query_id);
334 }
335
336 return false;
337 }
338
339 /**
340 * Escape string used in sql query
341 * Note: Do not use for bytea values if we may use them at a later stage
342 */
343 function sql_escape($msg)
344 {
345 return @pg_escape_string($msg);
346 }
347
348 /**
349 * Build LIKE expression
350 * @access private
351 */
352 function _sql_like_expression($expression)
353 {
354 return $expression;
355 }
356
357 /**
358 * return sql error array
359 * @access private
360 */
361 function _sql_error()
362 {
363 return array(
364 'message' => (!$this->db_connect_id) ? @pg_last_error() : @pg_last_error($this->db_connect_id),
365 'code' => ''
366 );
367 }
368
369 /**
370 * Close sql connection
371 * @access private
372 */
373 function _sql_close()
374 {
375 return @pg_close($this->db_connect_id);
376 }
377
378 /**
379 * Build db-specific report
380 * @access private
381 */
382 function _sql_report($mode, $query = '')
383 {
384 switch ($mode)
385 {
386 case 'start':
387
388 $explain_query = $query;
389 if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
390 {
391 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
392 }
393 else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
394 {
395 $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
396 }
397
398 if (preg_match('/^SELECT/', $explain_query))
399 {
400 $html_table = false;
401
402 if ($result = @pg_query($this->db_connect_id, "EXPLAIN $explain_query"))
403 {
404 while ($row = @pg_fetch_assoc($result, NULL))
405 {
406 $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
407 }
408 }
409 @pg_free_result($result);
410
411 if ($html_table)
412 {
413 $this->html_hold .= '</table>';
414 }
415 }
416
417 break;
418
419 case 'fromcache':
420 $endtime = explode(' ', microtime());
421 $endtime = $endtime[0] + $endtime[1];
422
423 $result = @pg_query($this->db_connect_id, $query);
424 while ($void = @pg_fetch_assoc($result, NULL))
425 {
426 // Take the time spent on parsing rows into account
427 }
428 @pg_free_result($result);
429
430 $splittime = explode(' ', microtime());
431 $splittime = $splittime[0] + $splittime[1];
432
433 $this->sql_report('record_fromcache', $query, $endtime, $splittime);
434
435 break;
436 }
437 }
438 }
439
440 ?>