Subversion Repositories Applications.gtt

Rev

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

Rev 94 Rev 187
Line 3... Line 3...
3
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
Line 4... Line 4...
4
 
4
 
5
/**
5
/**
6
 * Contains the DB_common base class
6
 * Contains the DB_common base class
7
 *
7
 *
8
 * PHP versions 4 and 5
8
 * PHP 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
Line 16... Line 16...
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: common.php,v 1.137 2005/04/07 14:27:35 danielc Exp $
23
 * @version    CVS: $Id$
24
 * @link       http://pear.php.net/package/DB
24
 * @link       http://pear.php.net/package/DB
25
 */
25
 */
Line 26... Line 26...
26
 
26
 
27
/**
27
/**
Line 38... Line 38...
38
 * @category   Database
38
 * @category   Database
39
 * @package    DB
39
 * @package    DB
40
 * @author     Stig Bakken <ssb@php.net>
40
 * @author     Stig Bakken <ssb@php.net>
41
 * @author     Tomas V.V. Cox <cox@idecnet.com>
41
 * @author     Tomas V.V. Cox <cox@idecnet.com>
42
 * @author     Daniel Convissor <danielc@php.net>
42
 * @author     Daniel Convissor <danielc@php.net>
43
 * @copyright  1997-2005 The PHP Group
43
 * @copyright  1997-2007 The PHP Group
44
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
44
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
45
 * @version    Release: 1.7.6
45
 * @version    Release: 1.9.2
46
 * @link       http://pear.php.net/package/DB
46
 * @link       http://pear.php.net/package/DB
47
 */
47
 */
48
class DB_common extends PEAR
48
class DB_common extends PEAR
49
{
49
{
50
    // {{{ properties
50
    // {{{ properties
Line 119... Line 119...
119
     * The prepared queries
119
     * The prepared queries
120
     * @var array
120
     * @var array
121
     */
121
     */
122
    var $prepared_queries = array();
122
    var $prepared_queries = array();
Line -... Line 123...
-
 
123
 
-
 
124
    /**
-
 
125
     * Flag indicating that the last query was a manipulation query.
-
 
126
     * @access protected
-
 
127
     * @var boolean
-
 
128
     */
-
 
129
    var $_last_query_manip = false;
-
 
130
 
-
 
131
    /**
-
 
132
     * Flag indicating that the next query <em>must</em> be a manipulation
-
 
133
     * query.
-
 
134
     * @access protected
-
 
135
     * @var boolean
-
 
136
     */
-
 
137
    var $_next_query_manip = false;
Line 123... Line 138...
123
 
138
 
124
 
139
 
Line 125... Line 140...
125
    // }}}
140
    // }}}
126
    // {{{ DB_common
141
    // {{{ DB_common
127
 
142
 
128
    /**
143
    /**
129
     * This constructor calls <kbd>$this->PEAR('DB_Error')</kbd>
144
     * This constructor calls <kbd>$this->PEAR('DB_Error')</kbd>
130
     *
145
     *
131
     * @return void
146
     * @return void
132
     */
147
     */
133
    function DB_common()
148
    function __construct()
Line 134... Line 149...
134
    {
149
    {
Line 187... Line 202...
187
     * @return void
202
     * @return void
188
     */
203
     */
189
    function __wakeup()
204
    function __wakeup()
190
    {
205
    {
191
        if ($this->was_connected) {
206
        if ($this->was_connected) {
192
            $this->connect($this->dsn, $this->options);
207
            $this->connect($this->dsn, $this->options['persistent']);
193
        }
208
        }
194
    }
209
    }
Line 195... Line 210...
195
 
210
 
196
    // }}}
211
    // }}}
Line 244... Line 259...
244
     * @see DB_common::quoteSmart(), DB_common::escapeSimple()
259
     * @see DB_common::quoteSmart(), DB_common::escapeSimple()
245
     * @deprecated Method deprecated some time before Release 1.2
260
     * @deprecated Method deprecated some time before Release 1.2
246
     */
261
     */
247
    function quoteString($string)
262
    function quoteString($string)
248
    {
263
    {
249
        $string = $this->quote($string);
264
        $string = $this->quoteSmart($string);
250
        if ($string{0} == "'") {
265
        if ($string{0} == "'") {
251
            return substr($string, 1, -1);
266
            return substr($string, 1, -1);
252
        }
267
        }
253
        return $string;
268
        return $string;
254
    }
269
    }
Line 267... Line 282...
267
     * @see DB_common::quoteSmart(), DB_common::escapeSimple()
282
     * @see DB_common::quoteSmart(), DB_common::escapeSimple()
268
     * @deprecated Deprecated in release 1.6.0
283
     * @deprecated Deprecated in release 1.6.0
269
     */
284
     */
270
    function quote($string = null)
285
    function quote($string = null)
271
    {
286
    {
272
        return ($string === null) ? 'NULL'
287
        return $this->quoteSmart($string);
273
                                  : "'" . str_replace("'", "''", $string) . "'";
-
 
274
    }
288
    }
Line 275... Line 289...
275
 
289
 
276
    // }}}
290
    // }}}
Line 422... Line 436...
422
     * @see DB_common::escapeSimple()
436
     * @see DB_common::escapeSimple()
423
     * @since Method available since Release 1.6.0
437
     * @since Method available since Release 1.6.0
424
     */
438
     */
425
    function quoteSmart($in)
439
    function quoteSmart($in)
426
    {
440
    {
427
        if (is_int($in) || is_double($in)) {
441
        if (is_int($in)) {
428
            return $in;
442
            return $in;
-
 
443
        } elseif (is_float($in)) {
-
 
444
            return $this->quoteFloat($in);
429
        } elseif (is_bool($in)) {
445
        } elseif (is_bool($in)) {
430
            return $in ? 1 : 0;
446
            return $this->quoteBoolean($in);
431
        } elseif (is_null($in)) {
447
        } elseif (is_null($in)) {
432
            return 'NULL';
448
            return 'NULL';
433
        } else {
449
        } else {
-
 
450
            if ($this->dbsyntax == 'access'
-
 
451
                && preg_match('/^#.+#$/', $in))
-
 
452
            {
-
 
453
                return $this->escapeSimple($in);
-
 
454
            }
434
            return "'" . $this->escapeSimple($in) . "'";
455
            return "'" . $this->escapeSimple($in) . "'";
435
        }
456
        }
436
    }
457
    }
Line 437... Line 458...
437
 
458
 
-
 
459
    // }}}
-
 
460
    // {{{ quoteBoolean()
-
 
461
 
-
 
462
    /**
-
 
463
     * Formats a boolean value for use within a query in a locale-independent
-
 
464
     * manner.
-
 
465
     *
-
 
466
     * @param boolean the boolean value to be quoted.
-
 
467
     * @return string the quoted string.
-
 
468
     * @see DB_common::quoteSmart()
-
 
469
     * @since Method available since release 1.7.8.
-
 
470
     */
-
 
471
    function quoteBoolean($boolean) {
-
 
472
        return $boolean ? '1' : '0';
-
 
473
    }
-
 
474
     
-
 
475
    // }}}
-
 
476
    // {{{ quoteFloat()
-
 
477
 
-
 
478
    /**
-
 
479
     * Formats a float value for use within a query in a locale-independent
-
 
480
     * manner.
-
 
481
     *
-
 
482
     * @param float the float value to be quoted.
-
 
483
     * @return string the quoted string.
-
 
484
     * @see DB_common::quoteSmart()
-
 
485
     * @since Method available since release 1.7.8.
-
 
486
     */
-
 
487
    function quoteFloat($float) {
-
 
488
        return "'".$this->escapeSimple(str_replace(',', '.', strval(floatval($float))))."'";
-
 
489
    }
-
 
490
     
438
    // }}}
491
    // }}}
Line 439... Line 492...
439
    // {{{ escapeSimple()
492
    // {{{ escapeSimple()
440
 
493
 
441
    /**
494
    /**
Line 835... Line 888...
835
        $sth = $this->autoPrepare($table, array_keys($fields_values), $mode,
888
        $sth = $this->autoPrepare($table, array_keys($fields_values), $mode,
836
                                  $where);
889
                                  $where);
837
        if (DB::isError($sth)) {
890
        if (DB::isError($sth)) {
838
            return $sth;
891
            return $sth;
839
        }
892
        }
840
        $ret =& $this->execute($sth, array_values($fields_values));
893
        $ret = $this->execute($sth, array_values($fields_values));
841
        $this->freePrepared($sth);
894
        $this->freePrepared($sth);
842
        return $ret;
895
        return $ret;
Line 843... Line 896...
843
 
896
 
Line 929... Line 982...
929
     * $data = array(
982
     * $data = array(
930
     *     "John's text",
983
     *     "John's text",
931
     *     "'it''s good'",
984
     *     "'it''s good'",
932
     *     'filename.txt'
985
     *     'filename.txt'
933
     * );
986
     * );
934
     * $res =& $db->execute($sth, $data);
987
     * $res = $db->execute($sth, $data);
935
     * </code>
988
     * </code>
936
     *
989
     *
937
     * @param resource $stmt  a DB statement resource returned from prepare()
990
     * @param resource $stmt  a DB statement resource returned from prepare()
938
     * @param mixed    $data  array, string or numeric data to be used in
991
     * @param mixed    $data  array, string or numeric data to be used in
939
     *                         execution of the statement.  Quantity of items
992
     *                         execution of the statement.  Quantity of items
Line 958... Line 1011...
958
        $result = $this->simpleQuery($realquery);
1011
        $result = $this->simpleQuery($realquery);
Line 959... Line 1012...
959
 
1012
 
960
        if ($result === DB_OK || DB::isError($result)) {
1013
        if ($result === DB_OK || DB::isError($result)) {
961
            return $result;
1014
            return $result;
962
        } else {
1015
        } else {
963
            $tmp =& new DB_result($this, $result);
1016
            $tmp = new DB_result($this, $result);
964
            return $tmp;
1017
            return $tmp;
965
        }
1018
        }
Line 966... Line 1019...
966
    }
1019
    }
Line 1039... Line 1092...
1039
     * @see DB_common::prepare(), DB_common::execute()
1092
     * @see DB_common::prepare(), DB_common::execute()
1040
     */
1093
     */
1041
    function executeMultiple($stmt, $data)
1094
    function executeMultiple($stmt, $data)
1042
    {
1095
    {
1043
        foreach ($data as $value) {
1096
        foreach ($data as $value) {
1044
            $res =& $this->execute($stmt, $value);
1097
            $res = $this->execute($stmt, $value);
1045
            if (DB::isError($res)) {
1098
            if (DB::isError($res)) {
1046
                return $res;
1099
                return $res;
1047
            }
1100
            }
1048
        }
1101
        }
1049
        return DB_OK;
1102
        return DB_OK;
Line 1152... Line 1205...
1152
        if (sizeof($params) > 0) {
1205
        if (sizeof($params) > 0) {
1153
            $sth = $this->prepare($query);
1206
            $sth = $this->prepare($query);
1154
            if (DB::isError($sth)) {
1207
            if (DB::isError($sth)) {
1155
                return $sth;
1208
                return $sth;
1156
            }
1209
            }
1157
            $ret =& $this->execute($sth, $params);
1210
            $ret = $this->execute($sth, $params);
1158
            $this->freePrepared($sth, false);
1211
            $this->freePrepared($sth, false);
1159
            return $ret;
1212
            return $ret;
1160
        } else {
1213
        } else {
1161
            $this->last_parameters = array();
1214
            $this->last_parameters = array();
1162
            $result = $this->simpleQuery($query);
1215
            $result = $this->simpleQuery($query);
1163
            if ($result === DB_OK || DB::isError($result)) {
1216
            if ($result === DB_OK || DB::isError($result)) {
1164
                return $result;
1217
                return $result;
1165
            } else {
1218
            } else {
1166
                $tmp =& new DB_result($this, $result);
1219
                $tmp = new DB_result($this, $result);
1167
                return $tmp;
1220
                return $tmp;
1168
            }
1221
            }
1169
        }
1222
        }
1170
    }
1223
    }
Line 1192... Line 1245...
1192
    {
1245
    {
1193
        $query = $this->modifyLimitQuery($query, $from, $count, $params);
1246
        $query = $this->modifyLimitQuery($query, $from, $count, $params);
1194
        if (DB::isError($query)){
1247
        if (DB::isError($query)){
1195
            return $query;
1248
            return $query;
1196
        }
1249
        }
1197
        $result =& $this->query($query, $params);
1250
        $result = $this->query($query, $params);
1198
        if (is_a($result, 'DB_result')) {
1251
        if (is_object($result) && is_a($result, 'DB_result')) {
1199
            $result->setOption('limit_from', $from);
1252
            $result->setOption('limit_from', $from);
1200
            $result->setOption('limit_count', $count);
1253
            $result->setOption('limit_count', $count);
1201
        }
1254
        }
1202
        return $result;
1255
        return $result;
1203
    }
1256
    }
Line 1227... Line 1280...
1227
        if (sizeof($params) > 0) {
1280
        if (sizeof($params) > 0) {
1228
            $sth = $this->prepare($query);
1281
            $sth = $this->prepare($query);
1229
            if (DB::isError($sth)) {
1282
            if (DB::isError($sth)) {
1230
                return $sth;
1283
                return $sth;
1231
            }
1284
            }
1232
            $res =& $this->execute($sth, $params);
1285
            $res = $this->execute($sth, $params);
1233
            $this->freePrepared($sth);
1286
            $this->freePrepared($sth);
1234
        } else {
1287
        } else {
1235
            $res =& $this->query($query);
1288
            $res = $this->query($query);
1236
        }
1289
        }
Line 1237... Line 1290...
1237
 
1290
 
1238
        if (DB::isError($res)) {
1291
        if (DB::isError($res)) {
1239
            return $res;
1292
            return $res;
Line 1291... Line 1344...
1291
        if (sizeof($params) > 0) {
1344
        if (sizeof($params) > 0) {
1292
            $sth = $this->prepare($query);
1345
            $sth = $this->prepare($query);
1293
            if (DB::isError($sth)) {
1346
            if (DB::isError($sth)) {
1294
                return $sth;
1347
                return $sth;
1295
            }
1348
            }
1296
            $res =& $this->execute($sth, $params);
1349
            $res = $this->execute($sth, $params);
1297
            $this->freePrepared($sth);
1350
            $this->freePrepared($sth);
1298
        } else {
1351
        } else {
1299
            $res =& $this->query($query);
1352
            $res = $this->query($query);
1300
        }
1353
        }
Line 1301... Line 1354...
1301
 
1354
 
1302
        if (DB::isError($res)) {
1355
        if (DB::isError($res)) {
1303
            return $res;
1356
            return $res;
Line 1342... Line 1395...
1342
 
1395
 
1343
            if (DB::isError($sth)) {
1396
            if (DB::isError($sth)) {
1344
                return $sth;
1397
                return $sth;
Line 1345... Line 1398...
1345
            }
1398
            }
1346
 
1399
 
1347
            $res =& $this->execute($sth, $params);
1400
            $res = $this->execute($sth, $params);
1348
            $this->freePrepared($sth);
1401
            $this->freePrepared($sth);
1349
        } else {
1402
        } else {
Line 1350... Line 1403...
1350
            $res =& $this->query($query);
1403
            $res = $this->query($query);
1351
        }
1404
        }
1352
 
1405
 
Line 1358... Line 1411...
1358
 
1411
 
1359
        if (!is_array($row = $res->fetchRow($fetchmode))) {
1412
        if (!is_array($row = $res->fetchRow($fetchmode))) {
1360
            $ret = array();
1413
            $ret = array();
1361
        } else {
1414
        } else {
1362
            if (!array_key_exists($col, $row)) {
1415
            if (!array_key_exists($col, $row)) {
1363
                $ret =& $this->raiseError(DB_ERROR_NOSUCHFIELD);
1416
                $ret = $this->raiseError(DB_ERROR_NOSUCHFIELD);
1364
            } else {
1417
            } else {
1365
                $ret = array($row[$col]);
1418
                $ret = array($row[$col]);
1366
                while (is_array($row = $res->fetchRow($fetchmode))) {
1419
                while (is_array($row = $res->fetchRow($fetchmode))) {
1367
                    $ret[] = $row[$col];
1420
                    $ret[] = $row[$col];
Line 1474... Line 1527...
1474
 
1527
 
1475
            if (DB::isError($sth)) {
1528
            if (DB::isError($sth)) {
1476
                return $sth;
1529
                return $sth;
Line 1477... Line 1530...
1477
            }
1530
            }
1478
 
1531
 
1479
            $res =& $this->execute($sth, $params);
1532
            $res = $this->execute($sth, $params);
1480
            $this->freePrepared($sth);
1533
            $this->freePrepared($sth);
1481
        } else {
1534
        } else {
Line 1482... Line 1535...
1482
            $res =& $this->query($query);
1535
            $res = $this->query($query);
1483
        }
1536
        }
1484
 
1537
 
Line 1489... Line 1542...
1489
            $fetchmode = $this->fetchmode;
1542
            $fetchmode = $this->fetchmode;
1490
        }
1543
        }
1491
        $cols = $res->numCols();
1544
        $cols = $res->numCols();
Line 1492... Line 1545...
1492
 
1545
 
1493
        if ($cols < 2) {
1546
        if ($cols < 2) {
1494
            $tmp =& $this->raiseError(DB_ERROR_TRUNCATED);
1547
            $tmp = $this->raiseError(DB_ERROR_TRUNCATED);
1495
            return $tmp;
1548
            return $tmp;
Line 1496... Line 1549...
1496
        }
1549
        }
Line 1601... Line 1654...
1601
 
1654
 
1602
            if (DB::isError($sth)) {
1655
            if (DB::isError($sth)) {
1603
                return $sth;
1656
                return $sth;
Line 1604... Line 1657...
1604
            }
1657
            }
1605
 
1658
 
1606
            $res =& $this->execute($sth, $params);
1659
            $res = $this->execute($sth, $params);
1607
            $this->freePrepared($sth);
1660
            $this->freePrepared($sth);
1608
        } else {
1661
        } else {
Line 1609... Line 1662...
1609
            $res =& $this->query($query);
1662
            $res = $this->query($query);
1610
        }
1663
        }
1611
 
1664
 
Line 1625... Line 1678...
1625
        }
1678
        }
Line 1626... Line 1679...
1626
 
1679
 
Line 1627... Line 1680...
1627
        $res->free();
1680
        $res->free();
1628
 
1681
 
1629
        if (DB::isError($row)) {
1682
        if (DB::isError($row)) {
1630
            $tmp =& $this->raiseError($row);
1683
            $tmp = $this->raiseError($row);
1631
            return $tmp;
1684
            return $tmp;
1632
        }
1685
        }
Line 1812... Line 1865...
1812
     *                 parameter is ignored.
1865
     *                 parameter is ignored.
1813
     * @param string  extra debug information.  Defaults to the last
1866
     * @param string  extra debug information.  Defaults to the last
1814
     *                 query and native error code.
1867
     *                 query and native error code.
1815
     * @param mixed   native error code, integer or string depending the
1868
     * @param mixed   native error code, integer or string depending the
1816
     *                 backend
1869
     *                 backend
-
 
1870
     * @param mixed   dummy parameter for E_STRICT compatibility with
-
 
1871
     *                 PEAR::raiseError
-
 
1872
     * @param mixed   dummy parameter for E_STRICT compatibility with
-
 
1873
     *                 PEAR::raiseError
1817
     *
1874
     *
1818
     * @return object  the PEAR_Error object
1875
     * @return object  the PEAR_Error object
1819
     *
1876
     *
1820
     * @see PEAR_Error
1877
     * @see PEAR_Error
1821
     */
1878
     */
1822
    function &raiseError($code = DB_ERROR, $mode = null, $options = null,
1879
    function &raiseError($code = DB_ERROR, $mode = null, $options = null,
1823
                         $userinfo = null, $nativecode = null)
1880
                         $userinfo = null, $nativecode = null, $dummy1 = null,
-
 
1881
                         $dummy2 = null)
1824
    {
1882
    {
1825
        // The error is yet a DB error object
1883
        // The error is yet a DB error object
1826
        if (is_object($code)) {
1884
        if (is_object($code)) {
1827
            // because we the static PEAR::raiseError, our global
1885
            // because we the static PEAR::raiseError, our global
1828
            // handler should be used if it is set
1886
            // handler should be used if it is set
Line 2101... Line 2159...
2101
    {
2159
    {
2102
        return $this->raiseError(DB_ERROR_UNSUPPORTED);
2160
        return $this->raiseError(DB_ERROR_UNSUPPORTED);
2103
    }
2161
    }
Line 2104... Line 2162...
2104
 
2162
 
-
 
2163
    // }}}
-
 
2164
    // {{{ nextQueryIsManip()
-
 
2165
 
-
 
2166
    /**
-
 
2167
     * Sets (or unsets) a flag indicating that the next query will be a
-
 
2168
     * manipulation query, regardless of the usual DB::isManip() heuristics.
-
 
2169
     *
-
 
2170
     * @param boolean true to set the flag overriding the isManip() behaviour,
-
 
2171
     * false to clear it and fall back onto isManip()
-
 
2172
     *
-
 
2173
     * @return void
-
 
2174
     *
-
 
2175
     * @access public
-
 
2176
     */
-
 
2177
    function nextQueryIsManip($manip)
-
 
2178
    {
-
 
2179
        $this->_next_query_manip = $manip;
-
 
2180
    }
-
 
2181
 
-
 
2182
    // }}}
-
 
2183
    // {{{ _checkManip()
-
 
2184
 
-
 
2185
    /**
-
 
2186
     * Checks if the given query is a manipulation query. This also takes into
-
 
2187
     * account the _next_query_manip flag and sets the _last_query_manip flag
-
 
2188
     * (and resets _next_query_manip) according to the result.
-
 
2189
     *
-
 
2190
     * @param string The query to check.
-
 
2191
     *
-
 
2192
     * @return boolean true if the query is a manipulation query, false
-
 
2193
     * otherwise
-
 
2194
     *
-
 
2195
     * @access protected
-
 
2196
     */
-
 
2197
    function _checkManip($query)
-
 
2198
    {
-
 
2199
        if ($this->_next_query_manip || DB::isManip($query)) {
-
 
2200
            $this->_last_query_manip = true;
-
 
2201
        } else {
-
 
2202
            $this->_last_query_manip = false;
-
 
2203
        }
-
 
2204
        $this->_next_query_manip = false;
-
 
2205
        return $this->_last_query_manip;
-
 
2206
        $manip = $this->_next_query_manip;
-
 
2207
    }
-
 
2208
 
2105
    // }}}
2209
    // }}}
Line 2106... Line 2210...
2106
    // {{{ _rtrimArrayValues()
2210
    // {{{ _rtrimArrayValues()
2107
 
2211
 
2108
    /**
2212
    /**