1 | <?php |
---|
2 | /* |
---|
3 | V4.51 29 July 2004 (c) 2000-2004 John Lim (jlim#natsoft.com.my). All rights reserved. |
---|
4 | Released under both BSD license and Lesser GPL library license. |
---|
5 | Whenever there is any discrepancy between the two licenses, |
---|
6 | the BSD license will take precedence. |
---|
7 | Set tabs to 4 for best viewing. |
---|
8 | |
---|
9 | Latest version is available at http://adodb.sourceforge.net |
---|
10 | |
---|
11 | Requires ODBC. Works on Windows and Unix. |
---|
12 | */ |
---|
13 | // security - hide paths |
---|
14 | if (!defined('ADODB_DIR')) die(); |
---|
15 | |
---|
16 | /*-------------------------------------------------------------------------------------- |
---|
17 | --------------------------------------------------------------------------------------*/ |
---|
18 | |
---|
19 | |
---|
20 | class ADODB_pdo extends ADOConnection { |
---|
21 | var $databaseType = "pdo"; |
---|
22 | var $dataProvider = "pdo"; |
---|
23 | var $fmtDate = "'Y-m-d'"; |
---|
24 | var $fmtTimeStamp = "'Y-m-d, h:i:sA'"; |
---|
25 | var $replaceQuote = "''"; // string to use to replace quotes |
---|
26 | var $hasAffectedRows = true; |
---|
27 | var $_bindInputArray = true; |
---|
28 | var $_genSeqSQL = "create table %s (id integer)"; |
---|
29 | var $_autocommit = true; |
---|
30 | var $_haserrorfunctions = true; |
---|
31 | var $_lastAffectedRows = 0; |
---|
32 | |
---|
33 | var $stmt = false; |
---|
34 | |
---|
35 | function ADODB_pdo() |
---|
36 | { |
---|
37 | } |
---|
38 | |
---|
39 | |
---|
40 | // returns true or false |
---|
41 | function _connect($argDSN, $argUsername, $argPassword, $argDatabasename, $persist=false) |
---|
42 | { |
---|
43 | $this->_connectionID = new PDO($argDSN, $argUsername, $argPassword); |
---|
44 | if ($this->_connectionID) { |
---|
45 | switch(ADODB_ASSOC_CASE){ |
---|
46 | case 0: $m = PDO_CASE_LOWER; break; |
---|
47 | case 1: $m = PDO_CASE_UPPER; break; |
---|
48 | default: |
---|
49 | case 2: $m = PDO_CASE_NATURAL; break; |
---|
50 | } |
---|
51 | |
---|
52 | //$this->_connectionID->setAttribute(PDO_ATTR_ERRMODE,PDO_ERRMODE_SILENT ); |
---|
53 | $this->_connectionID->setAttribute(PDO_ATTR_CASE,$m); |
---|
54 | |
---|
55 | //$this->_connectionID->setAttribute(PDO_ATTR_AUTOCOMMIT,true); |
---|
56 | |
---|
57 | return true; |
---|
58 | } |
---|
59 | return false; |
---|
60 | } |
---|
61 | |
---|
62 | // returns true or false |
---|
63 | function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename) |
---|
64 | { |
---|
65 | return $this->_connect($argDSN, $argUsername, $argPassword, $argDatabasename, true); |
---|
66 | } |
---|
67 | |
---|
68 | function ErrorMsg() |
---|
69 | { |
---|
70 | if ($this->_stmt) $arr = $this->_stmt->errorInfo(); |
---|
71 | else $arr = $this->_connectionID->errorInfo(); |
---|
72 | |
---|
73 | if ($arr) { |
---|
74 | if ($arr[0]) return $arr[2]; |
---|
75 | else return ''; |
---|
76 | } else return '-1'; |
---|
77 | } |
---|
78 | |
---|
79 | function InParameter(&$stmt,&$var,$name,$maxLen=4000,$type=false) |
---|
80 | { |
---|
81 | $obj = $stmt[1]; |
---|
82 | if ($type) $obj->bindParam($name,$var,$type,$maxLen); |
---|
83 | else $obj->bindParam($name, $var); |
---|
84 | } |
---|
85 | |
---|
86 | function ErrorNo() |
---|
87 | { |
---|
88 | |
---|
89 | if ($this->_stmt) return $this->_stmt->errorCode(); |
---|
90 | else return $this->_connectionID->errorInfo(); |
---|
91 | } |
---|
92 | |
---|
93 | function BeginTrans() |
---|
94 | { |
---|
95 | if (!$this->hasTransactions) return false; |
---|
96 | if ($this->transOff) return true; |
---|
97 | $this->transCnt += 1; |
---|
98 | $this->_autocommit = false; |
---|
99 | $this->_connectionID->setAttribute(PDO_ATTR_AUTOCOMMIT,false); |
---|
100 | return $this->_connectionID->beginTransaction(); |
---|
101 | } |
---|
102 | |
---|
103 | function CommitTrans($ok=true) |
---|
104 | { |
---|
105 | if ($this->transOff) return true; |
---|
106 | if (!$ok) return $this->RollbackTrans(); |
---|
107 | if ($this->transCnt) $this->transCnt -= 1; |
---|
108 | $this->_autocommit = true; |
---|
109 | |
---|
110 | $ret = $this->_connectionID->commit(); |
---|
111 | $this->_connectionID->setAttribute(PDO_ATTR_AUTOCOMMIT,true); |
---|
112 | return $ret; |
---|
113 | } |
---|
114 | |
---|
115 | function RollbackTrans() |
---|
116 | { |
---|
117 | if ($this->transOff) return true; |
---|
118 | if ($this->transCnt) $this->transCnt -= 1; |
---|
119 | $this->_autocommit = true; |
---|
120 | |
---|
121 | $ret = $this->_connectionID->rollback(); |
---|
122 | $this->_connectionID->setAttribute(PDO_ATTR_AUTOCOMMIT,true); |
---|
123 | return $ret; |
---|
124 | } |
---|
125 | |
---|
126 | function Prepare($sql) |
---|
127 | { |
---|
128 | $this->_stmt = $this->_connectionID->prepare($sql); |
---|
129 | if ($this->_stmt) return array($sql,$this->_stmt); |
---|
130 | |
---|
131 | return false; |
---|
132 | } |
---|
133 | |
---|
134 | function PrepareStmt($sql) |
---|
135 | { |
---|
136 | $stmt = $this->_connectionID->prepare($sql); |
---|
137 | if (!$stmt) return false; |
---|
138 | $obj = new ADOPDOStatement($stmt,$this); |
---|
139 | return $obj; |
---|
140 | } |
---|
141 | |
---|
142 | /* returns queryID or false */ |
---|
143 | function _query($sql,$inputarr=false) |
---|
144 | { |
---|
145 | if (is_array($sql)) { |
---|
146 | $stmt = $sql[1]; |
---|
147 | } else { |
---|
148 | $stmt = $this->_connectionID->prepare($sql); |
---|
149 | } |
---|
150 | if ($stmt) { |
---|
151 | if ($inputarr) $stmt->execute($inputarr); |
---|
152 | else $stmt->execute(); |
---|
153 | } |
---|
154 | $this->_stmt = $stmt; |
---|
155 | return $stmt; |
---|
156 | } |
---|
157 | |
---|
158 | // returns true or false |
---|
159 | function _close() |
---|
160 | { |
---|
161 | $this->_stmt = false; |
---|
162 | return true; |
---|
163 | } |
---|
164 | |
---|
165 | function _affectedrows() |
---|
166 | { |
---|
167 | return ($this->_stmt) ? $this->_stmt->rowCount() : 0; |
---|
168 | } |
---|
169 | |
---|
170 | function _insertid() |
---|
171 | { |
---|
172 | return ($this->_connectionID) ? $this->_connectionID->lastInsertId() : 0; |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | class ADOPDOStatement { |
---|
177 | |
---|
178 | var $databaseType = "pdo"; |
---|
179 | var $dataProvider = "pdo"; |
---|
180 | var $_stmt; |
---|
181 | var $_connectionID; |
---|
182 | |
---|
183 | function ADOPDOStatement($stmt,$connection) |
---|
184 | { |
---|
185 | $this->_stmt = $stmt; |
---|
186 | $this->_connectionID = $connection; |
---|
187 | } |
---|
188 | |
---|
189 | function Execute($inputArr=false) |
---|
190 | { |
---|
191 | $savestmt = $this->_connectionID->_stmt; |
---|
192 | $rs = $this->_connectionID->Execute(array(false,$this->_stmt),$inputArr); |
---|
193 | $this->_connectionID->_stmt = $savestmt; |
---|
194 | return $rs; |
---|
195 | } |
---|
196 | |
---|
197 | function InParameter(&$var,$name,$maxLen=4000,$type=false) |
---|
198 | { |
---|
199 | |
---|
200 | if ($type) $this->_stmt->bindParam($name,$var,$type,$maxLen); |
---|
201 | else $this->_stmt->bindParam($name, $var); |
---|
202 | } |
---|
203 | |
---|
204 | function Affected_Rows() |
---|
205 | { |
---|
206 | return ($this->_stmt) ? $this->_stmt->rowCount() : 0; |
---|
207 | } |
---|
208 | |
---|
209 | function ErrorMsg() |
---|
210 | { |
---|
211 | if ($this->_stmt) $arr = $this->_stmt->errorInfo(); |
---|
212 | else $arr = $this->_connectionID->errorInfo(); |
---|
213 | print_r($arr); |
---|
214 | if ($arr) { |
---|
215 | if ($arr[0]) return $arr[2]; |
---|
216 | else return ''; |
---|
217 | } else return '-1'; |
---|
218 | } |
---|
219 | |
---|
220 | function ErrorNo() |
---|
221 | { |
---|
222 | if ($this->_stmt) return $this->_stmt->errorCode(); |
---|
223 | else return $this->_connectionID->errorInfo(); |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | /*-------------------------------------------------------------------------------------- |
---|
228 | Class Name: Recordset |
---|
229 | --------------------------------------------------------------------------------------*/ |
---|
230 | |
---|
231 | class ADORecordSet_pdo extends ADORecordSet { |
---|
232 | |
---|
233 | var $bind = false; |
---|
234 | var $databaseType = "pdo"; |
---|
235 | var $dataProvider = "pdo"; |
---|
236 | |
---|
237 | function ADORecordSet_pdo($id,$mode=false) |
---|
238 | { |
---|
239 | if ($mode === false) { |
---|
240 | global $ADODB_FETCH_MODE; |
---|
241 | $mode = $ADODB_FETCH_MODE; |
---|
242 | } |
---|
243 | switch($mode) { |
---|
244 | default: |
---|
245 | case ADODB_FETCH_BOTH: $mode = PDO_FETCH_BOTH; break; |
---|
246 | case ADODB_FETCH_NUM: $mode = PDO_FETCH_NUM; break; |
---|
247 | case ADODB_FETCH_ASSOC: $mode = PDO_FETCH_ASSOC; break; |
---|
248 | } |
---|
249 | $this->fetchMode = $mode; |
---|
250 | |
---|
251 | $this->_queryID = $id; |
---|
252 | $this->ADORecordSet($id); |
---|
253 | } |
---|
254 | |
---|
255 | |
---|
256 | // returns the field object |
---|
257 | function &FetchField($fieldOffset = -1) |
---|
258 | { |
---|
259 | |
---|
260 | $off=$fieldOffset+1; // offsets begin at 1 |
---|
261 | |
---|
262 | $o= new ADOFieldObject(); |
---|
263 | $o->name = @odbc_field_name($this->_queryID,$off); |
---|
264 | $o->type = @odbc_field_type($this->_queryID,$off); |
---|
265 | $o->max_length = @odbc_field_len($this->_queryID,$off); |
---|
266 | if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name); |
---|
267 | else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name); |
---|
268 | return $o; |
---|
269 | } |
---|
270 | |
---|
271 | function Init() |
---|
272 | { |
---|
273 | if ($this->_inited) return; |
---|
274 | $this->_inited = true; |
---|
275 | if ($this->_queryID) @$this->_initrs(); |
---|
276 | else { |
---|
277 | $this->_numOfRows = 0; |
---|
278 | $this->_numOfFields = 0; |
---|
279 | } |
---|
280 | if ($this->_numOfRows != 0 && $this->_currentRow == -1) { |
---|
281 | $this->_currentRow = 0; |
---|
282 | if ($this->EOF = ($this->_fetch() === false)) { |
---|
283 | $this->_numOfRows = 0; // _numOfRows could be -1 |
---|
284 | } |
---|
285 | $this->_numOfFields = sizeof($this->fields); |
---|
286 | } else { |
---|
287 | $this->EOF = true; |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | |
---|
292 | function _initrs() |
---|
293 | { |
---|
294 | global $ADODB_COUNTRECS; |
---|
295 | |
---|
296 | $this->_numOfRows = ($ADODB_COUNTRECS) ? @$this->_queryID->rowCount() : -1; |
---|
297 | if (!$this->_numOfRows) $this->_numOfRows = -1; |
---|
298 | $this->_numOfFields =0; |
---|
299 | } |
---|
300 | |
---|
301 | function _seek($row) |
---|
302 | { |
---|
303 | return false; |
---|
304 | } |
---|
305 | |
---|
306 | function _fetch() |
---|
307 | { |
---|
308 | $this->fields = $this->_queryID->fetch($this->fetchMode); |
---|
309 | return !empty($this->fields); |
---|
310 | } |
---|
311 | |
---|
312 | function _close() |
---|
313 | { |
---|
314 | $this->_queryID = false; |
---|
315 | } |
---|
316 | |
---|
317 | } |
---|
318 | |
---|
319 | ?> |
---|