Ignore:
Timestamp:
06/29/07 15:17:46 (17 years ago)
Author:
niltonneto
Message:

Versão nova do ADODB (4.5 para 4.95)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/phpgwapi/inc/adodb/drivers/adodb-pdo.inc.php

    r2 r34  
    11<?php 
    22/*  
    3 V4.51 29 July 2004  (c) 2000-2004 John Lim (jlim#natsoft.com.my). All rights reserved. 
     3V4.94 23 Jan 2007  (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved. 
    44  Released under both BSD license and Lesser GPL library license.  
    55  Whenever there is any discrepancy between the two licenses,  
     
    1010   
    1111  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 
    1216*/ 
    1317// security - hide paths 
    1418if (!defined('ADODB_DIR')) die(); 
     19 
     20 
     21/* 
     22enum pdo_param_type { 
     23PDO::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  
     28PDO::PARAM_INT, 1 
     29 
     30/* get_col ptr should point to start of the string buffer  
     31PDO::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  
     36PDO::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  
     40PDO::PARAM_STMT, 4 /* hierarchical result set  
     41 
     42/* get_col ptr should point to a zend_bool  
     43PDO::PARAM_BOOL, 5 
     44 
     45 
     46/* magic flag to denote a parameter as being input/output  
     47PDO::PARAM_INPUT_OUTPUT = 0x80000000 
     48}; 
     49*/ 
     50         
     51function adodb_pdo_type($t) 
     52{ 
     53        switch($t) { 
     54        case 2: return 'VARCHAR'; 
     55        case 3: return 'BLOB'; 
     56        default: return 'NUMERIC'; 
     57        } 
     58} 
    1559          
    1660/*-------------------------------------------------------------------------------------- 
    1761--------------------------------------------------------------------------------------*/ 
     62 
     63//////////////////////////////////////////////// 
     64 
     65 
     66 
     67class ADODB_pdo_base extends ADODB_pdo { 
     68 
     69        var $sysDate = "'?'"; 
     70        var $sysTimeStamp = "'?'"; 
     71         
     72 
     73        function _init($parentDriver) 
     74        { 
     75                $parentDriver->_bindInputArray = true; 
     76                #$parentDriver->_connectionID->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,true); 
     77        } 
     78         
     79        function ServerInfo() 
     80        { 
     81                return ADOConnection::ServerInfo(); 
     82        } 
     83         
     84        function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0) 
     85        { 
     86                $ret = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache); 
     87                return $ret; 
     88        } 
     89         
     90        function MetaTables() 
     91        { 
     92                return false; 
     93        } 
     94         
     95        function MetaColumns() 
     96        { 
     97                return false; 
     98        } 
     99} 
    18100 
    19101 
     
    31113        var $_lastAffectedRows = 0; 
    32114         
     115        var $_errormsg = false; 
     116        var $_errorno = false; 
     117         
     118        var $dsnType = ''; 
    33119        var $stmt = false; 
    34120         
    35         function ADODB_pdo()  
    36         { 
    37         } 
    38          
    39  
     121        function ADODB_pdo() 
     122        { 
     123        } 
     124         
     125        function _UpdatePDO() 
     126        { 
     127                $d = &$this->_driver; 
     128                $this->fmtDate = $d->fmtDate; 
     129                $this->fmtTimeStamp = $d->fmtTimeStamp; 
     130                $this->replaceQuote = $d->replaceQuote; 
     131                $this->sysDate = $d->sysDate; 
     132                $this->sysTimeStamp = $d->sysTimeStamp; 
     133                $this->random = $d->random; 
     134                $this->concat_operator = $d->concat_operator; 
     135                $this->nameQuote = $d->nameQuote; 
     136                                 
     137                $this->hasGenID = $d->hasGenID; 
     138                $this->_genIDSQL = $d->_genIDSQL; 
     139                $this->_genSeqSQL = $d->_genSeqSQL; 
     140                $this->_dropSeqSQL = $d->_dropSeqSQL; 
     141 
     142                $d->_init($this); 
     143        } 
     144         
     145        function Time() 
     146        { 
     147                if (!empty($this->_driver->_hasdual)) $sql = "select $this->sysTimeStamp from dual"; 
     148                else $sql = "select $this->sysTimeStamp"; 
     149                 
     150                $rs =& $this->_Execute($sql); 
     151                if ($rs && !$rs->EOF) return $this->UnixTimeStamp(reset($rs->fields)); 
     152                 
     153                return false; 
     154        } 
     155         
    40156        // returns true or false 
    41157        function _connect($argDSN, $argUsername, $argPassword, $argDatabasename, $persist=false) 
    42158        { 
    43                 $this->_connectionID = new PDO($argDSN, $argUsername, $argPassword); 
     159                $at = strpos($argDSN,':'); 
     160                $this->dsnType = substr($argDSN,0,$at); 
     161 
     162                if ($argDatabasename) { 
     163                        $argDSN .= ';dbname='.$argDatabasename; 
     164                } 
     165                try { 
     166                        $this->_connectionID = new PDO($argDSN, $argUsername, $argPassword); 
     167                } catch (Exception $e) { 
     168                        $this->_connectionID = false; 
     169                        $this->_errorno = -1; 
     170                        //var_dump($e); 
     171                        $this->_errormsg = 'Connection attempt failed: '.$e->getMessage(); 
     172                        return false; 
     173                } 
     174                 
    44175                if ($this->_connectionID) { 
    45176                        switch(ADODB_ASSOC_CASE){ 
    46                         case 0: $m = PDO_CASE_LOWER; break; 
    47                         case 1: $m = PDO_CASE_UPPER; break; 
     177                        case 0: $m = PDO::CASE_LOWER; break; 
     178                        case 1: $m = PDO::CASE_UPPER; break; 
    48179                        default: 
    49                         case 2: $m = PDO_CASE_NATURAL; break; 
     180                        case 2: $m = PDO::CASE_NATURAL; break; 
    50181                        } 
    51182                         
    52                         //$this->_connectionID->setAttribute(PDO_ATTR_ERRMODE,PDO_ERRMODE_SILENT ); 
    53                         $this->_connectionID->setAttribute(PDO_ATTR_CASE,$m); 
     183                        //$this->_connectionID->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_SILENT ); 
     184                        $this->_connectionID->setAttribute(PDO::ATTR_CASE,$m); 
    54185                         
    55                         //$this->_connectionID->setAttribute(PDO_ATTR_AUTOCOMMIT,true); 
     186                        $class = 'ADODB_pdo_'.$this->dsnType; 
     187                        //$this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true); 
     188                        switch($this->dsnType) { 
     189                        case 'oci': 
     190                        case 'mysql': 
     191                        case 'pgsql': 
     192                        case 'mssql': 
     193                                include_once(ADODB_DIR.'/drivers/adodb-pdo_'.$this->dsnType.'.inc.php'); 
     194                                break; 
     195                        } 
     196                        if (class_exists($class)) 
     197                                $this->_driver = new $class(); 
     198                        else 
     199                                $this->_driver = new ADODB_pdo_base(); 
    56200                         
     201                        $this->_driver->_connectionID = $this->_connectionID; 
     202                        $this->_UpdatePDO(); 
    57203                        return true; 
    58204                } 
     205                $this->_driver = new ADODB_pdo_base(); 
    59206                return false; 
    60207        } 
     
    66213        } 
    67214         
    68         function ErrorMsg() 
    69         { 
    70                 if ($this->_stmt) $arr = $this->_stmt->errorInfo(); 
    71                 else $arr = $this->_connectionID->errorInfo(); 
    72                  
    73                 if ($arr) { 
    74                         if ($arr[0]) return $arr[2]; 
    75                         else return ''; 
    76                 } else return '-1'; 
     215        /*------------------------------------------------------------------------------*/ 
     216         
     217         
     218        function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)  
     219        {        
     220                $save = $this->_driver->fetchMode; 
     221                $this->_driver->fetchMode = $this->fetchMode; 
     222                $this->_driver->debug = $this->debug; 
     223                $ret = $this->_driver->SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache); 
     224                $this->_driver->fetchMode = $save; 
     225                return $ret; 
     226        } 
     227         
     228         
     229        function ServerInfo() 
     230        { 
     231                return $this->_driver->ServerInfo(); 
     232        } 
     233         
     234        function MetaTables($ttype=false,$showSchema=false,$mask=false) 
     235        { 
     236                return $this->_driver->MetaTables($ttype,$showSchema,$mask); 
     237        } 
     238         
     239        function MetaColumns($table,$normalize=true) 
     240        { 
     241                return $this->_driver->MetaColumns($table,$normalize); 
    77242        } 
    78243         
     
    84249        } 
    85250         
     251         
     252        function ErrorMsg() 
     253        { 
     254                if ($this->_errormsg !== false) return $this->_errormsg; 
     255                if (!empty($this->_stmt)) $arr = $this->_stmt->errorInfo(); 
     256                else if (!empty($this->_connectionID)) $arr = $this->_connectionID->errorInfo(); 
     257                else return 'No Connection Established'; 
     258                 
     259                 
     260                if ($arr) { 
     261                        if (sizeof($arr)<2) return ''; 
     262                        if ((integer)$arr[1]) return $arr[2]; 
     263                        else return ''; 
     264                } else return '-1'; 
     265        } 
     266         
     267 
    86268        function ErrorNo() 
    87269        { 
    88                  
    89                 if ($this->_stmt) return $this->_stmt->errorCode(); 
    90                 else return $this->_connectionID->errorInfo(); 
     270                if ($this->_errorno !== false) return $this->_errorno; 
     271                if (!empty($this->_stmt)) $err = $this->_stmt->errorCode(); 
     272                else if (!empty($this->_connectionID)) { 
     273                        $arr = $this->_connectionID->errorInfo(); 
     274                        if (isset($arr[0])) $err = $arr[0]; 
     275                        else $err = -1; 
     276                } else 
     277                        return 0; 
     278                         
     279                if ($err == '00000') return 0; // allows empty check 
     280                return $err; 
    91281        } 
    92282 
     
    97287                $this->transCnt += 1; 
    98288                $this->_autocommit = false; 
    99                 $this->_connectionID->setAttribute(PDO_ATTR_AUTOCOMMIT,false); 
     289                $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,false); 
    100290                return $this->_connectionID->beginTransaction(); 
    101291        } 
     
    103293        function CommitTrans($ok=true)  
    104294        {  
     295                if (!$this->hasTransactions) return false; 
    105296                if ($this->transOff) return true;  
    106297                if (!$ok) return $this->RollbackTrans(); 
     
    109300                 
    110301                $ret = $this->_connectionID->commit(); 
    111                 $this->_connectionID->setAttribute(PDO_ATTR_AUTOCOMMIT,true); 
     302                $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true); 
    112303                return $ret; 
    113304        } 
     
    115306        function RollbackTrans() 
    116307        { 
     308                if (!$this->hasTransactions) return false; 
    117309                if ($this->transOff) return true;  
    118310                if ($this->transCnt) $this->transCnt -= 1; 
     
    120312                 
    121313                $ret = $this->_connectionID->rollback(); 
    122                 $this->_connectionID->setAttribute(PDO_ATTR_AUTOCOMMIT,true); 
     314                $this->_connectionID->setAttribute(PDO::ATTR_AUTOCOMMIT,true); 
    123315                return $ret; 
    124316        } 
     
    139331                return $obj; 
    140332        } 
    141  
     333         
     334         
    142335        /* returns queryID or false */ 
    143336        function _query($sql,$inputarr=false)  
     
    146339                        $stmt = $sql[1]; 
    147340                } else { 
    148                         $stmt = $this->_connectionID->prepare($sql);             
    149                 } 
     341                        $stmt = $this->_connectionID->prepare($sql); 
     342                } 
     343                #adodb_backtrace(); 
     344                #var_dump($this->_bindInputArray); 
    150345                if ($stmt) { 
    151                         if ($inputarr) $stmt->execute($inputarr); 
    152                         else $stmt->execute(); 
    153                 } 
    154                 $this->_stmt = $stmt; 
    155                 return $stmt; 
     346                        $this->_driver->debug = $this->debug; 
     347                        if ($inputarr) $ok = $stmt->execute($inputarr); 
     348                        else $ok = $stmt->execute(); 
     349                }  
     350                 
     351                 
     352                $this->_errormsg = false; 
     353                $this->_errorno = false; 
     354                         
     355                if ($ok) { 
     356                        $this->_stmt = $stmt; 
     357                        return $stmt; 
     358                } 
     359                 
     360                if ($stmt) { 
     361                         
     362                        $arr = $stmt->errorinfo(); 
     363                        if ((integer)$arr[1]) { 
     364                                $this->_errormsg = $arr[2]; 
     365                                $this->_errorno = $arr[1]; 
     366                        } 
     367 
     368                } else { 
     369                        $this->_errormsg = false; 
     370                        $this->_errorno = false; 
     371                } 
     372                return false; 
    156373        } 
    157374 
     
    211428                if ($this->_stmt) $arr = $this->_stmt->errorInfo(); 
    212429                else $arr = $this->_connectionID->errorInfo(); 
    213                 print_r($arr); 
    214                 if ($arr) { 
    215                         if ($arr[0]) return $arr[2]; 
     430 
     431                if (is_array($arr)) { 
     432                        if ((integer) $arr[0] && isset($arr[2])) return $arr[2]; 
    216433                        else return ''; 
    217434                } else return '-1'; 
     435        } 
     436         
     437        function NumCols() 
     438        { 
     439                return ($this->_stmt) ? $this->_stmt->columnCount() : 0; 
    218440        } 
    219441         
     
    241463                        $mode = $ADODB_FETCH_MODE; 
    242464                } 
     465                $this->adodbFetchMode = $mode; 
    243466                switch($mode) { 
    244                 default: 
    245                 case ADODB_FETCH_BOTH: $mode = PDO_FETCH_BOTH; break; 
    246                 case ADODB_FETCH_NUM: $mode = PDO_FETCH_NUM; break; 
    247                 case ADODB_FETCH_ASSOC:  $mode = PDO_FETCH_ASSOC; break; 
     467                case ADODB_FETCH_NUM: $mode = PDO::FETCH_NUM; break; 
     468                case ADODB_FETCH_ASSOC:  $mode = PDO::FETCH_ASSOC; break; 
     469                 
     470                case ADODB_FETCH_BOTH:  
     471                default: $mode = PDO::FETCH_BOTH; break; 
    248472                } 
    249473                $this->fetchMode = $mode; 
     
    253477        } 
    254478 
    255  
    256         // returns the field object 
    257         function &FetchField($fieldOffset = -1)  
    258         { 
    259                  
    260                 $off=$fieldOffset+1; // offsets begin at 1 
    261                  
    262                 $o= new ADOFieldObject(); 
    263                 $o->name = @odbc_field_name($this->_queryID,$off); 
    264                 $o->type = @odbc_field_type($this->_queryID,$off); 
    265                 $o->max_length = @odbc_field_len($this->_queryID,$off); 
    266                 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name); 
    267                 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name); 
    268                 return $o; 
    269         } 
    270479         
    271480        function Init() 
     
    283492                                $this->_numOfRows = 0; // _numOfRows could be -1 
    284493                        } 
    285                         $this->_numOfFields = sizeof($this->fields); 
    286494                } else { 
    287495                        $this->EOF = true; 
     
    289497        } 
    290498         
    291                  
    292499        function _initrs() 
    293500        { 
     
    296503                $this->_numOfRows = ($ADODB_COUNTRECS) ? @$this->_queryID->rowCount() : -1; 
    297504                if (!$this->_numOfRows) $this->_numOfRows = -1; 
    298                 $this->_numOfFields =0; 
    299         }        
     505                $this->_numOfFields = $this->_queryID->columnCount(); 
     506        } 
     507 
     508        // returns the field object 
     509        function &FetchField($fieldOffset = -1)  
     510        { 
     511                $off=$fieldOffset+1; // offsets begin at 1 
     512                 
     513                $o= new ADOFieldObject(); 
     514                $arr = @$this->_queryID->getColumnMeta($fieldOffset); 
     515                if (!$arr) { 
     516                        $o->name = 'bad getColumnMeta()'; 
     517                        $o->max_length = -1; 
     518                        $o->type = 'VARCHAR'; 
     519                        $o->precision = 0; 
     520        #               $false = false; 
     521                        return $o; 
     522                } 
     523                //adodb_pr($arr); 
     524                $o->name = $arr['name']; 
     525                if (isset($arr['native_type']) && $arr['native_type'] <> "null") $o->type = $arr['native_type']; 
     526                else $o->type = adodb_pdo_type($arr['pdo_type']); 
     527                $o->max_length = $arr['len']; 
     528                $o->precision = $arr['precision']; 
     529                 
     530                if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name); 
     531                else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name); 
     532                return $o; 
     533        } 
    300534         
    301535        function _seek($row) 
     
    306540        function _fetch() 
    307541        { 
     542                if (!$this->_queryID) return false; 
     543                 
    308544                $this->fields = $this->_queryID->fetch($this->fetchMode); 
    309545                return !empty($this->fields); 
     
    314550                $this->_queryID = false; 
    315551        } 
     552         
     553        function Fields($colname) 
     554        { 
     555                if ($this->adodbFetchMode != ADODB_FETCH_NUM) return @$this->fields[$colname]; 
     556                 
     557                if (!$this->bind) { 
     558                        $this->bind = array(); 
     559                        for ($i=0; $i < $this->_numOfFields; $i++) { 
     560                                $o = $this->FetchField($i); 
     561                                $this->bind[strtoupper($o->name)] = $i; 
     562                        } 
     563                } 
     564                 return $this->fields[$this->bind[strtoupper($colname)]]; 
     565        } 
    316566 
    317567} 
Note: See TracChangeset for help on using the changeset viewer.