Subversion Repositories Applications.papyrus

Rev

Rev 320 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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