Subversion Repositories Applications.papyrus

Rev

Rev 1987 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1987 Rev 1988
Line -... Line 1...
-
 
1
<?php
-
 
2
 
-
 
3
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
-
 
4
 
-
 
5
/**
-
 
6
 * The PEAR DB driver for PHP's mysqli extension
-
 
7
 * for interacting with MySQL databases
-
 
8
 *
-
 
9
 * PHP versions 4 and 5
-
 
10
 *
-
 
11
 * LICENSE: This source file is subject to version 3.0 of the PHP license
-
 
12
 * that is available through the world-wide-web at the following URI:
-
 
13
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
-
 
14
 * the PHP License and are unable to obtain it through the web, please
-
 
15
 * send a note to license@php.net so we can mail you a copy immediately.
-
 
16
 *
-
 
17
 * @category   Database
-
 
18
 * @package    DB
-
 
19
 * @author     Daniel Convissor <danielc@php.net>
-
 
20
 * @copyright  1997-2005 The PHP Group
-
 
21
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
-
 
22
 * @version    CVS: $Id: mysqli.php,v 1.3 2006-12-14 15:04:28 jp_milcent Exp $
-
 
23
 * @link       http://pear.php.net/package/DB
-
 
24
 */
-
 
25
 
-
 
26
/**
-
 
27
 * Obtain the DB_common class so it can be extended from
-
 
28
 */
-
 
29
require_once 'DB/common.php';
-
 
30
 
-
 
31
/**
-
 
32
 * The methods PEAR DB uses to interact with PHP's mysqli extension
-
 
33
 * for interacting with MySQL databases
-
 
34
 *
-
 
35
 * This is for MySQL versions 4.1 and above.  Requires PHP 5.
-
 
36
 *
-
 
37
 * Note that persistent connections no longer exist.
-
 
38
 *
-
 
39
 * These methods overload the ones declared in DB_common.
-
 
40
 *
-
 
41
 * @category   Database
-
 
42
 * @package    DB
-
 
43
 * @author     Daniel Convissor <danielc@php.net>
-
 
44
 * @copyright  1997-2005 The PHP Group
-
 
45
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
-
 
46
 * @version    Release: 1.7.6
-
 
47
 * @link       http://pear.php.net/package/DB
-
 
48
 * @since      Class functional since Release 1.6.3
-
 
49
 */
-
 
50
class DB_mysqli extends DB_common
-
 
51
{
-
 
52
    // {{{ properties
-
 
53
 
-
 
54
    /**
-
 
55
     * The DB driver type (mysql, oci8, odbc, etc.)
-
 
56
     * @var string
-
 
57
     */
-
 
58
    var $phptype = 'mysqli';
-
 
59
 
-
 
60
    /**
-
 
61
     * The database syntax variant to be used (db2, access, etc.), if any
-
 
62
     * @var string
-
 
63
     */
-
 
64
    var $dbsyntax = 'mysqli';
-
 
65
 
-
 
66
    /**
-
 
67
     * The capabilities of this DB implementation
-
 
68
     *
-
 
69
     * The 'new_link' element contains the PHP version that first provided
-
 
70
     * new_link support for this DBMS.  Contains false if it's unsupported.
-
 
71
     *
-
 
72
     * Meaning of the 'limit' element:
-
 
73
     *   + 'emulate' = emulate with fetch row by number
-
 
74
     *   + 'alter'   = alter the query
-
 
75
     *   + false     = skip rows
-
 
76
     *
-
 
77
     * @var array
-
 
78
     */
-
 
79
    var $features = array(
-
 
80
        'limit'         => 'alter',
-
 
81
        'new_link'      => false,
-
 
82
        'numrows'       => true,
-
 
83
        'pconnect'      => false,
-
 
84
        'prepare'       => false,
-
 
85
        'ssl'           => true,
-
 
86
        'transactions'  => true,
-
 
87
    );
-
 
88
 
-
 
89
    /**
-
 
90
     * A mapping of native error codes to DB error codes
-
 
91
     * @var array
-
 
92
     */
-
 
93
    var $errorcode_map = array(
-
 
94
        1004 => DB_ERROR_CANNOT_CREATE,
-
 
95
        1005 => DB_ERROR_CANNOT_CREATE,
-
 
96
        1006 => DB_ERROR_CANNOT_CREATE,
-
 
97
        1007 => DB_ERROR_ALREADY_EXISTS,
-
 
98
        1008 => DB_ERROR_CANNOT_DROP,
-
 
99
        1022 => DB_ERROR_ALREADY_EXISTS,
-
 
100
        1044 => DB_ERROR_ACCESS_VIOLATION,
-
 
101
        1046 => DB_ERROR_NODBSELECTED,
-
 
102
        1048 => DB_ERROR_CONSTRAINT,
-
 
103
        1049 => DB_ERROR_NOSUCHDB,
-
 
104
        1050 => DB_ERROR_ALREADY_EXISTS,
-
 
105
        1051 => DB_ERROR_NOSUCHTABLE,
-
 
106
        1054 => DB_ERROR_NOSUCHFIELD,
-
 
107
        1061 => DB_ERROR_ALREADY_EXISTS,
-
 
108
        1062 => DB_ERROR_ALREADY_EXISTS,
-
 
109
        1064 => DB_ERROR_SYNTAX,
-
 
110
        1091 => DB_ERROR_NOT_FOUND,
-
 
111
        1100 => DB_ERROR_NOT_LOCKED,
-
 
112
        1136 => DB_ERROR_VALUE_COUNT_ON_ROW,
-
 
113
        1142 => DB_ERROR_ACCESS_VIOLATION,
-
 
114
        1146 => DB_ERROR_NOSUCHTABLE,
-
 
115
        1216 => DB_ERROR_CONSTRAINT,
-
 
116
        1217 => DB_ERROR_CONSTRAINT,
-
 
117
    );
-
 
118
 
-
 
119
    /**
-
 
120
     * The raw database connection created by PHP
-
 
121
     * @var resource
-
 
122
     */
-
 
123
    var $connection;
-
 
124
 
-
 
125
    /**
-
 
126
     * The DSN information for connecting to a database
-
 
127
     * @var array
-
 
128
     */
-
 
129
    var $dsn = array();
-
 
130
 
-
 
131
 
-
 
132
    /**
-
 
133
     * Should data manipulation queries be committed automatically?
-
 
134
     * @var bool
-
 
135
     * @access private
-
 
136
     */
-
 
137
    var $autocommit = true;
-
 
138
 
-
 
139
    /**
-
 
140
     * The quantity of transactions begun
-
 
141
     *
-
 
142
     * {@internal  While this is private, it can't actually be designated
-
 
143
     * private in PHP 5 because it is directly accessed in the test suite.}}
-
 
144
     *
-
 
145
     * @var integer
-
 
146
     * @access private
-
 
147
     */
-
 
148
    var $transaction_opcount = 0;
-
 
149
 
-
 
150
    /**
-
 
151
     * The database specified in the DSN
-
 
152
     *
-
 
153
     * It's a fix to allow calls to different databases in the same script.
-
 
154
     *
-
 
155
     * @var string
-
 
156
     * @access private
-
 
157
     */
-
 
158
    var $_db = '';
-
 
159
 
-
 
160
    /**
-
 
161
     * Array for converting MYSQLI_*_FLAG constants to text values
-
 
162
     * @var    array
-
 
163
     * @access public
-
 
164
     * @since  Property available since Release 1.6.5
-
 
165
     */
-
 
166
    var $mysqli_flags = array(
-
 
167
        MYSQLI_NOT_NULL_FLAG        => 'not_null',
-
 
168
        MYSQLI_PRI_KEY_FLAG         => 'primary_key',
-
 
169
        MYSQLI_UNIQUE_KEY_FLAG      => 'unique_key',
-
 
170
        MYSQLI_MULTIPLE_KEY_FLAG    => 'multiple_key',
-
 
171
        MYSQLI_BLOB_FLAG            => 'blob',
-
 
172
        MYSQLI_UNSIGNED_FLAG        => 'unsigned',
-
 
173
        MYSQLI_ZEROFILL_FLAG        => 'zerofill',
-
 
174
        MYSQLI_AUTO_INCREMENT_FLAG  => 'auto_increment',
-
 
175
        MYSQLI_TIMESTAMP_FLAG       => 'timestamp',
-
 
176
        MYSQLI_SET_FLAG             => 'set',
-
 
177
        // MYSQLI_NUM_FLAG             => 'numeric',  // unnecessary
-
 
178
        // MYSQLI_PART_KEY_FLAG        => 'multiple_key',  // duplicatvie
-
 
179
        MYSQLI_GROUP_FLAG           => 'group_by'
-
 
180
    );
-
 
181
 
-
 
182
    /**
-
 
183
     * Array for converting MYSQLI_TYPE_* constants to text values
-
 
184
     * @var    array
-
 
185
     * @access public
-
 
186
     * @since  Property available since Release 1.6.5
-
 
187
     */
-
 
188
    var $mysqli_types = array(
-
 
189
        MYSQLI_TYPE_DECIMAL     => 'decimal',
-
 
190
        MYSQLI_TYPE_TINY        => 'tinyint',
-
 
191
        MYSQLI_TYPE_SHORT       => 'int',
-
 
192
        MYSQLI_TYPE_LONG        => 'int',
-
 
193
        MYSQLI_TYPE_FLOAT       => 'float',
-
 
194
        MYSQLI_TYPE_DOUBLE      => 'double',
-
 
195
        // MYSQLI_TYPE_NULL        => 'DEFAULT NULL',  // let flags handle it
-
 
196
        MYSQLI_TYPE_TIMESTAMP   => 'timestamp',
-
 
197
        MYSQLI_TYPE_LONGLONG    => 'bigint',
-
 
198
        MYSQLI_TYPE_INT24       => 'mediumint',
-
 
199
        MYSQLI_TYPE_DATE        => 'date',
-
 
200
        MYSQLI_TYPE_TIME        => 'time',
-
 
201
        MYSQLI_TYPE_DATETIME    => 'datetime',
-
 
202
        MYSQLI_TYPE_YEAR        => 'year',
-
 
203
        MYSQLI_TYPE_NEWDATE     => 'date',
-
 
204
        MYSQLI_TYPE_ENUM        => 'enum',
-
 
205
        MYSQLI_TYPE_SET         => 'set',
-
 
206
        MYSQLI_TYPE_TINY_BLOB   => 'tinyblob',
-
 
207
        MYSQLI_TYPE_MEDIUM_BLOB => 'mediumblob',
-
 
208
        MYSQLI_TYPE_LONG_BLOB   => 'longblob',
-
 
209
        MYSQLI_TYPE_BLOB        => 'blob',
-
 
210
        MYSQLI_TYPE_VAR_STRING  => 'varchar',
-
 
211
        MYSQLI_TYPE_STRING      => 'char',
-
 
212
        MYSQLI_TYPE_GEOMETRY    => 'geometry',
-
 
213
    );
-
 
214
 
-
 
215
 
-
 
216
    // }}}
-
 
217
    // {{{ constructor
-
 
218
 
-
 
219
    /**
-
 
220
     * This constructor calls <kbd>$this->DB_common()</kbd>
-
 
221
     *
-
 
222
     * @return void
-
 
223
     */
-
 
224
    function DB_mysqli()
-
 
225
    {
-
 
226
        $this->DB_common();
-
 
227
    }
-
 
228
 
-
 
229
    // }}}
-
 
230
    // {{{ connect()
-
 
231
 
-
 
232
    /**
-
 
233
     * Connect to the database server, log in and open the database
-
 
234
     *
-
 
235
     * Don't call this method directly.  Use DB::connect() instead.
-
 
236
     *
-
 
237
     * PEAR DB's mysqli driver supports the following extra DSN options:
-
 
238
     *   + When the 'ssl' $option passed to DB::connect() is true:
-
 
239
     *     + key      The path to the key file.
-
 
240
     *     + cert     The path to the certificate file.
-
 
241
     *     + ca       The path to the certificate authority file.
-
 
242
     *     + capath   The path to a directory that contains trusted SSL
-
 
243
     *                 CA certificates in pem format.
-
 
244
     *     + cipher   The list of allowable ciphers for SSL encryption.
-
 
245
     *
-
 
246
     * Example of how to connect using SSL:
-
 
247
     * <code>
-
 
248
     * require_once 'DB.php';
-
 
249
     * 
-
 
250
     * $dsn = array(
-
 
251
     *     'phptype'  => 'mysqli',
-
 
252
     *     'username' => 'someuser',
-
 
253
     *     'password' => 'apasswd',
-
 
254
     *     'hostspec' => 'localhost',
-
 
255
     *     'database' => 'thedb',
-
 
256
     *     'key'      => 'client-key.pem',
-
 
257
     *     'cert'     => 'client-cert.pem',
-
 
258
     *     'ca'       => 'cacert.pem',
-
 
259
     *     'capath'   => '/path/to/ca/dir',
-
 
260
     *     'cipher'   => 'AES',
-
 
261
     * );
-
 
262
     * 
-
 
263
     * $options = array(
-
 
264
     *     'ssl' => true,
-
 
265
     * );
-
 
266
     * 
-
 
267
     * $db =& DB::connect($dsn, $options);
-
 
268
     * if (PEAR::isError($db)) {
-
 
269
     *     die($db->getMessage());
-
 
270
     * }
-
 
271
     * </code>
-
 
272
     *
-
 
273
     * @param array $dsn         the data source name
-
 
274
     * @param bool  $persistent  should the connection be persistent?
-
 
275
     *
-
 
276
     * @return int  DB_OK on success. A DB_Error object on failure.
-
 
277
     */
-
 
278
    function connect($dsn, $persistent = false)
-
 
279
    {
-
 
280
        if (!PEAR::loadExtension('mysqli')) {
-
 
281
            return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
-
 
282
        }
-
 
283
 
-
 
284
        $this->dsn = $dsn;
-
 
285
        if ($dsn['dbsyntax']) {
-
 
286
            $this->dbsyntax = $dsn['dbsyntax'];
-
 
287
        }
-
 
288
 
-
 
289
        $ini = ini_get('track_errors');
-
 
290
        ini_set('track_errors', 1);
-
 
291
        $php_errormsg = '';
-
 
292
 
-
 
293
        if ($this->getOption('ssl') === true) {
-
 
294
            $init = mysqli_init();
-
 
295
            mysqli_ssl_set(
-
 
296
                $init,
-
 
297
                empty($dsn['key'])    ? null : $dsn['key'],
-
 
298
                empty($dsn['cert'])   ? null : $dsn['cert'],
-
 
299
                empty($dsn['ca'])     ? null : $dsn['ca'],
-
 
300
                empty($dsn['capath']) ? null : $dsn['capath'],
-
 
301
                empty($dsn['cipher']) ? null : $dsn['cipher']
-
 
302
            );
-
 
303
            if ($this->connection = @mysqli_real_connect(
-
 
304
                    $init,
-
 
305
                    $dsn['hostspec'],
-
 
306
                    $dsn['username'],
-
 
307
                    $dsn['password'],
-
 
308
                    $dsn['database'],
-
 
309
                    $dsn['port'],
-
 
310
                    $dsn['socket']))
-
 
311
            {
-
 
312
                $this->connection = $init;
-
 
313
            }
-
 
314
        } else {
-
 
315
            $this->connection = @mysqli_connect(
-
 
316
                $dsn['hostspec'],
-
 
317
                $dsn['username'],
-
 
318
                $dsn['password'],
-
 
319
                $dsn['database'],
-
 
320
                $dsn['port'],
-
 
321
                $dsn['socket']
-
 
322
            );
-
 
323
        }
-
 
324
 
-
 
325
        ini_set('track_errors', $ini);
-
 
326
 
-
 
327
        if (!$this->connection) {
-
 
328
            if (($err = @mysqli_connect_error()) != '') {
-
 
329
                return $this->raiseError(DB_ERROR_CONNECT_FAILED,
-
 
330
                                         null, null, null,
-
 
331
                                         $err);
-
 
332
            } else {
-
 
333
                return $this->raiseError(DB_ERROR_CONNECT_FAILED,
-
 
334
                                         null, null, null,
-
 
335
                                         $php_errormsg);
-
 
336
            }
-
 
337
        }
-
 
338
 
-
 
339
        if ($dsn['database']) {
-
 
340
            $this->_db = $dsn['database'];
-
 
341
        }
-
 
342
 
-
 
343
        return DB_OK;
-
 
344
    }
-
 
345
 
-
 
346
    // }}}
-
 
347
    // {{{ disconnect()
-
 
348
 
-
 
349
    /**
-
 
350
     * Disconnects from the database server
-
 
351
     *
-
 
352
     * @return bool  TRUE on success, FALSE on failure
-
 
353
     */
-
 
354
    function disconnect()
-
 
355
    {
-
 
356
        $ret = @mysqli_close($this->connection);
-
 
357
        $this->connection = null;
-
 
358
        return $ret;
-
 
359
    }
-
 
360
 
-
 
361
    // }}}
-
 
362
    // {{{ simpleQuery()
-
 
363
 
-
 
364
    /**
-
 
365
     * Sends a query to the database server
-
 
366
     *
-
 
367
     * @param string  the SQL query string
-
 
368
     *
-
 
369
     * @return mixed  + a PHP result resrouce for successful SELECT queries
-
 
370
     *                + the DB_OK constant for other successful queries
-
 
371
     *                + a DB_Error object on failure
-
 
372
     */
-
 
373
    function simpleQuery($query)
-
 
374
    {
-
 
375
        $ismanip = DB::isManip($query);
-
 
376
        $this->last_query = $query;
-
 
377
        $query = $this->modifyQuery($query);
-
 
378
        if ($this->_db) {
-
 
379
            if (!@mysqli_select_db($this->connection, $this->_db)) {
-
 
380
                return $this->mysqliRaiseError(DB_ERROR_NODBSELECTED);
-
 
381
            }
-
 
382
        }
-
 
383
        if (!$this->autocommit && $ismanip) {
-
 
384
            if ($this->transaction_opcount == 0) {
-
 
385
                $result = @mysqli_query($this->connection, 'SET AUTOCOMMIT=0');
-
 
386
                $result = @mysqli_query($this->connection, 'BEGIN');
-
 
387
                if (!$result) {
-
 
388
                    return $this->mysqliRaiseError();
-
 
389
                }
-
 
390
            }
-
 
391
            $this->transaction_opcount++;
-
 
392
        }
-
 
393
        $result = @mysqli_query($this->connection, $query);
-
 
394
        if (!$result) {
-
 
395
            return $this->mysqliRaiseError();
-
 
396
        }
-
 
397
        if (is_object($result)) {
-
 
398
            return $result;
-
 
399
        }
-
 
400
        return DB_OK;
-
 
401
    }
-
 
402
 
-
 
403
    // }}}
-
 
404
    // {{{ nextResult()
-
 
405
 
-
 
406
    /**
-
 
407
     * Move the internal mysql result pointer to the next available result.
-
 
408
     *
-
 
409
     * This method has not been implemented yet.
-
 
410
     *
-
 
411
     * @param resource $result a valid sql result resource
-
 
412
     * @return false
-
 
413
     * @access public
-
 
414
     */
-
 
415
    function nextResult($result)
-
 
416
    {
-
 
417
        return false;
-
 
418
    }
-
 
419
 
-
 
420
    // }}}
-
 
421
    // {{{ fetchInto()
-
 
422
 
-
 
423
    /**
-
 
424
     * Places a row from the result set into the given array
-
 
425
     *
-
 
426
     * Formating of the array and the data therein are configurable.
-
 
427
     * See DB_result::fetchInto() for more information.
-
 
428
     *
-
 
429
     * This method is not meant to be called directly.  Use
-
 
430
     * DB_result::fetchInto() instead.  It can't be declared "protected"
-
 
431
     * because DB_result is a separate object.
-
 
432
     *
-
 
433
     * @param resource $result    the query result resource
-
 
434
     * @param array    $arr       the referenced array to put the data in
-
 
435
     * @param int      $fetchmode how the resulting array should be indexed
-
 
436
     * @param int      $rownum    the row number to fetch (0 = first row)
-
 
437
     *
-
 
438
     * @return mixed  DB_OK on success, NULL when the end of a result set is
-
 
439
     *                 reached or on failure
-
 
440
     *
-
 
441
     * @see DB_result::fetchInto()
-
 
442
     */
-
 
443
    function fetchInto($result, &$arr, $fetchmode, $rownum = null)
-
 
444
    {
-
 
445
        if ($rownum !== null) {
-
 
446
            if (!@mysqli_data_seek($result, $rownum)) {
-
 
447
                return null;
-
 
448
            }
-
 
449
        }
-
 
450
        if ($fetchmode & DB_FETCHMODE_ASSOC) {
-
 
451
            $arr = @mysqli_fetch_array($result, MYSQLI_ASSOC);
-
 
452
            if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
-
 
453
                $arr = array_change_key_case($arr, CASE_LOWER);
-
 
454
            }
-
 
455
        } else {
-
 
456
            $arr = @mysqli_fetch_row($result);
-
 
457
        }
-
 
458
        if (!$arr) {
-
 
459
            return null;
-
 
460
        }
-
 
461
        if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
-
 
462
            /*
-
 
463
             * Even though this DBMS already trims output, we do this because
-
 
464
             * a field might have intentional whitespace at the end that
-
 
465
             * gets removed by DB_PORTABILITY_RTRIM under another driver.
-
 
466
             */
-
 
467
            $this->_rtrimArrayValues($arr);
-
 
468
        }
-
 
469
        if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
-
 
470
            $this->_convertNullArrayValuesToEmpty($arr);
-
 
471
        }
-
 
472
        return DB_OK;
-
 
473
    }
-
 
474
 
-
 
475
    // }}}
-
 
476
    // {{{ freeResult()
-
 
477
 
-
 
478
    /**
-
 
479
     * Deletes the result set and frees the memory occupied by the result set
-
 
480
     *
-
 
481
     * This method is not meant to be called directly.  Use
-
 
482
     * DB_result::free() instead.  It can't be declared "protected"
-
 
483
     * because DB_result is a separate object.
-
 
484
     *
-
 
485
     * @param resource $result  PHP's query result resource
-
 
486
     *
-
 
487
     * @return bool  TRUE on success, FALSE if $result is invalid
-
 
488
     *
-
 
489
     * @see DB_result::free()
-
 
490
     */
-
 
491
    function freeResult($result)
-
 
492
    {
-
 
493
        return @mysqli_free_result($result);
-
 
494
    }
-
 
495
 
-
 
496
    // }}}
-
 
497
    // {{{ numCols()
-
 
498
 
-
 
499
    /**
-
 
500
     * Gets the number of columns in a result set
-
 
501
     *
-
 
502
     * This method is not meant to be called directly.  Use
-
 
503
     * DB_result::numCols() instead.  It can't be declared "protected"
-
 
504
     * because DB_result is a separate object.
-
 
505
     *
-
 
506
     * @param resource $result  PHP's query result resource
-
 
507
     *
-
 
508
     * @return int  the number of columns.  A DB_Error object on failure.
-
 
509
     *
-
 
510
     * @see DB_result::numCols()
-
 
511
     */
-
 
512
    function numCols($result)
-
 
513
    {
-
 
514
        $cols = @mysqli_num_fields($result);
-
 
515
        if (!$cols) {
-
 
516
            return $this->mysqliRaiseError();
-
 
517
        }
-
 
518
        return $cols;
-
 
519
    }
-
 
520
 
-
 
521
    // }}}
-
 
522
    // {{{ numRows()
-
 
523
 
-
 
524
    /**
-
 
525
     * Gets the number of rows in a result set
-
 
526
     *
-
 
527
     * This method is not meant to be called directly.  Use
-
 
528
     * DB_result::numRows() instead.  It can't be declared "protected"
-
 
529
     * because DB_res
1
530