Subversion Repositories Applications.gtt

Rev

Rev 94 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 187
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
 * Database independent query interface
6
 * Database independent query interface
7
 *
7
 *
8
 * PHP versions 4 and 5
8
 * PHP version 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-2007 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: DB.php,v 1.80 2005/02/16 02:16:00 danielc Exp $
23
 * @version    CVS: $Id$
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
// {{{ constants
33
// {{{ constants
34
// {{{ error codes
34
// {{{ error codes
35
 
35
 
36
/**#@+
36
/**#@+
37
 * One of PEAR DB's portable error codes.
37
 * One of PEAR DB's portable error codes.
38
 * @see DB_common::errorCode(), DB::errorMessage()
38
 * @see DB_common::errorCode(), DB::errorMessage()
39
 *
39
 *
40
 * {@internal If you add an error code here, make sure you also add a textual
40
 * {@internal If you add an error code here, make sure you also add a textual
41
 * version of it in DB::errorMessage().}}
41
 * version of it in DB::errorMessage().}}
42
 */
42
 */
43
 
43
 
44
/**
44
/**
45
 * The code returned by many methods upon success
45
 * The code returned by many methods upon success
46
 */
46
 */
47
define('DB_OK', 1);
47
define('DB_OK', 1);
48
 
48
 
49
/**
49
/**
50
 * Unkown error
50
 * Unkown error
51
 */
51
 */
52
define('DB_ERROR', -1);
52
define('DB_ERROR', -1);
53
 
53
 
54
/**
54
/**
55
 * Syntax error
55
 * Syntax error
56
 */
56
 */
57
define('DB_ERROR_SYNTAX', -2);
57
define('DB_ERROR_SYNTAX', -2);
58
 
58
 
59
/**
59
/**
60
 * Tried to insert a duplicate value into a primary or unique index
60
 * Tried to insert a duplicate value into a primary or unique index
61
 */
61
 */
62
define('DB_ERROR_CONSTRAINT', -3);
62
define('DB_ERROR_CONSTRAINT', -3);
63
 
63
 
64
/**
64
/**
65
 * An identifier in the query refers to a non-existant object
65
 * An identifier in the query refers to a non-existant object
66
 */
66
 */
67
define('DB_ERROR_NOT_FOUND', -4);
67
define('DB_ERROR_NOT_FOUND', -4);
68
 
68
 
69
/**
69
/**
70
 * Tried to create a duplicate object
70
 * Tried to create a duplicate object
71
 */
71
 */
72
define('DB_ERROR_ALREADY_EXISTS', -5);
72
define('DB_ERROR_ALREADY_EXISTS', -5);
73
 
73
 
74
/**
74
/**
75
 * The current driver does not support the action you attempted
75
 * The current driver does not support the action you attempted
76
 */
76
 */
77
define('DB_ERROR_UNSUPPORTED', -6);
77
define('DB_ERROR_UNSUPPORTED', -6);
78
 
78
 
79
/**
79
/**
80
 * The number of parameters does not match the number of placeholders
80
 * The number of parameters does not match the number of placeholders
81
 */
81
 */
82
define('DB_ERROR_MISMATCH', -7);
82
define('DB_ERROR_MISMATCH', -7);
83
 
83
 
84
/**
84
/**
85
 * A literal submitted did not match the data type expected
85
 * A literal submitted did not match the data type expected
86
 */
86
 */
87
define('DB_ERROR_INVALID', -8);
87
define('DB_ERROR_INVALID', -8);
88
 
88
 
89
/**
89
/**
90
 * The current DBMS does not support the action you attempted
90
 * The current DBMS does not support the action you attempted
91
 */
91
 */
92
define('DB_ERROR_NOT_CAPABLE', -9);
92
define('DB_ERROR_NOT_CAPABLE', -9);
93
 
93
 
94
/**
94
/**
95
 * A literal submitted was too long so the end of it was removed
95
 * A literal submitted was too long so the end of it was removed
96
 */
96
 */
97
define('DB_ERROR_TRUNCATED', -10);
97
define('DB_ERROR_TRUNCATED', -10);
98
 
98
 
99
/**
99
/**
100
 * A literal number submitted did not match the data type expected
100
 * A literal number submitted did not match the data type expected
101
 */
101
 */
102
define('DB_ERROR_INVALID_NUMBER', -11);
102
define('DB_ERROR_INVALID_NUMBER', -11);
103
 
103
 
104
/**
104
/**
105
 * A literal date submitted did not match the data type expected
105
 * A literal date submitted did not match the data type expected
106
 */
106
 */
107
define('DB_ERROR_INVALID_DATE', -12);
107
define('DB_ERROR_INVALID_DATE', -12);
108
 
108
 
109
/**
109
/**
110
 * Attempt to divide something by zero
110
 * Attempt to divide something by zero
111
 */
111
 */
112
define('DB_ERROR_DIVZERO', -13);
112
define('DB_ERROR_DIVZERO', -13);
113
 
113
 
114
/**
114
/**
115
 * A database needs to be selected
115
 * A database needs to be selected
116
 */
116
 */
117
define('DB_ERROR_NODBSELECTED', -14);
117
define('DB_ERROR_NODBSELECTED', -14);
118
 
118
 
119
/**
119
/**
120
 * Could not create the object requested
120
 * Could not create the object requested
121
 */
121
 */
122
define('DB_ERROR_CANNOT_CREATE', -15);
122
define('DB_ERROR_CANNOT_CREATE', -15);
123
 
123
 
124
/**
124
/**
125
 * Could not drop the database requested because it does not exist
125
 * Could not drop the database requested because it does not exist
126
 */
126
 */
127
define('DB_ERROR_CANNOT_DROP', -17);
127
define('DB_ERROR_CANNOT_DROP', -17);
128
 
128
 
129
/**
129
/**
130
 * An identifier in the query refers to a non-existant table
130
 * An identifier in the query refers to a non-existant table
131
 */
131
 */
132
define('DB_ERROR_NOSUCHTABLE', -18);
132
define('DB_ERROR_NOSUCHTABLE', -18);
133
 
133
 
134
/**
134
/**
135
 * An identifier in the query refers to a non-existant column
135
 * An identifier in the query refers to a non-existant column
136
 */
136
 */
137
define('DB_ERROR_NOSUCHFIELD', -19);
137
define('DB_ERROR_NOSUCHFIELD', -19);
138
 
138
 
139
/**
139
/**
140
 * The data submitted to the method was inappropriate
140
 * The data submitted to the method was inappropriate
141
 */
141
 */
142
define('DB_ERROR_NEED_MORE_DATA', -20);
142
define('DB_ERROR_NEED_MORE_DATA', -20);
143
 
143
 
144
/**
144
/**
145
 * The attempt to lock the table failed
145
 * The attempt to lock the table failed
146
 */
146
 */
147
define('DB_ERROR_NOT_LOCKED', -21);
147
define('DB_ERROR_NOT_LOCKED', -21);
148
 
148
 
149
/**
149
/**
150
 * The number of columns doesn't match the number of values
150
 * The number of columns doesn't match the number of values
151
 */
151
 */
152
define('DB_ERROR_VALUE_COUNT_ON_ROW', -22);
152
define('DB_ERROR_VALUE_COUNT_ON_ROW', -22);
153
 
153
 
154
/**
154
/**
155
 * The DSN submitted has problems
155
 * The DSN submitted has problems
156
 */
156
 */
157
define('DB_ERROR_INVALID_DSN', -23);
157
define('DB_ERROR_INVALID_DSN', -23);
158
 
158
 
159
/**
159
/**
160
 * Could not connect to the database
160
 * Could not connect to the database
161
 */
161
 */
162
define('DB_ERROR_CONNECT_FAILED', -24);
162
define('DB_ERROR_CONNECT_FAILED', -24);
163
 
163
 
164
/**
164
/**
165
 * The PHP extension needed for this DBMS could not be found
165
 * The PHP extension needed for this DBMS could not be found
166
 */
166
 */
167
define('DB_ERROR_EXTENSION_NOT_FOUND',-25);
167
define('DB_ERROR_EXTENSION_NOT_FOUND',-25);
168
 
168
 
169
/**
169
/**
170
 * The present user has inadequate permissions to perform the task requestd
170
 * The present user has inadequate permissions to perform the task requestd
171
 */
171
 */
172
define('DB_ERROR_ACCESS_VIOLATION', -26);
172
define('DB_ERROR_ACCESS_VIOLATION', -26);
173
 
173
 
174
/**
174
/**
175
 * The database requested does not exist
175
 * The database requested does not exist
176
 */
176
 */
177
define('DB_ERROR_NOSUCHDB', -27);
177
define('DB_ERROR_NOSUCHDB', -27);
178
 
178
 
179
/**
179
/**
180
 * Tried to insert a null value into a column that doesn't allow nulls
180
 * Tried to insert a null value into a column that doesn't allow nulls
181
 */
181
 */
182
define('DB_ERROR_CONSTRAINT_NOT_NULL',-29);
182
define('DB_ERROR_CONSTRAINT_NOT_NULL',-29);
183
/**#@-*/
183
/**#@-*/
184
 
184
 
185
 
185
 
186
// }}}
186
// }}}
187
// {{{ prepared statement-related
187
// {{{ prepared statement-related
188
 
188
 
189
 
189
 
190
/**#@+
190
/**#@+
191
 * Identifiers for the placeholders used in prepared statements.
191
 * Identifiers for the placeholders used in prepared statements.
192
 * @see DB_common::prepare()
192
 * @see DB_common::prepare()
193
 */
193
 */
194
 
194
 
195
/**
195
/**
196
 * Indicates a scalar (<kbd>?</kbd>) placeholder was used
196
 * Indicates a scalar (<kbd>?</kbd>) placeholder was used
197
 *
197
 *
198
 * Quote and escape the value as necessary.
198
 * Quote and escape the value as necessary.
199
 */
199
 */
200
define('DB_PARAM_SCALAR', 1);
200
define('DB_PARAM_SCALAR', 1);
201
 
201
 
202
/**
202
/**
203
 * Indicates an opaque (<kbd>&</kbd>) placeholder was used
203
 * Indicates an opaque (<kbd>&</kbd>) placeholder was used
204
 *
204
 *
205
 * The value presented is a file name.  Extract the contents of that file
205
 * The value presented is a file name.  Extract the contents of that file
206
 * and place them in this column.
206
 * and place them in this column.
207
 */
207
 */
208
define('DB_PARAM_OPAQUE', 2);
208
define('DB_PARAM_OPAQUE', 2);
209
 
209
 
210
/**
210
/**
211
 * Indicates a misc (<kbd>!</kbd>) placeholder was used
211
 * Indicates a misc (<kbd>!</kbd>) placeholder was used
212
 *
212
 *
213
 * The value should not be quoted or escaped.
213
 * The value should not be quoted or escaped.
214
 */
214
 */
215
define('DB_PARAM_MISC',   3);
215
define('DB_PARAM_MISC',   3);
216
/**#@-*/
216
/**#@-*/
217
 
217
 
218
 
218
 
219
// }}}
219
// }}}
220
// {{{ binary data-related
220
// {{{ binary data-related
221
 
221
 
222
 
222
 
223
/**#@+
223
/**#@+
224
 * The different ways of returning binary data from queries.
224
 * The different ways of returning binary data from queries.
225
 */
225
 */
226
 
226
 
227
/**
227
/**
228
 * Sends the fetched data straight through to output
228
 * Sends the fetched data straight through to output
229
 */
229
 */
230
define('DB_BINMODE_PASSTHRU', 1);
230
define('DB_BINMODE_PASSTHRU', 1);
231
 
231
 
232
/**
232
/**
233
 * Lets you return data as usual
233
 * Lets you return data as usual
234
 */
234
 */
235
define('DB_BINMODE_RETURN', 2);
235
define('DB_BINMODE_RETURN', 2);
236
 
236
 
237
/**
237
/**
238
 * Converts the data to hex format before returning it
238
 * Converts the data to hex format before returning it
239
 *
239
 *
240
 * For example the string "123" would become "313233".
240
 * For example the string "123" would become "313233".
241
 */
241
 */
242
define('DB_BINMODE_CONVERT', 3);
242
define('DB_BINMODE_CONVERT', 3);
243
/**#@-*/
243
/**#@-*/
244
 
244
 
245
 
245
 
246
// }}}
246
// }}}
247
// {{{ fetch modes
247
// {{{ fetch modes
248
 
248
 
249
 
249
 
250
/**#@+
250
/**#@+
251
 * Fetch Modes.
251
 * Fetch Modes.
252
 * @see DB_common::setFetchMode()
252
 * @see DB_common::setFetchMode()
253
 */
253
 */
254
 
254
 
255
/**
255
/**
256
 * Indicates the current default fetch mode should be used
256
 * Indicates the current default fetch mode should be used
257
 * @see DB_common::$fetchmode
257
 * @see DB_common::$fetchmode
258
 */
258
 */
259
define('DB_FETCHMODE_DEFAULT', 0);
259
define('DB_FETCHMODE_DEFAULT', 0);
260
 
260
 
261
/**
261
/**
262
 * Column data indexed by numbers, ordered from 0 and up
262
 * Column data indexed by numbers, ordered from 0 and up
263
 */
263
 */
264
define('DB_FETCHMODE_ORDERED', 1);
264
define('DB_FETCHMODE_ORDERED', 1);
265
 
265
 
266
/**
266
/**
267
 * Column data indexed by column names
267
 * Column data indexed by column names
268
 */
268
 */
269
define('DB_FETCHMODE_ASSOC', 2);
269
define('DB_FETCHMODE_ASSOC', 2);
270
 
270
 
271
/**
271
/**
272
 * Column data as object properties
272
 * Column data as object properties
273
 */
273
 */
274
define('DB_FETCHMODE_OBJECT', 3);
274
define('DB_FETCHMODE_OBJECT', 3);
275
 
275
 
276
/**
276
/**
277
 * For multi-dimensional results, make the column name the first level
277
 * For multi-dimensional results, make the column name the first level
278
 * of the array and put the row number in the second level of the array
278
 * of the array and put the row number in the second level of the array
279
 *
279
 *
280
 * This is flipped from the normal behavior, which puts the row numbers
280
 * This is flipped from the normal behavior, which puts the row numbers
281
 * in the first level of the array and the column names in the second level.
281
 * in the first level of the array and the column names in the second level.
282
 */
282
 */
283
define('DB_FETCHMODE_FLIPPED', 4);
283
define('DB_FETCHMODE_FLIPPED', 4);
284
/**#@-*/
284
/**#@-*/
285
 
285
 
286
/**#@+
286
/**#@+
287
 * Old fetch modes.  Left here for compatibility.
287
 * Old fetch modes.  Left here for compatibility.
288
 */
288
 */
289
define('DB_GETMODE_ORDERED', DB_FETCHMODE_ORDERED);
289
define('DB_GETMODE_ORDERED', DB_FETCHMODE_ORDERED);
290
define('DB_GETMODE_ASSOC',   DB_FETCHMODE_ASSOC);
290
define('DB_GETMODE_ASSOC',   DB_FETCHMODE_ASSOC);
291
define('DB_GETMODE_FLIPPED', DB_FETCHMODE_FLIPPED);
291
define('DB_GETMODE_FLIPPED', DB_FETCHMODE_FLIPPED);
292
/**#@-*/
292
/**#@-*/
293
 
293
 
294
 
294
 
295
// }}}
295
// }}}
296
// {{{ tableInfo() && autoPrepare()-related
296
// {{{ tableInfo() && autoPrepare()-related
297
 
297
 
298
 
298
 
299
/**#@+
299
/**#@+
300
 * The type of information to return from the tableInfo() method.
300
 * The type of information to return from the tableInfo() method.
301
 *
301
 *
302
 * Bitwised constants, so they can be combined using <kbd>|</kbd>
302
 * Bitwised constants, so they can be combined using <kbd>|</kbd>
303
 * and removed using <kbd>^</kbd>.
303
 * and removed using <kbd>^</kbd>.
304
 *
304
 *
305
 * @see DB_common::tableInfo()
305
 * @see DB_common::tableInfo()
306
 *
306
 *
307
 * {@internal Since the TABLEINFO constants are bitwised, if more of them are
307
 * {@internal Since the TABLEINFO constants are bitwised, if more of them are
308
 * added in the future, make sure to adjust DB_TABLEINFO_FULL accordingly.}}
308
 * added in the future, make sure to adjust DB_TABLEINFO_FULL accordingly.}}
309
 */
309
 */
310
define('DB_TABLEINFO_ORDER', 1);
310
define('DB_TABLEINFO_ORDER', 1);
311
define('DB_TABLEINFO_ORDERTABLE', 2);
311
define('DB_TABLEINFO_ORDERTABLE', 2);
312
define('DB_TABLEINFO_FULL', 3);
312
define('DB_TABLEINFO_FULL', 3);
313
/**#@-*/
313
/**#@-*/
314
 
314
 
315
 
315
 
316
/**#@+
316
/**#@+
317
 * The type of query to create with the automatic query building methods.
317
 * The type of query to create with the automatic query building methods.
318
 * @see DB_common::autoPrepare(), DB_common::autoExecute()
318
 * @see DB_common::autoPrepare(), DB_common::autoExecute()
319
 */
319
 */
320
define('DB_AUTOQUERY_INSERT', 1);
320
define('DB_AUTOQUERY_INSERT', 1);
321
define('DB_AUTOQUERY_UPDATE', 2);
321
define('DB_AUTOQUERY_UPDATE', 2);
322
/**#@-*/
322
/**#@-*/
323
 
323
 
324
 
324
 
325
// }}}
325
// }}}
326
// {{{ portability modes
326
// {{{ portability modes
327
 
327
 
328
 
328
 
329
/**#@+
329
/**#@+
330
 * Portability Modes.
330
 * Portability Modes.
331
 *
331
 *
332
 * Bitwised constants, so they can be combined using <kbd>|</kbd>
332
 * Bitwised constants, so they can be combined using <kbd>|</kbd>
333
 * and removed using <kbd>^</kbd>.
333
 * and removed using <kbd>^</kbd>.
334
 *
334
 *
335
 * @see DB_common::setOption()
335
 * @see DB_common::setOption()
336
 *
336
 *
337
 * {@internal Since the PORTABILITY constants are bitwised, if more of them are
337
 * {@internal Since the PORTABILITY constants are bitwised, if more of them are
338
 * added in the future, make sure to adjust DB_PORTABILITY_ALL accordingly.}}
338
 * added in the future, make sure to adjust DB_PORTABILITY_ALL accordingly.}}
339
 */
339
 */
340
 
340
 
341
/**
341
/**
342
 * Turn off all portability features
342
 * Turn off all portability features
343
 */
343
 */
344
define('DB_PORTABILITY_NONE', 0);
344
define('DB_PORTABILITY_NONE', 0);
345
 
345
 
346
/**
346
/**
347
 * Convert names of tables and fields to lower case
347
 * Convert names of tables and fields to lower case
348
 * when using the get*(), fetch*() and tableInfo() methods
348
 * when using the get*(), fetch*() and tableInfo() methods
349
 */
349
 */
350
define('DB_PORTABILITY_LOWERCASE', 1);
350
define('DB_PORTABILITY_LOWERCASE', 1);
351
 
351
 
352
/**
352
/**
353
 * Right trim the data output by get*() and fetch*()
353
 * Right trim the data output by get*() and fetch*()
354
 */
354
 */
355
define('DB_PORTABILITY_RTRIM', 2);
355
define('DB_PORTABILITY_RTRIM', 2);
356
 
356
 
357
/**
357
/**
358
 * Force reporting the number of rows deleted
358
 * Force reporting the number of rows deleted
359
 */
359
 */
360
define('DB_PORTABILITY_DELETE_COUNT', 4);
360
define('DB_PORTABILITY_DELETE_COUNT', 4);
361
 
361
 
362
/**
362
/**
363
 * Enable hack that makes numRows() work in Oracle
363
 * Enable hack that makes numRows() work in Oracle
364
 */
364
 */
365
define('DB_PORTABILITY_NUMROWS', 8);
365
define('DB_PORTABILITY_NUMROWS', 8);
366
 
366
 
367
/**
367
/**
368
 * Makes certain error messages in certain drivers compatible
368
 * Makes certain error messages in certain drivers compatible
369
 * with those from other DBMS's
369
 * with those from other DBMS's
370
 *
370
 *
371
 * + mysql, mysqli:  change unique/primary key constraints
371
 * + mysql, mysqli:  change unique/primary key constraints
372
 *   DB_ERROR_ALREADY_EXISTS -> DB_ERROR_CONSTRAINT
372
 *   DB_ERROR_ALREADY_EXISTS -> DB_ERROR_CONSTRAINT
373
 *
373
 *
374
 * + odbc(access):  MS's ODBC driver reports 'no such field' as code
374
 * + odbc(access):  MS's ODBC driver reports 'no such field' as code
375
 *   07001, which means 'too few parameters.'  When this option is on
375
 *   07001, which means 'too few parameters.'  When this option is on
376
 *   that code gets mapped to DB_ERROR_NOSUCHFIELD.
376
 *   that code gets mapped to DB_ERROR_NOSUCHFIELD.
377
 */
377
 */
378
define('DB_PORTABILITY_ERRORS', 16);
378
define('DB_PORTABILITY_ERRORS', 16);
379
 
379
 
380
/**
380
/**
381
 * Convert null values to empty strings in data output by
381
 * Convert null values to empty strings in data output by
382
 * get*() and fetch*()
382
 * get*() and fetch*()
383
 */
383
 */
384
define('DB_PORTABILITY_NULL_TO_EMPTY', 32);
384
define('DB_PORTABILITY_NULL_TO_EMPTY', 32);
385
 
385
 
386
/**
386
/**
387
 * Turn on all portability features
387
 * Turn on all portability features
388
 */
388
 */
389
define('DB_PORTABILITY_ALL', 63);
389
define('DB_PORTABILITY_ALL', 63);
390
/**#@-*/
390
/**#@-*/
391
 
391
 
392
// }}}
392
// }}}
393
 
393
 
394
 
394
 
395
// }}}
395
// }}}
396
// {{{ class DB
396
// {{{ class DB
397
 
397
 
398
/**
398
/**
399
 * Database independent query interface
399
 * Database independent query interface
400
 *
400
 *
401
 * The main "DB" class is simply a container class with some static
401
 * The main "DB" class is simply a container class with some static
402
 * methods for creating DB objects as well as some utility functions
402
 * methods for creating DB objects as well as some utility functions
403
 * common to all parts of DB.
403
 * common to all parts of DB.
404
 *
404
 *
405
 * The object model of DB is as follows (indentation means inheritance):
405
 * The object model of DB is as follows (indentation means inheritance):
406
 * <pre>
406
 * <pre>
407
 * DB           The main DB class.  This is simply a utility class
407
 * DB           The main DB class.  This is simply a utility class
408
 *              with some "static" methods for creating DB objects as
408
 *              with some "static" methods for creating DB objects as
409
 *              well as common utility functions for other DB classes.
409
 *              well as common utility functions for other DB classes.
410
 *
410
 *
411
 * DB_common    The base for each DB implementation.  Provides default
411
 * DB_common    The base for each DB implementation.  Provides default
412
 * |            implementations (in OO lingo virtual methods) for
412
 * |            implementations (in OO lingo virtual methods) for
413
 * |            the actual DB implementations as well as a bunch of
413
 * |            the actual DB implementations as well as a bunch of
414
 * |            query utility functions.
414
 * |            query utility functions.
415
 * |
415
 * |
416
 * +-DB_mysql   The DB implementation for MySQL.  Inherits DB_common.
416
 * +-DB_mysql   The DB implementation for MySQL.  Inherits DB_common.
417
 *              When calling DB::factory or DB::connect for MySQL
417
 *              When calling DB::factory or DB::connect for MySQL
418
 *              connections, the object returned is an instance of this
418
 *              connections, the object returned is an instance of this
419
 *              class.
419
 *              class.
420
 * </pre>
420
 * </pre>
421
 *
421
 *
422
 * @category   Database
422
 * @category   Database
423
 * @package    DB
423
 * @package    DB
424
 * @author     Stig Bakken <ssb@php.net>
424
 * @author     Stig Bakken <ssb@php.net>
425
 * @author     Tomas V.V.Cox <cox@idecnet.com>
425
 * @author     Tomas V.V.Cox <cox@idecnet.com>
426
 * @author     Daniel Convissor <danielc@php.net>
426
 * @author     Daniel Convissor <danielc@php.net>
427
 * @copyright  1997-2005 The PHP Group
427
 * @copyright  1997-2007 The PHP Group
428
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
428
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
429
 * @version    Release: 1.7.6
429
 * @version    Release: 1.9.2
430
 * @link       http://pear.php.net/package/DB
430
 * @link       http://pear.php.net/package/DB
431
 */
431
 */
432
class DB
432
class DB
433
{
433
{
434
    // {{{ &factory()
434
    // {{{ factory()
435
 
435
 
436
    /**
436
    /**
437
     * Create a new DB object for the specified database type but don't
437
     * Create a new DB object for the specified database type but don't
438
     * connect to the database
438
     * connect to the database
439
     *
439
     *
440
     * @param string $type     the database type (eg "mysql")
440
     * @param string $type     the database type (eg "mysql")
441
     * @param array  $options  an associative array of option names and values
441
     * @param array  $options  an associative array of option names and values
442
     *
442
     *
443
     * @return object  a new DB object.  A DB_Error object on failure.
443
     * @return object  a new DB object.  A DB_Error object on failure.
444
     *
444
     *
445
     * @see DB_common::setOption()
445
     * @see DB_common::setOption()
446
     */
446
     */
447
    function &factory($type, $options = false)
447
    public static function factory($type, $options = false)
448
    {
448
    {
449
        if (!is_array($options)) {
449
        if (!is_array($options)) {
450
            $options = array('persistent' => $options);
450
            $options = array('persistent' => $options);
451
        }
451
        }
452
 
452
 
453
        if (isset($options['debug']) && $options['debug'] >= 2) {
453
        if (isset($options['debug']) && $options['debug'] >= 2) {
454
            // expose php errors with sufficient debug level
454
            // expose php errors with sufficient debug level
455
            include_once "DB/{$type}.php";
455
            include_once "DB/{$type}.php";
456
        } else {
456
        } else {
457
            @include_once "DB/{$type}.php";
457
            @include_once "DB/{$type}.php";
458
        }
458
        }
459
 
459
 
460
        $classname = "DB_${type}";
460
        $classname = "DB_${type}";
461
 
461
 
462
        if (!class_exists($classname)) {
462
        if (!class_exists($classname)) {
463
            $tmp = PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null,
463
            $tmp = PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null,
464
                                    "Unable to include the DB/{$type}.php"
464
                                    "Unable to include the DB/{$type}.php"
465
                                    . " file for '$dsn'",
465
                                    . " file for '$dsn'",
466
                                    'DB_Error', true);
466
                                    'DB_Error', true);
467
            return $tmp;
467
            return $tmp;
468
        }
468
        }
469
 
469
 
470
        @$obj =& new $classname;
470
        @$obj = new $classname;
471
 
471
 
472
        foreach ($options as $option => $value) {
472
        foreach ($options as $option => $value) {
473
            $test = $obj->setOption($option, $value);
473
            $test = $obj->setOption($option, $value);
474
            if (DB::isError($test)) {
474
            if (DB::isError($test)) {
475
                return $test;
475
                return $test;
476
            }
476
            }
477
        }
477
        }
478
 
478
 
479
        return $obj;
479
        return $obj;
480
    }
480
    }
481
 
481
 
482
    // }}}
482
    // }}}
483
    // {{{ &connect()
483
    // {{{ connect()
484
 
484
 
485
    /**
485
    /**
486
     * Create a new DB object including a connection to the specified database
486
     * Create a new DB object including a connection to the specified database
487
     *
487
     *
488
     * Example 1.
488
     * Example 1.
489
     * <code>
489
     * <code>
490
     * require_once 'DB.php';
490
     * require_once 'DB.php';
491
     *
491
     *
492
     * $dsn = 'pgsql://user:password@host/database';
492
     * $dsn = 'pgsql://user:password@host/database';
493
     * $options = array(
493
     * $options = array(
494
     *     'debug'       => 2,
494
     *     'debug'       => 2,
495
     *     'portability' => DB_PORTABILITY_ALL,
495
     *     'portability' => DB_PORTABILITY_ALL,
496
     * );
496
     * );
497
     *
497
     *
498
     * $db =& DB::connect($dsn, $options);
498
     * $db = DB::connect($dsn, $options);
499
     * if (PEAR::isError($db)) {
499
     * if (PEAR::isError($db)) {
500
     *     die($db->getMessage());
500
     *     die($db->getMessage());
501
     * }
501
     * }
502
     * </code>
502
     * </code>
503
     *
503
     *
504
     * @param mixed $dsn      the string "data source name" or array in the
504
     * @param mixed $dsn      the string "data source name" or array in the
505
     *                         format returned by DB::parseDSN()
505
     *                         format returned by DB::parseDSN()
506
     * @param array $options  an associative array of option names and values
506
     * @param array $options  an associative array of option names and values
507
     *
507
     *
508
     * @return object  a new DB object.  A DB_Error object on failure.
508
     * @return object  a new DB object.  A DB_Error object on failure.
509
     *
509
     *
510
     * @uses DB_dbase::connect(), DB_fbsql::connect(), DB_ibase::connect(),
510
     * @uses DB_dbase::connect(), DB_fbsql::connect(), DB_ibase::connect(),
511
     *       DB_ifx::connect(), DB_msql::connect(), DB_mssql::connect(),
511
     *       DB_ifx::connect(), DB_msql::connect(), DB_mssql::connect(),
512
     *       DB_mysql::connect(), DB_mysqli::connect(), DB_oci8::connect(),
512
     *       DB_mysql::connect(), DB_mysqli::connect(), DB_oci8::connect(),
513
     *       DB_odbc::connect(), DB_pgsql::connect(), DB_sqlite::connect(),
513
     *       DB_odbc::connect(), DB_pgsql::connect(), DB_sqlite::connect(),
514
     *       DB_sybase::connect()
514
     *       DB_sybase::connect()
515
     *
515
     *
516
     * @uses DB::parseDSN(), DB_common::setOption(), PEAR::isError()
516
     * @uses DB::parseDSN(), DB_common::setOption(), PEAR::isError()
517
     */
517
     */
518
    function &connect($dsn, $options = array())
518
    public static function connect($dsn, $options = array())
519
    {
519
    {
520
        $dsninfo = DB::parseDSN($dsn);
520
        $dsninfo = DB::parseDSN($dsn);
521
        $type = $dsninfo['phptype'];
521
        $type = $dsninfo['phptype'];
522
 
522
 
523
        if (!is_array($options)) {
523
        if (!is_array($options)) {
524
            /*
524
            /*
525
             * For backwards compatibility.  $options used to be boolean,
525
             * For backwards compatibility.  $options used to be boolean,
526
             * indicating whether the connection should be persistent.
526
             * indicating whether the connection should be persistent.
527
             */
527
             */
528
            $options = array('persistent' => $options);
528
            $options = array('persistent' => $options);
529
        }
529
        }
530
 
530
 
531
        if (isset($options['debug']) && $options['debug'] >= 2) {
531
        if (isset($options['debug']) && $options['debug'] >= 2) {
532
            // expose php errors with sufficient debug level
532
            // expose php errors with sufficient debug level
533
            include_once "DB/${type}.php";
533
            include_once "DB/${type}.php";
534
        } else {
534
        } else {
535
            @include_once "DB/${type}.php";
535
            @include_once "DB/${type}.php";
536
        }
536
        }
537
 
537
 
538
        $classname = "DB_${type}";
538
        $classname = "DB_${type}";
539
        if (!class_exists($classname)) {
539
        if (!class_exists($classname)) {
540
            $tmp = PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null,
540
            $tmp = PEAR::raiseError(null, DB_ERROR_NOT_FOUND, null, null,
541
                                    "Unable to include the DB/{$type}.php"
541
                                    "Unable to include the DB/{$type}.php"
542
                                    . " file for '$dsn'",
542
                                    . " file for '"
-
 
543
                                    . DB::getDSNString($dsn, true) . "'",
543
                                    'DB_Error', true);
544
                                    'DB_Error', true);
544
            return $tmp;
545
            return $tmp;
545
        }
546
        }
546
 
547
 
547
        @$obj =& new $classname;
548
        @$obj = new $classname;
548
 
549
 
549
        foreach ($options as $option => $value) {
550
        foreach ($options as $option => $value) {
550
            $test = $obj->setOption($option, $value);
551
            $test = $obj->setOption($option, $value);
551
            if (DB::isError($test)) {
552
            if (DB::isError($test)) {
552
                return $test;
553
                return $test;
553
            }
554
            }
554
        }
555
        }
555
 
556
 
556
        $err = $obj->connect($dsninfo, $obj->getOption('persistent'));
557
        $err = $obj->connect($dsninfo, $obj->getOption('persistent'));
557
        if (DB::isError($err)) {
558
        if (DB::isError($err)) {
-
 
559
            if (is_array($dsn)) {
-
 
560
                $err->addUserInfo(DB::getDSNString($dsn, true));
-
 
561
            } else {
558
            $err->addUserInfo($dsn);
562
                $err->addUserInfo($dsn);
-
 
563
            }
559
            return $err;
564
            return $err;
560
        }
565
        }
561
 
566
 
562
        return $obj;
567
        return $obj;
563
    }
568
    }
564
 
569
 
565
    // }}}
570
    // }}}
566
    // {{{ apiVersion()
571
    // {{{ apiVersion()
567
 
572
 
568
    /**
573
    /**
569
     * Return the DB API version
574
     * Return the DB API version
570
     *
575
     *
571
     * @return string  the DB API version number
576
     * @return string  the DB API version number
572
     */
577
     */
573
    function apiVersion()
578
    function apiVersion()
574
    {
579
    {
575
        return '1.7.6';
580
        return '1.9.2';
576
    }
581
    }
577
 
582
 
578
    // }}}
583
    // }}}
579
    // {{{ isError()
584
    // {{{ isError()
580
 
585
 
581
    /**
586
    /**
582
     * Determines if a variable is a DB_Error object
587
     * Determines if a variable is a DB_Error object
583
     *
588
     *
584
     * @param mixed $value  the variable to check
589
     * @param mixed $value  the variable to check
585
     *
590
     *
586
     * @return bool  whether $value is DB_Error object
591
     * @return bool  whether $value is DB_Error object
587
     */
592
     */
588
    function isError($value)
593
    public static function isError($value)
589
    {
594
    {
590
        return is_a($value, 'DB_Error');
595
        return is_object($value) && is_a($value, 'DB_Error');		
591
    }
596
    }
592
 
597
 
593
    // }}}
598
    // }}}
594
    // {{{ isConnection()
599
    // {{{ isConnection()
595
 
600
 
596
    /**
601
    /**
597
     * Determines if a value is a DB_<driver> object
602
     * Determines if a value is a DB_<driver> object
598
     *
603
     *
599
     * @param mixed $value  the value to test
604
     * @param mixed $value  the value to test
600
     *
605
     *
601
     * @return bool  whether $value is a DB_<driver> object
606
     * @return bool  whether $value is a DB_<driver> object
602
     */
607
     */
603
    function isConnection($value)
608
    public static function isConnection($value)
604
    {
609
    {
605
        return (is_object($value) &&
610
        return (is_object($value) &&
606
                is_subclass_of($value, 'db_common') &&
611
                is_subclass_of($value, 'db_common') &&
607
                method_exists($value, 'simpleQuery'));
612
                method_exists($value, 'simpleQuery'));
608
    }
613
    }
609
 
614
 
610
    // }}}
615
    // }}}
611
    // {{{ isManip()
616
    // {{{ isManip()
612
 
617
 
613
    /**
618
    /**
614
     * Tell whether a query is a data manipulation or data definition query
619
     * Tell whether a query is a data manipulation or data definition query
615
     *
620
     *
616
     * Examples of data manipulation queries are INSERT, UPDATE and DELETE.
621
     * Examples of data manipulation queries are INSERT, UPDATE and DELETE.
617
     * Examples of data definition queries are CREATE, DROP, ALTER, GRANT,
622
     * Examples of data definition queries are CREATE, DROP, ALTER, GRANT,
618
     * REVOKE.
623
     * REVOKE.
619
     *
624
     *
620
     * @param string $query  the query
625
     * @param string $query  the query
621
     *
626
     *
622
     * @return boolean  whether $query is a data manipulation query
627
     * @return boolean  whether $query is a data manipulation query
623
     */
628
     */
624
    function isManip($query)
629
    public static function isManip($query)
625
    {
630
    {
626
        $manips = 'INSERT|UPDATE|DELETE|REPLACE|'
631
        $manips = 'INSERT|UPDATE|DELETE|REPLACE|'
627
                . 'CREATE|DROP|'
632
                . 'CREATE|DROP|'
628
                . 'LOAD DATA|SELECT .* INTO|COPY|'
633
                . 'LOAD DATA|SELECT .* INTO .* FROM|COPY|'
629
                . 'ALTER|GRANT|REVOKE|'
634
                . 'ALTER|GRANT|REVOKE|'
630
                . 'LOCK|UNLOCK';
635
                . 'LOCK|UNLOCK';
631
        if (preg_match('/^\s*"?(' . $manips . ')\s+/i', $query)) {
636
        if (preg_match('/^\s*"?(' . $manips . ')\s+/i', $query)) {
632
            return true;
637
            return true;
633
        }
638
        }
634
        return false;
639
        return false;
635
    }
640
    }
636
 
641
 
637
    // }}}
642
    // }}}
638
    // {{{ errorMessage()
643
    // {{{ errorMessage()
639
 
644
 
640
    /**
645
    /**
641
     * Return a textual error message for a DB error code
646
     * Return a textual error message for a DB error code
642
     *
647
     *
643
     * @param integer $value  the DB error code
648
     * @param integer $value  the DB error code
644
     *
649
     *
645
     * @return string  the error message or false if the error code was
650
     * @return string  the error message or false if the error code was
646
     *                  not recognized
651
     *                  not recognized
647
     */
652
     */
648
    function errorMessage($value)
653
    public static function errorMessage($value)
649
    {
654
    {
650
        static $errorMessages;
655
        static $errorMessages;
651
        if (!isset($errorMessages)) {
656
        if (!isset($errorMessages)) {
652
            $errorMessages = array(
657
            $errorMessages = array(
653
                DB_ERROR                    => 'unknown error',
658
                DB_ERROR                    => 'unknown error',
654
                DB_ERROR_ACCESS_VIOLATION   => 'insufficient permissions',
659
                DB_ERROR_ACCESS_VIOLATION   => 'insufficient permissions',
655
                DB_ERROR_ALREADY_EXISTS     => 'already exists',
660
                DB_ERROR_ALREADY_EXISTS     => 'already exists',
656
                DB_ERROR_CANNOT_CREATE      => 'can not create',
661
                DB_ERROR_CANNOT_CREATE      => 'can not create',
657
                DB_ERROR_CANNOT_DROP        => 'can not drop',
662
                DB_ERROR_CANNOT_DROP        => 'can not drop',
658
                DB_ERROR_CONNECT_FAILED     => 'connect failed',
663
                DB_ERROR_CONNECT_FAILED     => 'connect failed',
659
                DB_ERROR_CONSTRAINT         => 'constraint violation',
664
                DB_ERROR_CONSTRAINT         => 'constraint violation',
660
                DB_ERROR_CONSTRAINT_NOT_NULL=> 'null value violates not-null constraint',
665
                DB_ERROR_CONSTRAINT_NOT_NULL=> 'null value violates not-null constraint',
661
                DB_ERROR_DIVZERO            => 'division by zero',
666
                DB_ERROR_DIVZERO            => 'division by zero',
662
                DB_ERROR_EXTENSION_NOT_FOUND=> 'extension not found',
667
                DB_ERROR_EXTENSION_NOT_FOUND=> 'extension not found',
663
                DB_ERROR_INVALID            => 'invalid',
668
                DB_ERROR_INVALID            => 'invalid',
664
                DB_ERROR_INVALID_DATE       => 'invalid date or time',
669
                DB_ERROR_INVALID_DATE       => 'invalid date or time',
665
                DB_ERROR_INVALID_DSN        => 'invalid DSN',
670
                DB_ERROR_INVALID_DSN        => 'invalid DSN',
666
                DB_ERROR_INVALID_NUMBER     => 'invalid number',
671
                DB_ERROR_INVALID_NUMBER     => 'invalid number',
667
                DB_ERROR_MISMATCH           => 'mismatch',
672
                DB_ERROR_MISMATCH           => 'mismatch',
668
                DB_ERROR_NEED_MORE_DATA     => 'insufficient data supplied',
673
                DB_ERROR_NEED_MORE_DATA     => 'insufficient data supplied',
669
                DB_ERROR_NODBSELECTED       => 'no database selected',
674
                DB_ERROR_NODBSELECTED       => 'no database selected',
670
                DB_ERROR_NOSUCHDB           => 'no such database',
675
                DB_ERROR_NOSUCHDB           => 'no such database',
671
                DB_ERROR_NOSUCHFIELD        => 'no such field',
676
                DB_ERROR_NOSUCHFIELD        => 'no such field',
672
                DB_ERROR_NOSUCHTABLE        => 'no such table',
677
                DB_ERROR_NOSUCHTABLE        => 'no such table',
673
                DB_ERROR_NOT_CAPABLE        => 'DB backend not capable',
678
                DB_ERROR_NOT_CAPABLE        => 'DB backend not capable',
674
                DB_ERROR_NOT_FOUND          => 'not found',
679
                DB_ERROR_NOT_FOUND          => 'not found',
675
                DB_ERROR_NOT_LOCKED         => 'not locked',
680
                DB_ERROR_NOT_LOCKED         => 'not locked',
676
                DB_ERROR_SYNTAX             => 'syntax error',
681
                DB_ERROR_SYNTAX             => 'syntax error',
677
                DB_ERROR_UNSUPPORTED        => 'not supported',
682
                DB_ERROR_UNSUPPORTED        => 'not supported',
678
                DB_ERROR_TRUNCATED          => 'truncated',
683
                DB_ERROR_TRUNCATED          => 'truncated',
679
                DB_ERROR_VALUE_COUNT_ON_ROW => 'value count on row',
684
                DB_ERROR_VALUE_COUNT_ON_ROW => 'value count on row',
680
                DB_OK                       => 'no error',
685
                DB_OK                       => 'no error',
681
            );
686
            );
682
        }
687
        }
683
 
688
 
684
        if (DB::isError($value)) {
689
        if (DB::isError($value)) {
685
            $value = $value->getCode();
690
            $value = $value->getCode();
686
        }
691
        }
687
 
692
 
688
        return isset($errorMessages[$value]) ? $errorMessages[$value]
693
        return isset($errorMessages[$value]) ? $errorMessages[$value]
689
                     : $errorMessages[DB_ERROR];
694
                     : $errorMessages[DB_ERROR];
690
    }
695
    }
691
 
696
 
692
    // }}}
697
    // }}}
693
    // {{{ parseDSN()
698
    // {{{ parseDSN()
694
 
699
 
695
    /**
700
    /**
696
     * Parse a data source name
701
     * Parse a data source name
697
     *
702
     *
698
     * Additional keys can be added by appending a URI query string to the
703
     * Additional keys can be added by appending a URI query string to the
699
     * end of the DSN.
704
     * end of the DSN.
700
     *
705
     *
701
     * The format of the supplied DSN is in its fullest form:
706
     * The format of the supplied DSN is in its fullest form:
702
     * <code>
707
     * <code>
703
     *  phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true
708
     *  phptype(dbsyntax)://username:password@protocol+hostspec/database?option=8&another=true
704
     * </code>
709
     * </code>
705
     *
710
     *
706
     * Most variations are allowed:
711
     * Most variations are allowed:
707
     * <code>
712
     * <code>
708
     *  phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644
713
     *  phptype://username:password@protocol+hostspec:110//usr/db_file.db?mode=0644
709
     *  phptype://username:password@hostspec/database_name
714
     *  phptype://username:password@hostspec/database_name
710
     *  phptype://username:password@hostspec
715
     *  phptype://username:password@hostspec
711
     *  phptype://username@hostspec
716
     *  phptype://username@hostspec
712
     *  phptype://hostspec/database
717
     *  phptype://hostspec/database
713
     *  phptype://hostspec
718
     *  phptype://hostspec
714
     *  phptype(dbsyntax)
719
     *  phptype(dbsyntax)
715
     *  phptype
720
     *  phptype
716
     * </code>
721
     * </code>
717
     *
722
     *
718
     * @param string $dsn Data Source Name to be parsed
723
     * @param string $dsn Data Source Name to be parsed
719
     *
724
     *
720
     * @return array an associative array with the following keys:
725
     * @return array an associative array with the following keys:
721
     *  + phptype:  Database backend used in PHP (mysql, odbc etc.)
726
     *  + phptype:  Database backend used in PHP (mysql, odbc etc.)
722
     *  + dbsyntax: Database used with regards to SQL syntax etc.
727
     *  + dbsyntax: Database used with regards to SQL syntax etc.
723
     *  + protocol: Communication protocol to use (tcp, unix etc.)
728
     *  + protocol: Communication protocol to use (tcp, unix etc.)
724
     *  + hostspec: Host specification (hostname[:port])
729
     *  + hostspec: Host specification (hostname[:port])
725
     *  + database: Database to use on the DBMS server
730
     *  + database: Database to use on the DBMS server
726
     *  + username: User name for login
731
     *  + username: User name for login
727
     *  + password: Password for login
732
     *  + password: Password for login
728
     */
733
     */
729
    function parseDSN($dsn)
734
    public static function parseDSN($dsn)
730
    {
735
    {
731
        $parsed = array(
736
        $parsed = array(
732
            'phptype'  => false,
737
            'phptype'  => false,
733
            'dbsyntax' => false,
738
            'dbsyntax' => false,
734
            'username' => false,
739
            'username' => false,
735
            'password' => false,
740
            'password' => false,
736
            'protocol' => false,
741
            'protocol' => false,
737
            'hostspec' => false,
742
            'hostspec' => false,
738
            'port'     => false,
743
            'port'     => false,
739
            'socket'   => false,
744
            'socket'   => false,
740
            'database' => false,
745
            'database' => false,
741
        );
746
        );
742
 
747
 
743
        if (is_array($dsn)) {
748
        if (is_array($dsn)) {
744
            $dsn = array_merge($parsed, $dsn);
749
            $dsn = array_merge($parsed, $dsn);
745
            if (!$dsn['dbsyntax']) {
750
            if (!$dsn['dbsyntax']) {
746
                $dsn['dbsyntax'] = $dsn['phptype'];
751
                $dsn['dbsyntax'] = $dsn['phptype'];
747
            }
752
            }
748
            return $dsn;
753
            return $dsn;
749
        }
754
        }
750
 
755
 
751
        // Find phptype and dbsyntax
756
        // Find phptype and dbsyntax
752
        if (($pos = strpos($dsn, '://')) !== false) {
757
        if (($pos = strpos($dsn, '://')) !== false) {
753
            $str = substr($dsn, 0, $pos);
758
            $str = substr($dsn, 0, $pos);
754
            $dsn = substr($dsn, $pos + 3);
759
            $dsn = substr($dsn, $pos + 3);
755
        } else {
760
        } else {
756
            $str = $dsn;
761
            $str = $dsn;
757
            $dsn = null;
762
            $dsn = null;
758
        }
763
        }
759
 
764
 
760
        // Get phptype and dbsyntax
765
        // Get phptype and dbsyntax
761
        // $str => phptype(dbsyntax)
766
        // $str => phptype(dbsyntax)
762
        if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
767
        if (preg_match('|^(.+?)\((.*?)\)$|', $str, $arr)) {
763
            $parsed['phptype']  = $arr[1];
768
            $parsed['phptype']  = $arr[1];
764
            $parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2];
769
            $parsed['dbsyntax'] = !$arr[2] ? $arr[1] : $arr[2];
765
        } else {
770
        } else {
766
            $parsed['phptype']  = $str;
771
            $parsed['phptype']  = $str;
767
            $parsed['dbsyntax'] = $str;
772
            $parsed['dbsyntax'] = $str;
768
        }
773
        }
769
 
774
 
770
        if (!count($dsn)) {
775
        if (!count($dsn)) {
771
            return $parsed;
776
            return $parsed;
772
        }
777
        }
773
 
778
 
774
        // Get (if found): username and password
779
        // Get (if found): username and password
775
        // $dsn => username:password@protocol+hostspec/database
780
        // $dsn => username:password@protocol+hostspec/database
776
        if (($at = strrpos($dsn,'@')) !== false) {
781
        if (($at = strrpos($dsn,'@')) !== false) {
777
            $str = substr($dsn, 0, $at);
782
            $str = substr($dsn, 0, $at);
778
            $dsn = substr($dsn, $at + 1);
783
            $dsn = substr($dsn, $at + 1);
779
            if (($pos = strpos($str, ':')) !== false) {
784
            if (($pos = strpos($str, ':')) !== false) {
780
                $parsed['username'] = rawurldecode(substr($str, 0, $pos));
785
                $parsed['username'] = rawurldecode(substr($str, 0, $pos));
781
                $parsed['password'] = rawurldecode(substr($str, $pos + 1));
786
                $parsed['password'] = rawurldecode(substr($str, $pos + 1));
782
            } else {
787
            } else {
783
                $parsed['username'] = rawurldecode($str);
788
                $parsed['username'] = rawurldecode($str);
784
            }
789
            }
785
        }
790
        }
786
 
791
 
787
        // Find protocol and hostspec
792
        // Find protocol and hostspec
788
 
793
 
789
        if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
794
        if (preg_match('|^([^(]+)\((.*?)\)/?(.*?)$|', $dsn, $match)) {
790
            // $dsn => proto(proto_opts)/database
795
            // $dsn => proto(proto_opts)/database
791
            $proto       = $match[1];
796
            $proto       = $match[1];
792
            $proto_opts  = $match[2] ? $match[2] : false;
797
            $proto_opts  = $match[2] ? $match[2] : false;
793
            $dsn         = $match[3];
798
            $dsn         = $match[3];
794
 
799
 
795
        } else {
800
        } else {
796
            // $dsn => protocol+hostspec/database (old format)
801
            // $dsn => protocol+hostspec/database (old format)
797
            if (strpos($dsn, '+') !== false) {
802
            if (strpos($dsn, '+') !== false) {
798
                list($proto, $dsn) = explode('+', $dsn, 2);
803
                list($proto, $dsn) = explode('+', $dsn, 2);
799
            }
804
            }
800
            if (strpos($dsn, '/') !== false) {
805
            if (strpos($dsn, '/') !== false) {
801
                list($proto_opts, $dsn) = explode('/', $dsn, 2);
806
                list($proto_opts, $dsn) = explode('/', $dsn, 2);
802
            } else {
807
            } else {
803
                $proto_opts = $dsn;
808
                $proto_opts = $dsn;
804
                $dsn = null;
809
                $dsn = null;
805
            }
810
            }
806
        }
811
        }
807
 
812
 
808
        // process the different protocol options
813
        // process the different protocol options
809
        $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
814
        $parsed['protocol'] = (!empty($proto)) ? $proto : 'tcp';
810
        $proto_opts = rawurldecode($proto_opts);
815
        $proto_opts = rawurldecode($proto_opts);
-
 
816
        if (strpos($proto_opts, ':') !== false) {
-
 
817
            list($proto_opts, $parsed['port']) = explode(':', $proto_opts);
-
 
818
        }
811
        if ($parsed['protocol'] == 'tcp') {
819
        if ($parsed['protocol'] == 'tcp') {
812
            if (strpos($proto_opts, ':') !== false) {
-
 
813
                list($parsed['hostspec'],
-
 
814
                     $parsed['port']) = explode(':', $proto_opts);
-
 
815
            } else {
-
 
816
                $parsed['hostspec'] = $proto_opts;
820
            $parsed['hostspec'] = $proto_opts;
817
            }
-
 
818
        } elseif ($parsed['protocol'] == 'unix') {
821
        } elseif ($parsed['protocol'] == 'unix') {
819
            $parsed['socket'] = $proto_opts;
822
            $parsed['socket'] = $proto_opts;
820
        }
823
        }
821
 
824
 
822
        // Get dabase if any
825
        // Get dabase if any
823
        // $dsn => database
826
        // $dsn => database
824
        if ($dsn) {
827
        if ($dsn) {
825
            if (($pos = strpos($dsn, '?')) === false) {
828
            if (($pos = strpos($dsn, '?')) === false) {
826
                // /database
829
                // /database
827
                $parsed['database'] = rawurldecode($dsn);
830
                $parsed['database'] = rawurldecode($dsn);
828
            } else {
831
            } else {
829
                // /database?param1=value1&param2=value2
832
                // /database?param1=value1&param2=value2
830
                $parsed['database'] = rawurldecode(substr($dsn, 0, $pos));
833
                $parsed['database'] = rawurldecode(substr($dsn, 0, $pos));
831
                $dsn = substr($dsn, $pos + 1);
834
                $dsn = substr($dsn, $pos + 1);
832
                if (strpos($dsn, '&') !== false) {
835
                if (strpos($dsn, '&') !== false) {
833
                    $opts = explode('&', $dsn);
836
                    $opts = explode('&', $dsn);
834
                } else { // database?param1=value1
837
                } else { // database?param1=value1
835
                    $opts = array($dsn);
838
                    $opts = array($dsn);
836
                }
839
                }
837
                foreach ($opts as $opt) {
840
                foreach ($opts as $opt) {
838
                    list($key, $value) = explode('=', $opt);
841
                    list($key, $value) = explode('=', $opt);
839
                    if (!isset($parsed[$key])) {
842
                    if (!isset($parsed[$key])) {
840
                        // don't allow params overwrite
843
                        // don't allow params overwrite
841
                        $parsed[$key] = rawurldecode($value);
844
                        $parsed[$key] = rawurldecode($value);
842
                    }
845
                    }
843
                }
846
                }
844
            }
847
            }
845
        }
848
        }
846
 
849
 
847
        return $parsed;
850
        return $parsed;
848
    }
851
    }
849
 
852
 
850
    // }}}
853
    // }}}
-
 
854
    // {{{ getDSNString()
-
 
855
 
-
 
856
    /**
-
 
857
     * Returns the given DSN in a string format suitable for output.
-
 
858
     *
-
 
859
     * @param array|string the DSN to parse and format
-
 
860
     * @param boolean true to hide the password, false to include it
-
 
861
     * @return string
-
 
862
     */
-
 
863
    public static function getDSNString($dsn, $hidePassword) {
-
 
864
        /* Calling parseDSN will ensure that we have all the array elements
-
 
865
         * defined, and means that we deal with strings and array in the same
-
 
866
         * manner. */
-
 
867
        $dsnArray = DB::parseDSN($dsn);
-
 
868
        
-
 
869
        if ($hidePassword) {
-
 
870
            $dsnArray['password'] = 'PASSWORD';
-
 
871
        }
-
 
872
 
-
 
873
        /* Protocol is special-cased, as using the default "tcp" along with an
-
 
874
         * Oracle TNS connection string fails. */
-
 
875
        if (is_string($dsn) && strpos($dsn, 'tcp') === false && $dsnArray['protocol'] == 'tcp') {
-
 
876
            $dsnArray['protocol'] = false;
-
 
877
        }
-
 
878
        
-
 
879
        // Now we just have to construct the actual string. This is ugly.
-
 
880
        $dsnString = $dsnArray['phptype'];
-
 
881
        if ($dsnArray['dbsyntax']) {
-
 
882
            $dsnString .= '('.$dsnArray['dbsyntax'].')';
-
 
883
        }
-
 
884
        $dsnString .= '://'
-
 
885
                     .$dsnArray['username']
-
 
886
                     .':'
-
 
887
                     .$dsnArray['password']
-
 
888
                     .'@'
-
 
889
                     .$dsnArray['protocol'];
-
 
890
        if ($dsnArray['socket']) {
-
 
891
            $dsnString .= '('.$dsnArray['socket'].')';
-
 
892
        }
-
 
893
        if ($dsnArray['protocol'] && $dsnArray['hostspec']) {
-
 
894
            $dsnString .= '+';
-
 
895
        }
-
 
896
        $dsnString .= $dsnArray['hostspec'];
-
 
897
        if ($dsnArray['port']) {
-
 
898
            $dsnString .= ':'.$dsnArray['port'];
-
 
899
        }
-
 
900
        $dsnString .= '/'.$dsnArray['database'];
-
 
901
        
-
 
902
        /* Option handling. Unfortunately, parseDSN simply places options into
-
 
903
         * the top-level array, so we'll first get rid of the fields defined by
-
 
904
         * DB and see what's left. */
-
 
905
        unset($dsnArray['phptype'],
-
 
906
              $dsnArray['dbsyntax'],
-
 
907
              $dsnArray['username'],
-
 
908
              $dsnArray['password'],
-
 
909
              $dsnArray['protocol'],
-
 
910
              $dsnArray['socket'],
-
 
911
              $dsnArray['hostspec'],
-
 
912
              $dsnArray['port'],
-
 
913
              $dsnArray['database']
-
 
914
        );
-
 
915
        if (count($dsnArray) > 0) {
-
 
916
            $dsnString .= '?';
-
 
917
            $i = 0;
-
 
918
            foreach ($dsnArray as $key => $value) {
-
 
919
                if (++$i > 1) {
-
 
920
                    $dsnString .= '&';
-
 
921
                }
-
 
922
                $dsnString .= $key.'='.$value;
-
 
923
            }
-
 
924
        }
-
 
925
 
-
 
926
        return $dsnString;
-
 
927
    }
-
 
928
    
-
 
929
    // }}}
851
}
930
}
852
 
931
 
853
// }}}
932
// }}}
854
// {{{ class DB_Error
933
// {{{ class DB_Error
855
 
934
 
856
/**
935
/**
857
 * DB_Error implements a class for reporting portable database error
936
 * DB_Error implements a class for reporting portable database error
858
 * messages
937
 * messages
859
 *
938
 *
860
 * @category   Database
939
 * @category   Database
861
 * @package    DB
940
 * @package    DB
862
 * @author     Stig Bakken <ssb@php.net>
941
 * @author     Stig Bakken <ssb@php.net>
863
 * @copyright  1997-2005 The PHP Group
942
 * @copyright  1997-2007 The PHP Group
864
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
943
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
865
 * @version    Release: 1.7.6
944
 * @version    Release: 1.9.2
866
 * @link       http://pear.php.net/package/DB
945
 * @link       http://pear.php.net/package/DB
867
 */
946
 */
868
class DB_Error extends PEAR_Error
947
class DB_Error extends PEAR_Error
869
{
948
{
870
    // {{{ constructor
949
    // {{{ constructor
871
 
950
 
872
    /**
951
    /**
873
     * DB_Error constructor
952
     * DB_Error constructor
874
     *
953
     *
875
     * @param mixed $code       DB error code, or string with error message
954
     * @param mixed $code       DB error code, or string with error message
876
     * @param int   $mode       what "error mode" to operate in
955
     * @param int   $mode       what "error mode" to operate in
877
     * @param int   $level      what error level to use for $mode &
956
     * @param int   $level      what error level to use for $mode &
878
     *                           PEAR_ERROR_TRIGGER
957
     *                           PEAR_ERROR_TRIGGER
879
     * @param mixed $debuginfo  additional debug info, such as the last query
958
     * @param mixed $debuginfo  additional debug info, such as the last query
880
     *
959
     *
881
     * @see PEAR_Error
960
     * @see PEAR_Error
882
     */
961
     */
883
    function DB_Error($code = DB_ERROR, $mode = PEAR_ERROR_RETURN,
962
    function __construct($code = DB_ERROR, $mode = PEAR_ERROR_RETURN,
884
                      $level = E_USER_NOTICE, $debuginfo = null)
963
                      $level = E_USER_NOTICE, $debuginfo = null)
885
    {
964
    {
886
        if (is_int($code)) {
965
        if (is_int($code)) {
887
            $this->PEAR_Error('DB Error: ' . DB::errorMessage($code), $code,
966
            parent::__construct('DB Error: ' . DB::errorMessage($code), $code,
888
                              $mode, $level, $debuginfo);
967
                              $mode, $level, $debuginfo);
889
        } else {
968
        } else {
890
            $this->PEAR_Error("DB Error: $code", DB_ERROR,
969
            parent::__construct("DB Error: $code", DB_ERROR,
891
                              $mode, $level, $debuginfo);
970
                              $mode, $level, $debuginfo);
892
        }
971
        }
893
    }
972
    }
-
 
973
 
-
 
974
    /**
-
 
975
     * Workaround to both avoid the "Redefining already defined constructor"
-
 
976
     * PHP error and provide backward compatibility in case someone is calling
-
 
977
     * DB_Error() dynamically
-
 
978
     */
-
 
979
    public function __call($method, $arguments)
-
 
980
    {
-
 
981
        if ($method == 'DB_Error') {
-
 
982
            return call_user_func_array(array($this, '__construct'), $arguments);
-
 
983
        }
-
 
984
        trigger_error(
-
 
985
            'Call to undefined method DB_Error::' . $method . '()', E_USER_ERROR
-
 
986
        );
894
 
987
    }
895
    // }}}
988
    // }}}
896
}
989
}
897
 
990
 
898
// }}}
991
// }}}
899
// {{{ class DB_result
992
// {{{ class DB_result
900
 
993
 
901
/**
994
/**
902
 * This class implements a wrapper for a DB result set
995
 * This class implements a wrapper for a DB result set
903
 *
996
 *
904
 * A new instance of this class will be returned by the DB implementation
997
 * A new instance of this class will be returned by the DB implementation
905
 * after processing a query that returns data.
998
 * after processing a query that returns data.
906
 *
999
 *
907
 * @category   Database
1000
 * @category   Database
908
 * @package    DB
1001
 * @package    DB
909
 * @author     Stig Bakken <ssb@php.net>
1002
 * @author     Stig Bakken <ssb@php.net>
910
 * @copyright  1997-2005 The PHP Group
1003
 * @copyright  1997-2007 The PHP Group
911
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
1004
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
912
 * @version    Release: 1.7.6
1005
 * @version    Release: 1.9.2
913
 * @link       http://pear.php.net/package/DB
1006
 * @link       http://pear.php.net/package/DB
914
 */
1007
 */
915
class DB_result
1008
class DB_result
916
{
1009
{
917
    // {{{ properties
1010
    // {{{ properties
918
 
1011
 
919
    /**
1012
    /**
920
     * Should results be freed automatically when there are no more rows?
1013
     * Should results be freed automatically when there are no more rows?
921
     * @var boolean
1014
     * @var boolean
922
     * @see DB_common::$options
1015
     * @see DB_common::$options
923
     */
1016
     */
924
    var $autofree;
1017
    var $autofree;
925
 
1018
 
926
    /**
1019
    /**
927
     * A reference to the DB_<driver> object
1020
     * A reference to the DB_<driver> object
928
     * @var object
1021
     * @var object
929
     */
1022
     */
930
    var $dbh;
1023
    var $dbh;
931
 
1024
 
932
    /**
1025
    /**
933
     * The current default fetch mode
1026
     * The current default fetch mode
934
     * @var integer
1027
     * @var integer
935
     * @see DB_common::$fetchmode
1028
     * @see DB_common::$fetchmode
936
     */
1029
     */
937
    var $fetchmode;
1030
    var $fetchmode;
938
 
1031
 
939
    /**
1032
    /**
940
     * The name of the class into which results should be fetched when
1033
     * The name of the class into which results should be fetched when
941
     * DB_FETCHMODE_OBJECT is in effect
1034
     * DB_FETCHMODE_OBJECT is in effect
942
     *
1035
     *
943
     * @var string
1036
     * @var string
944
     * @see DB_common::$fetchmode_object_class
1037
     * @see DB_common::$fetchmode_object_class
945
     */
1038
     */
946
    var $fetchmode_object_class;
1039
    var $fetchmode_object_class;
947
 
1040
 
948
    /**
1041
    /**
949
     * The number of rows to fetch from a limit query
1042
     * The number of rows to fetch from a limit query
950
     * @var integer
1043
     * @var integer
951
     */
1044
     */
952
    var $limit_count = null;
1045
    var $limit_count = null;
953
 
1046
 
954
    /**
1047
    /**
955
     * The row to start fetching from in limit queries
1048
     * The row to start fetching from in limit queries
956
     * @var integer
1049
     * @var integer
957
     */
1050
     */
958
    var $limit_from = null;
1051
    var $limit_from = null;
959
 
1052
 
960
    /**
1053
    /**
961
     * The execute parameters that created this result
1054
     * The execute parameters that created this result
962
     * @var array
1055
     * @var array
963
     * @since Property available since Release 1.7.0
1056
     * @since Property available since Release 1.7.0
964
     */
1057
     */
965
    var $parameters;
1058
    var $parameters;
966
 
1059
 
967
    /**
1060
    /**
968
     * The query string that created this result
1061
     * The query string that created this result
969
     *
1062
     *
970
     * Copied here incase it changes in $dbh, which is referenced
1063
     * Copied here incase it changes in $dbh, which is referenced
971
     *
1064
     *
972
     * @var string
1065
     * @var string
973
     * @since Property available since Release 1.7.0
1066
     * @since Property available since Release 1.7.0
974
     */
1067
     */
975
    var $query;
1068
    var $query;
976
 
1069
 
977
    /**
1070
    /**
978
     * The query result resource id created by PHP
1071
     * The query result resource id created by PHP
979
     * @var resource
1072
     * @var resource
980
     */
1073
     */
981
    var $result;
1074
    var $result;
982
 
1075
 
983
    /**
1076
    /**
984
     * The present row being dealt with
1077
     * The present row being dealt with
985
     * @var integer
1078
     * @var integer
986
     */
1079
     */
987
    var $row_counter = null;
1080
    var $row_counter = null;
988
 
1081
 
989
    /**
1082
    /**
990
     * The prepared statement resource id created by PHP in $dbh
1083
     * The prepared statement resource id created by PHP in $dbh
991
     *
1084
     *
992
     * This resource is only available when the result set was created using
1085
     * This resource is only available when the result set was created using
993
     * a driver's native execute() method, not PEAR DB's emulated one.
1086
     * a driver's native execute() method, not PEAR DB's emulated one.
994
     *
1087
     *
995
     * Copied here incase it changes in $dbh, which is referenced
1088
     * Copied here incase it changes in $dbh, which is referenced
996
     *
1089
     *
997
     * {@internal  Mainly here because the InterBase/Firebird API is only
1090
     * {@internal  Mainly here because the InterBase/Firebird API is only
998
     * able to retrieve data from result sets if the statemnt handle is
1091
     * able to retrieve data from result sets if the statemnt handle is
999
     * still in scope.}}
1092
     * still in scope.}}
1000
     *
1093
     *
1001
     * @var resource
1094
     * @var resource
1002
     * @since Property available since Release 1.7.0
1095
     * @since Property available since Release 1.7.0
1003
     */
1096
     */
1004
    var $statement;
1097
    var $statement;
1005
 
1098
 
1006
 
1099
 
1007
    // }}}
1100
    // }}}
1008
    // {{{ constructor
1101
    // {{{ constructor
1009
 
1102
 
1010
    /**
1103
    /**
1011
     * This constructor sets the object's properties
1104
     * This constructor sets the object's properties
1012
     *
1105
     *
1013
     * @param object   &$dbh     the DB object reference
1106
     * @param object   &$dbh     the DB object reference
1014
     * @param resource $result   the result resource id
1107
     * @param resource $result   the result resource id
1015
     * @param array    $options  an associative array with result options
1108
     * @param array    $options  an associative array with result options
1016
     *
1109
     *
1017
     * @return void
1110
     * @return void
1018
     */
1111
     */
1019
    function DB_result(&$dbh, $result, $options = array())
1112
    function __construct(&$dbh, $result, $options = array())
1020
    {
1113
    {
1021
        $this->autofree    = $dbh->options['autofree'];
1114
        $this->autofree    = $dbh->options['autofree'];
1022
        $this->dbh         = &$dbh;
1115
        $this->dbh         = &$dbh;
1023
        $this->fetchmode   = $dbh->fetchmode;
1116
        $this->fetchmode   = $dbh->fetchmode;
1024
        $this->fetchmode_object_class = $dbh->fetchmode_object_class;
1117
        $this->fetchmode_object_class = $dbh->fetchmode_object_class;
1025
        $this->parameters  = $dbh->last_parameters;
1118
        $this->parameters  = $dbh->last_parameters;
1026
        $this->query       = $dbh->last_query;
1119
        $this->query       = $dbh->last_query;
1027
        $this->result      = $result;
1120
        $this->result      = $result;
1028
        $this->statement   = empty($dbh->last_stmt) ? null : $dbh->last_stmt;
1121
        $this->statement   = empty($dbh->last_stmt) ? null : $dbh->last_stmt;
1029
        foreach ($options as $key => $value) {
1122
        foreach ($options as $key => $value) {
1030
            $this->setOption($key, $value);
1123
            $this->setOption($key, $value);
1031
        }
1124
        }
1032
    }
1125
    }
1033
 
1126
 
1034
    /**
1127
    /**
1035
     * Set options for the DB_result object
1128
     * Set options for the DB_result object
1036
     *
1129
     *
1037
     * @param string $key    the option to set
1130
     * @param string $key    the option to set
1038
     * @param mixed  $value  the value to set the option to
1131
     * @param mixed  $value  the value to set the option to
1039
     *
1132
     *
1040
     * @return void
1133
     * @return void
1041
     */
1134
     */
1042
    function setOption($key, $value = null)
1135
    function setOption($key, $value = null)
1043
    {
1136
    {
1044
        switch ($key) {
1137
        switch ($key) {
1045
            case 'limit_from':
1138
            case 'limit_from':
1046
                $this->limit_from = $value;
1139
                $this->limit_from = $value;
1047
                break;
1140
                break;
1048
            case 'limit_count':
1141
            case 'limit_count':
1049
                $this->limit_count = $value;
1142
                $this->limit_count = $value;
1050
        }
1143
        }
1051
    }
1144
    }
1052
 
1145
 
1053
    // }}}
1146
    // }}}
1054
    // {{{ fetchRow()
1147
    // {{{ fetchRow()
1055
 
1148
 
1056
    /**
1149
    /**
1057
     * Fetch a row of data and return it by reference into an array
1150
     * Fetch a row of data and return it by reference into an array
1058
     *
1151
     *
1059
     * The type of array returned can be controlled either by setting this
1152
     * The type of array returned can be controlled either by setting this
1060
     * method's <var>$fetchmode</var> parameter or by changing the default
1153
     * method's <var>$fetchmode</var> parameter or by changing the default
1061
     * fetch mode setFetchMode() before calling this method.
1154
     * fetch mode setFetchMode() before calling this method.
1062
     *
1155
     *
1063
     * There are two options for standardizing the information returned
1156
     * There are two options for standardizing the information returned
1064
     * from databases, ensuring their values are consistent when changing
1157
     * from databases, ensuring their values are consistent when changing
1065
     * DBMS's.  These portability options can be turned on when creating a
1158
     * DBMS's.  These portability options can be turned on when creating a
1066
     * new DB object or by using setOption().
1159
     * new DB object or by using setOption().
1067
     *
1160
     *
1068
     *   + <var>DB_PORTABILITY_LOWERCASE</var>
1161
     *   + <var>DB_PORTABILITY_LOWERCASE</var>
1069
     *     convert names of fields to lower case
1162
     *     convert names of fields to lower case
1070
     *
1163
     *
1071
     *   + <var>DB_PORTABILITY_RTRIM</var>
1164
     *   + <var>DB_PORTABILITY_RTRIM</var>
1072
     *     right trim the data
1165
     *     right trim the data
1073
     *
1166
     *
1074
     * @param int $fetchmode  the constant indicating how to format the data
1167
     * @param int $fetchmode  the constant indicating how to format the data
1075
     * @param int $rownum     the row number to fetch (index starts at 0)
1168
     * @param int $rownum     the row number to fetch (index starts at 0)
1076
     *
1169
     *
1077
     * @return mixed  an array or object containing the row's data,
1170
     * @return mixed  an array or object containing the row's data,
1078
     *                 NULL when the end of the result set is reached
1171
     *                 NULL when the end of the result set is reached
1079
     *                 or a DB_Error object on failure.
1172
     *                 or a DB_Error object on failure.
1080
     *
1173
     *
1081
     * @see DB_common::setOption(), DB_common::setFetchMode()
1174
     * @see DB_common::setOption(), DB_common::setFetchMode()
1082
     */
1175
     */
1083
    function &fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null)
1176
    function &fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null)
1084
    {
1177
    {
1085
        if ($fetchmode === DB_FETCHMODE_DEFAULT) {
1178
        if ($fetchmode === DB_FETCHMODE_DEFAULT) {
1086
            $fetchmode = $this->fetchmode;
1179
            $fetchmode = $this->fetchmode;
1087
        }
1180
        }
1088
        if ($fetchmode === DB_FETCHMODE_OBJECT) {
1181
        if ($fetchmode === DB_FETCHMODE_OBJECT) {
1089
            $fetchmode = DB_FETCHMODE_ASSOC;
1182
            $fetchmode = DB_FETCHMODE_ASSOC;
1090
            $object_class = $this->fetchmode_object_class;
1183
            $object_class = $this->fetchmode_object_class;
1091
        }
1184
        }
1092
        if ($this->limit_from !== null) {
1185
        if (is_null($rownum) && $this->limit_from !== null) {
1093
            if ($this->row_counter === null) {
1186
            if ($this->row_counter === null) {
1094
                $this->row_counter = $this->limit_from;
1187
                $this->row_counter = $this->limit_from;
1095
                // Skip rows
1188
                // Skip rows
1096
                if ($this->dbh->features['limit'] === false) {
1189
                if ($this->dbh->features['limit'] === false) {
1097
                    $i = 0;
1190
                    $i = 0;
1098
                    while ($i++ < $this->limit_from) {
1191
                    while ($i++ < $this->limit_from) {
1099
                        $this->dbh->fetchInto($this->result, $arr, $fetchmode);
1192
                        $this->dbh->fetchInto($this->result, $arr, $fetchmode);
1100
                    }
1193
                    }
1101
                }
1194
                }
1102
            }
1195
            }
1103
            if ($this->row_counter >= ($this->limit_from + $this->limit_count))
1196
            if ($this->row_counter >= ($this->limit_from + $this->limit_count))
1104
            {
1197
            {
1105
                if ($this->autofree) {
1198
                if ($this->autofree) {
1106
                    $this->free();
1199
                    $this->free();
1107
                }
1200
                }
1108
                $tmp = null;
1201
                $tmp = null;
1109
                return $tmp;
1202
                return $tmp;
1110
            }
1203
            }
1111
            if ($this->dbh->features['limit'] === 'emulate') {
1204
            if ($this->dbh->features['limit'] === 'emulate') {
1112
                $rownum = $this->row_counter;
1205
                $rownum = $this->row_counter;
1113
            }
1206
            }
1114
            $this->row_counter++;
1207
            $this->row_counter++;
1115
        }
1208
        }
1116
        $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum);
1209
        $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum);
1117
        if ($res === DB_OK) {
1210
        if ($res === DB_OK) {
1118
            if (isset($object_class)) {
1211
            if (isset($object_class)) {
1119
                // The default mode is specified in the
1212
                // The default mode is specified in the
1120
                // DB_common::fetchmode_object_class property
1213
                // DB_common::fetchmode_object_class property
1121
                if ($object_class == 'stdClass') {
1214
                if ($object_class == 'stdClass') {
1122
                    $arr = (object) $arr;
1215
                    $arr = (object) $arr;
1123
                } else {
1216
                } else {
1124
                    $arr = &new $object_class($arr);
1217
                    $arr = new $object_class($arr);
1125
                }
1218
                }
1126
            }
1219
            }
1127
            return $arr;
1220
            return $arr;
1128
        }
1221
        }
1129
        if ($res == null && $this->autofree) {
1222
        if ($res == null && $this->autofree) {
1130
            $this->free();
1223
            $this->free();
1131
        }
1224
        }
1132
        return $res;
1225
        return $res;
1133
    }
1226
    }
1134
 
1227
 
1135
    // }}}
1228
    // }}}
1136
    // {{{ fetchInto()
1229
    // {{{ fetchInto()
1137
 
1230
 
1138
    /**
1231
    /**
1139
     * Fetch a row of data into an array which is passed by reference
1232
     * Fetch a row of data into an array which is passed by reference
1140
     *
1233
     *
1141
     * The type of array returned can be controlled either by setting this
1234
     * The type of array returned can be controlled either by setting this
1142
     * method's <var>$fetchmode</var> parameter or by changing the default
1235
     * method's <var>$fetchmode</var> parameter or by changing the default
1143
     * fetch mode setFetchMode() before calling this method.
1236
     * fetch mode setFetchMode() before calling this method.
1144
     *
1237
     *
1145
     * There are two options for standardizing the information returned
1238
     * There are two options for standardizing the information returned
1146
     * from databases, ensuring their values are consistent when changing
1239
     * from databases, ensuring their values are consistent when changing
1147
     * DBMS's.  These portability options can be turned on when creating a
1240
     * DBMS's.  These portability options can be turned on when creating a
1148
     * new DB object or by using setOption().
1241
     * new DB object or by using setOption().
1149
     *
1242
     *
1150
     *   + <var>DB_PORTABILITY_LOWERCASE</var>
1243
     *   + <var>DB_PORTABILITY_LOWERCASE</var>
1151
     *     convert names of fields to lower case
1244
     *     convert names of fields to lower case
1152
     *
1245
     *
1153
     *   + <var>DB_PORTABILITY_RTRIM</var>
1246
     *   + <var>DB_PORTABILITY_RTRIM</var>
1154
     *     right trim the data
1247
     *     right trim the data
1155
     *
1248
     *
1156
     * @param array &$arr       the variable where the data should be placed
1249
     * @param array &$arr       the variable where the data should be placed
1157
     * @param int   $fetchmode  the constant indicating how to format the data
1250
     * @param int   $fetchmode  the constant indicating how to format the data
1158
     * @param int   $rownum     the row number to fetch (index starts at 0)
1251
     * @param int   $rownum     the row number to fetch (index starts at 0)
1159
     *
1252
     *
1160
     * @return mixed  DB_OK if a row is processed, NULL when the end of the
1253
     * @return mixed  DB_OK if a row is processed, NULL when the end of the
1161
     *                 result set is reached or a DB_Error object on failure
1254
     *                 result set is reached or a DB_Error object on failure
1162
     *
1255
     *
1163
     * @see DB_common::setOption(), DB_common::setFetchMode()
1256
     * @see DB_common::setOption(), DB_common::setFetchMode()
1164
     */
1257
     */
1165
    function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null)
1258
    function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum = null)
1166
    {
1259
    {
1167
        if ($fetchmode === DB_FETCHMODE_DEFAULT) {
1260
        if ($fetchmode === DB_FETCHMODE_DEFAULT) {
1168
            $fetchmode = $this->fetchmode;
1261
            $fetchmode = $this->fetchmode;
1169
        }
1262
        }
1170
        if ($fetchmode === DB_FETCHMODE_OBJECT) {
1263
        if ($fetchmode === DB_FETCHMODE_OBJECT) {
1171
            $fetchmode = DB_FETCHMODE_ASSOC;
1264
            $fetchmode = DB_FETCHMODE_ASSOC;
1172
            $object_class = $this->fetchmode_object_class;
1265
            $object_class = $this->fetchmode_object_class;
1173
        }
1266
        }
1174
        if ($this->limit_from !== null) {
1267
        if (is_null($rownum) && $this->limit_from !== null) {
1175
            if ($this->row_counter === null) {
1268
            if ($this->row_counter === null) {
1176
                $this->row_counter = $this->limit_from;
1269
                $this->row_counter = $this->limit_from;
1177
                // Skip rows
1270
                // Skip rows
1178
                if ($this->dbh->features['limit'] === false) {
1271
                if ($this->dbh->features['limit'] === false) {
1179
                    $i = 0;
1272
                    $i = 0;
1180
                    while ($i++ < $this->limit_from) {
1273
                    while ($i++ < $this->limit_from) {
1181
                        $this->dbh->fetchInto($this->result, $arr, $fetchmode);
1274
                        $this->dbh->fetchInto($this->result, $arr, $fetchmode);
1182
                    }
1275
                    }
1183
                }
1276
                }
1184
            }
1277
            }
1185
            if ($this->row_counter >= (
1278
            if ($this->row_counter >= (
1186
                    $this->limit_from + $this->limit_count))
1279
                    $this->limit_from + $this->limit_count))
1187
            {
1280
            {
1188
                if ($this->autofree) {
1281
                if ($this->autofree) {
1189
                    $this->free();
1282
                    $this->free();
1190
                }
1283
                }
1191
                return null;
1284
                return null;
1192
            }
1285
            }
1193
            if ($this->dbh->features['limit'] === 'emulate') {
1286
            if ($this->dbh->features['limit'] === 'emulate') {
1194
                $rownum = $this->row_counter;
1287
                $rownum = $this->row_counter;
1195
            }
1288
            }
1196
 
1289
 
1197
            $this->row_counter++;
1290
            $this->row_counter++;
1198
        }
1291
        }
1199
        $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum);
1292
        $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum);
1200
        if ($res === DB_OK) {
1293
        if ($res === DB_OK) {
1201
            if (isset($object_class)) {
1294
            if (isset($object_class)) {
1202
                // default mode specified in the
1295
                // default mode specified in the
1203
                // DB_common::fetchmode_object_class property
1296
                // DB_common::fetchmode_object_class property
1204
                if ($object_class == 'stdClass') {
1297
                if ($object_class == 'stdClass') {
1205
                    $arr = (object) $arr;
1298
                    $arr = (object) $arr;
1206
                } else {
1299
                } else {
1207
                    $arr = new $object_class($arr);
1300
                    $arr = new $object_class($arr);
1208
                }
1301
                }
1209
            }
1302
            }
1210
            return DB_OK;
1303
            return DB_OK;
1211
        }
1304
        }
1212
        if ($res == null && $this->autofree) {
1305
        if ($res == null && $this->autofree) {
1213
            $this->free();
1306
            $this->free();
1214
        }
1307
        }
1215
        return $res;
1308
        return $res;
1216
    }
1309
    }
1217
 
1310
 
1218
    // }}}
1311
    // }}}
1219
    // {{{ numCols()
1312
    // {{{ numCols()
1220
 
1313
 
1221
    /**
1314
    /**
1222
     * Get the the number of columns in a result set
1315
     * Get the the number of columns in a result set
1223
     *
1316
     *
1224
     * @return int  the number of columns.  A DB_Error object on failure.
1317
     * @return int  the number of columns.  A DB_Error object on failure.
1225
     */
1318
     */
1226
    function numCols()
1319
    function numCols()
1227
    {
1320
    {
1228
        return $this->dbh->numCols($this->result);
1321
        return $this->dbh->numCols($this->result);
1229
    }
1322
    }
1230
 
1323
 
1231
    // }}}
1324
    // }}}
1232
    // {{{ numRows()
1325
    // {{{ numRows()
1233
 
1326
 
1234
    /**
1327
    /**
1235
     * Get the number of rows in a result set
1328
     * Get the number of rows in a result set
1236
     *
1329
     *
1237
     * @return int  the number of rows.  A DB_Error object on failure.
1330
     * @return int  the number of rows.  A DB_Error object on failure.
1238
     */
1331
     */
1239
    function numRows()
1332
    function numRows()
1240
    {
1333
    {
1241
        if ($this->dbh->features['numrows'] === 'emulate'
1334
        if ($this->dbh->features['numrows'] === 'emulate'
1242
            && $this->dbh->options['portability'] & DB_PORTABILITY_NUMROWS)
1335
            && $this->dbh->options['portability'] & DB_PORTABILITY_NUMROWS)
1243
        {
1336
        {
1244
            if ($this->dbh->features['prepare']) {
1337
            if ($this->dbh->features['prepare']) {
1245
                $res = $this->dbh->query($this->query, $this->parameters);
1338
                $res = $this->dbh->query($this->query, $this->parameters);
1246
            } else {
1339
            } else {
1247
                $res = $this->dbh->query($this->query);
1340
                $res = $this->dbh->query($this->query);
1248
            }
1341
            }
1249
            if (DB::isError($res)) {
1342
            if (DB::isError($res)) {
1250
                return $res;
1343
                return $res;
1251
            }
1344
            }
1252
            $i = 0;
1345
            $i = 0;
1253
            while ($res->fetchInto($tmp, DB_FETCHMODE_ORDERED)) {
1346
            while ($res->fetchInto($tmp, DB_FETCHMODE_ORDERED)) {
1254
                $i++;
1347
                $i++;
1255
            }
1348
            }
1256
            return $i;
1349
            $count = $i;
1257
        } else {
1350
        } else {
1258
            return $this->dbh->numRows($this->result);
1351
            $count = $this->dbh->numRows($this->result);
1259
        }
1352
        }
-
 
1353
 
-
 
1354
        /* fbsql is checked for here because limit queries are implemented
-
 
1355
         * using a TOP() function, which results in fbsql_num_rows still
-
 
1356
         * returning the total number of rows that would have been returned,
-
 
1357
         * rather than the real number. As a result, we'll just do the limit
-
 
1358
         * calculations for fbsql in the same way as a database with emulated
-
 
1359
         * limits. Unfortunately, we can't just do this in DB_fbsql::numRows()
-
 
1360
         * because that only gets the result resource, rather than the full
-
 
1361
         * DB_Result object. */
-
 
1362
        if (($this->dbh->features['limit'] === 'emulate'
-
 
1363
             && $this->limit_from !== null)
-
 
1364
            || $this->dbh->phptype == 'fbsql') {
-
 
1365
            $limit_count = is_null($this->limit_count) ? $count : $this->limit_count;
-
 
1366
            if ($count < $this->limit_from) {
-
 
1367
                $count = 0;
-
 
1368
            } elseif ($count < ($this->limit_from + $limit_count)) {
-
 
1369
                $count -= $this->limit_from;
-
 
1370
            } else {
-
 
1371
                $count = $limit_count;
-
 
1372
            }
-
 
1373
        }
-
 
1374
 
-
 
1375
        return $count;
1260
    }
1376
    }
1261
 
1377
 
1262
    // }}}
1378
    // }}}
1263
    // {{{ nextResult()
1379
    // {{{ nextResult()
1264
 
1380
 
1265
    /**
1381
    /**
1266
     * Get the next result if a batch of queries was executed
1382
     * Get the next result if a batch of queries was executed
1267
     *
1383
     *
1268
     * @return bool  true if a new result is available or false if not
1384
     * @return bool  true if a new result is available or false if not
1269
     */
1385
     */
1270
    function nextResult()
1386
    function nextResult()
1271
    {
1387
    {
1272
        return $this->dbh->nextResult($this->result);
1388
        return $this->dbh->nextResult($this->result);
1273
    }
1389
    }
1274
 
1390
 
1275
    // }}}
1391
    // }}}
1276
    // {{{ free()
1392
    // {{{ free()
1277
 
1393
 
1278
    /**
1394
    /**
1279
     * Frees the resources allocated for this result set
1395
     * Frees the resources allocated for this result set
1280
     *
1396
     *
1281
     * @return bool  true on success.  A DB_Error object on failure.
1397
     * @return bool  true on success.  A DB_Error object on failure.
1282
     */
1398
     */
1283
    function free()
1399
    function free()
1284
    {
1400
    {
1285
        $err = $this->dbh->freeResult($this->result);
1401
        $err = $this->dbh->freeResult($this->result);
1286
        if (DB::isError($err)) {
1402
        if (DB::isError($err)) {
1287
            return $err;
1403
            return $err;
1288
        }
1404
        }
1289
        $this->result = false;
1405
        $this->result = false;
1290
        $this->statement = false;
1406
        $this->statement = false;
1291
        return true;
1407
        return true;
1292
    }
1408
    }
1293
 
1409
 
1294
    // }}}
1410
    // }}}
1295
    // {{{ tableInfo()
1411
    // {{{ tableInfo()
1296
 
1412
 
1297
    /**
1413
    /**
1298
     * @see DB_common::tableInfo()
1414
     * @see DB_common::tableInfo()
1299
     * @deprecated Method deprecated some time before Release 1.2
1415
     * @deprecated Method deprecated some time before Release 1.2
1300
     */
1416
     */
1301
    function tableInfo($mode = null)
1417
    function tableInfo($mode = null)
1302
    {
1418
    {
1303
        if (is_string($mode)) {
1419
        if (is_string($mode)) {
1304
            return $this->dbh->raiseError(DB_ERROR_NEED_MORE_DATA);
1420
            return $this->dbh->raiseError(DB_ERROR_NEED_MORE_DATA);
1305
        }
1421
        }
1306
        return $this->dbh->tableInfo($this, $mode);
1422
        return $this->dbh->tableInfo($this, $mode);
1307
    }
1423
    }
1308
 
1424
 
1309
    // }}}
1425
    // }}}
1310
    // {{{ getQuery()
1426
    // {{{ getQuery()
1311
 
1427
 
1312
    /**
1428
    /**
1313
     * Determine the query string that created this result
1429
     * Determine the query string that created this result
1314
     *
1430
     *
1315
     * @return string  the query string
1431
     * @return string  the query string
1316
     *
1432
     *
1317
     * @since Method available since Release 1.7.0
1433
     * @since Method available since Release 1.7.0
1318
     */
1434
     */
1319
    function getQuery()
1435
    function getQuery()
1320
    {
1436
    {
1321
        return $this->query;
1437
        return $this->query;
1322
    }
1438
    }
1323
 
1439
 
1324
    // }}}
1440
    // }}}
1325
    // {{{ getRowCounter()
1441
    // {{{ getRowCounter()
1326
 
1442
 
1327
    /**
1443
    /**
1328
     * Tells which row number is currently being processed
1444
     * Tells which row number is currently being processed
1329
     *
1445
     *
1330
     * @return integer  the current row being looked at.  Starts at 1.
1446
     * @return integer  the current row being looked at.  Starts at 1.
1331
     */
1447
     */
1332
    function getRowCounter()
1448
    function getRowCounter()
1333
    {
1449
    {
1334
        return $this->row_counter;
1450
        return $this->row_counter;
1335
    }
1451
    }
1336
 
1452
 
1337
    // }}}
1453
    // }}}
1338
}
1454
}
1339
 
1455
 
1340
// }}}
1456
// }}}
1341
// {{{ class DB_row
1457
// {{{ class DB_row
1342
 
1458
 
1343
/**
1459
/**
1344
 * PEAR DB Row Object
1460
 * PEAR DB Row Object
1345
 *
1461
 *
1346
 * The object contains a row of data from a result set.  Each column's data
1462
 * The object contains a row of data from a result set.  Each column's data
1347
 * is placed in a property named for the column.
1463
 * is placed in a property named for the column.
1348
 *
1464
 *
1349
 * @category   Database
1465
 * @category   Database
1350
 * @package    DB
1466
 * @package    DB
1351
 * @author     Stig Bakken <ssb@php.net>
1467
 * @author     Stig Bakken <ssb@php.net>
1352
 * @copyright  1997-2005 The PHP Group
1468
 * @copyright  1997-2007 The PHP Group
1353
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
1469
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
1354
 * @version    Release: 1.7.6
1470
 * @version    Release: 1.9.2
1355
 * @link       http://pear.php.net/package/DB
1471
 * @link       http://pear.php.net/package/DB
1356
 * @see        DB_common::setFetchMode()
1472
 * @see        DB_common::setFetchMode()
1357
 */
1473
 */
1358
class DB_row
1474
class DB_row
1359
{
1475
{
1360
    // {{{ constructor
1476
    // {{{ constructor
1361
 
1477
 
1362
    /**
1478
    /**
1363
     * The constructor places a row's data into properties of this object
1479
     * The constructor places a row's data into properties of this object
1364
     *
1480
     *
1365
     * @param array  the array containing the row's data
1481
     * @param array  the array containing the row's data
1366
     *
1482
     *
1367
     * @return void
1483
     * @return void
1368
     */
1484
     */
1369
    function DB_row(&$arr)
1485
    function __construct(&$arr)
1370
    {
1486
    {
1371
        foreach ($arr as $key => $value) {
1487
        foreach ($arr as $key => $value) {
1372
            $this->$key = &$arr[$key];
1488
            $this->$key = &$arr[$key];
1373
        }
1489
        }
1374
    }
1490
    }
1375
 
1491
 
1376
    // }}}
1492
    // }}}
1377
}
1493
}
1378
 
1494
 
1379
// }}}
1495
// }}}
1380
 
1496
 
1381
/*
1497
/*
1382
 * Local variables:
1498
 * Local variables:
1383
 * tab-width: 4
1499
 * tab-width: 4
1384
 * c-basic-offset: 4
1500
 * c-basic-offset: 4
1385
 * End:
1501
 * End:
1386
 */
1502
 */
1387
 
1503
 
1388
?>
1504
?>