Subversion-Projekte lars-tiefland.niewerth

Revision

Revision 7 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// +----------------------------------------------------------------------+
3
// | PHP versions 4 and 5                                                 |
4
// +----------------------------------------------------------------------+
5
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox,                 |
6
// | Stig. S. Bakken, Lukas Smith                                         |
7
// | All rights reserved.                                                 |
8
// +----------------------------------------------------------------------+
9
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
10
// | API as well as database abstraction for PHP applications.            |
11
// | This LICENSE is in the BSD license style.                            |
12
// |                                                                      |
13
// | Redistribution and use in source and binary forms, with or without   |
14
// | modification, are permitted provided that the following conditions   |
15
// | are met:                                                             |
16
// |                                                                      |
17
// | Redistributions of source code must retain the above copyright       |
18
// | notice, this list of conditions and the following disclaimer.        |
19
// |                                                                      |
20
// | Redistributions in binary form must reproduce the above copyright    |
21
// | notice, this list of conditions and the following disclaimer in the  |
22
// | documentation and/or other materials provided with the distribution. |
23
// |                                                                      |
24
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
25
// | Lukas Smith nor the names of his contributors may be used to endorse |
26
// | or promote products derived from this software without specific prior|
27
// | written permission.                                                  |
28
// |                                                                      |
29
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
30
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
31
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
32
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
33
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
34
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
35
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
36
// |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
37
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
38
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
39
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
40
// | POSSIBILITY OF SUCH DAMAGE.                                          |
41
// +----------------------------------------------------------------------+
42
// | Author: Lukas Smith <smith@pooteeweet.org>                           |
43
// +----------------------------------------------------------------------+
44
//
45
// $Id: Extended.php,v 1.57 2006/10/09 17:09:19 davidc Exp $
46
 
47
/**
48
 * @package  MDB2
49
 * @category Database
50
 * @author   Lukas Smith <smith@pooteeweet.org>
51
 */
52
 
53
/**
54
 * Used by autoPrepare()
55
 */
56
define('MDB2_AUTOQUERY_INSERT', 1);
57
define('MDB2_AUTOQUERY_UPDATE', 2);
58
define('MDB2_AUTOQUERY_DELETE', 3);
59
define('MDB2_AUTOQUERY_SELECT', 4);
60
 
61
/**
62
 * MDB2_Extended: class which adds several high level methods to MDB2
63
 *
64
 * @package MDB2
65
 * @category Database
66
 * @author Lukas Smith <smith@pooteeweet.org>
67
 */
68
class MDB2_Extended extends MDB2_Module_Common
69
{
70
    // {{{ autoPrepare()
71
 
72
    /**
73
     * Generate an insert, update or delete query and call prepare() on it
74
     *
75
     * @param string table
76
     * @param array the fields names
77
     * @param int type of query to build
78
     *                          MDB2_AUTOQUERY_INSERT
79
     *                          MDB2_AUTOQUERY_UPDATE
80
     *                          MDB2_AUTOQUERY_DELETE
81
     *                          MDB2_AUTOQUERY_SELECT
82
     * @param string (in case of update and delete queries, this string will be put after the sql WHERE statement)
83
     * @param array that contains the types of the placeholders
84
     * @param mixed array that contains the types of the columns in
85
     *                        the result set or MDB2_PREPARE_RESULT, if set to
86
     *                        MDB2_PREPARE_MANIP the query is handled as a manipulation query
87
     *
88
     * @return resource handle for the query
89
     * @see buildManipSQL
90
     * @access public
91
     */
92
    function autoPrepare($table, $table_fields, $mode = MDB2_AUTOQUERY_INSERT,
93
        $where = false, $types = null, $result_types = MDB2_PREPARE_MANIP)
94
    {
95
        $query = $this->buildManipSQL($table, $table_fields, $mode, $where);
96
        if (PEAR::isError($query)) {
97
            return $query;
98
        }
99
        $db =& $this->getDBInstance();
100
        if (PEAR::isError($db)) {
101
            return $db;
102
        }
103
        return $db->prepare($query, $types, $result_types);
104
    }
105
    // }}}
106
 
107
    // {{{ autoExecute()
108
 
109
    /**
110
     * Generate an insert, update or delete query and call prepare() and execute() on it
111
     *
112
     * @param string name of the table
113
     * @param array assoc ($key=>$value) where $key is a field name and $value its value
114
     * @param int type of query to build
115
     *                          MDB2_AUTOQUERY_INSERT
116
     *                          MDB2_AUTOQUERY_UPDATE
117
     *                          MDB2_AUTOQUERY_DELETE
118
     *                          MDB2_AUTOQUERY_SELECT
119
     * @param string (in case of update and delete queries, this string will be put after the sql WHERE statement)
120
     * @param array that contains the types of the placeholders
121
     * @param string which specifies which result class to use
122
     * @param mixed  array that contains the types of the columns in
123
     *                        the result set or MDB2_PREPARE_RESULT, if set to
124
     *                        MDB2_PREPARE_MANIP the query is handled as a manipulation query
125
     *
126
     * @return bool|MDB2_Error true on success, a MDB2 error on failure
127
     * @see buildManipSQL
128
     * @see autoPrepare
129
     * @access public
130
    */
131
    function &autoExecute($table, $fields_values, $mode = MDB2_AUTOQUERY_INSERT,
132
        $where = false, $types = null, $result_class = true, $result_types = MDB2_PREPARE_MANIP)
133
    {
134
        $fields_values = (array)$fields_values;
135
        if ($mode == MDB2_AUTOQUERY_SELECT) {
136
            if (is_array($result_types)) {
137
                $keys = array_keys($result_types);
138
            } elseif (!empty($fields_values)) {
139
                $keys = $fields_values;
140
            } else {
141
                $keys = array();
142
            }
143
        } else {
144
            $keys = array_keys($fields_values);
145
        }
146
        $params = array_values($fields_values);
147
        if (empty($params)) {
148
            $query = $this->buildManipSQL($table, $keys, $mode, $where);
149
 
150
            $db =& $this->getDBInstance();
151
            if (PEAR::isError($db)) {
152
                return $db;
153
            }
154
            if ($mode == MDB2_AUTOQUERY_SELECT) {
155
                $result =& $db->query($query, $result_types, $result_class);
156
            } else {
157
                $result = $db->exec($query);
158
            }
159
        } else {
160
            $stmt = $this->autoPrepare($table, $keys, $mode, $where, $types, $result_types);
161
            if (PEAR::isError($stmt)) {
162
                return $stmt;
163
            }
164
            $result =& $stmt->execute($params, $result_class);
165
            $stmt->free();
166
        }
167
        return $result;
168
    }
169
    // }}}
170
 
171
    // {{{ buildManipSQL()
172
 
173
    /**
174
     * Make automaticaly an sql query for prepare()
175
     *
176
     * Example : buildManipSQL('table_sql', array('field1', 'field2', 'field3'), MDB2_AUTOQUERY_INSERT)
177
     *           will return the string : INSERT INTO table_sql (field1,field2,field3) VALUES (?,?,?)
178
     * NB : - This belongs more to a SQL Builder class, but this is a simple facility
179
     *      - Be carefull ! If you don't give a $where param with an UPDATE/DELETE query, all
180
     *        the records of the table will be updated/deleted !
181
     *
182
     * @param string name of the table
183
     * @param ordered array containing the fields names
184
     * @param int type of query to build
185
     *                          MDB2_AUTOQUERY_INSERT
186
     *                          MDB2_AUTOQUERY_UPDATE
187
     *                          MDB2_AUTOQUERY_DELETE
188
     *                          MDB2_AUTOQUERY_SELECT
189
     * @param string (in case of update and delete queries, this string will be put after the sql WHERE statement)
190
     *
191
     * @return string sql query for prepare()
192
     * @access public
193
     */
194
    function buildManipSQL($table, $table_fields, $mode, $where = false)
195
    {
196
        $db =& $this->getDBInstance();
197
        if (PEAR::isError($db)) {
198
            return $db;
199
        }
200
 
201
        if (!empty($table_fields) && $db->options['quote_identifier']) {
202
            foreach ($table_fields as $key => $field) {
203
                $table_fields[$key] = $db->quoteIdentifier($field);
204
            }
205
        }
206
 
207
        if ($where !== false && !is_null($where)) {
208
            if (is_array($where)) {
209
                $where = implode(' AND ', $where);
210
            }
211
            $where = ' WHERE '.$where;
212
        }
213
 
214
        switch ($mode) {
215
        case MDB2_AUTOQUERY_INSERT:
216
            if (empty($table_fields)) {
217
                return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
218
                'Insert requires table fields', __FUNCTION__);
219
            }
220
            $cols = implode(', ', $table_fields);
221
            $values = '?'.str_repeat(', ?', (count($table_fields) - 1));
222
            return 'INSERT INTO '.$table.' ('.$cols.') VALUES ('.$values.')';
223
            break;
224
        case MDB2_AUTOQUERY_UPDATE:
225
            if (empty($table_fields)) {
226
                return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
227
                'Update requires table fields', __FUNCTION__);
228
            }
229
            $set = implode(' = ?, ', $table_fields).' = ?';
230
            $sql = 'UPDATE '.$table.' SET '.$set.$where;
231
            return $sql;
232
            break;
233
        case MDB2_AUTOQUERY_DELETE:
234
            $sql = 'DELETE FROM '.$table.$where;
235
            return $sql;
236
            break;
237
        case MDB2_AUTOQUERY_SELECT:
238
            $cols = !empty($table_fields) ? implode(', ', $table_fields) : '*';
239
            $sql = 'SELECT '.$cols.' FROM '.$table.$where;
240
            return $sql;
241
            break;
242
        }
243
        return $db->raiseError(MDB2_ERROR_SYNTAX, null, null,
244
                'Non existant mode', __FUNCTION__);
245
    }
246
    // }}}
247
 
248
    // {{{ limitQuery()
249
 
250
    /**
251
     * Generates a limited query
252
     *
253
     * @param string query
254
     * @param array that contains the types of the columns in the result set
255
     * @param integer the numbers of rows to fetch
256
     * @param integer the row to start to fetching
257
     * @param string which specifies which result class to use
258
     * @param mixed   string which specifies which class to wrap results in
259
     *
260
     * @return MDB2_Result|MDB2_Error result set on success, a MDB2 error on failure
261
     * @access public
262
     */
263
    function &limitQuery($query, $types, $limit, $offset = 0, $result_class = true,
264
        $result_wrap_class = false)
265
    {
266
        $db =& $this->getDBInstance();
267
        if (PEAR::isError($db)) {
268
            return $db;
269
        }
270
 
271
        $result = $db->setLimit($limit, $offset);
272
        if (PEAR::isError($result)) {
273
            return $result;
274
        }
275
        $result =& $db->query($query, $types, $result_class, $result_wrap_class);
276
        return $result;
277
    }
278
    // }}}
279
 
280
    // {{{ execParam()
281
 
282
    /**
283
     * Execute a parameterized DML statement.
284
     *
285
     * @param string the SQL query
286
     * @param array if supplied, prepare/execute will be used
287
     *       with this array as execute parameters
288
     * @param array that contains the types of the values defined in $params
289
     *
290
     * @return int|MDB2_Error affected rows on success, a MDB2 error on failure
291
     * @access public
292
     */
293
    function execParam($query, $params = array(), $param_types = null)
294
    {
295
        $db =& $this->getDBInstance();
296
        if (PEAR::isError($db)) {
297
            return $db;
298
        }
299
 
300
        settype($params, 'array');
301
        if (empty($params)) {
302
            return $db->exec($query);
303
        }
304
 
305
        $stmt = $db->prepare($query, $param_types, MDB2_PREPARE_MANIP);
306
        if (PEAR::isError($stmt)) {
307
            return $stmt;
308
        }
309
 
310
        $result = $stmt->execute($params);
311
        if (PEAR::isError($result)) {
312
            return $result;
313
        }
314
 
315
        $stmt->free();
316
        return $result;
317
    }
318
    // }}}
319
 
320
    // {{{ getOne()
321
 
322
    /**
323
     * Fetch the first column of the first row of data returned from a query.
324
     * Takes care of doing the query and freeing the results when finished.
325
     *
326
     * @param string the SQL query
327
     * @param string that contains the type of the column in the result set
328
     * @param array if supplied, prepare/execute will be used
329
     *       with this array as execute parameters
330
     * @param array that contains the types of the values defined in $params
331
     * @param int|string which column to return
332
     *
333
     * @return scalar|MDB2_Error data on success, a MDB2 error on failure
334
     * @access public
335
     */
336
    function getOne($query, $type = null, $params = array(),
337
        $param_types = null, $colnum = 0)
338
    {
339
        $db =& $this->getDBInstance();
340
        if (PEAR::isError($db)) {
341
            return $db;
342
        }
343
 
344
        settype($params, 'array');
345
        settype($type, 'array');
346
        if (empty($params)) {
347
            return $db->queryOne($query, $type, $colnum);
348
        }
349
 
350
        $stmt = $db->prepare($query, $param_types, $type);
351
        if (PEAR::isError($stmt)) {
352
            return $stmt;
353
        }
354
 
355
        $result = $stmt->execute($params);
356
        if (!MDB2::isResultCommon($result)) {
357
            return $result;
358
        }
359
 
360
        $one = $result->fetchOne($colnum);
361
        $stmt->free();
362
        $result->free();
363
        return $one;
364
    }
365
    // }}}
366
 
367
    // {{{ getRow()
368
 
369
    /**
370
     * Fetch the first row of data returned from a query.  Takes care
371
     * of doing the query and freeing the results when finished.
372
     *
373
     * @param string the SQL query
374
     * @param array that contains the types of the columns in the result set
375
     * @param array if supplied, prepare/execute will be used
376
     *       with this array as execute parameters
377
     * @param array that contains the types of the values defined in $params
378
     * @param int the fetch mode to use
379
     *
380
     * @return array|MDB2_Error data on success, a MDB2 error on failure
381
     * @access public
382
     */
383
    function getRow($query, $types = null, $params = array(),
384
        $param_types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT)
385
    {
386
        $db =& $this->getDBInstance();
387
        if (PEAR::isError($db)) {
388
            return $db;
389
        }
390
 
391
        settype($params, 'array');
392
        if (empty($params)) {
393
            return $db->queryRow($query, $types, $fetchmode);
394
        }
395
 
396
        $stmt = $db->prepare($query, $param_types, $types);
397
        if (PEAR::isError($stmt)) {
398
            return $stmt;
399
        }
400
 
401
        $result = $stmt->execute($params);
402
        if (!MDB2::isResultCommon($result)) {
403
            return $result;
404
        }
405
 
406
        $row = $result->fetchRow($fetchmode);
407
        $stmt->free();
408
        $result->free();
409
        return $row;
410
    }
411
    // }}}
412
 
413
    // {{{ getCol()
414
 
415
    /**
416
     * Fetch a single column from a result set and return it as an
417
     * indexed array.
418
     *
419
     * @param string the SQL query
420
     * @param string that contains the type of the column in the result set
421
     * @param array if supplied, prepare/execute will be used
422
     *       with this array as execute parameters
423
     * @param array that contains the types of the values defined in $params
424
     * @param int|string which column to return
425
     *
426
     * @return array|MDB2_Error data on success, a MDB2 error on failure
427
     * @access public
428
     */
429
    function getCol($query, $type = null, $params = array(),
430
        $param_types = null, $colnum = 0)
431
    {
432
        $db =& $this->getDBInstance();
433
        if (PEAR::isError($db)) {
434
            return $db;
435
        }
436
 
437
        settype($params, 'array');
438
        settype($type, 'array');
439
        if (empty($params)) {
440
            return $db->queryCol($query, $type, $colnum);
441
        }
442
 
443
        $stmt = $db->prepare($query, $param_types, $type);
444
        if (PEAR::isError($stmt)) {
445
            return $stmt;
446
        }
447
 
448
        $result = $stmt->execute($params);
449
        if (!MDB2::isResultCommon($result)) {
450
            return $result;
451
        }
452
 
453
        $col = $result->fetchCol($colnum);
454
        $stmt->free();
455
        $result->free();
456
        return $col;
457
    }
458
    // }}}
459
 
460
    // {{{ getAll()
461
 
462
    /**
463
     * Fetch all the rows returned from a query.
464
     *
465
     * @param string the SQL query
466
     * @param array that contains the types of the columns in the result set
467
     * @param array if supplied, prepare/execute will be used
468
     *       with this array as execute parameters
469
     * @param array that contains the types of the values defined in $params
470
     * @param int the fetch mode to use
471
     * @param bool if set to true, the $all will have the first
472
     *       column as its first dimension
473
     * @param bool $force_array used only when the query returns exactly
474
     *       two columns. If true, the values of the returned array will be
475
     *       one-element arrays instead of scalars.
476
     * @param bool $group if true, the values of the returned array is
477
     *       wrapped in another array.  If the same key value (in the first
478
     *       column) repeats itself, the values will be appended to this array
479
     *       instead of overwriting the existing values.
480
     *
481
     * @return array|MDB2_Error data on success, a MDB2 error on failure
482
     * @access public
483
     */
484
    function getAll($query, $types = null, $params = array(),
485
        $param_types = null, $fetchmode = MDB2_FETCHMODE_DEFAULT,
486
        $rekey = false, $force_array = false, $group = false)
487
    {
488
        $db =& $this->getDBInstance();
489
        if (PEAR::isError($db)) {
490
            return $db;
491
        }
492
 
493
        settype($params, 'array');
494
        if (empty($params)) {
495
            return $db->queryAll($query, $types, $fetchmode, $rekey, $force_array, $group);
496
        }
497
 
498
        $stmt = $db->prepare($query, $param_types, $types);
499
        if (PEAR::isError($stmt)) {
500
            return $stmt;
501
        }
502
 
503
        $result = $stmt->execute($params);
504
        if (!MDB2::isResultCommon($result)) {
505
            return $result;
506
        }
507
 
508
        $all = $result->fetchAll($fetchmode, $rekey, $force_array, $group);
509
        $stmt->free();
510
        $result->free();
511
        return $all;
512
    }
513
    // }}}
514
 
515
    // {{{ getAssoc()
516
 
517
    /**
518
     * Fetch the entire result set of a query and return it as an
519
     * associative array using the first column as the key.
520
     *
521
     * If the result set contains more than two columns, the value
522
     * will be an array of the values from column 2-n.  If the result
523
     * set contains only two columns, the returned value will be a
524
     * scalar with the value of the second column (unless forced to an
525
     * array with the $force_array parameter).  A MDB2 error code is
526
     * returned on errors.  If the result set contains fewer than two
527
     * columns, a MDB2_ERROR_TRUNCATED error is returned.
528
     *
529
     * For example, if the table 'mytable' contains:
530
     *
531
     *   ID      TEXT       DATE
532
     * --------------------------------
533
     *   1       'one'      944679408
534
     *   2       'two'      944679408
535
     *   3       'three'    944679408
536
     *
537
     * Then the call getAssoc('SELECT id,text FROM mytable') returns:
538
     *    array(
539
     *      '1' => 'one',
540
     *      '2' => 'two',
541
     *      '3' => 'three',
542
     *    )
543
     *
544
     * ...while the call getAssoc('SELECT id,text,date FROM mytable') returns:
545
     *    array(
546
     *      '1' => array('one', '944679408'),
547
     *      '2' => array('two', '944679408'),
548
     *      '3' => array('three', '944679408')
549
     *    )
550
     *
551
     * If the more than one row occurs with the same value in the
552
     * first column, the last row overwrites all previous ones by
553
     * default.  Use the $group parameter if you don't want to
554
     * overwrite like this.  Example:
555
     *
556
     * getAssoc('SELECT category,id,name FROM mytable', null, null
557
     *           MDB2_FETCHMODE_ASSOC, false, true) returns:
558
     *    array(
559
     *      '1' => array(array('id' => '4', 'name' => 'number four'),
560
     *                   array('id' => '6', 'name' => 'number six')
561
     *             ),
562
     *      '9' => array(array('id' => '4', 'name' => 'number four'),
563
     *                   array('id' => '6', 'name' => 'number six')
564
     *             )
565
     *    )
566
     *
567
     * Keep in mind that database functions in PHP usually return string
568
     * values for results regardless of the database's internal type.
569
     *
570
     * @param string the SQL query
571
     * @param array that contains the types of the columns in the result set
572
     * @param array if supplied, prepare/execute will be used
573
     *       with this array as execute parameters
574
     * @param array that contains the types of the values defined in $params
575
     * @param bool $force_array used only when the query returns
576
     * exactly two columns.  If TRUE, the values of the returned array
577
     * will be one-element arrays instead of scalars.
578
     * @param bool $group if TRUE, the values of the returned array
579
     *       is wrapped in another array.  If the same key value (in the first
580
     *       column) repeats itself, the values will be appended to this array
581
     *       instead of overwriting the existing values.
582
     *
583
     * @return array|MDB2_Error data on success, a MDB2 error on failure
584
     * @access public
585
     */
586
    function getAssoc($query, $types = null, $params = array(), $param_types = null,
587
        $fetchmode = MDB2_FETCHMODE_DEFAULT, $force_array = false, $group = false)
588
    {
589
        $db =& $this->getDBInstance();
590
        if (PEAR::isError($db)) {
591
            return $db;
592
        }
593
 
594
        settype($params, 'array');
595
        if (empty($params)) {
596
            return $db->queryAll($query, $types, $fetchmode, true, $force_array, $group);
597
        }
598
 
599
        $stmt = $db->prepare($query, $param_types, $types);
600
        if (PEAR::isError($stmt)) {
601
            return $stmt;
602
        }
603
 
604
        $result = $stmt->execute($params);
605
        if (!MDB2::isResultCommon($result)) {
606
            return $result;
607
        }
608
 
609
        $all = $result->fetchAll($fetchmode, true, $force_array, $group);
610
        $stmt->free();
611
        $result->free();
612
        return $all;
613
    }
614
    // }}}
615
 
616
    // {{{ executeMultiple()
617
 
618
    /**
619
     * This function does several execute() calls on the same statement handle.
620
     * $params must be an array indexed numerically from 0, one execute call is
621
     * done for every 'row' in the array.
622
     *
623
     * If an error occurs during execute(), executeMultiple() does not execute
624
     * the unfinished rows, but rather returns that error.
625
     *
626
     * @param resource query handle from prepare()
627
     * @param array numeric array containing the data to insert into the query
628
     *
629
     * @return bool|MDB2_Error true on success, a MDB2 error on failure
630
     * @access public
631
     * @see prepare(), execute()
632
     */
633
    function executeMultiple(&$stmt, $params = null)
634
    {
635
        for ($i = 0, $j = count($params); $i < $j; $i++) {
636
            $result = $stmt->execute($params[$i]);
637
            if (PEAR::isError($result)) {
638
                return $result;
639
            }
640
        }
641
        return MDB2_OK;
642
    }
643
    // }}}
644
 
645
    // {{{ getBeforeID()
646
 
647
    /**
648
     * Returns the next free id of a sequence if the RDBMS
649
     * does not support auto increment
650
     *
651
     * @param string name of the table into which a new row was inserted
652
     * @param string name of the field into which a new row was inserted
653
     * @param bool when true the sequence is automatic created, if it not exists
654
     * @param bool if the returned value should be quoted
655
     *
656
     * @return int|MDB2_Error id on success, a MDB2 error on failure
657
     * @access public
658
     */
659
    function getBeforeID($table, $field = null, $ondemand = true, $quote = true)
660
    {
661
        $db =& $this->getDBInstance();
662
        if (PEAR::isError($db)) {
663
            return $db;
664
        }
665
 
666
        if ($db->supports('auto_increment') !== true) {
667
            $seq = $table.(empty($field) ? '' : '_'.$field);
668
            $id = $db->nextID($seq, $ondemand);
669
            if (!$quote || PEAR::isError($id)) {
670
                return $id;
671
            }
672
            return $db->quote($id, 'integer');
673
        } elseif (!$quote) {
674
            return null;
675
        }
676
        return 'NULL';
677
    }
678
    // }}}
679
 
680
    // {{{ getAfterID()
681
 
682
    /**
683
     * Returns the autoincrement ID if supported or $id
684
     *
685
     * @param mixed value as returned by getBeforeId()
686
     * @param string name of the table into which a new row was inserted
687
     * @param string name of the field into which a new row was inserted
688
     *
689
     * @return int|MDB2_Error id on success, a MDB2 error on failure
690
     * @access public
691
     */
692
    function getAfterID($id, $table, $field = null)
693
    {
694
        $db =& $this->getDBInstance();
695
        if (PEAR::isError($db)) {
696
            return $db;
697
        }
698
 
699
        if ($db->supports('auto_increment') !== true) {
700
            return $id;
701
        }
702
        return $db->lastInsertID($table, $field);
703
    }
704
    // }}}
705
}
706
?>