Zikula 1.0.1
[ class tree: Zikula 1.0.1 ] [ index: Zikula 1.0.1 ] [ all elements ]

Source for file adodb-pdo.inc.php

Documentation is available at adodb-pdo.inc.php

  1. <?php
  2. /* 
  3. V4.97 22 Jan 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.
  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 2return 'VARCHAR';
  55.     case 3return 'BLOB';
  56.     defaultreturn 'NUMERIC';
  57.     }
  58. }
  59.      
  60. /*--------------------------------------------------------------------------------------
  61. --------------------------------------------------------------------------------------*/
  62.  
  63. ////////////////////////////////////////////////
  64.  
  65.  
  66.  
  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;
  79.     
  80.     var $_errormsg = false;
  81.     var $_errorno = false;
  82.     
  83.     var $dsnType = '';
  84.     var $stmt = false;
  85.     
  86.     function ADODB_pdo()
  87.     {
  88.     }
  89.     
  90.     function _UpdatePDO()
  91.     {
  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;
  101.                 
  102.         $this->hasGenID = $d->hasGenID;
  103.         $this->_genIDSQL $d->_genIDSQL;
  104.         $this->_genSeqSQL = $d->_genSeqSQL;
  105.         $this->_dropSeqSQL $d->_dropSeqSQL;
  106.  
  107.         $d->_init($this);
  108.     }
  109.     
  110.     function Time()
  111.     {
  112.         if (!empty($this->_driver->_hasdual)) $sql "select $this->sysTimeStamp from dual";
  113.         else $sql "select $this->sysTimeStamp";
  114.         
  115.         $rs =$this->_Execute($sql);
  116.         if ($rs && !$rs->EOFreturn $this->UnixTimeStamp(reset($rs->fields));
  117.         
  118.         return false;
  119.     }
  120.     
  121.     // returns true or false
  122.     function _connect($argDSN$argUsername$argPassword$argDatabasename$persist=false)
  123.     {
  124.         $at strpos($argDSN,':');
  125.         $this->dsnType = substr($argDSN,0,$at);
  126.  
  127.         if ($argDatabasename{
  128.             $argDSN .= ';dbname='.$argDatabasename;
  129.         }
  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;
  138.         }
  139.         
  140.         if ($this->_connectionID{
  141.             switch(ADODB_ASSOC_CASE){
  142.             case 0$m PDO::CASE_LOWERbreak;
  143.             case 1$m PDO::CASE_UPPERbreak;
  144.             default:
  145.             case 2$m PDO::CASE_NATURALbreak;
  146.             }
  147.             
  148.             //$this->_connectionID->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT );
  149.             $this->_connectionID->setAttribute(PDO::ATTR_CASE,$m);
  150.             
  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;
  160.             }
  161.             if (class_exists($class))
  162.                 $this->_driver new $class();
  163.             else
  164.                 $this->_driver new ADODB_pdo_base();
  165.             
  166.             $this->_driver->_connectionID $this->_connectionID;
  167.             $this->_UpdatePDO();
  168.             return true;
  169.         }
  170.         $this->_driver new ADODB_pdo_base();
  171.         return false;
  172.     }
  173.     
  174.     // returns true or false
  175.     function _pconnect($argDSN$argUsername$argPassword$argDatabasename)
  176.     {
  177.         return $this->_connect($argDSN$argUsername$argPassword$argDatabasenametrue);
  178.     }
  179.     
  180.     /*------------------------------------------------------------------------------*/
  181.     
  182.     
  183.     function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0
  184.     {    
  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;
  191.     }
  192.     
  193.     
  194.     function ServerInfo()
  195.     {
  196.         return $this->_driver->ServerInfo();
  197.     }
  198.     
  199.     function MetaTables($ttype=false,$showSchema=false,$mask=false)
  200.     {
  201.         return $this->_driver->MetaTables($ttype,$showSchema,$mask);
  202.     }
  203.     
  204.     function MetaColumns($table,$normalize=true)
  205.     {
  206.         return $this->_driver->MetaColumns($table,$normalize);
  207.     }
  208.     
  209.     function InParameter(&$stmt,&$var,$name,$maxLen=4000,$type=false)
  210.     {
  211.         $obj $stmt[1];
  212.         if ($type$obj->bindParam($name,$var,$type,$maxLen);
  213.         else $obj->bindParam($name$var);
  214.     }
  215.     
  216.     
  217.     function ErrorMsg()
  218.     {
  219.         if ($this->_errormsg !== falsereturn $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';
  223.         
  224.         
  225.         if ($arr{
  226.              if (sizeof($arr)<2return '';
  227.             if ((integer)$arr[1]return $arr[2];
  228.             else return '';
  229.         else return '-1';
  230.     }
  231.     
  232.  
  233.     function ErrorNo()
  234.     {
  235.         if ($this->_errorno !== falsereturn $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;
  243.             
  244.         if ($err == '00000'return 0// allows empty check
  245.         return $err;
  246.     }
  247.  
  248.     function BeginTrans()
  249.     {    
  250.         if (!$this->hasTransactionsreturn false;
  251.         if ($this->transOffreturn true
  252.         $this->transCnt += 1;
  253.         $this->_autocommit = false;
  254.         $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,false);
  255.         return $this->_connectionID->beginTransaction();
  256.     }
  257.     
  258.     function CommitTrans($ok=true
  259.     
  260.         if (!$this->hasTransactionsreturn false;
  261.         if ($this->transOffreturn true
  262.         if (!$okreturn $this->RollbackTrans();
  263.         if ($this->transCnt$this->transCnt -= 1;
  264.         $this->_autocommit = true;
  265.         
  266.         $ret $this->_connectionID->commit();
  267.         $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
  268.         return $ret;
  269.     }
  270.     
  271.     function RollbackTrans()
  272.     {
  273.         if (!$this->hasTransactionsreturn false;
  274.         if ($this->transOffreturn true
  275.         if ($this->transCnt$this->transCnt -= 1;
  276.         $this->_autocommit = true;
  277.         
  278.         $ret $this->_connectionID->rollback();
  279.         $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true);
  280.         return $ret;
  281.     }
  282.     
  283.     function Prepare($sql)
  284.     {
  285.         $this->_stmt $this->_connectionID->prepare($sql);
  286.         if ($this->_stmtreturn array($sql,$this->_stmt);
  287.         
  288.         return false;
  289.     }
  290.     
  291.     function PrepareStmt($sql)
  292.     {
  293.         $stmt $this->_connectionID->prepare($sql);
  294.         if (!$stmtreturn false;
  295.         $obj new ADOPDOStatement($stmt,$this);
  296.         return $obj;
  297.     }
  298.     
  299.     
  300.     /* returns queryID or false */
  301.     function _query($sql,$inputarr=false
  302.     {
  303.         if (is_array($sql)) {
  304.             $stmt $sql[1];
  305.         else {
  306.             $stmt $this->_connectionID->prepare($sql);
  307.         }
  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();
  314.         
  315.         
  316.         
  317.         $this->_errormsg = false;
  318.         $this->_errorno = false;
  319.             
  320.         if ($ok{
  321.             $this->_stmt $stmt;
  322.             return $stmt;
  323.         }
  324.         
  325.         if ($stmt{
  326.             
  327.             $arr $stmt->errorinfo();
  328.             if ((integer)$arr[1]{
  329.                 $this->_errormsg = $arr[2];
  330.                 $this->_errorno = $arr[1];
  331.             }
  332.  
  333.         else {
  334.             $this->_errormsg = false;
  335.             $this->_errorno = false;
  336.         }
  337.         return false;
  338.     }
  339.  
  340.     // returns true or false
  341.     function _close()
  342.     {
  343.         $this->_stmt false;
  344.         return true;
  345.     }
  346.  
  347.     function _affectedrows()
  348.     {
  349.         return ($this->_stmt$this->_stmt->rowCount(0;
  350.     }
  351.     
  352.     function _insertid()
  353.     {
  354.         return ($this->_connectionID$this->_connectionID->lastInsertId(0;
  355.     }
  356. }
  357.  
  358.  
  359.  
  360. class ADODB_pdo_base extends ADODB_pdo {
  361.  
  362.     var $sysDate = "'?'";
  363.     var $sysTimeStamp = "'?'";
  364.     
  365.  
  366.     function _init($parentDriver)
  367.     {
  368.         $parentDriver->_bindInputArray true;
  369.         #$parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true);
  370.     }
  371.     
  372.     function ServerInfo()
  373.     {
  374.         return ADOConnection::ServerInfo();
  375.     }
  376.     
  377.     function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
  378.     {
  379.         $ret ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
  380.         return $ret;
  381.     }
  382.     
  383.     function MetaTables()
  384.     {
  385.         return false;
  386.     }
  387.     
  388.     function MetaColumns()
  389.     {
  390.         return false;
  391.     }
  392. }
  393.  
  394.  
  395. class ADOPDOStatement {
  396.  
  397.     var $databaseType = "pdo";        
  398.     var $dataProvider = "pdo";
  399.     var $_stmt;
  400.     var $_connectionID;
  401.     
  402.     function ADOPDOStatement($stmt,$connection)
  403.     {
  404.         $this->_stmt = $stmt;
  405.         $this->_connectionID = $connection;
  406.     }
  407.     
  408.     function Execute($inputArr=false)
  409.     {
  410.         $savestmt $this->_connectionID->_stmt;
  411.         $rs $this->_connectionID->Execute(array(false,$this->_stmt),$inputArr);
  412.         $this->_connectionID->_stmt $savestmt;
  413.         return $rs;
  414.     }
  415.     
  416.     function InParameter(&$var,$name,$maxLen=4000,$type=false)
  417.     {
  418.  
  419.         if ($type$this->_stmt->bindParam($name,$var,$type,$maxLen);
  420.         else $this->_stmt->bindParam($name$var);
  421.     }
  422.     
  423.     function Affected_Rows()
  424.     {
  425.         return ($this->_stmt$this->_stmt->rowCount(0;
  426.     }
  427.     
  428.     function ErrorMsg()
  429.     {
  430.         if ($this->_stmt$arr $this->_stmt->errorInfo();
  431.         else $arr $this->_connectionID->errorInfo();
  432.  
  433.         if (is_array($arr)) {
  434.             if ((integer) $arr[0&& isset($arr[2])) return $arr[2];
  435.             else return '';
  436.         else return '-1';
  437.     }
  438.     
  439.     function NumCols()
  440.     {
  441.         return ($this->_stmt$this->_stmt->columnCount(0;
  442.     }
  443.     
  444.     function ErrorNo()
  445.     {
  446.         if ($this->_stmtreturn $this->_stmt->errorCode();
  447.         else return $this->_connectionID->errorInfo();
  448.     }
  449. }
  450.  
  451. /*--------------------------------------------------------------------------------------
  452.      Class Name: Recordset
  453. --------------------------------------------------------------------------------------*/
  454.  
  455. class ADORecordSet_pdo extends ADORecordSet {    
  456.     
  457.     var $bind = false;
  458.     var $databaseType = "pdo";        
  459.     var $dataProvider = "pdo";
  460.     
  461.     function ADORecordSet_pdo($id,$mode=false)
  462.     {
  463.         if ($mode === false{  
  464.             global $ADODB_FETCH_MODE;
  465.             $mode $ADODB_FETCH_MODE;
  466.         }
  467.         $this->adodbFetchMode $mode;
  468.         switch($mode{
  469.         case ADODB_FETCH_NUM$mode PDO::FETCH_NUMbreak;
  470.         case ADODB_FETCH_ASSOC:  $mode PDO::FETCH_ASSOCbreak;
  471.         
  472.         case ADODB_FETCH_BOTH
  473.         default$mode PDO::FETCH_BOTHbreak;
  474.         }
  475.         $this->fetchMode = $mode;
  476.         
  477.         $this->_queryID = $id;
  478.         $this->ADORecordSet($id);
  479.     }
  480.  
  481.     
  482.     function Init()
  483.     {
  484.         if ($this->_initedreturn;
  485.         $this->_inited = true;
  486.         if ($this->_queryID@$this->_initrs();
  487.         else {
  488.             $this->_numOfRows = 0;
  489.             $this->_numOfFields = 0;
  490.         }
  491.         if ($this->_numOfRows != && $this->_currentRow == -1{
  492.             $this->_currentRow = 0;
  493.             if ($this->EOF = ($this->_fetch(=== false)) {
  494.                 $this->_numOfRows = 0// _numOfRows could be -1
  495.             }
  496.         else {
  497.             $this->EOF = true;
  498.         }
  499.     }
  500.     
  501.     function _initrs()
  502.     {
  503.     global $ADODB_COUNTRECS;
  504.     
  505.         $this->_numOfRows = ($ADODB_COUNTRECS@$this->_queryID->rowCount(: -1;
  506.         if (!$this->_numOfRows$this->_numOfRows = -1;
  507.         $this->_numOfFields = $this->_queryID->columnCount();
  508.     }
  509.  
  510.     // returns the field object
  511.     function &FetchField($fieldOffset = -1
  512.     {
  513.         $off=$fieldOffset+1// offsets begin at 1
  514.         
  515.         $onew 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;
  524.         }
  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'];
  531.         
  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;
  535.     }
  536.     
  537.     function _seek($row)
  538.     {
  539.         return false;
  540.     }
  541.     
  542.     function _fetch()
  543.     {
  544.         if (!$this->_queryIDreturn false;
  545.         
  546.         $this->fields = $this->_queryID->fetch($this->fetchMode);
  547.         return !empty($this->fields);
  548.     }
  549.     
  550.     function _close(
  551.     {
  552.         $this->_queryID = false;
  553.     }
  554.     
  555.     function Fields($colname)
  556.     {
  557.         if ($this->adodbFetchMode != ADODB_FETCH_NUMreturn @$this->fields[$colname];
  558.         
  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;
  564.             }
  565.         }
  566.          return $this->fields[$this->bind[strtoupper($colname)]];
  567.     }
  568.  
  569. }
  570.  
  571. ?>

Documentation generated on Fri, 18 Jul 2008 21:41:12 +0200 by phpDocumentor 1.4.1