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 4... Line 4...
4
 
4
 
5
/**
5
/**
6
 * The PEAR DB driver for PHP's oci8 extension
6
 * The PEAR DB driver for PHP's oci8 extension
7
 * for interacting with Oracle databases
7
 * for interacting with Oracle databases
8
 *
8
 *
9
 * PHP versions 4 and 5
9
 * PHP version 5
10
 *
10
 *
11
 * LICENSE: This source file is subject to version 3.0 of the PHP license
11
 * LICENSE: This source file is subject to version 3.0 of the PHP license
12
 * that is available through the world-wide-web at the following URI:
12
 * that is available through the world-wide-web at the following URI:
13
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
13
 * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
Line 16... Line 16...
16
 *
16
 *
17
 * @category   Database
17
 * @category   Database
18
 * @package    DB
18
 * @package    DB
19
 * @author     James L. Pine <jlp@valinux.com>
19
 * @author     James L. Pine <jlp@valinux.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: oci8.php,v 1.103 2005/04/11 15:10:22 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 43... Line 43...
43
 *
43
 *
44
 * @category   Database
44
 * @category   Database
45
 * @package    DB
45
 * @package    DB
46
 * @author     James L. Pine <jlp@valinux.com>
46
 * @author     James L. Pine <jlp@valinux.com>
47
 * @author     Daniel Convissor <danielc@php.net>
47
 * @author     Daniel Convissor <danielc@php.net>
48
 * @copyright  1997-2005 The PHP Group
48
 * @copyright  1997-2007 The PHP Group
49
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
49
 * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
50
 * @version    Release: 1.7.6
50
 * @version    Release: 1.9.2
51
 * @link       http://pear.php.net/package/DB
51
 * @link       http://pear.php.net/package/DB
52
 */
52
 */
53
class DB_oci8 extends DB_common
53
class DB_oci8 extends DB_common
54
{
54
{
55
    // {{{ properties
55
    // {{{ properties
Line 92... Line 92...
92
    /**
92
    /**
93
     * A mapping of native error codes to DB error codes
93
     * A mapping of native error codes to DB error codes
94
     * @var array
94
     * @var array
95
     */
95
     */
96
    var $errorcode_map = array(
96
    var $errorcode_map = array(
97
        1    => DB_ERROR_CONSTRAINT,
97
        1     => DB_ERROR_CONSTRAINT,
98
        900  => DB_ERROR_SYNTAX,
98
        900   => DB_ERROR_SYNTAX,
99
        904  => DB_ERROR_NOSUCHFIELD,
99
        904   => DB_ERROR_NOSUCHFIELD,
100
        913  => DB_ERROR_VALUE_COUNT_ON_ROW,
100
        913   => DB_ERROR_VALUE_COUNT_ON_ROW,
101
        921  => DB_ERROR_SYNTAX,
101
        921   => DB_ERROR_SYNTAX,
102
        923  => DB_ERROR_SYNTAX,
102
        923   => DB_ERROR_SYNTAX,
103
        942  => DB_ERROR_NOSUCHTABLE,
103
        942   => DB_ERROR_NOSUCHTABLE,
104
        955  => DB_ERROR_ALREADY_EXISTS,
104
        955   => DB_ERROR_ALREADY_EXISTS,
105
        1400 => DB_ERROR_CONSTRAINT_NOT_NULL,
105
        1400  => DB_ERROR_CONSTRAINT_NOT_NULL,
106
        1401 => DB_ERROR_INVALID,
106
        1401  => DB_ERROR_INVALID,
107
        1407 => DB_ERROR_CONSTRAINT_NOT_NULL,
107
        1407  => DB_ERROR_CONSTRAINT_NOT_NULL,
108
        1418 => DB_ERROR_NOT_FOUND,
108
        1418  => DB_ERROR_NOT_FOUND,
109
        1476 => DB_ERROR_DIVZERO,
109
        1476  => DB_ERROR_DIVZERO,
110
        1722 => DB_ERROR_INVALID_NUMBER,
110
        1722  => DB_ERROR_INVALID_NUMBER,
111
        2289 => DB_ERROR_NOSUCHTABLE,
111
        2289  => DB_ERROR_NOSUCHTABLE,
112
        2291 => DB_ERROR_CONSTRAINT,
112
        2291  => DB_ERROR_CONSTRAINT,
113
        2292 => DB_ERROR_CONSTRAINT,
113
        2292  => DB_ERROR_CONSTRAINT,
114
        2449 => DB_ERROR_CONSTRAINT,
114
        2449  => DB_ERROR_CONSTRAINT,
-
 
115
        12899 => DB_ERROR_INVALID,
115
    );
116
    );
Line 116... Line 117...
116
 
117
 
117
    /**
118
    /**
118
     * The raw database connection created by PHP
119
     * The raw database connection created by PHP
Line 158... Line 159...
158
     * @var array
159
     * @var array
159
     * @access private
160
     * @access private
160
     */
161
     */
161
    var $manip_query = array();
162
    var $manip_query = array();
Line -... Line 163...
-
 
163
 
-
 
164
    /**
-
 
165
     * Store of prepared SQL queries.
-
 
166
     * @var array
-
 
167
     * @access private
-
 
168
     */
-
 
169
    var $_prepared_queries = array();
Line 162... Line 170...
162
 
170
 
163
 
171
 
Line 164... Line 172...
164
    // }}}
172
    // }}}
165
    // {{{ constructor
173
    // {{{ constructor
166
 
174
 
167
    /**
175
    /**
168
     * This constructor calls <kbd>$this->DB_common()</kbd>
176
     * This constructor calls <kbd>parent::__construct()</kbd>
169
     *
177
     *
170
     * @return void
178
     * @return void
171
     */
179
     */
172
    function DB_oci8()
180
    function __construct()
Line 173... Line 181...
173
    {
181
    {
174
        $this->DB_common();
182
        parent::__construct();
Line 214... Line 222...
214
        $this->dsn = $dsn;
222
        $this->dsn = $dsn;
215
        if ($dsn['dbsyntax']) {
223
        if ($dsn['dbsyntax']) {
216
            $this->dbsyntax = $dsn['dbsyntax'];
224
            $this->dbsyntax = $dsn['dbsyntax'];
217
        }
225
        }
Line -... Line 226...
-
 
226
 
-
 
227
        // Backwards compatibility with DB < 1.7.0
-
 
228
        if (empty($dsn['database']) && !empty($dsn['hostspec'])) {
-
 
229
            $db = $dsn['hostspec'];
-
 
230
        } else {
-
 
231
            $db = $dsn['database'];
-
 
232
        }
218
 
233
 
219
        if (function_exists('oci_connect')) {
234
        if (function_exists('oci_connect')) {
220
            if (isset($dsn['new_link'])
235
            if (isset($dsn['new_link'])
221
                && ($dsn['new_link'] == 'true' || $dsn['new_link'] === true))
236
                && ($dsn['new_link'] == 'true' || $dsn['new_link'] === true))
222
            {
237
            {
223
                $connect_function = 'oci_new_connect';
238
                $connect_function = 'oci_new_connect';
224
            } else {
239
            } else {
225
                $connect_function = $persistent ? 'oci_pconnect'
240
                $connect_function = $persistent ? 'oci_pconnect'
226
                                    : 'oci_connect';
241
                                    : 'oci_connect';
227
            }
-
 
228
 
-
 
229
            // Backwards compatibility with DB < 1.7.0
242
            }
230
            if (empty($dsn['database']) && !empty($dsn['hostspec'])) {
243
            if (isset($this->dsn['port']) && $this->dsn['port']) {
231
                $db = $dsn['hostspec'];
-
 
232
            } else {
-
 
233
                $db = $dsn['database'];
244
                $db = '//'.$db.':'.$this->dsn['port'];
Line 234... Line 245...
234
            }
245
            }
235
 
246
 
236
            $char = empty($dsn['charset']) ? null : $dsn['charset'];
247
            $char = empty($dsn['charset']) ? null : $dsn['charset'];
Line 246... Line 257...
246
                                                       null,
257
                                                       null,
247
                                                       $char);
258
                                                       $char);
248
            }
259
            }
249
        } else {
260
        } else {
250
            $connect_function = $persistent ? 'OCIPLogon' : 'OCILogon';
261
            $connect_function = $persistent ? 'OCIPLogon' : 'OCILogon';
251
            if ($dsn['hostspec']) {
262
            if ($db) {
252
                $this->connection = @$connect_function($dsn['username'],
263
                $this->connection = @$connect_function($dsn['username'],
253
                                                       $dsn['password'],
264
                                                       $dsn['password'],
254
                                                       $dsn['hostspec']);
265
                                                       $db);
255
            } elseif ($dsn['username'] || $dsn['password']) {
266
            } elseif ($dsn['username'] || $dsn['password']) {
256
                $this->connection = @$connect_function($dsn['username'],
267
                $this->connection = @$connect_function($dsn['username'],
257
                                                       $dsn['password']);
268
                                                       $dsn['password']);
258
            }
269
            }
259
        }
270
        }
Line 320... Line 331...
320
        }
331
        }
321
        if (!$success) {
332
        if (!$success) {
322
            return $this->oci8RaiseError($result);
333
            return $this->oci8RaiseError($result);
323
        }
334
        }
324
        $this->last_stmt = $result;
335
        $this->last_stmt = $result;
325
        if (DB::isManip($query)) {
336
        if ($this->_checkManip($query)) {
326
            return DB_OK;
337
            return DB_OK;
327
        } else {
338
        } else {
328
            @ocisetprefetch($result, $this->options['result_buffering']);
339
            @ocisetprefetch($result, $this->options['result_buffering']);
329
            return $result;
340
            return $result;
330
        }
341
        }
Line 413... Line 424...
413
     *
424
     *
414
     * @see DB_result::free()
425
     * @see DB_result::free()
415
     */
426
     */
416
    function freeResult($result)
427
    function freeResult($result)
417
    {
428
    {
418
        return @OCIFreeStatement($result);
429
        return is_resource($result) ? OCIFreeStatement($result) : false;
419
    }
430
    }
Line 420... Line 431...
420
 
431
 
421
    /**
432
    /**
422
     * Frees the internal resources associated with a prepared query
433
     * Frees the internal resources associated with a prepared query
Line 439... Line 450...
439
            @ocifreestatement($stmt);
450
            @ocifreestatement($stmt);
440
        }
451
        }
441
        if (isset($this->prepare_types[(int)$stmt])) {
452
        if (isset($this->prepare_types[(int)$stmt])) {
442
            unset($this->prepare_types[(int)$stmt]);
453
            unset($this->prepare_types[(int)$stmt]);
443
            unset($this->manip_query[(int)$stmt]);
454
            unset($this->manip_query[(int)$stmt]);
-
 
455
            unset($this->_prepared_queries[(int)$stmt]);
444
        } else {
456
        } else {
445
            return false;
457
            return false;
446
        }
458
        }
447
        return true;
459
        return true;
448
    }
460
    }
Line 474... Line 486...
474
        {
486
        {
475
            $countquery = 'SELECT COUNT(*) FROM ('.$this->last_query.')';
487
            $countquery = 'SELECT COUNT(*) FROM ('.$this->last_query.')';
476
            $save_query = $this->last_query;
488
            $save_query = $this->last_query;
477
            $save_stmt = $this->last_stmt;
489
            $save_stmt = $this->last_stmt;
Line 478... Line -...
478
 
-
 
479
            if (count($this->_data)) {
-
 
480
                $smt = $this->prepare('SELECT COUNT(*) FROM ('.$this->last_query.')');
-
 
481
                $count = $this->execute($smt, $this->_data);
-
 
482
            } else {
490
 
483
                $count =& $this->query($countquery);
-
 
Line -... Line 491...
-
 
491
            $count = $this->query($countquery);
-
 
492
 
-
 
493
            // Restore the last query and statement.
-
 
494
            $this->last_query = $save_query;
484
            }
495
            $this->last_stmt = $save_stmt;
485
 
496
            
486
            if (DB::isError($count) ||
497
            if (DB::isError($count) ||
487
                DB::isError($row = $count->fetchRow(DB_FETCHMODE_ORDERED)))
-
 
488
            {
-
 
489
                $this->last_query = $save_query;
498
                DB::isError($row = $count->fetchRow(DB_FETCHMODE_ORDERED)))
490
                $this->last_stmt = $save_stmt;
499
            {
-
 
500
                return $this->raiseError(DB_ERROR_NOT_CAPABLE);
491
                return $this->raiseError(DB_ERROR_NOT_CAPABLE);
501
            }
492
            }
502
 
493
            return $row[0];
503
            return $row[0];
494
        }
504
        }
Line 588... Line 598...
588
        if (!$stmt = @OCIParse($this->connection, $newquery)) {
598
        if (!$stmt = @OCIParse($this->connection, $newquery)) {
589
            return $this->oci8RaiseError();
599
            return $this->oci8RaiseError();
590
        }
600
        }
591
        $this->prepare_types[(int)$stmt] = $types;
601
        $this->prepare_types[(int)$stmt] = $types;
592
        $this->manip_query[(int)$stmt] = DB::isManip($query);
602
        $this->manip_query[(int)$stmt] = DB::isManip($query);
-
 
603
        $this->_prepared_queries[(int)$stmt] = $newquery;
593
        return $stmt;
604
        return $stmt;
594
    }
605
    }
Line 595... Line 606...
595
 
606
 
596
    // }}}
607
    // }}}
Line 618... Line 629...
618
     */
629
     */
619
    function &execute($stmt, $data = array())
630
    function &execute($stmt, $data = array())
620
    {
631
    {
621
        $data = (array)$data;
632
        $data = (array)$data;
622
        $this->last_parameters = $data;
633
        $this->last_parameters = $data;
-
 
634
        $this->last_query = $this->_prepared_queries[(int)$stmt];
623
        $this->_data = $data;
635
        $this->_data = $data;
Line 624... Line 636...
624
 
636
 
625
        $types =& $this->prepare_types[(int)$stmt];
637
        $types = $this->prepare_types[(int)$stmt];
626
        if (count($types) != count($data)) {
638
        if (count($types) != count($data)) {
627
            $tmp =& $this->raiseError(DB_ERROR_MISMATCH);
639
            $tmp = $this->raiseError(DB_ERROR_MISMATCH);
628
            return $tmp;
640
            return $tmp;
Line 629... Line 641...
629
        }
641
        }
630
 
642
 
Line 641... Line 653...
641
                $data[$key] = preg_replace("/^'(.*)'$/", "\\1", $data[$key]);
653
                $data[$key] = preg_replace("/^'(.*)'$/", "\\1", $data[$key]);
642
                $data[$key] = str_replace("''", "'", $data[$key]);
654
                $data[$key] = str_replace("''", "'", $data[$key]);
643
            } elseif ($types[$i] == DB_PARAM_OPAQUE) {
655
            } elseif ($types[$i] == DB_PARAM_OPAQUE) {
644
                $fp = @fopen($data[$key], 'rb');
656
                $fp = @fopen($data[$key], 'rb');
645
                if (!$fp) {
657
                if (!$fp) {
646
                    $tmp =& $this->raiseError(DB_ERROR_ACCESS_VIOLATION);
658
                    $tmp = $this->raiseError(DB_ERROR_ACCESS_VIOLATION);
647
                    return $tmp;
659
                    return $tmp;
648
                }
660
                }
649
                $data[$key] = fread($fp, filesize($data[$key]));
661
                $data[$key] = fread($fp, filesize($data[$key]));
650
                fclose($fp);
662
                fclose($fp);
-
 
663
            } elseif ($types[$i] == DB_PARAM_SCALAR) {
-
 
664
                // Floats have to be converted to a locale-neutral
-
 
665
                // representation.
-
 
666
                if (is_float($data[$key])) {
-
 
667
                    $data[$key] = $this->quoteFloat($data[$key]);
-
 
668
                }
651
            }
669
            }
652
            if (!@OCIBindByName($stmt, ':bind' . $i, $data[$key], -1)) {
670
            if (!@OCIBindByName($stmt, ':bind' . $i, $data[$key], -1)) {
653
                $tmp = $this->oci8RaiseError($stmt);
671
                $tmp = $this->oci8RaiseError($stmt);
654
                return $tmp;
672
                return $tmp;
655
            }
673
            }
-
 
674
            $this->last_query = preg_replace("/:bind$i(?!\d)/",
-
 
675
                    $this->quoteSmart($data[$key]), $this->last_query, 1);
656
            $i++;
676
            $i++;
657
        }
677
        }
658
        if ($this->autocommit) {
678
        if ($this->autocommit) {
659
            $success = @OCIExecute($stmt, OCI_COMMIT_ON_SUCCESS);
679
            $success = @OCIExecute($stmt, OCI_COMMIT_ON_SUCCESS);
660
        } else {
680
        } else {
Line 663... Line 683...
663
        if (!$success) {
683
        if (!$success) {
664
            $tmp = $this->oci8RaiseError($stmt);
684
            $tmp = $this->oci8RaiseError($stmt);
665
            return $tmp;
685
            return $tmp;
666
        }
686
        }
667
        $this->last_stmt = $stmt;
687
        $this->last_stmt = $stmt;
-
 
688
        if ($this->manip_query[(int)$stmt] || $this->_next_query_manip) {
668
        if ($this->manip_query[(int)$stmt]) {
689
            $this->_last_query_manip = true;
-
 
690
            $this->_next_query_manip = false;
669
            $tmp = DB_OK;
691
            $tmp = DB_OK;
670
        } else {
692
        } else {
-
 
693
            $this->_last_query_manip = false;
671
            @ocisetprefetch($stmt, $this->options['result_buffering']);
694
            @ocisetprefetch($stmt, $this->options['result_buffering']);
672
            $tmp =& new DB_result($this, $stmt);
695
            $tmp = new DB_result($this, $stmt);
673
        }
696
        }
674
        return $tmp;
697
        return $tmp;
675
    }
698
    }
Line 676... Line 699...
676
 
699
 
Line 795... Line 818...
795
        // coding a "home" SQL parser
818
        // coding a "home" SQL parser
Line 796... Line 819...
796
 
819
 
797
        if (count($params)) {
820
        if (count($params)) {
798
            $result = $this->prepare("SELECT * FROM ($query) "
821
            $result = $this->prepare("SELECT * FROM ($query) "
799
                                     . 'WHERE NULL = NULL');
822
                                     . 'WHERE NULL = NULL');
800
            $tmp =& $this->execute($result, $params);
823
            $tmp = $this->execute($result, $params);
801
        } else {
824
        } else {
Line 802... Line 825...
802
            $q_fields = "SELECT * FROM ($query) WHERE NULL = NULL";
825
            $q_fields = "SELECT * FROM ($query) WHERE NULL = NULL";
803
 
826
 
Line 855... Line 878...
855
    {
878
    {
856
        $seqname = $this->getSequenceName($seq_name);
879
        $seqname = $this->getSequenceName($seq_name);
857
        $repeat = 0;
880
        $repeat = 0;
858
        do {
881
        do {
859
            $this->expectError(DB_ERROR_NOSUCHTABLE);
882
            $this->expectError(DB_ERROR_NOSUCHTABLE);
860
            $result =& $this->query("SELECT ${seqname}.nextval FROM dual");
883
            $result = $this->query("SELECT ${seqname}.nextval FROM dual");
861
            $this->popExpect();
884
            $this->popExpect();
862
            if ($ondemand && DB::isError($result) &&
885
            if ($ondemand && DB::isError($result) &&
863
                $result->getCode() == DB_ERROR_NOSUCHTABLE) {
886
                $result->getCode() == DB_ERROR_NOSUCHTABLE) {
864
                $repeat = 1;
887
                $repeat = 1;
865
                $result = $this->createSequence($seq_name);
888
                $result = $this->createSequence($seq_name);
Line 1013... Line 1036...
1013
                return $this->oci8RaiseError(DB_ERROR_NEED_MORE_DATA);
1036
                return $this->oci8RaiseError(DB_ERROR_NEED_MORE_DATA);
1014
            }
1037
            }
1015
            if (!@OCIExecute($stmt, OCI_DEFAULT)) {
1038
            if (!@OCIExecute($stmt, OCI_DEFAULT)) {
1016
                return $this->oci8RaiseError($stmt);
1039
                return $this->oci8RaiseError($stmt);
1017
            }
1040
            }
1018
 
1041
            
1019
            $i = 0;
1042
            $i = 0;
1020
            while (@OCIFetch($stmt)) {
1043
            while (@OCIFetch($stmt)) {
1021
                $res[$i] = array(
1044
                $res[$i] = array(
1022
                    'table' => $case_func($result),
1045
                    'table' => $case_func($result),
1023
                    'name'  => $case_func(@OCIResult($stmt, 1)),
1046
                    'name'  => $case_func(@OCIResult($stmt, 1)),
Line 1096... Line 1119...
1096
        switch ($type) {
1119
        switch ($type) {
1097
            case 'tables':
1120
            case 'tables':
1098
                return 'SELECT table_name FROM user_tables';
1121
                return 'SELECT table_name FROM user_tables';
1099
            case 'synonyms':
1122
            case 'synonyms':
1100
                return 'SELECT synonym_name FROM user_synonyms';
1123
                return 'SELECT synonym_name FROM user_synonyms';
-
 
1124
            case 'views':
-
 
1125
                return 'SELECT view_name FROM user_views';
1101
            default:
1126
            default:
1102
                return null;
1127
                return null;
1103
        }
1128
        }
1104
    }
1129
    }
Line 1105... Line 1130...
1105
 
1130
 
-
 
1131
    // }}}
-
 
1132
    // {{{ quoteFloat()
-
 
1133
 
-
 
1134
    /**
-
 
1135
     * Formats a float value for use within a query in a locale-independent
-
 
1136
     * manner.
-
 
1137
     *
-
 
1138
     * @param float the float value to be quoted.
-
 
1139
     * @return string the quoted string.
-
 
1140
     * @see DB_common::quoteSmart()
-
 
1141
     * @since Method available since release 1.7.8.
-
 
1142
     */
-
 
1143
    function quoteFloat($float) {
-
 
1144
        return $this->escapeSimple(str_replace(',', '.', strval(floatval($float))));
-
 
1145
    }
-
 
1146
     
Line 1106... Line 1147...
1106
    // }}}
1147
    // }}}
Line 1107... Line 1148...
1107
 
1148
 
1108
}
1149
}