3 V5.19 23-Apr-2014 (c) 2000-2014 John Lim (jlim#natsoft.com). 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.
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();
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
30 /* get_col ptr should point to start of the string buffer
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
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
46 /* magic flag to denote a parameter as being input/output
47 PDO::PARAM_INPUT_OUTPUT = 0x80000000
51 function adodb_pdo_type($t)
54 case 2: return 'VARCHAR';
55 case 3: return 'BLOB';
56 default: return 'NUMERIC';
60 /*--------------------------------------------------------------------------------------
61 --------------------------------------------------------------------------------------*/
63 ////////////////////////////////////////////////
70 class ADODB_pdo extends ADOConnection {
71 var $databaseType = "pdo";
72 var $dataProvider = "pdo";
73 var $fmtDate = "'Y-m-d'";
74 var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
75 var $replaceQuote = "''"; // string to use to replace quotes
76 var $hasAffectedRows = true;
77 var $_bindInputArray = true;
78 var $_genSeqSQL = "create table %s (id integer)";
79 var $_autocommit = true;
80 var $_haserrorfunctions = true;
81 var $_lastAffectedRows = 0;
83 var $_errormsg = false;
84 var $_errorno = false;
96 $this->fmtDate = $d->fmtDate;
97 $this->fmtTimeStamp = $d->fmtTimeStamp;
98 $this->replaceQuote = $d->replaceQuote;
99 $this->sysDate = $d->sysDate;
100 $this->sysTimeStamp = $d->sysTimeStamp;
101 $this->random = $d->random;
102 $this->concat_operator = $d->concat_operator;
103 $this->nameQuote = $d->nameQuote;
105 $this->hasGenID = $d->hasGenID;
106 $this->_genIDSQL = $d->_genIDSQL;
107 $this->_genSeqSQL = $d->_genSeqSQL;
108 $this->_dropSeqSQL = $d->_dropSeqSQL;
115 if (!empty($this->_driver->_hasdual)) $sql = "select $this->sysTimeStamp from dual";
116 else $sql = "select $this->sysTimeStamp";
118 $rs = $this->_Execute($sql);
119 if ($rs && !$rs->EOF) return $this->UnixTimeStamp(reset($rs->fields));
124 // returns true or false
125 function _connect($argDSN, $argUsername, $argPassword, $argDatabasename, $persist=false)
127 $at = strpos($argDSN,':');
128 $this->dsnType = substr($argDSN,0,$at);
130 if ($argDatabasename) {
131 $argDSN .= ';dbname='.$argDatabasename;
134 $this->_connectionID = new PDO($argDSN, $argUsername, $argPassword);
135 } catch (Exception $e) {
136 $this->_connectionID = false;
137 $this->_errorno = -1;
139 $this->_errormsg = 'Connection attempt failed: '.$e->getMessage();
143 if ($this->_connectionID) {
144 switch(ADODB_ASSOC_CASE){
145 case 0: $m = PDO::CASE_LOWER; break;
146 case 1: $m = PDO::CASE_UPPER; break;
148 case 2: $m = PDO::CASE_NATURAL; break;
151 //$this->_connectionID->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT );
152 $this->_connectionID->setAttribute(PDO::ATTR_CASE,$m);
154 $class = 'ADODB_pdo_'.$this->dsnType;
155 //$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
156 switch($this->dsnType) {
162 include_once(ADODB_DIR.'/drivers/adodb-pdo_'.$this->dsnType.'.inc.php');
165 if (class_exists($class))
166 $this->_driver = new $class();
168 $this->_driver = new ADODB_pdo_base();
170 $this->_driver->_connectionID = $this->_connectionID;
174 $this->_driver = new ADODB_pdo_base();
180 $args = func_get_args();
181 if(method_exists($this->_driver, 'Concat'))
182 return call_user_func_array(array($this->_driver, 'Concat'), $args);
184 if (PHP_VERSION >= 5.3) return call_user_func_array('parent::Concat', $args);
185 return call_user_func_array(array($this,'parent::Concat'), $args);
188 // returns true or false
189 function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
191 return $this->_connect($argDSN, $argUsername, $argPassword, $argDatabasename, true);
194 /*------------------------------------------------------------------------------*/
197 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
199 $save = $this->_driver->fetchMode;
200 $this->_driver->fetchMode = $this->fetchMode;
201 $this->_driver->debug = $this->debug;
202 $ret = $this->_driver->SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
203 $this->_driver->fetchMode = $save;
208 function ServerInfo()
210 return $this->_driver->ServerInfo();
213 function MetaTables($ttype=false,$showSchema=false,$mask=false)
215 return $this->_driver->MetaTables($ttype,$showSchema,$mask);
218 function MetaColumns($table,$normalize=true)
220 return $this->_driver->MetaColumns($table,$normalize);
223 function InParameter(&$stmt,&$var,$name,$maxLen=4000,$type=false)
226 if ($type) $obj->bindParam($name,$var,$type,$maxLen);
227 else $obj->bindParam($name, $var);
230 function OffsetDate($dayFraction,$date=false)
232 return $this->_driver->OffsetDate($dayFraction,$date);
237 if ($this->_errormsg !== false) return $this->_errormsg;
238 if (!empty($this->_stmt)) $arr = $this->_stmt->errorInfo();
239 else if (!empty($this->_connectionID)) $arr = $this->_connectionID->errorInfo();
240 else return 'No Connection Established';
244 if (sizeof($arr)<2) return '';
245 if ((integer)$arr[0]) return $arr[2];
253 if ($this->_errorno !== false) return $this->_errorno;
254 if (!empty($this->_stmt)) $err = $this->_stmt->errorCode();
255 else if (!empty($this->_connectionID)) {
256 $arr = $this->_connectionID->errorInfo();
257 if (isset($arr[0])) $err = $arr[0];
262 if ($err == '00000') return 0; // allows empty check
266 function SetTransactionMode($transaction_mode)
268 if(method_exists($this->_driver, 'SetTransactionMode'))
269 return $this->_driver->SetTransactionMode($transaction_mode);
271 return parent::SetTransactionMode($seqname);
274 function BeginTrans()
276 if(method_exists($this->_driver, 'BeginTrans'))
277 return $this->_driver->BeginTrans();
279 if (!$this->hasTransactions) return false;
280 if ($this->transOff) return true;
281 $this->transCnt += 1;
282 $this->_autocommit = false;
283 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,false);
284 return $this->_connectionID->beginTransaction();
287 function CommitTrans($ok=true)
289 if(method_exists($this->_driver, 'CommitTrans'))
290 return $this->_driver->CommitTrans($ok);
292 if (!$this->hasTransactions) return false;
293 if ($this->transOff) return true;
294 if (!$ok) return $this->RollbackTrans();
295 if ($this->transCnt) $this->transCnt -= 1;
296 $this->_autocommit = true;
298 $ret = $this->_connectionID->commit();
299 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
303 function RollbackTrans()
305 if(method_exists($this->_driver, 'RollbackTrans'))
306 return $this->_driver->RollbackTrans();
308 if (!$this->hasTransactions) return false;
309 if ($this->transOff) return true;
310 if ($this->transCnt) $this->transCnt -= 1;
311 $this->_autocommit = true;
313 $ret = $this->_connectionID->rollback();
314 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
318 function Prepare($sql)
320 $this->_stmt = $this->_connectionID->prepare($sql);
321 if ($this->_stmt) return array($sql,$this->_stmt);
326 function PrepareStmt($sql)
328 $stmt = $this->_connectionID->prepare($sql);
329 if (!$stmt) return false;
330 $obj = new ADOPDOStatement($stmt,$this);
334 function CreateSequence($seqname='adodbseq',$startID=1)
336 if(method_exists($this->_driver, 'CreateSequence'))
337 return $this->_driver->CreateSequence($seqname, $startID);
339 return parent::CreateSequence($seqname, $startID);
342 function DropSequence($seqname='adodbseq')
344 if(method_exists($this->_driver, 'DropSequence'))
345 return $this->_driver->DropSequence($seqname);
347 return parent::DropSequence($seqname);
350 function GenID($seqname='adodbseq',$startID=1)
352 if(method_exists($this->_driver, 'GenID'))
353 return $this->_driver->GenID($seqname, $startID);
355 return parent::GenID($seqname, $startID);
359 /* returns queryID or false */
360 function _query($sql,$inputarr=false)
362 if (is_array($sql)) {
365 $stmt = $this->_connectionID->prepare($sql);
368 #var_dump($this->_bindInputArray);
370 $this->_driver->debug = $this->debug;
371 if ($inputarr) $ok = $stmt->execute($inputarr);
372 else $ok = $stmt->execute();
376 $this->_errormsg = false;
377 $this->_errorno = false;
380 $this->_stmt = $stmt;
386 $arr = $stmt->errorinfo();
387 if ((integer)$arr[1]) {
388 $this->_errormsg = $arr[2];
389 $this->_errorno = $arr[1];
393 $this->_errormsg = false;
394 $this->_errorno = false;
399 // returns true or false
402 $this->_stmt = false;
406 function _affectedrows()
408 return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
413 return ($this->_connectionID) ? $this->_connectionID->lastInsertId() : 0;
417 class ADODB_pdo_base extends ADODB_pdo {
419 var $sysDate = "'?'";
420 var $sysTimeStamp = "'?'";
423 function _init($parentDriver)
425 $parentDriver->_bindInputArray = true;
426 #$parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
429 function ServerInfo()
431 return ADOConnection::ServerInfo();
434 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
436 $ret = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
440 function MetaTables()
445 function MetaColumns()
451 class ADOPDOStatement {
453 var $databaseType = "pdo";
454 var $dataProvider = "pdo";
458 function ADOPDOStatement($stmt,$connection)
460 $this->_stmt = $stmt;
461 $this->_connectionID = $connection;
464 function Execute($inputArr=false)
466 $savestmt = $this->_connectionID->_stmt;
467 $rs = $this->_connectionID->Execute(array(false,$this->_stmt),$inputArr);
468 $this->_connectionID->_stmt = $savestmt;
472 function InParameter(&$var,$name,$maxLen=4000,$type=false)
475 if ($type) $this->_stmt->bindParam($name,$var,$type,$maxLen);
476 else $this->_stmt->bindParam($name, $var);
479 function Affected_Rows()
481 return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
486 if ($this->_stmt) $arr = $this->_stmt->errorInfo();
487 else $arr = $this->_connectionID->errorInfo();
489 if (is_array($arr)) {
490 if ((integer) $arr[0] && isset($arr[2])) return $arr[2];
497 return ($this->_stmt) ? $this->_stmt->columnCount() : 0;
502 if ($this->_stmt) return $this->_stmt->errorCode();
503 else return $this->_connectionID->errorInfo();
507 /*--------------------------------------------------------------------------------------
508 Class Name: Recordset
509 --------------------------------------------------------------------------------------*/
511 class ADORecordSet_pdo extends ADORecordSet {
514 var $databaseType = "pdo";
515 var $dataProvider = "pdo";
517 function ADORecordSet_pdo($id,$mode=false)
519 if ($mode === false) {
520 global $ADODB_FETCH_MODE;
521 $mode = $ADODB_FETCH_MODE;
523 $this->adodbFetchMode = $mode;
525 case ADODB_FETCH_NUM: $mode = PDO::FETCH_NUM; break;
526 case ADODB_FETCH_ASSOC: $mode = PDO::FETCH_ASSOC; break;
528 case ADODB_FETCH_BOTH:
529 default: $mode = PDO::FETCH_BOTH; break;
531 $this->fetchMode = $mode;
533 $this->_queryID = $id;
534 $this->ADORecordSet($id);
540 if ($this->_inited) return;
541 $this->_inited = true;
542 if ($this->_queryID) @$this->_initrs();
544 $this->_numOfRows = 0;
545 $this->_numOfFields = 0;
547 if ($this->_numOfRows != 0 && $this->_currentRow == -1) {
548 $this->_currentRow = 0;
549 if ($this->EOF = ($this->_fetch() === false)) {
550 $this->_numOfRows = 0; // _numOfRows could be -1
559 global $ADODB_COUNTRECS;
561 $this->_numOfRows = ($ADODB_COUNTRECS) ? @$this->_queryID->rowCount() : -1;
562 if (!$this->_numOfRows) $this->_numOfRows = -1;
563 $this->_numOfFields = $this->_queryID->columnCount();
566 // returns the field object
567 function FetchField($fieldOffset = -1)
569 $off=$fieldOffset+1; // offsets begin at 1
571 $o= new ADOFieldObject();
572 $arr = @$this->_queryID->getColumnMeta($fieldOffset);
574 $o->name = 'bad getColumnMeta()';
576 $o->type = 'VARCHAR';
582 $o->name = $arr['name'];
583 if (isset($arr['native_type']) && $arr['native_type'] <> "null") $o->type = $arr['native_type'];
584 else $o->type = adodb_pdo_type($arr['pdo_type']);
585 $o->max_length = $arr['len'];
586 $o->precision = $arr['precision'];
588 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
589 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
600 if (!$this->_queryID) return false;
602 $this->fields = $this->_queryID->fetch($this->fetchMode);
603 return !empty($this->fields);
608 $this->_queryID = false;
611 function Fields($colname)
613 if ($this->adodbFetchMode != ADODB_FETCH_NUM) return @$this->fields[$colname];
616 $this->bind = array();
617 for ($i=0; $i < $this->_numOfFields; $i++) {
618 $o = $this->FetchField($i);
619 $this->bind[strtoupper($o->name)] = $i;
622 return $this->fields[$this->bind[strtoupper($colname)]];