]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/adodb/drivers/adodb-pdo.inc.php
Upgrade adodb
[SourceForge/phpwiki.git] / lib / WikiDB / adodb / drivers / adodb-pdo.inc.php
1 <?php
2 /* 
3 V5.18 3 Sep 2012  (c) 2000-2012 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.
8   
9   Latest version is available at http://adodb.sourceforge.net
10   
11   Requires ODBC. Works on Windows and Unix.
12
13         Problems: 
14                 Where is float/decimal type in pdo_param_type
15                 LOB handling for CLOB/BLOB differs significantly
16 */
17 // security - hide paths
18 if (!defined('ADODB_DIR')) die();
19
20
21 /*
22 enum pdo_param_type {
23 PDO::PARAM_NULL, 0
24
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
29
30 /* get_col ptr should point to start of the string buffer 
31 PDO::PARAM_STR, 2
32
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
37
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 
41
42 /* get_col ptr should point to a zend_bool 
43 PDO::PARAM_BOOL, 5
44
45
46 /* magic flag to denote a parameter as being input/output 
47 PDO::PARAM_INPUT_OUTPUT = 0x80000000
48 };
49 */
50         
51 function adodb_pdo_type($t)
52 {
53         switch($t) {
54         case 2: return 'VARCHAR';
55         case 3: return 'BLOB';
56         default: return 'NUMERIC';
57         }
58 }
59          
60 /*--------------------------------------------------------------------------------------
61 --------------------------------------------------------------------------------------*/
62
63 ////////////////////////////////////////////////
64
65
66
67
68
69
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;
82         
83         var $_errormsg = false;
84         var $_errorno = false;
85         
86         var $dsnType = '';
87         var $stmt = false;
88         
89         function ADODB_pdo()
90         {
91         }
92         
93         function _UpdatePDO()
94         {
95                 $d = $this->_driver;
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;
104                                 
105                 $this->hasGenID = $d->hasGenID;
106                 $this->_genIDSQL = $d->_genIDSQL;
107                 $this->_genSeqSQL = $d->_genSeqSQL;
108                 $this->_dropSeqSQL = $d->_dropSeqSQL;
109
110                 $d->_init($this);
111         }
112         
113         function Time()
114         {
115                 if (!empty($this->_driver->_hasdual)) $sql = "select $this->sysTimeStamp from dual";
116                 else $sql = "select $this->sysTimeStamp";
117                 
118                 $rs = $this->_Execute($sql);
119                 if ($rs && !$rs->EOF) return $this->UnixTimeStamp(reset($rs->fields));
120                 
121                 return false;
122         }
123         
124         // returns true or false
125         function _connect($argDSN, $argUsername, $argPassword, $argDatabasename, $persist=false)
126         {
127                 $at = strpos($argDSN,':');
128                 $this->dsnType = substr($argDSN,0,$at);
129
130                 if ($argDatabasename) {
131                         $argDSN .= ';dbname='.$argDatabasename;
132                 }
133                 try {
134                         $this->_connectionID = new PDO($argDSN, $argUsername, $argPassword);
135                 } catch (Exception $e) {
136                         $this->_connectionID = false;
137                         $this->_errorno = -1;
138                         //var_dump($e);
139                         $this->_errormsg = 'Connection attempt failed: '.$e->getMessage();
140                         return false;
141                 }
142                 
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;
147                         default:
148                         case 2: $m = PDO::CASE_NATURAL; break;
149                         }
150                         
151                         //$this->_connectionID->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT );
152                         $this->_connectionID->setAttribute(PDO::ATTR_CASE,$m);
153                         
154                         $class = 'ADODB_pdo_'.$this->dsnType;
155                         //$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
156                         switch($this->dsnType) {
157                         case 'oci':
158                         case 'mysql':
159                         case 'pgsql':
160                         case 'mssql':
161                         case 'sqlite':
162                                 include_once(ADODB_DIR.'/drivers/adodb-pdo_'.$this->dsnType.'.inc.php');
163                                 break;
164                         }
165                         if (class_exists($class))
166                                 $this->_driver = new $class();
167                         else
168                                 $this->_driver = new ADODB_pdo_base();
169                         
170                         $this->_driver->_connectionID = $this->_connectionID;
171                         $this->_UpdatePDO();
172                         return true;
173                 }
174                 $this->_driver = new ADODB_pdo_base();
175                 return false;
176         }
177         
178         function Concat() 
179         {
180                 $args = func_get_args();
181                 if(method_exists($this->_driver, 'Concat')) 
182                         return call_user_func_array(array($this->_driver, 'Concat'), $args); 
183                 
184                 return call_user_func_array(array($this,'parent::Concat'), $args); 
185         }
186         
187         // returns true or false
188         function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
189         {
190                 return $this->_connect($argDSN, $argUsername, $argPassword, $argDatabasename, true);
191         }
192         
193         /*------------------------------------------------------------------------------*/
194         
195         
196         function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) 
197         {       
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;
203                 return $ret;
204         }
205         
206         
207         function ServerInfo()
208         {
209                 return $this->_driver->ServerInfo();
210         }
211         
212         function MetaTables($ttype=false,$showSchema=false,$mask=false)
213         {
214                 return $this->_driver->MetaTables($ttype,$showSchema,$mask);
215         }
216         
217         function MetaColumns($table,$normalize=true)
218         {
219                 return $this->_driver->MetaColumns($table,$normalize);
220         }
221         
222         function InParameter(&$stmt,&$var,$name,$maxLen=4000,$type=false)
223         {
224                 $obj = $stmt[1];
225                 if ($type) $obj->bindParam($name,$var,$type,$maxLen);
226                 else $obj->bindParam($name, $var);
227         }
228         
229         function OffsetDate($dayFraction,$date=false)
230     {   
231         return $this->_driver->OffsetDate($dayFraction,$date);
232     }
233         
234         function ErrorMsg()
235         {
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';
240                 
241                 
242                 if ($arr) {
243                         if (sizeof($arr)<2) return '';
244                         if ((integer)$arr[0]) return $arr[2];
245                         else return '';
246                 } else return '-1';
247         }
248         
249
250         function ErrorNo()
251         {
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];
257                         else $err = -1;
258                 } else
259                         return 0;
260                         
261                 if ($err == '00000') return 0; // allows empty check
262                 return $err;
263         }
264
265         function SetTransactionMode($transaction_mode) 
266         {
267                 if(method_exists($this->_driver, 'SetTransactionMode')) 
268                         return $this->_driver->SetTransactionMode($transaction_mode); 
269                 
270                 return parent::SetTransactionMode($seqname); 
271         }
272
273         function BeginTrans()
274         {       
275                 if(method_exists($this->_driver, 'BeginTrans')) 
276                         return $this->_driver->BeginTrans(); 
277                 
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();
284         }
285         
286         function CommitTrans($ok=true) 
287         { 
288                 if(method_exists($this->_driver, 'CommitTrans')) 
289                         return $this->_driver->CommitTrans($ok); 
290                 
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;
296                 
297                 $ret = $this->_connectionID->commit();
298                 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
299                 return $ret;
300         }
301         
302         function RollbackTrans()
303         {
304                 if(method_exists($this->_driver, 'RollbackTrans')) 
305                         return $this->_driver->RollbackTrans(); 
306                 
307                 if (!$this->hasTransactions) return false;
308                 if ($this->transOff) return true; 
309                 if ($this->transCnt) $this->transCnt -= 1;
310                 $this->_autocommit = true;
311                 
312                 $ret = $this->_connectionID->rollback();
313                 $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
314                 return $ret;
315         }
316         
317         function Prepare($sql)
318         {
319                 $this->_stmt = $this->_connectionID->prepare($sql);
320                 if ($this->_stmt) return array($sql,$this->_stmt);
321                 
322                 return false;
323         }
324         
325         function PrepareStmt($sql)
326         {
327                 $stmt = $this->_connectionID->prepare($sql);
328                 if (!$stmt) return false;
329                 $obj = new ADOPDOStatement($stmt,$this);
330                 return $obj;
331         }
332         
333         function CreateSequence($seqname='adodbseq',$startID=1)
334         {
335                 if(method_exists($this->_driver, 'CreateSequence')) 
336                         return $this->_driver->CreateSequence($seqname, $startID); 
337                 
338                 return parent::CreateSequence($seqname, $startID); 
339         }
340         
341         function DropSequence($seqname='adodbseq')
342         {
343                 if(method_exists($this->_driver, 'DropSequence')) 
344                         return $this->_driver->DropSequence($seqname); 
345                 
346                 return parent::DropSequence($seqname); 
347         }
348
349         function GenID($seqname='adodbseq',$startID=1)
350         {
351                 if(method_exists($this->_driver, 'GenID')) 
352                         return $this->_driver->GenID($seqname, $startID); 
353                 
354                 return parent::GenID($seqname, $startID); 
355         }
356
357         
358         /* returns queryID or false */
359         function _query($sql,$inputarr=false) 
360         {
361                 if (is_array($sql)) {
362                         $stmt = $sql[1];
363                 } else {
364                         $stmt = $this->_connectionID->prepare($sql);
365                 }
366                 #adodb_backtrace();
367                 #var_dump($this->_bindInputArray);
368                 if ($stmt) {
369                         $this->_driver->debug = $this->debug;
370                         if ($inputarr) $ok = $stmt->execute($inputarr);
371                         else $ok = $stmt->execute();
372                 } 
373                 
374                 
375                 $this->_errormsg = false;
376                 $this->_errorno = false;
377                         
378                 if ($ok) {
379                         $this->_stmt = $stmt;
380                         return $stmt;
381                 }
382                 
383                 if ($stmt) {
384                         
385                         $arr = $stmt->errorinfo();
386                         if ((integer)$arr[1]) {
387                                 $this->_errormsg = $arr[2];
388                                 $this->_errorno = $arr[1];
389                         }
390
391                 } else {
392                         $this->_errormsg = false;
393                         $this->_errorno = false;
394                 }
395                 return false;
396         }
397
398         // returns true or false
399         function _close()
400         {
401                 $this->_stmt = false;
402                 return true;
403         }
404
405         function _affectedrows()
406         {
407                 return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
408         }
409         
410         function _insertid()
411         {
412                 return ($this->_connectionID) ? $this->_connectionID->lastInsertId() : 0;
413         }
414 }
415
416 class ADODB_pdo_base extends ADODB_pdo {
417
418         var $sysDate = "'?'";
419         var $sysTimeStamp = "'?'";
420         
421
422         function _init($parentDriver)
423         {
424                 $parentDriver->_bindInputArray = true;
425                 #$parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
426         }
427         
428         function ServerInfo()
429         {
430                 return ADOConnection::ServerInfo();
431         }
432         
433         function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
434         {
435                 $ret = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
436                 return $ret;
437         }
438         
439         function MetaTables()
440         {
441                 return false;
442         }
443         
444         function MetaColumns()
445         {
446                 return false;
447         }
448 }
449
450 class ADOPDOStatement {
451
452         var $databaseType = "pdo";              
453         var $dataProvider = "pdo";
454         var $_stmt;
455         var $_connectionID;
456         
457         function ADOPDOStatement($stmt,$connection)
458         {
459                 $this->_stmt = $stmt;
460                 $this->_connectionID = $connection;
461         }
462         
463         function Execute($inputArr=false)
464         {
465                 $savestmt = $this->_connectionID->_stmt;
466                 $rs = $this->_connectionID->Execute(array(false,$this->_stmt),$inputArr);
467                 $this->_connectionID->_stmt = $savestmt;
468                 return $rs;
469         }
470         
471         function InParameter(&$var,$name,$maxLen=4000,$type=false)
472         {
473
474                 if ($type) $this->_stmt->bindParam($name,$var,$type,$maxLen);
475                 else $this->_stmt->bindParam($name, $var);
476         }
477         
478         function Affected_Rows()
479         {
480                 return ($this->_stmt) ? $this->_stmt->rowCount() : 0;
481         }
482         
483         function ErrorMsg()
484         {
485                 if ($this->_stmt) $arr = $this->_stmt->errorInfo();
486                 else $arr = $this->_connectionID->errorInfo();
487
488                 if (is_array($arr)) {
489                         if ((integer) $arr[0] && isset($arr[2])) return $arr[2];
490                         else return '';
491                 } else return '-1';
492         }
493         
494         function NumCols()
495         {
496                 return ($this->_stmt) ? $this->_stmt->columnCount() : 0;
497         }
498         
499         function ErrorNo()
500         {
501                 if ($this->_stmt) return $this->_stmt->errorCode();
502                 else return $this->_connectionID->errorInfo();
503         }
504 }
505
506 /*--------------------------------------------------------------------------------------
507          Class Name: Recordset
508 --------------------------------------------------------------------------------------*/
509
510 class ADORecordSet_pdo extends ADORecordSet {   
511         
512         var $bind = false;
513         var $databaseType = "pdo";              
514         var $dataProvider = "pdo";
515         
516         function ADORecordSet_pdo($id,$mode=false)
517         {
518                 if ($mode === false) {  
519                         global $ADODB_FETCH_MODE;
520                         $mode = $ADODB_FETCH_MODE;
521                 }
522                 $this->adodbFetchMode = $mode;
523                 switch($mode) {
524                 case ADODB_FETCH_NUM: $mode = PDO::FETCH_NUM; break;
525                 case ADODB_FETCH_ASSOC:  $mode = PDO::FETCH_ASSOC; break;
526                 
527                 case ADODB_FETCH_BOTH: 
528                 default: $mode = PDO::FETCH_BOTH; break;
529                 }
530                 $this->fetchMode = $mode;
531                 
532                 $this->_queryID = $id;
533                 $this->ADORecordSet($id);
534         }
535
536         
537         function Init()
538         {
539                 if ($this->_inited) return;
540                 $this->_inited = true;
541                 if ($this->_queryID) @$this->_initrs();
542                 else {
543                         $this->_numOfRows = 0;
544                         $this->_numOfFields = 0;
545                 }
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
550                         }
551                 } else {
552                         $this->EOF = true;
553                 }
554         }
555         
556         function _initrs()
557         {
558         global $ADODB_COUNTRECS;
559         
560                 $this->_numOfRows = ($ADODB_COUNTRECS) ? @$this->_queryID->rowCount() : -1;
561                 if (!$this->_numOfRows) $this->_numOfRows = -1;
562                 $this->_numOfFields = $this->_queryID->columnCount();
563         }
564
565         // returns the field object
566         function FetchField($fieldOffset = -1) 
567         {
568                 $off=$fieldOffset+1; // offsets begin at 1
569                 
570                 $o= new ADOFieldObject();
571                 $arr = @$this->_queryID->getColumnMeta($fieldOffset);
572                 if (!$arr) {
573                         $o->name = 'bad getColumnMeta()';
574                         $o->max_length = -1;
575                         $o->type = 'VARCHAR';
576                         $o->precision = 0;
577         #               $false = false;
578                         return $o;
579                 }
580                 //adodb_pr($arr);
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'];
586                 
587                 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
588                 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
589                 return $o;
590         }
591         
592         function _seek($row)
593         {
594                 return false;
595         }
596         
597         function _fetch()
598         {
599                 if (!$this->_queryID) return false;
600                 
601                 $this->fields = $this->_queryID->fetch($this->fetchMode);
602                 return !empty($this->fields);
603         }
604         
605         function _close() 
606         {
607                 $this->_queryID = false;
608         }
609         
610         function Fields($colname)
611         {
612                 if ($this->adodbFetchMode != ADODB_FETCH_NUM) return @$this->fields[$colname];
613                 
614                 if (!$this->bind) {
615                         $this->bind = array();
616                         for ($i=0; $i < $this->_numOfFields; $i++) {
617                                 $o = $this->FetchField($i);
618                                 $this->bind[strtoupper($o->name)] = $i;
619                         }
620                 }
621                  return $this->fields[$this->bind[strtoupper($colname)]];
622         }
623
624 }
625
626 ?>