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

« back to all changes in this revision

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

  • Committer: dcoles
  • Date: 2008-02-13 04:10:55 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:443
Added Forum application along with unmodifed version of phpBB3 "Olympus" 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
*
 
4
* @package dbal
 
5
* @version $Id: mysqli.php,v 1.36 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
* MySQLi Database Abstraction Layer
 
23
* mysqli-extension has to be compiled with:
 
24
* MySQL 4.1+ or MySQL 5.0+
 
25
* @package dbal
 
26
*/
 
27
class dbal_mysqli extends dbal
 
28
{
 
29
        var $multi_insert = true;
 
30
 
 
31
        /**
 
32
        * Connect to server
 
33
        */
 
34
        function sql_connect($sqlserver, $sqluser, $sqlpassword, $database, $port = false, $persistency = false , $new_link = false)
 
35
        {
 
36
                $this->persistency = $persistency;
 
37
                $this->user = $sqluser;
 
38
                $this->server = $sqlserver;
 
39
                $this->dbname = $database;
 
40
                $port = (!$port) ? NULL : $port;
 
41
 
 
42
                // Persistant connections not supported by the mysqli extension?
 
43
                $this->db_connect_id = @mysqli_connect($this->server, $this->user, $sqlpassword, $this->dbname, $port);
 
44
 
 
45
                if ($this->db_connect_id && $this->dbname != '')
 
46
                {
 
47
                        @mysqli_query($this->db_connect_id, "SET NAMES 'utf8'");
 
48
                        // enforce strict mode on databases that support it
 
49
                        if (mysqli_get_server_version($this->db_connect_id) >= 50002)
 
50
                        {
 
51
                                $result = @mysqli_query($this->db_connect_id, 'SELECT @@session.sql_mode AS sql_mode');
 
52
                                $row = @mysqli_fetch_assoc($result);
 
53
                                @mysqli_free_result($result);
 
54
                                $modes = array_map('trim', explode(',', $row['sql_mode']));
 
55
 
 
56
                                // TRADITIONAL includes STRICT_ALL_TABLES and STRICT_TRANS_TABLES
 
57
                                if (!in_array('TRADITIONAL', $modes))
 
58
                                {
 
59
                                        if (!in_array('STRICT_ALL_TABLES', $modes))
 
60
                                        {
 
61
                                                $modes[] = 'STRICT_ALL_TABLES';
 
62
                                        }
 
63
 
 
64
                                        if (!in_array('STRICT_TRANS_TABLES', $modes))
 
65
                                        {
 
66
                                                $modes[] = 'STRICT_TRANS_TABLES';
 
67
                                        }
 
68
                                }
 
69
 
 
70
                                $mode = implode(',', $modes);
 
71
                                @mysqli_query($this->db_connect_id, "SET SESSION sql_mode='{$mode}'");
 
72
                        }
 
73
                        return $this->db_connect_id;
 
74
                }
 
75
 
 
76
                return $this->sql_error('');
 
77
        }
 
78
 
 
79
        /**
 
80
        * Version information about used database
 
81
        */
 
82
        function sql_server_info()
 
83
        {
 
84
                return 'MySQL(i) ' . @mysqli_get_server_info($this->db_connect_id);
 
85
        }
 
86
 
 
87
        /**
 
88
        * SQL Transaction
 
89
        * @access private
 
90
        */
 
91
        function _sql_transaction($status = 'begin')
 
92
        {
 
93
                switch ($status)
 
94
                {
 
95
                        case 'begin':
 
96
                                return @mysqli_autocommit($this->db_connect_id, false);
 
97
                        break;
 
98
 
 
99
                        case 'commit':
 
100
                                $result = @mysqli_commit($this->db_connect_id);
 
101
                                @mysqli_autocommit($this->db_connect_id, true);
 
102
                                return $result;
 
103
                        break;
 
104
 
 
105
                        case 'rollback':
 
106
                                $result = @mysqli_rollback($this->db_connect_id);
 
107
                                @mysqli_autocommit($this->db_connect_id, true);
 
108
                                return $result;
 
109
                        break;
 
110
                }
 
111
 
 
112
                return true;
 
113
        }
 
114
 
 
115
        /**
 
116
        * Base query method
 
117
        *
 
118
        * @param        string  $query          Contains the SQL query which shall be executed
 
119
        * @param        int             $cache_ttl      Either 0 to avoid caching or the time in seconds which the result shall be kept in cache
 
120
        * @return       mixed                           When casted to bool the returned value returns true on success and false on failure
 
121
        *
 
122
        * @access       public
 
123
        */
 
124
        function sql_query($query = '', $cache_ttl = 0)
 
125
        {
 
126
                if ($query != '')
 
127
                {
 
128
                        global $cache;
 
129
 
 
130
                        // EXPLAIN only in extra debug mode
 
131
                        if (defined('DEBUG_EXTRA'))
 
132
                        {
 
133
                                $this->sql_report('start', $query);
 
134
                        }
 
135
 
 
136
                        $this->query_result = ($cache_ttl && method_exists($cache, 'sql_load')) ? $cache->sql_load($query) : false;
 
137
                        $this->sql_add_num_queries($this->query_result);
 
138
 
 
139
                        if ($this->query_result === false)
 
140
                        {
 
141
                                if (($this->query_result = @mysqli_query($this->db_connect_id, $query)) === false)
 
142
                                {
 
143
                                        $this->sql_error($query);
 
144
                                }
 
145
 
 
146
                                if (defined('DEBUG_EXTRA'))
 
147
                                {
 
148
                                        $this->sql_report('stop', $query);
 
149
                                }
 
150
 
 
151
                                if ($cache_ttl && method_exists($cache, 'sql_save'))
 
152
                                {
 
153
                                        $cache->sql_save($query, $this->query_result, $cache_ttl);
 
154
                                }
 
155
                        }
 
156
                        else if (defined('DEBUG_EXTRA'))
 
157
                        {
 
158
                                $this->sql_report('fromcache', $query);
 
159
                        }
 
160
                }
 
161
                else
 
162
                {
 
163
                        return false;
 
164
                }
 
165
 
 
166
                return ($this->query_result) ? $this->query_result : false;
 
167
        }
 
168
 
 
169
        /**
 
170
        * Build LIMIT query
 
171
        */
 
172
        function _sql_query_limit($query, $total, $offset = 0, $cache_ttl = 0)
 
173
        {
 
174
                $this->query_result = false;
 
175
 
 
176
                // if $total is set to 0 we do not want to limit the number of rows
 
177
                if ($total == 0)
 
178
                {
 
179
                        // MySQL 4.1+ no longer supports -1 in limit queries
 
180
                        $total = '18446744073709551615';
 
181
                }
 
182
 
 
183
                $query .= "\n LIMIT " . ((!empty($offset)) ? $offset . ', ' . $total : $total);
 
184
 
 
185
                return $this->sql_query($query, $cache_ttl);
 
186
        }
 
187
 
 
188
        /**
 
189
        * Return number of affected rows
 
190
        */
 
191
        function sql_affectedrows()
 
192
        {
 
193
                return ($this->db_connect_id) ? @mysqli_affected_rows($this->db_connect_id) : false;
 
194
        }
 
195
 
 
196
        /**
 
197
        * Fetch current row
 
198
        */
 
199
        function sql_fetchrow($query_id = false)
 
200
        {
 
201
                global $cache;
 
202
 
 
203
                if ($query_id === false)
 
204
                {
 
205
                        $query_id = $this->query_result;
 
206
                }
 
207
 
 
208
                if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
 
209
                {
 
210
                        return $cache->sql_fetchrow($query_id);
 
211
                }
 
212
 
 
213
                return ($query_id !== false) ? @mysqli_fetch_assoc($query_id) : false;
 
214
        }
 
215
 
 
216
        /**
 
217
        * Seek to given row number
 
218
        * rownum is zero-based
 
219
        */
 
220
        function sql_rowseek($rownum, &$query_id)
 
221
        {
 
222
                global $cache;
 
223
 
 
224
                if ($query_id === false)
 
225
                {
 
226
                        $query_id = $this->query_result;
 
227
                }
 
228
 
 
229
                if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
 
230
                {
 
231
                        return $cache->sql_rowseek($rownum, $query_id);
 
232
                }
 
233
 
 
234
                return ($query_id !== false) ? @mysqli_data_seek($query_id, $rownum) : false;
 
235
        }
 
236
 
 
237
        /**
 
238
        * Get last inserted id after insert statement
 
239
        */
 
240
        function sql_nextid()
 
241
        {
 
242
                return ($this->db_connect_id) ? @mysqli_insert_id($this->db_connect_id) : false;
 
243
        }
 
244
 
 
245
        /**
 
246
        * Free sql result
 
247
        */
 
248
        function sql_freeresult($query_id = false)
 
249
        {
 
250
                global $cache;
 
251
 
 
252
                if ($query_id === false)
 
253
                {
 
254
                        $query_id = $this->query_result;
 
255
                }
 
256
 
 
257
                if (!is_object($query_id) && isset($cache->sql_rowset[$query_id]))
 
258
                {
 
259
                        return $cache->sql_freeresult($query_id);
 
260
                }
 
261
 
 
262
                return @mysqli_free_result($query_id);
 
263
        }
 
264
 
 
265
        /**
 
266
        * Escape string used in sql query
 
267
        */
 
268
        function sql_escape($msg)
 
269
        {
 
270
                return @mysqli_real_escape_string($this->db_connect_id, $msg);
 
271
        }
 
272
 
 
273
        /**
 
274
        * Build LIKE expression
 
275
        * @access private
 
276
        */
 
277
        function _sql_like_expression($expression)
 
278
        {
 
279
                return $expression;
 
280
        }
 
281
 
 
282
        /**
 
283
        * Build db-specific query data
 
284
        * @access private
 
285
        */
 
286
        function _sql_custom_build($stage, $data)
 
287
        {
 
288
                switch ($stage)
 
289
                {
 
290
                        case 'FROM':
 
291
                                $data = '(' . $data . ')';
 
292
                        break;
 
293
                }
 
294
 
 
295
                return $data;
 
296
        }
 
297
 
 
298
        /**
 
299
        * return sql error array
 
300
        * @access private
 
301
        */
 
302
        function _sql_error()
 
303
        {
 
304
                if (!$this->db_connect_id)
 
305
                {
 
306
                        return array(
 
307
                                'message'       => @mysqli_connect_error(),
 
308
                                'code'          => @mysqli_connect_errno()
 
309
                        );
 
310
                }
 
311
 
 
312
                return array(
 
313
                        'message'       => @mysqli_error($this->db_connect_id),
 
314
                        'code'          => @mysqli_errno($this->db_connect_id)
 
315
                );
 
316
        }
 
317
 
 
318
        /**
 
319
        * Close sql connection
 
320
        * @access private
 
321
        */
 
322
        function _sql_close()
 
323
        {
 
324
                return @mysqli_close($this->db_connect_id);
 
325
        }
 
326
 
 
327
        /**
 
328
        * Build db-specific report
 
329
        * @access private
 
330
        */
 
331
        function _sql_report($mode, $query = '')
 
332
        {
 
333
                static $test_prof;
 
334
 
 
335
                // current detection method, might just switch to see the existance of INFORMATION_SCHEMA.PROFILING
 
336
                if ($test_prof === null)
 
337
                {
 
338
                        $test_prof = false;
 
339
                        if (strpos(mysqli_get_server_info($this->db_connect_id), 'community') !== false)
 
340
                        {
 
341
                                $ver = mysqli_get_server_version($this->db_connect_id);
 
342
                                if ($ver >= 50037 && $ver < 50100)
 
343
                                {
 
344
                                        $test_prof = true;
 
345
                                }
 
346
                        }
 
347
                }
 
348
 
 
349
                switch ($mode)
 
350
                {
 
351
                        case 'start':
 
352
 
 
353
                                $explain_query = $query;
 
354
                                if (preg_match('/UPDATE ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 
355
                                {
 
356
                                        $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 
357
                                }
 
358
                                else if (preg_match('/DELETE FROM ([a-z0-9_]+).*?WHERE(.*)/s', $query, $m))
 
359
                                {
 
360
                                        $explain_query = 'SELECT * FROM ' . $m[1] . ' WHERE ' . $m[2];
 
361
                                }
 
362
 
 
363
                                if (preg_match('/^SELECT/', $explain_query))
 
364
                                {
 
365
                                        $html_table = false;
 
366
 
 
367
                                        // begin profiling
 
368
                                        if ($test_prof)
 
369
                                        {
 
370
                                                @mysqli_query($this->db_connect_id, 'SET profiling = 1;');
 
371
                                        }
 
372
 
 
373
                                        if ($result = @mysqli_query($this->db_connect_id, "EXPLAIN $explain_query"))
 
374
                                        {
 
375
                                                while ($row = @mysqli_fetch_assoc($result))
 
376
                                                {
 
377
                                                        $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 
378
                                                }
 
379
                                        }
 
380
                                        @mysqli_free_result($result);
 
381
 
 
382
                                        if ($html_table)
 
383
                                        {
 
384
                                                $this->html_hold .= '</table>';
 
385
                                        }
 
386
 
 
387
                                        if ($test_prof)
 
388
                                        {
 
389
                                                $html_table = false;
 
390
 
 
391
                                                // get the last profile
 
392
                                                if ($result = @mysqli_query($this->db_connect_id, 'SHOW PROFILE ALL;'))
 
393
                                                {
 
394
                                                        $this->html_hold .= '<br />';
 
395
                                                        while ($row = @mysqli_fetch_assoc($result))
 
396
                                                        {
 
397
                                                                // make <unknown> HTML safe
 
398
                                                                if (!empty($row['Source_function']))
 
399
                                                                {
 
400
                                                                        $row['Source_function'] = str_replace(array('<', '>'), array('&lt;', '&gt;'), $row['Source_function']);
 
401
                                                                }
 
402
 
 
403
                                                                // remove unsupported features
 
404
                                                                foreach ($row as $key => $val)
 
405
                                                                {
 
406
                                                                        if ($val === null)
 
407
                                                                        {
 
408
                                                                                unset($row[$key]);
 
409
                                                                        }
 
410
                                                                }
 
411
                                                                $html_table = $this->sql_report('add_select_row', $query, $html_table, $row);
 
412
                                                        }
 
413
                                                }
 
414
                                                @mysqli_free_result($result);
 
415
 
 
416
                                                if ($html_table)
 
417
                                                {
 
418
                                                        $this->html_hold .= '</table>';
 
419
                                                }
 
420
 
 
421
                                                @mysqli_query($this->db_connect_id, 'SET profiling = 0;');
 
422
                                        }
 
423
                                }
 
424
 
 
425
                        break;
 
426
 
 
427
                        case 'fromcache':
 
428
                                $endtime = explode(' ', microtime());
 
429
                                $endtime = $endtime[0] + $endtime[1];
 
430
 
 
431
                                $result = @mysqli_query($this->db_connect_id, $query);
 
432
                                while ($void = @mysqli_fetch_assoc($result))
 
433
                                {
 
434
                                        // Take the time spent on parsing rows into account
 
435
                                }
 
436
                                @mysqli_free_result($result);
 
437
 
 
438
                                $splittime = explode(' ', microtime());
 
439
                                $splittime = $splittime[0] + $splittime[1];
 
440
 
 
441
                                $this->sql_report('record_fromcache', $query, $endtime, $splittime);
 
442
 
 
443
                        break;
 
444
                }
 
445
        }
 
446
}
 
447
 
 
448
?>
 
 
b'\\ No newline at end of file'