3 V5.14 8 Sept 2011 (c) 2000-2011 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 return call_user_func_array(array($this,'parent::Concat'), $args);
187 // returns true or false
188 function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
190 return $this->_connect($argDSN, $argUsername, $argPassword, $argDatabasename, true);
193 /*------------------------------------------------------------------------------*/
196 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
198 $save = $this->_driver
->fetchMode
;
199 $this->_driver
->fetchMode
= $this->fetchMode
;
200 $this->_driver
->debug
= $this->debug
;
201 $ret = $this->_driver
->SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
202 $this->_driver
->fetchMode
= $save;
207 function ServerInfo()
209 return $this->_driver
->ServerInfo();
212 function MetaTables($ttype=false,$showSchema=false,$mask=false)
214 return $this->_driver
->MetaTables($ttype,$showSchema,$mask);
217 function MetaColumns($table,$normalize=true)
219 return $this->_driver
->MetaColumns($table,$normalize);
222 function InParameter(&$stmt,&$var,$name,$maxLen=4000,$type=false)
225 if ($type) $obj->bindParam($name,$var,$type,$maxLen);
226 else $obj->bindParam($name, $var);
229 function OffsetDate($dayFraction,$date=false)
231 return $this->_driver
->OffsetDate($dayFraction,$date);
236 if ($this->_errormsg
!== false) return $this->_errormsg
;
237 if (!empty($this->_stmt
)) $arr = $this->_stmt
->errorInfo();
238 else if (!empty($this->_connectionID
)) $arr = $this->_connectionID
->errorInfo();
239 else return 'No Connection Established';
243 if (sizeof($arr)<2) return '';
244 if ((integer)$arr[1]) return $arr[2];
252 if ($this->_errorno
!== false) return $this->_errorno
;
253 if (!empty($this->_stmt
)) $err = $this->_stmt
->errorCode();
254 else if (!empty($this->_connectionID
)) {
255 $arr = $this->_connectionID
->errorInfo();
256 if (isset($arr[0])) $err = $arr[0];
261 if ($err == '00000') return 0; // allows empty check
265 function SetTransactionMode($transaction_mode)
267 if(method_exists($this->_driver
, 'SetTransactionMode'))
268 return $this->_driver
->SetTransactionMode($transaction_mode);
270 return parent
::SetTransactionMode($seqname);
273 function BeginTrans()
275 if(method_exists($this->_driver
, 'BeginTrans'))
276 return $this->_driver
->BeginTrans();
278 if (!$this->hasTransactions
) return false;
279 if ($this->transOff
) return true;
280 $this->transCnt +
= 1;
281 $this->_autocommit
= false;
282 $this->_connectionID
->setAttribute(PDO
::ATTR_AUTOCOMMIT
,false);
283 return $this->_connectionID
->beginTransaction();
286 function CommitTrans($ok=true)
288 if(method_exists($this->_driver
, 'CommitTrans'))
289 return $this->_driver
->CommitTrans($ok);
291 if (!$this->hasTransactions
) return false;
292 if ($this->transOff
) return true;
293 if (!$ok) return $this->RollbackTrans();
294 if ($this->transCnt
) $this->transCnt
-= 1;
295 $this->_autocommit
= true;
297 $ret = $this->_connectionID
->commit();
298 $this->_connectionID
->setAttribute(PDO
::ATTR_AUTOCOMMIT
,true);
302 function RollbackTrans()
304 if(method_exists($this->_driver
, 'RollbackTrans'))
305 return $this->_driver
->RollbackTrans();
307 if (!$this->hasTransactions
) return false;
308 if ($this->transOff
) return true;
309 if ($this->transCnt
) $this->transCnt
-= 1;
310 $this->_autocommit
= true;
312 $ret = $this->_connectionID
->rollback();
313 $this->_connectionID
->setAttribute(PDO
::ATTR_AUTOCOMMIT
,true);
317 function Prepare($sql)
319 $this->_stmt
= $this->_connectionID
->prepare($sql);
320 if ($this->_stmt
) return array($sql,$this->_stmt
);
325 function PrepareStmt($sql)
327 $stmt = $this->_connectionID
->prepare($sql);
328 if (!$stmt) return false;
329 $obj = new ADOPDOStatement($stmt,$this);
333 function CreateSequence($seqname='adodbseq',$startID=1)
335 if(method_exists($this->_driver
, 'CreateSequence'))
336 return $this->_driver
->CreateSequence($seqname, $startID);
338 return parent
::CreateSequence($seqname, $startID);
341 function DropSequence($seqname='adodbseq')
343 if(method_exists($this->_driver
, 'DropSequence'))
344 return $this->_driver
->DropSequence($seqname);
346 return parent
::DropSequence($seqname);
349 function GenID($seqname='adodbseq',$startID=1)
351 if(method_exists($this->_driver
, 'GenID'))
352 return $this->_driver
->GenID($seqname, $startID);
354 return parent
::GenID($seqname, $startID);
358 /* returns queryID or false */
359 function _query($sql,$inputarr=false)
361 if (is_array($sql)) {
364 $stmt = $this->_connectionID
->prepare($sql);
367 #var_dump($this->_bindInputArray);
369 $this->_driver
->debug
= $this->debug
;
370 if ($inputarr) $ok = $stmt->execute($inputarr);
371 else $ok = $stmt->execute();
375 $this->_errormsg
= false;
376 $this->_errorno
= false;
379 $this->_stmt
= $stmt;
385 $arr = $stmt->errorinfo();
386 if ((integer)$arr[1]) {
387 $this->_errormsg
= $arr[2];
388 $this->_errorno
= $arr[1];
392 $this->_errormsg
= false;
393 $this->_errorno
= false;
398 // returns true or false
401 $this->_stmt
= false;
405 function _affectedrows()
407 return ($this->_stmt
) ?
$this->_stmt
->rowCount() : 0;
412 return ($this->_connectionID
) ?
$this->_connectionID
->lastInsertId() : 0;
416 class ADODB_pdo_base
extends ADODB_pdo
{
418 var $sysDate = "'?'";
419 var $sysTimeStamp = "'?'";
422 function _init($parentDriver)
424 $parentDriver->_bindInputArray
= true;
425 #$parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
428 function ServerInfo()
430 return ADOConnection
::ServerInfo();
433 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
435 $ret = ADOConnection
::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
439 function MetaTables()
444 function MetaColumns()
450 class ADOPDOStatement
{
452 var $databaseType = "pdo";
453 var $dataProvider = "pdo";
457 function ADOPDOStatement($stmt,$connection)
459 $this->_stmt
= $stmt;
460 $this->_connectionID
= $connection;
463 function Execute($inputArr=false)
465 $savestmt = $this->_connectionID
->_stmt
;
466 $rs = $this->_connectionID
->Execute(array(false,$this->_stmt
),$inputArr);
467 $this->_connectionID
->_stmt
= $savestmt;
471 function InParameter(&$var,$name,$maxLen=4000,$type=false)
474 if ($type) $this->_stmt
->bindParam($name,$var,$type,$maxLen);
475 else $this->_stmt
->bindParam($name, $var);
478 function Affected_Rows()
480 return ($this->_stmt
) ?
$this->_stmt
->rowCount() : 0;
485 if ($this->_stmt
) $arr = $this->_stmt
->errorInfo();
486 else $arr = $this->_connectionID
->errorInfo();
488 if (is_array($arr)) {
489 if ((integer) $arr[0] && isset($arr[2])) return $arr[2];
496 return ($this->_stmt
) ?
$this->_stmt
->columnCount() : 0;
501 if ($this->_stmt
) return $this->_stmt
->errorCode();
502 else return $this->_connectionID
->errorInfo();
506 /*--------------------------------------------------------------------------------------
507 Class Name: Recordset
508 --------------------------------------------------------------------------------------*/
510 class ADORecordSet_pdo
extends ADORecordSet
{
513 var $databaseType = "pdo";
514 var $dataProvider = "pdo";
516 function ADORecordSet_pdo($id,$mode=false)
518 if ($mode === false) {
519 global $ADODB_FETCH_MODE;
520 $mode = $ADODB_FETCH_MODE;
522 $this->adodbFetchMode
= $mode;
524 case ADODB_FETCH_NUM
: $mode = PDO
::FETCH_NUM
; break;
525 case ADODB_FETCH_ASSOC
: $mode = PDO
::FETCH_ASSOC
; break;
527 case ADODB_FETCH_BOTH
:
528 default: $mode = PDO
::FETCH_BOTH
; break;
530 $this->fetchMode
= $mode;
532 $this->_queryID
= $id;
533 $this->ADORecordSet($id);
539 if ($this->_inited
) return;
540 $this->_inited
= true;
541 if ($this->_queryID
) @$this->_initrs();
543 $this->_numOfRows
= 0;
544 $this->_numOfFields
= 0;
546 if ($this->_numOfRows
!= 0 && $this->_currentRow
== -1) {
547 $this->_currentRow
= 0;
548 if ($this->EOF
= ($this->_fetch() === false)) {
549 $this->_numOfRows
= 0; // _numOfRows could be -1
558 global $ADODB_COUNTRECS;
560 $this->_numOfRows
= ($ADODB_COUNTRECS) ?
@$this->_queryID
->rowCount() : -1;
561 if (!$this->_numOfRows
) $this->_numOfRows
= -1;
562 $this->_numOfFields
= $this->_queryID
->columnCount();
565 // returns the field object
566 function FetchField($fieldOffset = -1)
568 $off=$fieldOffset+
1; // offsets begin at 1
570 $o= new ADOFieldObject();
571 $arr = @$this->_queryID
->getColumnMeta($fieldOffset);
573 $o->name
= 'bad getColumnMeta()';
575 $o->type
= 'VARCHAR';
581 $o->name
= $arr['name'];
582 if (isset($arr['native_type']) && $arr['native_type'] <> "null") $o->type
= $arr['native_type'];
583 else $o->type
= adodb_pdo_type($arr['pdo_type']);
584 $o->max_length
= $arr['len'];
585 $o->precision
= $arr['precision'];
587 if (ADODB_ASSOC_CASE
== 0) $o->name
= strtolower($o->name
);
588 else if (ADODB_ASSOC_CASE
== 1) $o->name
= strtoupper($o->name
);
599 if (!$this->_queryID
) return false;
601 $this->fields
= $this->_queryID
->fetch($this->fetchMode
);
602 return !empty($this->fields
);
607 $this->_queryID
= false;
610 function Fields($colname)
612 if ($this->adodbFetchMode
!= ADODB_FETCH_NUM
) return @$this->fields
[$colname];
615 $this->bind
= array();
616 for ($i=0; $i < $this->_numOfFields
; $i++
) {
617 $o = $this->FetchField($i);
618 $this->bind
[strtoupper($o->name
)] = $i;
621 return $this->fields
[$this->bind
[strtoupper($colname)]];