Subversion Repositories Applications.papyrus

Rev

Rev 443 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
320 jpm 1
<?php
2
 
3
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
 
5
/**
6
 * Contains the DB_common base class
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * LICENSE: This source file is subject to version 3.0 of the PHP license
11
 * that is available through the world-wide-web at the following URI:
12
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
13
 * the PHP License and are unable to obtain it through the web, please
14
 * send a note to license@php.net so we can mail you a copy immediately.
15
 *
16
 * @category   Database
17
 * @package    DB
18
 * @author     Stig Bakken <ssb@php.net>
19
 * @author     Tomas V.V. Cox <cox@idecnet.com>
20
 * @author     Daniel Convissor <danielc@php.net>
21
 * @copyright  1997-2005 The PHP Group
22
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
23
 * @version    CVS: $Id: common.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
24
 * @link       http://pear.php.net/package/DB
25
 */
26
 
27
/**
28
 * Obtain the PEAR class so it can be extended from
29
 */
30
require_once 'PEAR.php';
31
 
32
/**
33
 * DB_common is the base class from which each database driver class extends
34
 *
35
 * All common methods are declared here.  If a given DBMS driver contains
36
 * a particular method, that method will overload the one here.
37
 *
38
 * @category   Database
39
 * @package    DB
40
 * @author     Stig Bakken <ssb@php.net>
41
 * @author     Tomas V.V. Cox <cox@idecnet.com>
42
 * @author     Daniel Convissor <danielc@php.net>
43
 * @copyright  1997-2005 The PHP Group
44
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
45
 * @version    Release: 1.7.5
46
 * @link       http://pear.php.net/package/DB
47
 */
48
class DB_common extends PEAR
49
{
50
    // {{{ properties
51
 
52
    /**
53
     * The current default fetch mode
54
     * @var integer
55
     */
56
    var $fetchmode = DB_FETCHMODE_ORDERED;
57
 
58
    /**
59
     * The name of the class into which results should be fetched when
60
     * DB_FETCHMODE_OBJECT is in effect
61
     *
62
     * @var string
63
     */
64
    var $fetchmode_object_class = 'stdClass';
65
 
66
    /**
67
     * Was a connection present when the object was serialized()?
68
     * @var bool
69
     * @see DB_common::__sleep(), DB_common::__wake()
70
     */
71
    var $was_connected = null;
72
 
73
    /**
74
     * The most recently executed query
75
     * @var string
76
     */
77
    var $last_query = '';
78
 
79
    /**
80
     * Run-time configuration options
81
     *
82
     * The 'optimize' option has been deprecated.  Use the 'portability'
83
     * option instead.
84
     *
85
     * @var array
86
     * @see DB_common::setOption()
87
     */
88
    var $options = array(
89
        'result_buffering' => 500,
90
        'persistent' => false,
91
        'ssl' => false,
92
        'debug' => 0,
93
        'seqname_format' => '%s_seq',
94
        'autofree' => false,
95
        'portability' => DB_PORTABILITY_NONE,
96
        'optimize' => 'performance',  // Deprecated.  Use 'portability'.
97
    );
98
 
99
    /**
100
     * The parameters from the most recently executed query
101
     * @var array
102
     * @since Property available since Release 1.7.0
103
     */
104
    var $last_parameters = array();
105
 
106
    /**
107
     * The elements from each prepared statement
108
     * @var array
109
     */
110
    var $prepare_tokens = array();
111
 
112
    /**
113
     * The data types of the various elements in each prepared statement
114
     * @var array
115
     */
116
    var $prepare_types = array();
117
 
118
    /**
119
     * The prepared queries
120
     * @var array
121
     */
122
    var $prepared_queries = array();
123
 
124
 
125
    // }}}
126
    // {{{ DB_common
127
 
128
    /**
129
     * This constructor calls <kbd>$this->PEAR('DB_Error')</kbd>
130
     *
131
     * @return void
132
     */
133
    function DB_common()
134
    {
135
        $this->PEAR('DB_Error');
136
    }
137
 
138
    // }}}
139
    // {{{ __sleep()
140
 
141
    /**
142
     * Automatically indicates which properties should be saved
143
     * when PHP's serialize() function is called
144
     *
145
     * @return array  the array of properties names that should be saved
146
     */
147
    function __sleep()
148
    {
149
        if ($this->connection) {
150
            // Don't disconnect(), people use serialize() for many reasons
151
            $this->was_connected = true;
152
        } else {
153
            $this->was_connected = false;
154
        }
155
        if (isset($this->autocommit)) {
156
            return array('autocommit',
157
                         'dbsyntax',
158
                         'dsn',
159
                         'features',
160
                         'fetchmode',
161
                         'fetchmode_object_class',
162
                         'options',
163
                         'was_connected',
164
                   );
165
        } else {
166
            return array('dbsyntax',
167
                         'dsn',
168
                         'features',
169
                         'fetchmode',
170
                         'fetchmode_object_class',
171
                         'options',
172
                         'was_connected',
173
                   );
174
        }
175
    }
176
 
177
    // }}}
178
    // {{{ __wakeup()
179
 
180
    /**
181
     * Automatically reconnects to the database when PHP's unserialize()
182
     * function is called
183
     *
184
     * The reconnection attempt is only performed if the object was connected
185
     * at the time PHP's serialize() function was run.
186
     *
187
     * @return void
188
     */
189
    function __wakeup()
190
    {
191
        if ($this->was_connected) {
192
            $this->connect($this->dsn, $this->options);
193
        }
194
    }
195
 
196
    // }}}
197
    // {{{ __toString()
198
 
199
    /**
200
     * Automatic string conversion for PHP 5
201
     *
202
     * @return string  a string describing the current PEAR DB object
203
     *
204
     * @since Method available since Release 1.7.0
205
     */
206
    function __toString()
207
    {
208
        $info = strtolower(get_class($this));
209
        $info .=  ': (phptype=' . $this->phptype .
210
                  ', dbsyntax=' . $this->dbsyntax .
211
                  ')';
212
        if ($this->connection) {
213
            $info .= ' [connected]';
214
        }
215
        return $info;
216
    }
217
 
218
    // }}}
219
    // {{{ toString()
220
 
221
    /**
222
     * DEPRECATED:  String conversion method
223
     *
224
     * @return string  a string describing the current PEAR DB object
225
     *
226
     * @deprecated Method deprecated in Release 1.7.0
227
     */
228
    function toString()
229
    {
230
        return $this->__toString();
231
    }
232
 
233
    // }}}
234
    // {{{ quoteString()
235
 
236
    /**
237
     * DEPRECATED: Quotes a string so it can be safely used within string
238
     * delimiters in a query
239
     *
240
     * @param string $string  the string to be quoted
241
     *
242
     * @return string  the quoted string
243
     *
244
     * @see DB_common::quoteSmart(), DB_common::escapeSimple()
245
     * @deprecated Method deprecated some time before Release 1.2
246
     */
247
    function quoteString($string)
248
    {
249
        $string = $this->quote($string);
250
        if ($string{0} == "'") {
251
            return substr($string, 1, -1);
252
        }
253
        return $string;
254
    }
255
 
256
    // }}}
257
    // {{{ quote()
258
 
259
    /**
260
     * DEPRECATED: Quotes a string so it can be safely used in a query
261
     *
262
     * @param string $string  the string to quote
263
     *
264
     * @return string  the quoted string or the string <samp>NULL</samp>
265
     *                  if the value submitted is <kbd>null</kbd>.
266
     *
267
     * @see DB_common::quoteSmart(), DB_common::escapeSimple()
268
     * @deprecated Deprecated in release 1.6.0
269
     */
270
    function quote($string = null)
271
    {
272
        return ($string === null) ? 'NULL'
273
                                  : "'" . str_replace("'", "''", $string) . "'";
274
    }
275
 
276
    // }}}
277
    // {{{ quoteIdentifier()
278
 
279
    /**
280
     * Quotes a string so it can be safely used as a table or column name
281
     *
282
     * Delimiting style depends on which database driver is being used.
283
     *
284
     * NOTE: just because you CAN use delimited identifiers doesn't mean
285
     * you SHOULD use them.  In general, they end up causing way more
286
     * problems than they solve.
287
     *
288
     * Portability is broken by using the following characters inside
289
     * delimited identifiers:
290
     *   + backtick (<kbd>`</kbd>) -- due to MySQL
291
     *   + double quote (<kbd>"</kbd>) -- due to Oracle
292
     *   + brackets (<kbd>[</kbd> or <kbd>]</kbd>) -- due to Access
293
     *
294
     * Delimited identifiers are known to generally work correctly under
295
     * the following drivers:
296
     *   + mssql
297
     *   + mysql
298
     *   + mysqli
299
     *   + oci8
300
     *   + odbc(access)
301
     *   + odbc(db2)
302
     *   + pgsql
303
     *   + sqlite
304
     *   + sybase (must execute <kbd>set quoted_identifier on</kbd> sometime
305
     *     prior to use)
306
     *
307
     * InterBase doesn't seem to be able to use delimited identifiers
308
     * via PHP 4.  They work fine under PHP 5.
309
     *
310
     * @param string $str  the identifier name to be quoted
311
     *
312
     * @return string  the quoted identifier
313
     *
314
     * @since Method available since Release 1.6.0
315
     */
316
    function quoteIdentifier($str)
317
    {
318
        return '"' . str_replace('"', '""', $str) . '"';
319
    }
320
 
321
    // }}}
322
    // {{{ quoteSmart()
323
 
324
    /**
325
     * Formats input so it can be safely used in a query
326
     *
327
     * The output depends on the PHP data type of input and the database
328
     * type being used.
329
     *
330
     * @param mixed $in  the data to be formatted
331
     *
332
     * @return mixed  the formatted data.  The format depends on the input's
333
     *                 PHP type:
334
     * <ul>
335
     *  <li>
336
     *    <kbd>input</kbd> -> <samp>returns</samp>
337
     *  </li>
338
     *  <li>
339
     *    <kbd>null</kbd> -> the string <samp>NULL</samp>
340
     *  </li>
341
     *  <li>
342
     *    <kbd>integer</kbd> or <kbd>double</kbd> -> the unquoted number
343
     *  </li>
344
     *  <li>
345
     *    <kbd>bool</kbd> -> output depends on the driver in use
346
     *    Most drivers return integers: <samp>1</samp> if
347
     *    <kbd>true</kbd> or <samp>0</samp> if
348
     *    <kbd>false</kbd>.
349
     *    Some return strings: <samp>TRUE</samp> if
350
     *    <kbd>true</kbd> or <samp>FALSE</samp> if
351
     *    <kbd>false</kbd>.
352
     *    Finally one returns strings: <samp>T</samp> if
353
     *    <kbd>true</kbd> or <samp>F</samp> if
354
     *    <kbd>false</kbd>. Here is a list of each DBMS,
355
     *    the values returned and the suggested column type:
356
     *    <ul>
357
     *      <li>
358
     *        <kbd>dbase</kbd> -> <samp>T/F</samp>
359
     *        (<kbd>Logical</kbd>)
360
     *      </li>
361
     *      <li>
362
     *        <kbd>fbase</kbd> -> <samp>TRUE/FALSE</samp>
363
     *        (<kbd>BOOLEAN</kbd>)
364
     *      </li>
365
     *      <li>
366
     *        <kbd>ibase</kbd> -> <samp>1/0</samp>
367
     *        (<kbd>SMALLINT</kbd>) [1]
368
     *      </li>
369
     *      <li>
370
     *        <kbd>ifx</kbd> -> <samp>1/0</samp>
371
     *        (<kbd>SMALLINT</kbd>) [1]
372
     *      </li>
373
     *      <li>
374
     *        <kbd>msql</kbd> -> <samp>1/0</samp>
375
     *        (<kbd>INTEGER</kbd>)
376
     *      </li>
377
     *      <li>
378
     *        <kbd>mssql</kbd> -> <samp>1/0</samp>
379
     *        (<kbd>BIT</kbd>)
380
     *      </li>
381
     *      <li>
382
     *        <kbd>mysql</kbd> -> <samp>1/0</samp>
383
     *        (<kbd>TINYINT(1)</kbd>)
384
     *      </li>
385
     *      <li>
386
     *        <kbd>mysqli</kbd> -> <samp>1/0</samp>
387
     *        (<kbd>TINYINT(1)</kbd>)
388
     *      </li>
389
     *      <li>
390
     *        <kbd>oci8</kbd> -> <samp>1/0</samp>
391
     *        (<kbd>NUMBER(1)</kbd>)
392
     *      </li>
393
     *      <li>
394
     *        <kbd>odbc</kbd> -> <samp>1/0</samp>
395
     *        (<kbd>SMALLINT</kbd>) [1]
396
     *      </li>
397
     *      <li>
398
     *        <kbd>pgsql</kbd> -> <samp>TRUE/FALSE</samp>
399
     *        (<kbd>BOOLEAN</kbd>)
400
     *      </li>
401
     *      <li>
402
     *        <kbd>sqlite</kbd> -> <samp>1/0</samp>
403
     *        (<kbd>INTEGER</kbd>)
404
     *      </li>
405
     *      <li>
406
     *        <kbd>sybase</kbd> -> <samp>1/0</samp>
407
     *        (<kbd>TINYINT(1)</kbd>)
408
     *      </li>
409
     *    </ul>
410
     *    [1] Accommodate the lowest common denominator because not all
411
     *    versions of have <kbd>BOOLEAN</kbd>.
412
     *  </li>
413
     *  <li>
414
     *    other (including strings and numeric strings) ->
415
     *    the data with single quotes escaped by preceeding
416
     *    single quotes, backslashes are escaped by preceeding
417
     *    backslashes, then the whole string is encapsulated
418
     *    between single quotes
419
     *  </li>
420
     * </ul>
421
     *
422
     * @see DB_common::escapeSimple()
423
     * @since Method available since Release 1.6.0
424
     */
425
    function quoteSmart($in)
426
    {
427
        if (is_int($in) || is_double($in)) {
428
            return $in;
429
        } elseif (is_bool($in)) {
430
            return $in ? 1 : 0;
431
        } elseif (is_null($in)) {
432
            return 'NULL';
433
        } else {
434
            return "'" . $this->escapeSimple($in) . "'";
435
        }
436
    }
437
 
438
    // }}}
439
    // {{{ escapeSimple()
440
 
441
    /**
442
     * Escapes a string according to the current DBMS's standards
443
     *
444
     * In SQLite, this makes things safe for inserts/updates, but may
445
     * cause problems when performing text comparisons against columns
446
     * containing binary data. See the
447
     * {@link http://php.net/sqlite_escape_string PHP manual} for more info.
448
     *
449
     * @param string $str  the string to be escaped
450
     *
451
     * @return string  the escaped string
452
     *
453
     * @see DB_common::quoteSmart()
454
     * @since Method available since Release 1.6.0
455
     */
456
    function escapeSimple($str)
457
    {
458
        return str_replace("'", "''", $str);
459
    }
460
 
461
    // }}}
462
    // {{{ provides()
463
 
464
    /**
465
     * Tells whether the present driver supports a given feature
466
     *
467
     * @param string $feature  the feature you're curious about
468
     *
469
     * @return bool  whether this driver supports $feature
470
     */
471
    function provides($feature)
472
    {
473
        return $this->features[$feature];
474
    }
475
 
476
    // }}}
477
    // {{{ setFetchMode()
478
 
479
    /**
480
     * Sets the fetch mode that should be used by default for query results
481
     *
482
     * @param integer $fetchmode   DB_FETCHMODE_ORDERED or DB_FETCHMODE_ASSOC,
483
     *                              possibly bit-wise OR'ed with
484
     *                              DB_FETCHMODE_FLIPPED
485
     *
486
     * @param string $object_class  the class name of the object to be returned
487
     *                               by the fetch methods when the
488
     *                               DB_FETCHMODE_OBJECT mode is selected.
489
     *                               If no class is specified by default a cast
490
     *                               to object from the assoc array row will be
491
     *                               done.  There is also the posibility to use
492
     *                               and extend the 'DB_row' class.
493
     *
494
     * @see DB_FETCHMODE_ORDERED, DB_FETCHMODE_ASSOC, DB_FETCHMODE_FLIPPED,
495
     *      DB_FETCHMODE_OBJECT
496
     */
497
    function setFetchMode($fetchmode, $object_class = 'stdClass')
498
    {
499
        switch ($fetchmode) {
500
            case DB_FETCHMODE_OBJECT:
501
                $this->fetchmode_object_class = $object_class;
502
            case DB_FETCHMODE_ORDERED:
503
            case DB_FETCHMODE_ASSOC:
504
                $this->fetchmode = $fetchmode;
505
                break;
506
            default:
507
                return $this->raiseError('invalid fetchmode mode');
508
        }
509
    }
510
 
511
    // }}}
512
    // {{{ setOption()
513
 
514
    /**
515
     * Sets run-time configuration options for PEAR DB
516
     *
517
     * Options, their data types, default values and description:
518
     * <ul>
519
     * <li>
520
     * <var>autofree</var> <kbd>boolean</kbd> = <samp>false</samp>
521
     *      <br />should results be freed automatically when there are no
522
     *            more rows?
523
     * </li><li>
524
     * <var>result_buffering</var> <kbd>integer</kbd> = <samp>500</samp>
525
     *      <br />how many rows of the result set should be buffered?
526
     *      <br />In mysql: mysql_unbuffered_query() is used instead of
527
     *            mysql_query() if this value is 0.  (Release 1.7.0)
528
     *      <br />In oci8: this value is passed to ocisetprefetch().
529
     *            (Release 1.7.0)
530
     * </li><li>
531
     * <var>debug</var> <kbd>integer</kbd> = <samp>0</samp>
532
     *      <br />debug level
533
     * </li><li>
534
     * <var>persistent</var> <kbd>boolean</kbd> = <samp>false</samp>
535
     *      <br />should the connection be persistent?
536
     * </li><li>
537
     * <var>portability</var> <kbd>integer</kbd> = <samp>DB_PORTABILITY_NONE</samp>
538
     *      <br />portability mode constant (see below)
539
     * </li><li>
540
     * <var>seqname_format</var> <kbd>string</kbd> = <samp>%s_seq</samp>
541
     *      <br />the sprintf() format string used on sequence names.  This
542
     *            format is applied to sequence names passed to
543
     *            createSequence(), nextID() and dropSequence().
544
     * </li><li>
545
     * <var>ssl</var> <kbd>boolean</kbd> = <samp>false</samp>
546
     *      <br />use ssl to connect?
547
     * </li>
548
     * </ul>
549
     *
550
     * -----------------------------------------
551
     *
552
     * PORTABILITY MODES
553
     *
554
     * These modes are bitwised, so they can be combined using <kbd>|</kbd>
555
     * and removed using <kbd>^</kbd>.  See the examples section below on how
556
     * to do this.
557
     *
558
     * <samp>DB_PORTABILITY_NONE</samp>
559
     * turn off all portability features
560
     *
561
     * This mode gets automatically turned on if the deprecated
562
     * <var>optimize</var> option gets set to <samp>performance</samp>.
563
     *
564
     *
565
     * <samp>DB_PORTABILITY_LOWERCASE</samp>
566
     * convert names of tables and fields to lower case when using
567
     * <kbd>get*()</kbd>, <kbd>fetch*()</kbd> and <kbd>tableInfo()</kbd>
568
     *
569
     * This mode gets automatically turned on in the following databases
570
     * if the deprecated option <var>optimize</var> gets set to
571
     * <samp>portability</samp>:
572
     * + oci8
573
     *
574
     *
575
     * <samp>DB_PORTABILITY_RTRIM</samp>
576
     * right trim the data output by <kbd>get*()</kbd> <kbd>fetch*()</kbd>
577
     *
578
     *
579
     * <samp>DB_PORTABILITY_DELETE_COUNT</samp>
580
     * force reporting the number of rows deleted
581
     *
582
     * Some DBMS's don't count the number of rows deleted when performing
583
     * simple <kbd>DELETE FROM tablename</kbd> queries.  This portability
584
     * mode tricks such DBMS's into telling the count by adding
585
     * <samp>WHERE 1=1</samp> to the end of <kbd>DELETE</kbd> queries.
586
     *
587
     * This mode gets automatically turned on in the following databases
588
     * if the deprecated option <var>optimize</var> gets set to
589
     * <samp>portability</samp>:
590
     * + fbsql
591
     * + mysql
592
     * + mysqli
593
     * + sqlite
594
     *
595
     *
596
     * <samp>DB_PORTABILITY_NUMROWS</samp>
597
     * enable hack that makes <kbd>numRows()</kbd> work in Oracle
598
     *
599
     * This mode gets automatically turned on in the following databases
600
     * if the deprecated option <var>optimize</var> gets set to
601
     * <samp>portability</samp>:
602
     * + oci8
603
     *
604
     *
605
     * <samp>DB_PORTABILITY_ERRORS</samp>
606
     * makes certain error messages in certain drivers compatible
607
     * with those from other DBMS's
608
     *
609
     * + mysql, mysqli:  change unique/primary key constraints
610
     *   DB_ERROR_ALREADY_EXISTS -> DB_ERROR_CONSTRAINT
611
     *
612
     * + odbc(access):  MS's ODBC driver reports 'no such field' as code
613
     *   07001, which means 'too few parameters.'  When this option is on
614
     *   that code gets mapped to DB_ERROR_NOSUCHFIELD.
615
     *   DB_ERROR_MISMATCH -> DB_ERROR_NOSUCHFIELD
616
     *
617
     * <samp>DB_PORTABILITY_NULL_TO_EMPTY</samp>
618
     * convert null values to empty strings in data output by get*() and
619
     * fetch*().  Needed because Oracle considers empty strings to be null,
620
     * while most other DBMS's know the difference between empty and null.
621
     *
622
     *
623
     * <samp>DB_PORTABILITY_ALL</samp>
624
     * turn on all portability features
625
     *
626
     * -----------------------------------------
627
     *
628
     * Example 1. Simple setOption() example
629
     * <code>
630
     * $db->setOption('autofree', true);
631
     * </code>
632
     *
633
     * Example 2. Portability for lowercasing and trimming
634
     * <code>
635
     * $db->setOption('portability',
636
     *                 DB_PORTABILITY_LOWERCASE | DB_PORTABILITY_RTRIM);
637
     * </code>
638
     *
639
     * Example 3. All portability options except trimming
640
     * <code>
641
     * $db->setOption('portability',
642
     *                 DB_PORTABILITY_ALL ^ DB_PORTABILITY_RTRIM);
643
     * </code>
644
     *
645
     * @param string $option option name
646
     * @param mixed  $value value for the option
647
     *
648
     * @return int  DB_OK on success.  A DB_Error object on failure.
649
     *
650
     * @see DB_common::$options
651
     */
652
    function setOption($option, $value)
653
    {
654
        if (isset($this->options[$option])) {
655
            $this->options[$option] = $value;
656
 
657
            /*
658
             * Backwards compatibility check for the deprecated 'optimize'
659
             * option.  Done here in case settings change after connecting.
660
             */
661
            if ($option == 'optimize') {
662
                if ($value == 'portability') {
663
                    switch ($this->phptype) {
664
                        case 'oci8':
665
                            $this->options['portability'] =
666
                                    DB_PORTABILITY_LOWERCASE |
667
                                    DB_PORTABILITY_NUMROWS;
668
                            break;
669
                        case 'fbsql':
670
                        case 'mysql':
671
                        case 'mysqli':
672
                        case 'sqlite':
673
                            $this->options['portability'] =
674
                                    DB_PORTABILITY_DELETE_COUNT;
675
                            break;
676
                    }
677
                } else {
678
                    $this->options['portability'] = DB_PORTABILITY_NONE;
679
                }
680
            }
681
 
682
            return DB_OK;
683
        }
684
        return $this->raiseError("unknown option $option");
685
    }
686
 
687
    // }}}
688
    // {{{ getOption()
689
 
690
    /**
691
     * Returns the value of an option
692
     *
693
     * @param string $option  the option name you're curious about
694
     *
695
     * @return mixed  the option's value
696
     */
697
    function getOption($option)
698
    {
699
        if (isset($this->options[$option])) {
700
            return $this->options[$option];
701
        }
702
        return $this->raiseError("unknown option $option");
703
    }
704
 
705
    // }}}
706
    // {{{ prepare()
707
 
708
    /**
709
     * Prepares a query for multiple execution with execute()
710
     *
711
     * Creates a query that can be run multiple times.  Each time it is run,
712
     * the placeholders, if any, will be replaced by the contents of
713
     * execute()'s $data argument.
714
     *
715
     * Three types of placeholders can be used:
716
     *   + <kbd>?</kbd>  scalar value (i.e. strings, integers).  The system
717
     *                   will automatically quote and escape the data.
718
     *   + <kbd>!</kbd>  value is inserted 'as is'
719
     *   + <kbd>&</kbd>  requires a file name.  The file's contents get
720
     *                   inserted into the query (i.e. saving binary
721
     *                   data in a db)
722
     *
723
     * Example 1.
724
     * <code>
725
     * $sth = $db->prepare('INSERT INTO tbl (a, b, c) VALUES (?, !, &)');
726
     * $data = array(
727
     *     "John's text",
728
     *     "'it''s good'",
729
     *     'filename.txt'
730
     * );
731
     * $res = $db->execute($sth, $data);
732
     * </code>
733
     *
734
     * Use backslashes to escape placeholder characters if you don't want
735
     * them to be interpreted as placeholders:
736
     * <pre>
737
     *    "UPDATE foo SET col=? WHERE col='over \& under'"
738
     * </pre>
739
     *
740
     * With some database backends, this is emulated.
741
     *
742
     * {@internal ibase and oci8 have their own prepare() methods.}}
743
     *
744
     * @param string $query  the query to be prepared
745
     *
746
     * @return mixed  DB statement resource on success. A DB_Error object
747
     *                 on failure.
748
     *
749
     * @see DB_common::execute()
750
     */
751
    function prepare($query)
752
    {
753
        $tokens   = preg_split('/((?<!\\\)[&?!])/', $query, -1,
754
                               PREG_SPLIT_DELIM_CAPTURE);
755
        $token     = 0;
756
        $types     = array();
757
        $newtokens = array();
758
 
759
        foreach ($tokens as $val) {
760
            switch ($val) {
761
                case '?':
762
                    $types[$token++] = DB_PARAM_SCALAR;
763
                    break;
764
                case '&':
765
                    $types[$token++] = DB_PARAM_OPAQUE;
766
                    break;
767
                case '!':
768
                    $types[$token++] = DB_PARAM_MISC;
769
                    break;
770
                default:
771
                    $newtokens[] = preg_replace('/\\\([&?!])/', "\\1", $val);
772
            }
773
        }
774
 
775
        $this->prepare_tokens[] = &$newtokens;
776
        end($this->prepare_tokens);
777
 
778
        $k = key($this->prepare_tokens);
779
        $this->prepare_types[$k] = $types;
780
        $this->prepared_queries[$k] = implode(' ', $newtokens);
781
 
782
        return $k;
783
    }
784
 
785
    // }}}
786
    // {{{ autoPrepare()
787
 
788
    /**
789
     * Automaticaly generates an insert or update query and pass it to prepare()
790
     *
791
     * @param string $table         the table name
792
     * @param array  $table_fields  the array of field names
793
     * @param int    $mode          a type of query to make:
794
     *                               DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE
795
     * @param string $where         for update queries: the WHERE clause to
796
     *                               append to the SQL statement.  Don't
797
     *                               include the "WHERE" keyword.
798
     *
799
     * @return resource  the query handle
800
     *
801
     * @uses DB_common::prepare(), DB_common::buildManipSQL()
802
     */
803
    function autoPrepare($table, $table_fields, $mode = DB_AUTOQUERY_INSERT,
804
                         $where = false)
805
    {
806
        $query = $this->buildManipSQL($table, $table_fields, $mode, $where);
807
        if (DB::isError($query)) {
808
            return $query;
809
        }
810
        return $this->prepare($query);
811
    }
812
 
813
    // }}}
814
    // {{{ autoExecute()
815
 
816
    /**
817
     * Automaticaly generates an insert or update query and call prepare()
818
     * and execute() with it
819
     *
820
     * @param string $table         the table name
821
     * @param array  $fields_values the associative array where $key is a
822
     *                               field name and $value its value
823
     * @param int    $mode          a type of query to make:
824
     *                               DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE
825
     * @param string $where         for update queries: the WHERE clause to
826
     *                               append to the SQL statement.  Don't
827
     *                               include the "WHERE" keyword.
828
     *
829
     * @return mixed  a new DB_result object for successful SELECT queries
830
     *                 or DB_OK for successul data manipulation queries.
831
     *                 A DB_Error object on failure.
832
     *
833
     * @uses DB_common::autoPrepare(), DB_common::execute()
834
     */
835
    function autoExecute($table, $fields_values, $mode = DB_AUTOQUERY_INSERT,
836
                         $where = false)
837
    {
838
        $sth = $this->autoPrepare($table, array_keys($fields_values), $mode,
839
                                  $where);
840
        if (DB::isError($sth)) {
841
            return $sth;
842
        }
843
        $ret =& $this->execute($sth, array_values($fields_values));
844
        $this->freePrepared($sth);
845
        return $ret;
846
 
847
    }
848
 
849
    // }}}
850
    // {{{ buildManipSQL()
851
 
852
    /**
853
     * Produces an SQL query string for autoPrepare()
854
     *
855
     * Example:
856
     * <pre>
857
     * buildManipSQL('table_sql', array('field1', 'field2', 'field3'),
858
     *               DB_AUTOQUERY_INSERT);
859
     * </pre>
860
     *
861
     * That returns
862
     * <samp>
863
     * INSERT INTO table_sql (field1,field2,field3) VALUES (?,?,?)
864
     * </samp>
865
     *
866
     * NOTES:
867
     *   - This belongs more to a SQL Builder class, but this is a simple
868
     *     facility.
869
     *   - Be carefull! If you don't give a $where param with an UPDATE
870
     *     query, all the records of the table will be updated!
871
     *
872
     * @param string $table         the table name
873
     * @param array  $table_fields  the array of field names
874
     * @param int    $mode          a type of query to make:
875
     *                               DB_AUTOQUERY_INSERT or DB_AUTOQUERY_UPDATE
876
     * @param string $where         for update queries: the WHERE clause to
877
     *                               append to the SQL statement.  Don't
878
     *                               include the "WHERE" keyword.
879
     *
880
     * @return string  the sql query for autoPrepare()
881
     */
882
    function buildManipSQL($table, $table_fields, $mode, $where = false)
883
    {
884
        if (count($table_fields) == 0) {
885
            return $this->raiseError(DB_ERROR_NEED_MORE_DATA);
886
        }
887
        $first = true;
888
        switch ($mode) {
889
            case DB_AUTOQUERY_INSERT:
890
                $values = '';
891
                $names = '';
892
                foreach ($table_fields as $value) {
893
                    if ($first) {
894
                        $first = false;
895
                    } else {
896
                        $names .= ',';
897
                        $values .= ',';
898
                    }
899
                    $names .= $value;
900
                    $values .= '?';
901
                }
902
                return "INSERT INTO $table ($names) VALUES ($values)";
903
            case DB_AUTOQUERY_UPDATE:
904
                $set = '';
905
                foreach ($table_fields as $value) {
906
                    if ($first) {
907
                        $first = false;
908
                    } else {
909
                        $set .= ',';
910
                    }
911
                    $set .= "$value = ?";
912
                }
913
                $sql = "UPDATE $table SET $set";
914
                if ($where) {
915
                    $sql .= " WHERE $where";
916
                }
917
                return $sql;
918
            default:
919
                return $this->raiseError(DB_ERROR_SYNTAX);
920
        }
921
    }
922
 
923
    // }}}
924
    // {{{ execute()
925
 
926
    /**
927
     * Executes a DB statement prepared with prepare()
928
     *
929
     * Example 1.
930
     * <code>
931
     * $sth = $db->prepare('INSERT INTO tbl (a, b, c) VALUES (?, !, &)');
932
     * $data = array(
933
     *     "John's text",
934
     *     "'it''s good'",
935
     *     'filename.txt'
936
     * );
937
     * $res =& $db->execute($sth, $data);
938
     * </code>
939
     *
940
     * @param resource $stmt  a DB statement resource returned from prepare()
941
     * @param mixed    $data  array, string or numeric data to be used in
942
     *                         execution of the statement.  Quantity of items
943
     *                         passed must match quantity of placeholders in
944
     *                         query:  meaning 1 placeholder for non-array
945
     *                         parameters or 1 placeholder per array element.
946
     *
947
     * @return mixed  a new DB_result object for successful SELECT queries
948
     *                 or DB_OK for successul data manipulation queries.
949
     *                 A DB_Error object on failure.
950
     *
951
     * {@internal ibase and oci8 have their own execute() methods.}}
952
     *
953
     * @see DB_common::prepare()
954
     */
955
    function &execute($stmt, $data = array())
956
    {
957
        $realquery = $this->executeEmulateQuery($stmt, $data);
958
        if (DB::isError($realquery)) {
959
            return $realquery;
960
        }
961
        $result = $this->simpleQuery($realquery);
962
 
963
        if ($result === DB_OK || DB::isError($result)) {
964
            return $result;
965
        } else {
966
            $tmp =& new DB_result($this, $result);
967
            return $tmp;
968
        }
969
    }
970
 
971
    // }}}
972
    // {{{ executeEmulateQuery()
973
 
974
    /**
975
     * Emulates executing prepared statements if the DBMS not support them
976
     *
977
     * @param resource $stmt  a DB statement resource returned from execute()
978
     * @param mixed    $data  array, string or numeric data to be used in
979
     *                         execution of the statement.  Quantity of items
980
     *                         passed must match quantity of placeholders in
981
     *                         query:  meaning 1 placeholder for non-array
982
     *                         parameters or 1 placeholder per array element.
983
     *
984
     * @return mixed  a string containing the real query run when emulating
985
     *                 prepare/execute.  A DB_Error object on failure.
986
     *
987
     * @access protected
988
     * @see DB_common::execute()
989
     */
990
    function executeEmulateQuery($stmt, $data = array())
991
    {
992
        $stmt = (int)$stmt;
993
        $data = (array)$data;
994
        $this->last_parameters = $data;
995
 
996
        if (count($this->prepare_types[$stmt]) != count($data)) {
997
            $this->last_query = $this->prepared_queries[$stmt];
998
            return $this->raiseError(DB_ERROR_MISMATCH);
999
        }
1000
 
1001
        $realquery = $this->prepare_tokens[$stmt][0];
1002
 
1003
        $i = 0;
1004
        foreach ($data as $value) {
1005
            if ($this->prepare_types[$stmt][$i] == DB_PARAM_SCALAR) {
1006
                $realquery .= $this->quoteSmart($value);
1007
            } elseif ($this->prepare_types[$stmt][$i] == DB_PARAM_OPAQUE) {
1008
                $fp = @fopen($value, 'rb');
1009
                if (!$fp) {
1010
                    return $this->raiseError(DB_ERROR_ACCESS_VIOLATION);
1011
                }
1012
                $realquery .= $this->quoteSmart(fread($fp, filesize($value)));
1013
                fclose($fp);
1014
            } else {
1015
                $realquery .= $value;
1016
            }
1017
 
1018
            $realquery .= $this->prepare_tokens[$stmt][++$i];
1019
        }
1020
 
1021
        return $realquery;
1022
    }
1023
 
1024
    // }}}
1025
    // {{{ executeMultiple()
1026
 
1027
    /**
1028
     * Performs several execute() calls on the same statement handle
1029
     *
1030
     * $data must be an array indexed numerically
1031
     * from 0, one execute call is done for every "row" in the array.
1032
     *
1033
     * If an error occurs during execute(), executeMultiple() does not
1034
     * execute the unfinished rows, but rather returns that error.
1035
     *
1036
     * @param resource $stmt  query handle from prepare()
1037
     * @param array    $data  numeric array containing the
1038
     *                         data to insert into the query
1039
     *
1040
     * @return int  DB_OK on success.  A DB_Error object on failure.
1041
     *
1042
     * @see DB_common::prepare(), DB_common::execute()
1043
     */
1044
    function executeMultiple($stmt, $data)
1045
    {
1046
        foreach ($data as $value) {
1047
            $res =& $this->execute($stmt, $value);
1048
            if (DB::isError($res)) {
1049
                return $res;
1050
            }
1051
        }
1052
        return DB_OK;
1053
    }
1054
 
1055
    // }}}
1056
    // {{{ freePrepared()
1057
 
1058
    /**
1059
     * Frees the internal resources associated with a prepared query
1060
     *
1061
     * @param resource $stmt           the prepared statement's PHP resource
1062
     * @param bool     $free_resource  should the PHP resource be freed too?
1063
     *                                  Use false if you need to get data
1064
     *                                  from the result set later.
1065
     *
1066
     * @return bool  TRUE on success, FALSE if $result is invalid
1067
     *
1068
     * @see DB_common::prepare()
1069
     */
1070
    function freePrepared($stmt, $free_resource = true)
1071
    {
1072
        $stmt = (int)$stmt;
1073
        if (isset($this->prepare_tokens[$stmt])) {
1074
            unset($this->prepare_tokens[$stmt]);
1075
            unset($this->prepare_types[$stmt]);
1076
            unset($this->prepared_queries[$stmt]);
1077
            return true;
1078
        }
1079
        return false;
1080
    }
1081
 
1082
    // }}}
1083
    // {{{ modifyQuery()
1084
 
1085
    /**
1086
     * Changes a query string for various DBMS specific reasons
1087
     *
1088
     * It is defined here to ensure all drivers have this method available.
1089
     *
1090
     * @param string $query  the query string to modify
1091
     *
1092
     * @return string  the modified query string
1093
     *
1094
     * @access protected
1095
     * @see DB_mysql::modifyQuery(), DB_oci8::modifyQuery(),
1096
     *      DB_sqlite::modifyQuery()
1097
     */
1098
    function modifyQuery($query)
1099
    {
1100
        return $query;
1101
    }
1102
 
1103
    // }}}
1104
    // {{{ modifyLimitQuery()
1105
 
1106
    /**
1107
     * Adds LIMIT clauses to a query string according to current DBMS standards
1108
     *
1109
     * It is defined here to assure that all implementations
1110
     * have this method defined.
1111
     *
1112
     * @param string $query   the query to modify
1113
     * @param int    $from    the row to start to fetching (0 = the first row)
1114
     * @param int    $count   the numbers of rows to fetch
1115
     * @param mixed  $params  array, string or numeric data to be used in
1116
     *                         execution of the statement.  Quantity of items
1117
     *                         passed must match quantity of placeholders in
1118
     *                         query:  meaning 1 placeholder for non-array
1119
     *                         parameters or 1 placeholder per array element.
1120
     *
1121
     * @return string  the query string with LIMIT clauses added
1122
     *
1123
     * @access protected
1124
     */
1125
    function modifyLimitQuery($query, $from, $count, $params = array())
1126
    {
1127
        return $query;
1128
    }
1129
 
1130
    // }}}
1131
    // {{{ query()
1132
 
1133
    /**
1134
     * Sends a query to the database server
1135
     *
1136
     * The query string can be either a normal statement to be sent directly
1137
     * to the server OR if <var>$params</var> are passed the query can have
1138
     * placeholders and it will be passed through prepare() and execute().
1139
     *
1140
     * @param string $query   the SQL query or the statement to prepare
1141
     * @param mixed  $params  array, string or numeric data to be used in
1142
     *                         execution of the statement.  Quantity of items
1143
     *                         passed must match quantity of placeholders in
1144
     *                         query:  meaning 1 placeholder for non-array
1145
     *                         parameters or 1 placeholder per array element.
1146
     *
1147
     * @return mixed  a new DB_result object for successful SELECT queries
1148
     *                 or DB_OK for successul data manipulation queries.
1149
     *                 A DB_Error object on failure.
1150
     *
1151
     * @see DB_result, DB_common::prepare(), DB_common::execute()
1152
     */
1153
    function &query($query, $params = array())
1154
    {
1155
        if (sizeof($params) > 0) {
1156
            $sth = $this->prepare($query);
1157
            if (DB::isError($sth)) {
1158
                return $sth;
1159
            }
1160
            $ret =& $this->execute($sth, $params);
1161
            $this->freePrepared($sth, false);
1162
            return $ret;
1163
        } else {
1164
            $this->last_parameters = array();
1165
            $result = $this->simpleQuery($query);
1166
            if ($result === DB_OK || DB::isError($result)) {
1167
                return $result;
1168
            } else {
1169
                $tmp =& new DB_result($this, $result);
1170
                return $tmp;
1171
            }
1172
        }
1173
    }
1174
 
1175
    // }}}
1176
    // {{{ limitQuery()
1177
 
1178
    /**
1179
     * Generates and executes a LIMIT query
1180
     *
1181
     * @param string $query   the query
1182
     * @param intr   $from    the row to start to fetching (0 = the first row)
1183
     * @param int    $count   the numbers of rows to fetch
1184
     * @param mixed  $params  array, string or numeric data to be used in
1185
     *                         execution of the statement.  Quantity of items
1186
     *                         passed must match quantity of placeholders in
1187
     *                         query:  meaning 1 placeholder for non-array
1188
     *                         parameters or 1 placeholder per array element.
1189
     *
1190
     * @return mixed  a new DB_result object for successful SELECT queries
1191
     *                 or DB_OK for successul data manipulation queries.
1192
     *                 A DB_Error object on failure.
1193
     */
1194
    function &limitQuery($query, $from, $count, $params = array())
1195
    {
1196
        $query = $this->modifyLimitQuery($query, $from, $count, $params);
1197
        if (DB::isError($query)){
1198
            return $query;
1199
        }
1200
        $result =& $this->query($query, $params);
1201
        if (is_a($result, 'DB_result')) {
1202
            $result->setOption('limit_from', $from);
1203
            $result->setOption('limit_count', $count);
1204
        }
1205
        return $result;
1206
    }
1207
 
1208
    // }}}
1209
    // {{{ getOne()
1210
 
1211
    /**
1212
     * Fetches the first column of the first row from a query result
1213
     *
1214
     * Takes care of doing the query and freeing the results when finished.
1215
     *
1216
     * @param string $query   the SQL query
1217
     * @param mixed  $params  array, string or numeric data to be used in
1218
     *                         execution of the statement.  Quantity of items
1219
     *                         passed must match quantity of placeholders in
1220
     *                         query:  meaning 1 placeholder for non-array
1221
     *                         parameters or 1 placeholder per array element.
1222
     *
1223
     * @return mixed  the returned value of the query.
1224
     *                 A DB_Error object on failure.
1225
     */
1226
    function &getOne($query, $params = array())
1227
    {
1228
        $params = (array)$params;
1229
        // modifyLimitQuery() would be nice here, but it causes BC issues
1230
        if (sizeof($params) > 0) {
1231
            $sth = $this->prepare($query);
1232
            if (DB::isError($sth)) {
1233
                return $sth;
1234
            }
1235
            $res =& $this->execute($sth, $params);
1236
            $this->freePrepared($sth);
1237
        } else {
1238
            $res =& $this->query($query);
1239
        }
1240
 
1241
        if (DB::isError($res)) {
1242
            return $res;
1243
        }
1244
 
1245
        $err = $res->fetchInto($row, DB_FETCHMODE_ORDERED);
1246
        $res->free();
1247
 
1248
        if ($err !== DB_OK) {
1249
            return $err;
1250
        }
1251
 
1252
        return $row[0];
1253
    }
1254
 
1255
    // }}}
1256
    // {{{ getRow()
1257
 
1258
    /**
1259
     * Fetches the first row of data returned from a query result
1260
     *
1261
     * Takes care of doing the query and freeing the results when finished.
1262
     *
1263
     * @param string $query   the SQL query
1264
     * @param mixed  $params  array, string or numeric data to be used in
1265
     *                         execution of the statement.  Quantity of items
1266
     *                         passed must match quantity of placeholders in
1267
     *                         query:  meaning 1 placeholder for non-array
1268
     *                         parameters or 1 placeholder per array element.
1269
     * @param int $fetchmode  the fetch mode to use
1270
     *
1271
     * @return array  the first row of results as an array.
1272
     *                 A DB_Error object on failure.
1273
     */
1274
    function &getRow($query, $params = array(),
1275
                     $fetchmode = DB_FETCHMODE_DEFAULT)
1276
    {
1277
        // compat check, the params and fetchmode parameters used to
1278
        // have the opposite order
1279
        if (!is_array($params)) {
1280
            if (is_array($fetchmode)) {
1281
                if ($params === null) {
1282
                    $tmp = DB_FETCHMODE_DEFAULT;
1283
                } else {
1284
                    $tmp = $params;
1285
                }
1286
                $params = $fetchmode;
1287
                $fetchmode = $tmp;
1288
            } elseif ($params !== null) {
1289
                $fetchmode = $params;
1290
                $params = array();
1291
            }
1292
        }
1293
        // modifyLimitQuery() would be nice here, but it causes BC issues
1294
        if (sizeof($params) > 0) {
1295
            $sth = $this->prepare($query);
1296
            if (DB::isError($sth)) {
1297
                return $sth;
1298
            }
1299
            $res =& $this->execute($sth, $params);
1300
            $this->freePrepared($sth);
1301
        } else {
1302
            $res =& $this->query($query);
1303
        }
1304
 
1305
        if (DB::isError($res)) {
1306
            return $res;
1307
        }
1308
 
1309
        $err = $res->fetchInto($row, $fetchmode);
1310
 
1311
        $res->free();
1312
 
1313
        if ($err !== DB_OK) {
1314
            return $err;
1315
        }
1316
 
1317
        return $row;
1318
    }
1319
 
1320
    // }}}
1321
    // {{{ getCol()
1322
 
1323
    /**
1324
     * Fetches a single column from a query result and returns it as an
1325
     * indexed array
1326
     *
1327
     * @param string $query   the SQL query
1328
     * @param mixed  $col     which column to return (integer [column number,
1329
     *                         starting at 0] or string [column name])
1330
     * @param mixed  $params  array, string or numeric data to be used in
1331
     *                         execution of the statement.  Quantity of items
1332
     *                         passed must match quantity of placeholders in
1333
     *                         query:  meaning 1 placeholder for non-array
1334
     *                         parameters or 1 placeholder per array element.
1335
     *
1336
     * @return array  the results as an array.  A DB_Error object on failure.
1337
     *
1338
     * @see DB_common::query()
1339
     */
1340
    function &getCol($query, $col = 0, $params = array())
1341
    {
1342
        $params = (array)$params;
1343
        if (sizeof($params) > 0) {
1344
            $sth = $this->prepare($query);
1345
 
1346
            if (DB::isError($sth)) {
1347
                return $sth;
1348
            }
1349
 
1350
            $res =& $this->execute($sth, $params);
1351
            $this->freePrepared($sth);
1352
        } else {
1353
            $res =& $this->query($query);
1354
        }
1355
 
1356
        if (DB::isError($res)) {
1357
            return $res;
1358
        }
1359
 
1360
        $fetchmode = is_int($col) ? DB_FETCHMODE_ORDERED : DB_FETCHMODE_ASSOC;
1361
 
1362
        if (!is_array($row = $res->fetchRow($fetchmode))) {
1363
            $ret = array();
1364
        } else {
1365
            if (!array_key_exists($col, $row)) {
1366
                $ret =& $this->raiseError(DB_ERROR_NOSUCHFIELD);
1367
            } else {
1368
                $ret = array($row[$col]);
1369
                while (is_array($row = $res->fetchRow($fetchmode))) {
1370
                    $ret[] = $row[$col];
1371
                }
1372
            }
1373
        }
1374
 
1375
        $res->free();
1376
 
1377
        if (DB::isError($row)) {
1378
            $ret = $row;
1379
        }
1380
 
1381
        return $ret;
1382
    }
1383
 
1384
    // }}}
1385
    // {{{ getAssoc()
1386
 
1387
    /**
1388
     * Fetches an entire query result and returns it as an
1389
     * associative array using the first column as the key
1390
     *
1391
     * If the result set contains more than two columns, the value
1392
     * will be an array of the values from column 2-n.  If the result
1393
     * set contains only two columns, the returned value will be a
1394
     * scalar with the value of the second column (unless forced to an
1395
     * array with the $force_array parameter).  A DB error code is
1396
     * returned on errors.  If the result set contains fewer than two
1397
     * columns, a DB_ERROR_TRUNCATED error is returned.
1398
     *
1399
     * For example, if the table "mytable" contains:
1400
     *
1401
     * <pre>
1402
     *  ID      TEXT       DATE
1403
     * --------------------------------
1404
     *  1       'one'      944679408
1405
     *  2       'two'      944679408
1406
     *  3       'three'    944679408
1407
     * </pre>
1408
     *
1409
     * Then the call getAssoc('SELECT id,text FROM mytable') returns:
1410
     * <pre>
1411
     *   array(
1412
     *     '1' => 'one',
1413
     *     '2' => 'two',
1414
     *     '3' => 'three',
1415
     *   )
1416
     * </pre>
1417
     *
1418
     * ...while the call getAssoc('SELECT id,text,date FROM mytable') returns:
1419
     * <pre>
1420
     *   array(
1421
     *     '1' => array('one', '944679408'),
1422
     *     '2' => array('two', '944679408'),
1423
     *     '3' => array('three', '944679408')
1424
     *   )
1425
     * </pre>
1426
     *
1427
     * If the more than one row occurs with the same value in the
1428
     * first column, the last row overwrites all previous ones by
1429
     * default.  Use the $group parameter if you don't want to
1430
     * overwrite like this.  Example:
1431
     *
1432
     * <pre>
1433
     * getAssoc('SELECT category,id,name FROM mytable', false, null,
1434
     *          DB_FETCHMODE_ASSOC, true) returns:
1435
     *
1436
     *   array(
1437
     *     '1' => array(array('id' => '4', 'name' => 'number four'),
1438
     *                  array('id' => '6', 'name' => 'number six')
1439
     *            ),
1440
     *     '9' => array(array('id' => '4', 'name' => 'number four'),
1441
     *                  array('id' => '6', 'name' => 'number six')
1442
     *            )
1443
     *   )
1444
     * </pre>
1445
     *
1446
     * Keep in mind that database functions in PHP usually return string
1447
     * values for results regardless of the database's internal type.
1448
     *
1449
     * @param string $query        the SQL query
1450
     * @param bool   $force_array  used only when the query returns
1451
     *                              exactly two columns.  If true, the values
1452
     *                              of the returned array will be one-element
1453
     *                              arrays instead of scalars.
1454
     * @param mixed  $params       array, string or numeric data to be used in
1455
     *                              execution of the statement.  Quantity of
1456
     *                              items passed must match quantity of
1457
     *                              placeholders in query:  meaning 1
1458
     *                              placeholder for non-array parameters or
1459
     *                              1 placeholder per array element.
1460
     * @param int   $fetchmode     the fetch mode to use
1461
     * @param bool  $group         if true, the values of the returned array
1462
     *                              is wrapped in another array.  If the same
1463
     *                              key value (in the first column) repeats
1464
     *                              itself, the values will be appended to
1465
     *                              this array instead of overwriting the
1466
     *                              existing values.
1467
     *
1468
     * @return array  the associative array containing the query results.
1469
     *                A DB_Error object on failure.
1470
     */
1471
    function &getAssoc($query, $force_array = false, $params = array(),
1472
                       $fetchmode = DB_FETCHMODE_DEFAULT, $group = false)
1473
    {
1474
        $params = (array)$params;
1475
        if (sizeof($params) > 0) {
1476
            $sth = $this->prepare($query);
1477
 
1478
            if (DB::isError($sth)) {
1479
                return $sth;
1480
            }
1481
 
1482
            $res =& $this->execute($sth, $params);
1483
            $this->freePrepared($sth);
1484
        } else {
1485
            $res =& $this->query($query);
1486
        }
1487
 
1488
        if (DB::isError($res)) {
1489
            return $res;
1490
        }
1491
        if ($fetchmode == DB_FETCHMODE_DEFAULT) {
1492
            $fetchmode = $this->fetchmode;
1493
        }
1494
        $cols = $res->numCols();
1495
 
1496
        if ($cols < 2) {
1497
            $tmp =& $this->raiseError(DB_ERROR_TRUNCATED);
1498
            return $tmp;
1499
        }
1500
 
1501
        $results = array();
1502
 
1503
        if ($cols > 2 || $force_array) {
1504
            // return array values
1505
            // XXX this part can be optimized
1506
            if ($fetchmode == DB_FETCHMODE_ASSOC) {
1507
                while (is_array($row = $res->fetchRow(DB_FETCHMODE_ASSOC))) {
1508
                    reset($row);
1509
                    $key = current($row);
1510
                    unset($row[key($row)]);
1511
                    if ($group) {
1512
                        $results[$key][] = $row;
1513
                    } else {
1514
                        $results[$key] = $row;
1515
                    }
1516
                }
1517
            } elseif ($fetchmode == DB_FETCHMODE_OBJECT) {
1518
                while ($row = $res->fetchRow(DB_FETCHMODE_OBJECT)) {
1519
                    $arr = get_object_vars($row);
1520
                    $key = current($arr);
1521
                    if ($group) {
1522
                        $results[$key][] = $row;
1523
                    } else {
1524
                        $results[$key] = $row;
1525
                    }
1526
                }
1527
            } else {
1528
                while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
1529
                    // we shift away the first element to get
1530
                    // indices running from 0 again
1531
                    $key = array_shift($row);
1532
                    if ($group) {
1533
                        $results[$key][] = $row;
1534
                    } else {
1535
                        $results[$key] = $row;
1536
                    }
1537
                }
1538
            }
1539
            if (DB::isError($row)) {
1540
                $results = $row;
1541
            }
1542
        } else {
1543
            // return scalar values
1544
            while (is_array($row = $res->fetchRow(DB_FETCHMODE_ORDERED))) {
1545
                if ($group) {
1546
                    $results[$row[0]][] = $row[1];
1547
                } else {
1548
                    $results[$row[0]] = $row[1];
1549
                }
1550
            }
1551
            if (DB::isError($row)) {
1552
                $results = $row;
1553
            }
1554
        }
1555
 
1556
        $res->free();
1557
 
1558
        return $results;
1559
    }
1560
 
1561
    // }}}
1562
    // {{{ getAll()
1563
 
1564
    /**
1565
     * Fetches all of the rows from a query result
1566
     *
1567
     * @param string $query      the SQL query
1568
     * @param mixed  $params     array, string or numeric data to be used in
1569
     *                            execution of the statement.  Quantity of
1570
     *                            items passed must match quantity of
1571
     *                            placeholders in query:  meaning 1
1572
     *                            placeholder for non-array parameters or
1573
     *                            1 placeholder per array element.
1574
     * @param int    $fetchmode  the fetch mode to use
1575
     *
1576
     * @return array  the nested array.  A DB_Error object on failure.
1577
     */
1578
    function &getAll($query, $params = array(),
1579
                     $fetchmode = DB_FETCHMODE_DEFAULT)
1580
    {
1581
        // compat check, the params and fetchmode parameters used to
1582
        // have the opposite order
1583
        if (!is_array($params)) {
1584
            if (is_array($fetchmode)) {
1585
                if ($params === null) {
1586
                    $tmp = DB_FETCHMODE_DEFAULT;
1587
                } else {
1588
                    $tmp = $params;
1589
                }
1590
                $params = $fetchmode;
1591
                $fetchmode = $tmp;
1592
            } elseif ($params !== null) {
1593
                $fetchmode = $params;
1594
                $params = array();
1595
            }
1596
        }
1597
 
1598
        if (sizeof($params) > 0) {
1599
            $sth = $this->prepare($query);
1600
 
1601
            if (DB::isError($sth)) {
1602
                return $sth;
1603
            }
1604
 
1605
            $res =& $this->execute($sth, $params);
1606
            $this->freePrepared($sth);
1607
        } else {
1608
            $res =& $this->query($query);
1609
        }
1610
 
1611
        if ($res === DB_OK || DB::isError($res)) {
1612
            return $res;
1613
        }
1614
 
1615
        $results = array();
1616
        while (DB_OK === $res->fetchInto($row, $fetchmode)) {
1617
            if ($fetchmode & DB_FETCHMODE_FLIPPED) {
1618
                foreach ($row as $key => $val) {
1619
                    $results[$key][] = $val;
1620
                }
1621
            } else {
1622
                $results[] = $row;
1623
            }
1624
        }
1625
 
1626
        $res->free();
1627
 
1628
        if (DB::isError($row)) {
1629
            $tmp =& $this->raiseError($row);
1630
            return $tmp;
1631
        }
1632
        return $results;
1633
    }
1634
 
1635
    // }}}
1636
    // {{{ autoCommit()
1637
 
1638
    /**
1639
     * Enables or disables automatic commits
1640
     *
1641
     * @param bool $onoff  true turns it on, false turns it off
1642
     *
1643
     * @return int  DB_OK on success.  A DB_Error object if the driver
1644
     *               doesn't support auto-committing transactions.
1645
     */
1646
    function autoCommit($onoff = false)
1647
    {
1648
        return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1649
    }
1650
 
1651
    // }}}
1652
    // {{{ commit()
1653
 
1654
    /**
1655
     * Commits the current transaction
1656
     *
1657
     * @return int  DB_OK on success.  A DB_Error object on failure.
1658
     */
1659
    function commit()
1660
    {
1661
        return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1662
    }
1663
 
1664
    // }}}
1665
    // {{{ rollback()
1666
 
1667
    /**
1668
     * Reverts the current transaction
1669
     *
1670
     * @return int  DB_OK on success.  A DB_Error object on failure.
1671
     */
1672
    function rollback()
1673
    {
1674
        return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1675
    }
1676
 
1677
    // }}}
1678
    // {{{ numRows()
1679
 
1680
    /**
1681
     * Determines the number of rows in a query result
1682
     *
1683
     * @param resource $result  the query result idenifier produced by PHP
1684
     *
1685
     * @return int  the number of rows.  A DB_Error object on failure.
1686
     */
1687
    function numRows($result)
1688
    {
1689
        return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1690
    }
1691
 
1692
    // }}}
1693
    // {{{ affectedRows()
1694
 
1695
    /**
1696
     * Determines the number of rows affected by a data maniuplation query
1697
     *
1698
     * 0 is returned for queries that don't manipulate data.
1699
     *
1700
     * @return int  the number of rows.  A DB_Error object on failure.
1701
     */
1702
    function affectedRows()
1703
    {
1704
        return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1705
    }
1706
 
1707
    // }}}
1708
    // {{{ getSequenceName()
1709
 
1710
    /**
1711
     * Generates the name used inside the database for a sequence
1712
     *
1713
     * The createSequence() docblock contains notes about storing sequence
1714
     * names.
1715
     *
1716
     * @param string $sqn  the sequence's public name
1717
     *
1718
     * @return string  the sequence's name in the backend
1719
     *
1720
     * @access protected
1721
     * @see DB_common::createSequence(), DB_common::dropSequence(),
1722
     *      DB_common::nextID(), DB_common::setOption()
1723
     */
1724
    function getSequenceName($sqn)
1725
    {
1726
        return sprintf($this->getOption('seqname_format'),
1727
                       preg_replace('/[^a-z0-9_.]/i', '_', $sqn));
1728
    }
1729
 
1730
    // }}}
1731
    // {{{ nextId()
1732
 
1733
    /**
1734
     * Returns the next free id in a sequence
1735
     *
1736
     * @param string  $seq_name  name of the sequence
1737
     * @param boolean $ondemand  when true, the seqence is automatically
1738
     *                            created if it does not exist
1739
     *
1740
     * @return int  the next id number in the sequence.
1741
     *               A DB_Error object on failure.
1742
     *
1743
     * @see DB_common::createSequence(), DB_common::dropSequence(),
1744
     *      DB_common::getSequenceName()
1745
     */
1746
    function nextId($seq_name, $ondemand = true)
1747
    {
1748
        return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1749
    }
1750
 
1751
    // }}}
1752
    // {{{ createSequence()
1753
 
1754
    /**
1755
     * Creates a new sequence
1756
     *
1757
     * The name of a given sequence is determined by passing the string
1758
     * provided in the <var>$seq_name</var> argument through PHP's sprintf()
1759
     * function using the value from the <var>seqname_format</var> option as
1760
     * the sprintf()'s format argument.
1761
     *
1762
     * <var>seqname_format</var> is set via setOption().
1763
     *
1764
     * @param string $seq_name  name of the new sequence
1765
     *
1766
     * @return int  DB_OK on success.  A DB_Error object on failure.
1767
     *
1768
     * @see DB_common::dropSequence(), DB_common::getSequenceName(),
1769
     *      DB_common::nextID()
1770
     */
1771
    function createSequence($seq_name)
1772
    {
1773
        return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1774
    }
1775
 
1776
    // }}}
1777
    // {{{ dropSequence()
1778
 
1779
    /**
1780
     * Deletes a sequence
1781
     *
1782
     * @param string $seq_name  name of the sequence to be deleted
1783
     *
1784
     * @return int  DB_OK on success.  A DB_Error object on failure.
1785
     *
1786
     * @see DB_common::createSequence(), DB_common::getSequenceName(),
1787
     *      DB_common::nextID()
1788
     */
1789
    function dropSequence($seq_name)
1790
    {
1791
        return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1792
    }
1793
 
1794
    // }}}
1795
    // {{{ raiseError()
1796
 
1797
    /**
1798
     * Communicates an error and invoke error callbacks, etc
1799
     *
1800
     * Basically a wrapper for PEAR::raiseError without the message string.
1801
     *
1802
     * @param mixed   integer error code, or a PEAR error object (all
1803
     *                 other parameters are ignored if this parameter is
1804
     *                 an object
1805
     * @param int     error mode, see PEAR_Error docs
1806
     * @param mixed   if error mode is PEAR_ERROR_TRIGGER, this is the
1807
     *                 error level (E_USER_NOTICE etc).  If error mode is
1808
     *                 PEAR_ERROR_CALLBACK, this is the callback function,
1809
     *                 either as a function name, or as an array of an
1810
     *                 object and method name.  For other error modes this
1811
     *                 parameter is ignored.
1812
     * @param string  extra debug information.  Defaults to the last
1813
     *                 query and native error code.
1814
     * @param mixed   native error code, integer or string depending the
1815
     *                 backend
1816
     *
1817
     * @return object  the PEAR_Error object
1818
     *
1819
     * @see PEAR_Error
1820
     */
1821
    function &raiseError($code = DB_ERROR, $mode = null, $options = null,
1822
                         $userinfo = null, $nativecode = null)
1823
    {
1824
        // The error is yet a DB error object
1825
        if (is_object($code)) {
1826
            // because we the static PEAR::raiseError, our global
1827
            // handler should be used if it is set
1828
            if ($mode === null && !empty($this->_default_error_mode)) {
1829
                $mode    = $this->_default_error_mode;
1830
                $options = $this->_default_error_options;
1831
            }
1832
            $tmp = PEAR::raiseError($code, null, $mode, $options,
1833
                                    null, null, true);
1834
            return $tmp;
1835
        }
1836
 
1837
        if ($userinfo === null) {
1838
            $userinfo = $this->last_query;
1839
        }
1840
 
1841
        if ($nativecode) {
1842
            $userinfo .= ' [nativecode=' . trim($nativecode) . ']';
1843
        } else {
1844
            $userinfo .= ' [DB Error: ' . DB::errorMessage($code) . ']';
1845
        }
1846
 
1847
        $tmp = PEAR::raiseError(null, $code, $mode, $options, $userinfo,
1848
                                'DB_Error', true);
1849
        return $tmp;
1850
    }
1851
 
1852
    // }}}
1853
    // {{{ errorNative()
1854
 
1855
    /**
1856
     * Gets the DBMS' native error code produced by the last query
1857
     *
1858
     * @return mixed  the DBMS' error code.  A DB_Error object on failure.
1859
     */
1860
    function errorNative()
1861
    {
1862
        return $this->raiseError(DB_ERROR_NOT_CAPABLE);
1863
    }
1864
 
1865
    // }}}
1866
    // {{{ errorCode()
1867
 
1868
    /**
1869
     * Maps native error codes to DB's portable ones
1870
     *
1871
     * Uses the <var>$errorcode_map</var> property defined in each driver.
1872
     *
1873
     * @param string|int $nativecode  the error code returned by the DBMS
1874
     *
1875
     * @return int  the portable DB error code.  Return DB_ERROR if the
1876
     *               current driver doesn't have a mapping for the
1877
     *               $nativecode submitted.
1878
     */
1879
    function errorCode($nativecode)
1880
    {
1881
        if (isset($this->errorcode_map[$nativecode])) {
1882
            return $this->errorcode_map[$nativecode];
1883
        }
1884
        // Fall back to DB_ERROR if there was no mapping.
1885
        return DB_ERROR;
1886
    }
1887
 
1888
    // }}}
1889
    // {{{ errorMessage()
1890
 
1891
    /**
1892
     * Maps a DB error code to a textual message
1893
     *
1894
     * @param integer $dbcode  the DB error code
1895
     *
1896
     * @return string  the error message corresponding to the error code
1897
     *                  submitted.  FALSE if the error code is unknown.
1898
     *
1899
     * @see DB::errorMessage()
1900
     */
1901
    function errorMessage($dbcode)
1902
    {
1903
        return DB::errorMessage($this->errorcode_map[$dbcode]);
1904
    }
1905
 
1906
    // }}}
1907
    // {{{ tableInfo()
1908
 
1909
    /**
1910
     * Returns information about a table or a result set
1911
     *
1912
     * The format of the resulting array depends on which <var>$mode</var>
1913
     * you select.  The sample output below is based on this query:
1914
     * <pre>
1915
     *    SELECT tblFoo.fldID, tblFoo.fldPhone, tblBar.fldId
1916
     *    FROM tblFoo
1917
     *    JOIN tblBar ON tblFoo.fldId = tblBar.fldId
1918
     * </pre>
1919
     *
1920
     * <ul>
1921
     * <li>
1922
     *
1923
     * <kbd>null</kbd> (default)
1924
     *   <pre>
1925
     *   [0] => Array (
1926
     *       [table] => tblFoo
1927
     *       [name] => fldId
1928
     *       [type] => int
1929
     *       [len] => 11
1930
     *       [flags] => primary_key not_null
1931
     *   )
1932
     *   [1] => Array (
1933
     *       [table] => tblFoo
1934
     *       [name] => fldPhone
1935
     *       [type] => string
1936
     *       [len] => 20
1937
     *       [flags] =>
1938
     *   )
1939
     *   [2] => Array (
1940
     *       [table] => tblBar
1941
     *       [name] => fldId
1942
     *       [type] => int
1943
     *       [len] => 11
1944
     *       [flags] => primary_key not_null
1945
     *   )
1946
     *   </pre>
1947
     *
1948
     * </li><li>
1949
     *
1950
     * <kbd>DB_TABLEINFO_ORDER</kbd>
1951
     *
1952
     *   <p>In addition to the information found in the default output,
1953
     *   a notation of the number of columns is provided by the
1954
     *   <samp>num_fields</samp> element while the <samp>order</samp>
1955
     *   element provides an array with the column names as the keys and
1956
     *   their location index number (corresponding to the keys in the
1957
     *   the default output) as the values.</p>
1958
     *
1959
     *   <p>If a result set has identical field names, the last one is
1960
     *   used.</p>
1961
     *
1962
     *   <pre>
1963
     *   [num_fields] => 3
1964
     *   [order] => Array (
1965
     *       [fldId] => 2
1966
     *       [fldTrans] => 1
1967
     *   )
1968
     *   </pre>
1969
     *
1970
     * </li><li>
1971
     *
1972
     * <kbd>DB_TABLEINFO_ORDERTABLE</kbd>
1973
     *
1974
     *   <p>Similar to <kbd>DB_TABLEINFO_ORDER</kbd> but adds more
1975
     *   dimensions to the array in which the table names are keys and
1976
     *   the field names are sub-keys.  This is helpful for queries that
1977
     *   join tables which have identical field names.</p>
1978
     *
1979
     *   <pre>
1980
     *   [num_fields] => 3
1981
     *   [ordertable] => Array (
1982
     *       [tblFoo] => Array (
1983
     *           [fldId] => 0
1984
     *           [fldPhone] => 1
1985
     *       )
1986
     *       [tblBar] => Array (
1987
     *           [fldId] => 2
1988
     *       )
1989
     *   )
1990
     *   </pre>
1991
     *
1992
     * </li>
1993
     * </ul>
1994
     *
1995
     * The <samp>flags</samp> element contains a space separated list
1996
     * of extra information about the field.  This data is inconsistent
1997
     * between DBMS's due to the way each DBMS works.
1998
     *   + <samp>primary_key</samp>
1999
     *   + <samp>unique_key</samp>
2000
     *   + <samp>multiple_key</samp>
2001
     *   + <samp>not_null</samp>
2002
     *
2003
     * Most DBMS's only provide the <samp>table</samp> and <samp>flags</samp>
2004
     * elements if <var>$result</var> is a table name.  The following DBMS's
2005
     * provide full information from queries:
2006
     *   + fbsql
2007
     *   + mysql
2008
     *
2009
     * If the 'portability' option has <samp>DB_PORTABILITY_LOWERCASE</samp>
2010
     * turned on, the names of tables and fields will be lowercased.
2011
     *
2012
     * @param object|string  $result  DB_result object from a query or a
2013
     *                                string containing the name of a table.
2014
     *                                While this also accepts a query result
2015
     *                                resource identifier, this behavior is
2016
     *                                deprecated.
2017
     * @param int  $mode   either unused or one of the tableInfo modes:
2018
     *                     <kbd>DB_TABLEINFO_ORDERTABLE</kbd>,
2019
     *                     <kbd>DB_TABLEINFO_ORDER</kbd> or
2020
     *                     <kbd>DB_TABLEINFO_FULL</kbd> (which does both).
2021
     *                     These are bitwise, so the first two can be
2022
     *                     combined using <kbd>|</kbd>.
2023
     *
2024
     * @return array  an associative array with the information requested.
2025
     *                 A DB_Error object on failure.
2026
     *
2027
     * @see DB_common::setOption()
2028
     */
2029
    function tableInfo($result, $mode = null)
2030
    {
2031
        /*
2032
         * If the DB_<driver> class has a tableInfo() method, that one
2033
         * overrides this one.  But, if the driver doesn't have one,
2034
         * this method runs and tells users about that fact.
2035
         */
2036
        return $this->raiseError(DB_ERROR_NOT_CAPABLE);
2037
    }
2038
 
2039
    // }}}
2040
    // {{{ getTables()
2041
 
2042
    /**
2043
     * Lists the tables in the current database
2044
     *
2045
     * @return array  the list of tables.  A DB_Error object on failure.
2046
     *
2047
     * @deprecated Method deprecated some time before Release 1.2
2048
     */
2049
    function getTables()
2050
    {
2051
        return $this->getListOf('tables');
2052
    }
2053
 
2054
    // }}}
2055
    // {{{ getListOf()
2056
 
2057
    /**
2058
     * Lists internal database information
2059
     *
2060
     * @param string $type  type of information being sought.
2061
     *                       Common items being sought are:
2062
     *                       tables, databases, users, views, functions
2063
     *                       Each DBMS's has its own capabilities.
2064
     *
2065
     * @return array  an array listing the items sought.
2066
     *                 A DB DB_Error object on failure.
2067
     */
2068
    function getListOf($type)
2069
    {
2070
        $sql = $this->getSpecialQuery($type);
2071
        if ($sql === null) {
2072
            $this->last_query = '';
2073
            return $this->raiseError(DB_ERROR_UNSUPPORTED);
2074
        } elseif (is_int($sql) || DB::isError($sql)) {
2075
            // Previous error
2076
            return $this->raiseError($sql);
2077
        } elseif (is_array($sql)) {
2078
            // Already the result
2079
            return $sql;
2080
        }
2081
        // Launch this query
2082
        return $this->getCol($sql);
2083
    }
2084
 
2085
    // }}}
2086
    // {{{ getSpecialQuery()
2087
 
2088
    /**
2089
     * Obtains the query string needed for listing a given type of objects
2090
     *
2091
     * @param string $type  the kind of objects you want to retrieve
2092
     *
2093
     * @return string  the SQL query string or null if the driver doesn't
2094
     *                  support the object type requested
2095
     *
2096
     * @access protected
2097
     * @see DB_common::getListOf()
2098
     */
2099
    function getSpecialQuery($type)
2100
    {
2101
        return $this->raiseError(DB_ERROR_UNSUPPORTED);
2102
    }
2103
 
2104
    // }}}
2105
    // {{{ _rtrimArrayValues()
2106
 
2107
    /**
2108
     * Right-trims all strings in an array
2109
     *
2110
     * @param array $array  the array to be trimmed (passed by reference)
2111
     *
2112
     * @return void
2113
     *
2114
     * @access protected
2115
     */
2116
    function _rtrimArrayValues(&$array)
2117
    {
2118
        foreach ($array as $key => $value) {
2119
            if (is_string($value)) {
2120
                $array[$key] = rtrim($value);
2121
            }
2122
        }
2123
    }
2124
 
2125
    // }}}
2126
    // {{{ _convertNullArrayValuesToEmpty()
2127
 
2128
    /**
2129
     * Converts all null values in an array to empty strings
2130
     *
2131
     * @param array  $array  the array to be de-nullified (passed by reference)
2132
     *
2133
     * @return void
2134
     *
2135
     * @access protected
2136
     */
2137
    function _convertNullArrayValuesToEmpty(&$array)
2138
    {
2139
        foreach ($array as $key => $value) {
2140
            if (is_null($value)) {
2141
                $array[$key] = '';
2142
            }
2143
        }
2144
    }
2145
 
2146
    // }}}
2147
}
2148
 
2149
/*
2150
 * Local variables:
2151
 * tab-width: 4
2152
 * c-basic-offset: 4
2153
 * End:
2154
 */
2155
 
2156
?>