Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / adodb / drivers / adodb-pdo.inc.php
blob5d43b587794be4c0b70340fe51d361303e5e1c68
1 <?php
2 /*
3 V4.98 13 Feb 2008 (c) 2000-2008 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.
9 Latest version is available at http://adodb.sourceforge.net
11 Requires ODBC. Works on Windows and Unix.
13 Problems:
14 Where is float/decimal type in pdo_param_type
15 LOB handling for CLOB/BLOB differs significantly
17 // security - hide paths
18 if (!defined('ADODB_DIR')) die();
22 enum pdo_param_type {
23 PDO::PARAM_NULL, 0
25 /* int as in long (the php native int type).
26 * If you mark a column as an int, PDO expects get_col to return
27 * a pointer to a long
28 PDO::PARAM_INT, 1
30 /* get_col ptr should point to start of the string buffer
31 PDO::PARAM_STR, 2
33 /* get_col: when len is 0 ptr should point to a php_stream *,
34 * otherwise it should behave like a string. Indicate a NULL field
35 * value by setting the ptr to NULL
36 PDO::PARAM_LOB, 3
38 /* get_col: will expect the ptr to point to a new PDOStatement object handle,
39 * but this isn't wired up yet
40 PDO::PARAM_STMT, 4 /* hierarchical result set
42 /* get_col ptr should point to a zend_bool
43 PDO::PARAM_BOOL, 5
46 /* magic flag to denote a parameter as being input/output
47 PDO::PARAM_INPUT_OUTPUT = 0x80000000
51 function adodb_pdo_type($t)
53 switch($t) {
54 case 2: return 'VARCHAR';
55 case 3: return 'BLOB';
56 default: return 'NUMERIC';
60 /*--------------------------------------------------------------------------------------
61 --------------------------------------------------------------------------------------*/
63 ////////////////////////////////////////////////
67 class ADODB_pdo extends ADOConnection {
68 var $databaseType = "pdo";
69 var $dataProvider = "pdo";
70 var $fmtDate = "'Y-m-d'";
71 var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
72 var $replaceQuote = "''"; // string to use to replace quotes
73 var $hasAffectedRows = true;
74 var $_bindInputArray = true;
75 var $_genSeqSQL = "create table %s (id integer)";
76 var $_autocommit = true;
77 var $_haserrorfunctions = true;
78 var $_lastAffectedRows = 0;
80 var $_errormsg = false;
81 var $_errorno = false;
83 var $dsnType = '';
84 var $stmt = false;
86 function ADODB_pdo()
90 function _UpdatePDO()
92 $d = &$this->_driver;
93 $this->fmtDate = $d->fmtDate;
94 $this->fmtTimeStamp = $d->fmtTimeStamp;
95 $this->replaceQuote = $d->replaceQuote;
96 $this->sysDate = $d->sysDate;
97 $this->sysTimeStamp = $d->sysTimeStamp;
98 $this->random = $d->random;
99 $this->concat_operator = $d->concat_operator;
100 $this->nameQuote = $d->nameQuote;
102 $this->hasGenID = $d->hasGenID;
103 $this->_genIDSQL = $d->_genIDSQL;
104 $this->_genSeqSQL = $d->_genSeqSQL;
105 $this->_dropSeqSQL = $d->_dropSeqSQL;
107 $d->_init($this);
110 function Time()
112 if (!empty($this->_driver->_hasdual)) $sql = "select $this->sysTimeStamp from dual";
113 else $sql = "select $this->sysTimeStamp";
115 $rs =& $this->_Execute($sql);
116 if ($rs && !$rs->EOF) return $this->UnixTimeStamp(reset($rs->fields));
118 return false;
121 // returns true or false
122 function _connect($argDSN, $argUsername, $argPassword, $argDatabasename, $persist=false)
124 $at = strpos($argDSN,':');
125 $this->dsnType = substr($argDSN,0,$at);
127 if ($argDatabasename) {
128 $argDSN .= ';dbname='.$argDatabasename;
130 try {
131 $this->_connectionID = new PDO($argDSN, $argUsername, $argPassword);
132 } catch (Exception $e) {
133 $this->_connectionID = false;
134 $this->_errorno = -1;
135 //var_dump($e);
136 $this->_errormsg = 'Connection attempt failed: '.$e->getMessage();
137 return false;
140 if ($this->_connectionID) {
141 switch(ADODB_ASSOC_CASE){
142 case 0: $m = PDO::CASE_LOWER; break;
143 case 1: $m = PDO::CASE_UPPER; break;
144 default:
145 case 2: $m = PDO::CASE_NATURAL; break;
148 //$this->_connectionID->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT );
149 $this->_connectionID->setAttribute(PDO::ATTR_CASE,$m);
151 $class = 'ADODB_pdo_'.$this->dsnType;
152 //$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
153 switch($this->dsnType) {
154 case 'oci':
155 case 'mysql':
156 case 'pgsql':
157 case 'mssql':
158 include_once(ADODB_DIR.'/drivers/adodb-pdo_'.$this->dsnType.'.inc.php');
159 break;
161 if (class_exists($class))
162 $this->_driver = new $class();
163 else
164 $this->_driver = new ADODB_pdo_base();
166 $this->_driver->_connectionID = $this->_connectionID;
167 $this->_UpdatePDO();
168 return true;
170 $this->_driver = new ADODB_pdo_base();
171 return false;
174 // returns true or false
175 function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
177 return $this->_connect($argDSN, $argUsername, $argPassword, $argDatabasename, true);
180 /*------------------------------------------------------------------------------*/
183 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
185 $save = $this->_driver->fetchMode;
186 $this->_driver->fetchMode = $this->fetchMode;
187 $this->_driver->debug = $this->debug;
188 $ret = $this->_driver->SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
189 $this->_driver->fetchMode = $save;
190 return $ret;
194 function ServerInfo()
196 return $this->_driver->ServerInfo();
199 function MetaTables($ttype=false,$showSchema=false,$mask=false)
201 return $this->_driver->MetaTables($ttype,$showSchema,$mask);
204 function MetaColumns($table,$normalize=true)
206 return $this->_driver->MetaColumns($table,$normalize);
209 function InParameter(&$stmt,&$var,$name,$maxLen=4000,$type=false)
211 $obj = $stmt[1];
212 if ($type) $obj->bindParam($name,$var,$type,$maxLen);
213 else $obj->bindParam($name, $var);
217 function ErrorMsg()
219 if ($this->_errormsg !== false) return $this->_errormsg;
220 if (!empty($this->_stmt)) $arr = $this->_stmt->errorInfo();
221 else if (!empty($this->_connectionID)) $arr = $this->_connectionID->errorInfo();
222 else return 'No Connection Established';
225 if ($arr) {
226 if (sizeof($arr)<2) return '';
227 if ((integer)$arr[1]) return $arr[2];
228 else return '';
229 } else return '-1';
233 function ErrorNo()
235 if ($this->_errorno !== false) return $this->_errorno;
236 if (!empty($this->_stmt)) $err = $this->_stmt->errorCode();
237 else if (!empty($this->_connectionID)) {
238 $arr = $this->_connectionID->errorInfo();
239 if (isset($arr[0])) $err = $arr[0];
240 else $err = -1;
241 } else
242 return 0;
244 if ($err == '00000') return 0; // allows empty check
245 return $err;
248 function BeginTrans()
250 if (!$this->hasTransactions) return false;
251 if ($this->transOff) return true;
252 $this->transCnt += 1;
253 $this->_autocommit = false;
254 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,false);
255 return $this->_connectionID->beginTransaction();
258 function CommitTrans($ok=true)
260 if (!$this->hasTransactions) return false;
261 if ($this->transOff) return true;
262 if (!$ok) return $this->RollbackTrans();
263 if ($this->transCnt) $this->transCnt -= 1;
264 $this->_autocommit = true;
266 $ret = $this->_connectionID->commit();
267 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
268 return $ret;
271 function RollbackTrans()
273 if (!$this->hasTransactions) return false;
274 if ($this->transOff) return true;
275 if ($this->transCnt) $this->transCnt -= 1;
276 $this->_autocommit = true;
278 $ret = $this->_connectionID->rollback();
279 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
280 return $ret;
283 function Prepare($sql)
285 $this->_stmt = $this->_connectionID->prepare($sql);
286 if ($this->_stmt) return array($sql,$this->_stmt);
288 return false;
291 function PrepareStmt($sql)
293 $stmt = $this->_connectionID->prepare($sql);
294 if (!$stmt) return false;
295 $obj = new ADOPDOStatement($stmt,$this);
296 return $obj;
300 /* returns queryID or false */
301 function _query($sql,$inputarr=false)
303 if (is_array($sql)) {
304 $stmt = $sql[1];
305 } else {
306 $stmt = $this->_connectionID->prepare($sql);
308 #adodb_backtrace();
309 #var_dump($this->_bindInputArray);
310 if ($stmt) {
311 $this->_driver->debug = $this->debug;
312 if ($inputarr) $ok = $stmt->execute($inputarr);
313 else $ok = $stmt->execute();
317 $this->_errormsg = false;
318 $this->_errorno = false;
320 if ($ok) {
321 $this->_stmt = $stmt;
322 return $stmt;
325 if ($stmt) {
327 $arr = $stmt->errorinfo();
328 if ((integer)$arr[1]) {
329 $this->_errormsg = $arr[2];
330 $this->_errorno = $arr[1];
333 } else {
334 $this->_errormsg = false;
335 $this->_errorno = false;
337 return false;
340 // returns true or false
341 function _close()
343 $this->_stmt = false;
344 return true;
347 function _affectedrows()
349 return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
352 function _insertid()
354 return ($this->_connectionID) ? $this->_connectionID->lastInsertId() : 0;
360 class ADODB_pdo_base extends ADODB_pdo {
362 var $sysDate = "'?'";
363 var $sysTimeStamp = "'?'";
366 function _init($parentDriver)
368 $parentDriver->_bindInputArray = true;
369 #$parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
372 function ServerInfo()
374 return ADOConnection::ServerInfo();
377 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
379 $ret = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
380 return $ret;
383 function MetaTables()
385 return false;
388 function MetaColumns()
390 return false;
395 class ADOPDOStatement {
397 var $databaseType = "pdo";
398 var $dataProvider = "pdo";
399 var $_stmt;
400 var $_connectionID;
402 function ADOPDOStatement($stmt,$connection)
404 $this->_stmt = $stmt;
405 $this->_connectionID = $connection;
408 function Execute($inputArr=false)
410 $savestmt = $this->_connectionID->_stmt;
411 $rs = $this->_connectionID->Execute(array(false,$this->_stmt),$inputArr);
412 $this->_connectionID->_stmt = $savestmt;
413 return $rs;
416 function InParameter(&$var,$name,$maxLen=4000,$type=false)
419 if ($type) $this->_stmt->bindParam($name,$var,$type,$maxLen);
420 else $this->_stmt->bindParam($name, $var);
423 function Affected_Rows()
425 return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
428 function ErrorMsg()
430 if ($this->_stmt) $arr = $this->_stmt->errorInfo();
431 else $arr = $this->_connectionID->errorInfo();
433 if (is_array($arr)) {
434 if ((integer) $arr[0] && isset($arr[2])) return $arr[2];
435 else return '';
436 } else return '-1';
439 function NumCols()
441 return ($this->_stmt) ? $this->_stmt->columnCount() : 0;
444 function ErrorNo()
446 if ($this->_stmt) return $this->_stmt->errorCode();
447 else return $this->_connectionID->errorInfo();
451 /*--------------------------------------------------------------------------------------
452 Class Name: Recordset
453 --------------------------------------------------------------------------------------*/
455 class ADORecordSet_pdo extends ADORecordSet {
457 var $bind = false;
458 var $databaseType = "pdo";
459 var $dataProvider = "pdo";
461 function ADORecordSet_pdo($id,$mode=false)
463 if ($mode === false) {
464 global $ADODB_FETCH_MODE;
465 $mode = $ADODB_FETCH_MODE;
467 $this->adodbFetchMode = $mode;
468 switch($mode) {
469 case ADODB_FETCH_NUM: $mode = PDO::FETCH_NUM; break;
470 case ADODB_FETCH_ASSOC: $mode = PDO::FETCH_ASSOC; break;
472 case ADODB_FETCH_BOTH:
473 default: $mode = PDO::FETCH_BOTH; break;
475 $this->fetchMode = $mode;
477 $this->_queryID = $id;
478 $this->ADORecordSet($id);
482 function Init()
484 if ($this->_inited) return;
485 $this->_inited = true;
486 if ($this->_queryID) @$this->_initrs();
487 else {
488 $this->_numOfRows = 0;
489 $this->_numOfFields = 0;
491 if ($this->_numOfRows != 0 && $this->_currentRow == -1) {
492 $this->_currentRow = 0;
493 if ($this->EOF = ($this->_fetch() === false)) {
494 $this->_numOfRows = 0; // _numOfRows could be -1
496 } else {
497 $this->EOF = true;
501 function _initrs()
503 global $ADODB_COUNTRECS;
505 $this->_numOfRows = ($ADODB_COUNTRECS) ? @$this->_queryID->rowCount() : -1;
506 if (!$this->_numOfRows) $this->_numOfRows = -1;
507 $this->_numOfFields = $this->_queryID->columnCount();
510 // returns the field object
511 function &FetchField($fieldOffset = -1)
513 $off=$fieldOffset+1; // offsets begin at 1
515 $o= new ADOFieldObject();
516 $arr = @$this->_queryID->getColumnMeta($fieldOffset);
517 if (!$arr) {
518 $o->name = 'bad getColumnMeta()';
519 $o->max_length = -1;
520 $o->type = 'VARCHAR';
521 $o->precision = 0;
522 # $false = false;
523 return $o;
525 //adodb_pr($arr);
526 $o->name = $arr['name'];
527 if (isset($arr['native_type']) && $arr['native_type'] <> "null") $o->type = $arr['native_type'];
528 else $o->type = adodb_pdo_type($arr['pdo_type']);
529 $o->max_length = $arr['len'];
530 $o->precision = $arr['precision'];
532 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
533 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
534 return $o;
537 function _seek($row)
539 return false;
542 function _fetch()
544 if (!$this->_queryID) return false;
546 $this->fields = $this->_queryID->fetch($this->fetchMode);
547 return !empty($this->fields);
550 function _close()
552 $this->_queryID = false;
555 function Fields($colname)
557 if ($this->adodbFetchMode != ADODB_FETCH_NUM) return @$this->fields[$colname];
559 if (!$this->bind) {
560 $this->bind = array();
561 for ($i=0; $i < $this->_numOfFields; $i++) {
562 $o = $this->FetchField($i);
563 $this->bind[strtoupper($o->name)] = $i;
566 return $this->fields[$this->bind[strtoupper($colname)]];