~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/php/phpBB3/includes/db/postgres.php

  • Committer: William Grant
  • Date: 2009-02-23 23:47:02 UTC
  • mfrom: (1099.1.211 new-dispatch)
  • Revision ID: grantw@unimelb.edu.au-20090223234702-db4b1llly46ignwo
Merge from lp:~ivle-dev/ivle/new-dispatch.

Pretty much everything changes. Reread the setup docs. Backup your databases.
Every file is now in a different installed location, the configuration system
is rewritten, the dispatch system is rewritten, URLs are different, the
database is different, worksheets and exercises are no longer on the
filesystem, we use a templating engine, jail service protocols are rewritten,
we don't repeat ourselves, we have authorization rewritten, phpBB is gone,
and probably lots of other things that I cannot remember.

This is certainly the biggest commit I have ever made, and hopefully
the largest I ever will.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
3
 
*
4
 
* @package dbal
5
 
* @version $Id: postgres.php,v 1.48 2007/10/05 14:36:32 acydburn Exp $
6
 
* @copyright (c) 2005 phpBB Group
7
 
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
8
 
*
9
 
*/
10
 
 
11
 
/**
12
 
* @ignore
13
 
*/
14
 
if (!defined('IN_PHPBB'))
15
 
{
16
 
        exit;
17
 
}
18
 
 
19
 
include_once($phpbb_root_path . 'includes/db/dbal.' . $phpEx);
20
 
 
21
 
/**
22
 
* PostgreSQL Database Abstraction Layer
23
 
* Minimum Requirement is Version 7.3+
24
 
* @package dbal
25
 
*/
26
 
class dbal_postgres extends dbal
27
 
{
28
 
        var $last_query_text = '';
29
 
        var $pgsql_version;
30
 
        
31
 
        /**
32
 
        * Connect to server
33
 
        */
34
 
        function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false, $new_link = false)
35
 
        {
36
 
                $connect_string = '';
37
 
 
38
 
                if ($sqluser)
39
 
                {
40
 
                        $connect_string .= "user=$sqluser ";
41
 
                }
42
 
 
43
 
                if ($sqlpassword)
44
 
                {
45
 
                        $connect_string .= "password=$sqlpassword ";
46
 
                }
47
 
 
48
 
                if ($sqlserver)
49
 
                {
50
 
                        if (strpos($sqlserver, ':') !== false)
51
 
                        {
52
 
                                list($sqlserver, $port) = explode(':', $sqlserver);
53
 
                        }
54
 
 
55
 
                        if ($sqlserver !== 'localhost')
56
 
                        {
57
 
                                $connect_string .= "host=$sqlserver ";
58
 
                        }
59
 
                
60
 
                        if ($port)
61
 
                        {
62
 
                                $connect_string .= "port=$port ";
63
 
                        }
64
 
                }
65
 
 
66
 
                $schema = '';
67
 
 
68
 
                if ($database)
69
 
                {
70
 
                        $this->dbname = $database;
71
 
                        if (strpos($database, '.') !== false)
72
 
                        {
73
 
                                list($database, $schema) = explode('.', $database);
74
 
                        }
75
 
                        $connect_string .= "dbname=$database";
76
 
                }
77
 
 
78
 
                $this->persistency = $persistency;
79
 
 
80
 
                $this->db_connect_id = ($this->persistency) ? @pg_pconnect($connect_string, $new_link) : @pg_connect($connect_string, $new_link);
81
 
 
82
 
                if ($this->db_connect_id)
83
 
                {
84
 
                        // determine what version of PostgreSQL is running, we can be more efficient if they are running 8.2+
85
 
                        if (version_compare(PHP_VERSION, '5.0.0', '>='))
86
 
                        {
87
 
                                $this->pgsql_version = @pg_parameter_status($this->db_connect_id, 'server_version');
88
 
                        }
89
 
                        else
90
 
                        {
91
 
                                $query_id = @pg_query($this->db_connect_id, 'SELECT VERSION()');
92
 
                                $row = @pg_fetch_assoc($query_id, null);
93
 
                                @pg_free_result($query_id);
94
 
 
95
 
                                if (!empty($row['version']))
96
 
                                {
97
 
                                        $this->pgsql_version = substr($row['version'], 10);
98
 
                                }
99
 
                        }
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
 
?>
 
 
b'\\ No newline at end of file'