1 |
dpavlin |
1.1 |
<? |
2 |
|
|
/* |
3 |
|
|
* $Id: dbi_oracle.php,v 1.7 2001/04/03 14:48:31 cfsl Exp $ |
4 |
|
|
* |
5 |
|
|
* This class is based on the Perl DBI. |
6 |
|
|
* New functionality should be added accordingly. |
7 |
|
|
* Please refer to the DBI documentation (perldoc DBI). |
8 |
|
|
* |
9 |
|
|
* This program is free software; you can redistribute it and/or modify |
10 |
|
|
* it under the terms of the GNU General Public License as published by |
11 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
12 |
|
|
* (at your option) any later version. |
13 |
|
|
* |
14 |
|
|
* This program is distributed in the hope that it will be useful, |
15 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
|
|
* GNU General Public License for more details. |
18 |
|
|
* |
19 |
|
|
* You should have received a copy of the GNU General Public License |
20 |
|
|
* along with this program; if not, write to the Free Software |
21 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
|
|
*/ |
23 |
|
|
|
24 |
|
|
class DBI { |
25 |
|
|
var $session; |
26 |
|
|
var $mode = OCI_COMMIT_ON_SUCCESS; |
27 |
|
|
var $debug = 1; |
28 |
|
|
var $Driver_name = "oracle"; |
29 |
|
|
|
30 |
|
|
function DBI ($db, $user, $password, $debug = 0) { |
31 |
|
|
$this->session = OCIPLogon($user, $password, $db); |
32 |
|
|
$this->debug = $debug; |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
function prepare ($query) { |
36 |
|
|
return new STH($this, $this->session, $query, $this->mode, $this->debug); |
37 |
|
|
} |
38 |
|
|
|
39 |
|
|
function autocommit ($value) { |
40 |
|
|
if ($value) { |
41 |
|
|
$this->mode = OCI_COMMIT_ON_SUCCESS; |
42 |
|
|
} else { |
43 |
|
|
$this->mode = OCI_DEFAULT; |
44 |
|
|
} |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
function commit () { |
48 |
|
|
return OCICommit($this->session); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
function rollback () { |
52 |
|
|
return OCIRollback($this->session); |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
function quote ($str) { |
56 |
|
|
if (get_magic_quotes_gpc()) { |
57 |
|
|
$str = stripslashes($str); |
58 |
|
|
} |
59 |
|
|
return "'".str_replace ("'", "''", $str)."'"; |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
function insert_id ($sequence) { |
63 |
|
|
$sth = new STH($this, $this->session, "SELECT $sequence.CURRVAL FROM DUAL", $this->mode, $this->debug); |
64 |
|
|
$sth->execute(); |
65 |
|
|
list($res) = $sth->fetchrow_array(); |
66 |
|
|
return $res; |
67 |
|
|
} |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
class STH { |
71 |
|
|
var $query; |
72 |
|
|
var $statement; |
73 |
|
|
var $mode; |
74 |
|
|
var $debug; |
75 |
|
|
var $dbi; |
76 |
|
|
var $placeholders; |
77 |
|
|
|
78 |
|
|
function STH (&$dbi, &$session, $query, $mode, $debug) { |
79 |
|
|
$this->dbi = &$dbi; |
80 |
|
|
$this->query = $query; |
81 |
|
|
$this->mode = $mode; |
82 |
|
|
$this->debug = $debug; |
83 |
|
|
$this->session = &$session; |
84 |
|
|
|
85 |
|
|
// Scan for placeholders |
86 |
|
|
|
87 |
|
|
$this->placeholders = array(); |
88 |
|
|
$quote = ''; |
89 |
|
|
for ($i = 0; $i < strlen($query); ++$i) { |
90 |
|
|
if ($query[$i] == "'") { |
91 |
|
|
if (empty($quote)) { |
92 |
|
|
$quote = "'"; |
93 |
|
|
} elseif ($quote == "'") { |
94 |
|
|
$quote = ''; |
95 |
|
|
} |
96 |
|
|
} elseif ($query[$i] == '"') { |
97 |
|
|
if (empty($quote)) { |
98 |
|
|
$quote = '"'; |
99 |
|
|
} elseif ($quote == '"') { |
100 |
|
|
$quote = ''; |
101 |
|
|
} |
102 |
|
|
} elseif ($query[$i] == '?') { |
103 |
|
|
if (empty($quote)) { |
104 |
|
|
array_push($this->placeholders, $i); |
105 |
|
|
} |
106 |
|
|
} |
107 |
|
|
} |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
function execute () { |
111 |
|
|
global $SERVER_NAME; |
112 |
|
|
$numargs = func_num_args(); |
113 |
|
|
$arg_list = func_get_args(); |
114 |
|
|
|
115 |
|
|
$parms = array(); |
116 |
|
|
|
117 |
|
|
for ($i = 0; $i < $numargs; $i++) { |
118 |
|
|
if (is_array($arg_list[$i])) { |
119 |
|
|
while (list($dummy,$parm) = each ($arg_list[$i])) { |
120 |
|
|
array_push($parms, $parm); |
121 |
|
|
} |
122 |
|
|
} else { |
123 |
|
|
array_push($parms,$arg_list[$i]); |
124 |
|
|
} |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
if (sizeof($parms) != sizeof($this->placeholders)) { |
128 |
|
|
print "<br><b>SQL Query ($this->query) contains ".sizeof($this->placeholders)." placeholders but ".sizeof($parms)." was passed</b><br>"; |
129 |
|
|
exit; |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
if (sizeof($parms) > 0) { |
133 |
|
|
$query = substr($this->query, 0, $this->placeholders[0]); |
134 |
|
|
for ($i = 0; $i < sizeof($parms) - 1; ++$i) { |
135 |
|
|
$query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1, $this->placeholders[$i + 1] - $this->placeholders[$i] - 1); |
136 |
|
|
} |
137 |
|
|
$query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1); |
138 |
|
|
} else { |
139 |
|
|
$query = $this->query; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
if ($this->debug) { // Log the query |
143 |
|
|
$fd = fopen("/tmp/dbi.$SERVER_NAME.log", "a") or die ("Couldn't append to file"); |
144 |
|
|
fputs($fd, date("M d H:i:s",time()).": ".$query."\n==================\n"); |
145 |
|
|
fclose($fd); |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
$this->statement = OCIParse($this->session, $query); |
149 |
|
|
if (!$this->statement) { |
150 |
|
|
print "<br><b>Could not parse SQL query: \"$query\"</b><br>"; |
151 |
|
|
exit; |
152 |
|
|
} |
153 |
|
|
if (!OCIExecute($this->statement, $this->mode)) { |
154 |
|
|
print "<br><b>Could not execute SQL query: \"".$query."\"</b><br>"; |
155 |
|
|
exit; |
156 |
|
|
} |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
function fetchrow_array () { |
160 |
|
|
$res = array(); |
161 |
|
|
OCIFetchInto($this->statement, $res); |
162 |
|
|
return $res; |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
function fetchrow_hash () { |
166 |
|
|
$res = array(); |
167 |
|
|
OCIFetchInto($this->statement, $res, OCI_ASSOC); |
168 |
|
|
return $res; |
169 |
|
|
} |
170 |
|
|
|
171 |
|
|
function rows () { |
172 |
|
|
return OCIRowCount ($this->statement); |
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
function finish () { |
176 |
|
|
OCIFreeStatement($this->statement); |
177 |
|
|
} |
178 |
|
|
} |
179 |
|
|
?> |