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-postgres64.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. 
     3 V4.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,  
     
    5050        $len = strlen($s); 
    5151        if ($len == 0) return "''"; 
    52         if (strncmp($s,"'",1) === 0 && substr(s,$len-1) == "'") return $s; // already quoted 
     52        if (strncmp($s,"'",1) === 0 && substr($s,$len-1) == "'") return $s; // already quoted 
    5353         
    5454        return "'".addslashes($s)."'"; 
     
    7474        var $metaColumnsSQL = "SELECT a.attname,t.typname,a.attlen,a.atttypmod,a.attnotnull,a.atthasdef,a.attnum  
    7575                FROM pg_class c, pg_attribute a,pg_type t  
    76                 WHERE relkind = 'r' AND (c.relname='%s' or c.relname = lower('%s')) and a.attname not like '....%%' 
     76                WHERE relkind in ('r','v') AND (c.relname='%s' or c.relname = lower('%s')) and a.attname not like '....%%' 
    7777AND a.attnum > 0 AND a.atttypid = t.oid AND a.attrelid = c.oid ORDER BY a.attnum"; 
    7878 
     79        // used when schema defined 
    7980        var $metaColumnsSQL1 = "SELECT a.attname, t.typname, a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum  
    8081FROM pg_class c, pg_attribute a, pg_type t, pg_namespace n  
    81 WHERE relkind = 'r' AND (c.relname='%s' or c.relname = lower('%s')) 
     82WHERE relkind in ('r','v') AND (c.relname='%s' or c.relname = lower('%s')) 
    8283 and c.relnamespace=n.oid and n.nspname='%s'  
    8384        and a.attname not like '....%%' AND a.attnum > 0  
     
    9192        var $hasLimit = false;  // set to true for pgsql 7 only. support pgsql/mysql SELECT * FROM TABLE LIMIT 10 
    9293        // below suggested by Freek Dijkstra  
    93         var $true = 't';                // string that represents TRUE for a database 
    94         var $false = 'f';               // string that represents FALSE for a database 
     94        var $true = 'TRUE';             // string that represents TRUE for a database 
     95        var $false = 'FALSE';           // string that represents FALSE for a database 
    9596        var $fmtDate = "'Y-m-d'";       // used by DBDate() as the default date format used by the database 
    96         var $fmtTimeStamp = "'Y-m-d G:i:s'"; // used by DBTimeStamp as the default timestamp fmt. 
     97        var $fmtTimeStamp = "'Y-m-d H:i:s'"; // used by DBTimeStamp as the default timestamp fmt. 
    9798        var $hasMoveFirst = true; 
    9899        var $hasGenID = true; 
     
    102103        var $metaDefaultsSQL = "SELECT d.adnum as num, d.adsrc as def from pg_attrdef d, pg_class c where d.adrelid=c.oid and c.relname='%s' order by d.adnum"; 
    103104        var $random = 'random()';               /// random function 
    104         var $autoRollback = true; // apparently pgsql does not autorollback properly before 4.3.4 
     105        var $autoRollback = true; // apparently pgsql does not autorollback properly before php 4.3.4 
    105106                                                        // http://bugs.php.net/bug.php?id=25404 
    106107                                                         
    107108        var $_bindInputArray = false; // requires postgresql 7.3+ and ability to modify database 
     109        var $disableBlobs = false; // set to true to disable blob checking, resulting in 2-5% improvement in performance. 
    108110         
    109111        // The last (fmtTimeStamp is not entirely correct:  
     
    129131                return $arr; 
    130132        } 
    131 /* 
     133 
    132134        function IfNull( $field, $ifNull )  
    133135        { 
    134                 return " NULLIF($field, $ifNull) "; // if PGSQL 
    135         } 
    136 */ 
     136                return " coalesce($field, $ifNull) ";  
     137        } 
     138 
    137139        // get the last id - never tested 
    138140        function pg_insert_id($tablename,$fieldname) 
     
    151153Unless you are very careful, you might end up with a tuple having  
    152154a different OID if a database must be reloaded. */ 
    153         function _insertid() 
     155        function _insertid($table,$column) 
    154156        { 
    155157                if (!is_resource($this->_resultid) || get_resource_type($this->_resultid) !== 'pgsql result') return false; 
    156                 return pg_getlastoid($this->_resultid); 
     158                $oid = pg_getlastoid($this->_resultid); 
     159                // to really return the id, we need the table and column-name, else we can only return the oid != id 
     160                return empty($table) || empty($column) ? $oid : $this->GetOne("SELECT $column FROM $table WHERE oid=".(int)$oid); 
    157161        } 
    158162 
    159163// I get this error with PHP before 4.0.6 - jlim 
    160 // Warning: This compilation does not support pg_cmdtuples() in d:/inetpub/wwwroot/php/adodb/adodb-postgres.inc.php on line 44 
     164// Warning: This compilation does not support pg_cmdtuples() in adodb-postgres.inc.php on line 44 
    161165   function _affectedrows() 
    162166   { 
     
    171175                if ($this->transOff) return true; 
    172176                $this->transCnt += 1; 
    173                 return @pg_Exec($this->_connectionID, "begin"); 
    174         } 
    175          
    176         function RowLock($tables,$where)  
     177                return @pg_Exec($this->_connectionID, "begin ".$this->_transmode); 
     178        } 
     179         
     180        function RowLock($tables,$where,$flds='1 as ignore')  
    177181        { 
    178182                if (!$this->transCnt) $this->BeginTrans(); 
    179                 return $this->GetOne("select 1 as ignore from $tables where $where for update"); 
     183                return $this->GetOne("select $flds from $tables where $where for update"); 
    180184        } 
    181185 
     
    199203         
    200204        function &MetaTables($ttype=false,$showSchema=false,$mask=false)  
    201         {        
     205        { 
     206                $info = $this->ServerInfo(); 
     207                if ($info['version'] >= 7.3) { 
     208                $this->metaTablesSQL = "select tablename,'T' from pg_tables where tablename not like 'pg\_%' 
     209                          and schemaname  not in ( 'pg_catalog','information_schema') 
     210        union  
     211        select viewname,'V' from pg_views where viewname not like 'pg\_%'  and schemaname  not in ( 'pg_catalog','information_schema') "; 
     212                } 
    202213                if ($mask) { 
    203214                        $save = $this->metaTablesSQL; 
    204215                        $mask = $this->qstr(strtolower($mask)); 
    205                         $this->metaTablesSQL = " 
    206 select tablename,'T' from pg_tables where tablename like $mask union  
     216                        if ($info['version']>=7.3) 
     217                                $this->metaTablesSQL = " 
     218select tablename,'T' from pg_tables where tablename like $mask and schemaname not in ( 'pg_catalog','information_schema')   
     219 union  
     220select viewname,'V' from pg_views where viewname like $mask and schemaname  not in ( 'pg_catalog','information_schema')  "; 
     221                        else 
     222                                $this->metaTablesSQL = " 
     223select tablename,'T' from pg_tables where tablename like $mask  
     224 union  
    207225select viewname,'V' from pg_views where viewname like $mask"; 
    208226                } 
     
    215233        } 
    216234         
    217         /* 
     235         
    218236        // if magic quotes disabled, use pg_escape_string() 
    219237        function qstr($s,$magic_quotes=false) 
    220238        { 
    221239                if (!$magic_quotes) { 
    222                         if (ADODB_PHPVER >= 0x4200) { 
    223                                 return  "'".pg_escape_string($s)."'"; 
    224                         } 
    225240                        if ($this->replaceQuote[0] == '\\'){ 
    226                                 $s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\0"),$s); 
     241                                $s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\\000"),$s); 
    227242                        } 
    228243                        return  "'".str_replace("'",$this->replaceQuote,$s)."'";  
     
    233248                return "'$s'"; 
    234249        } 
    235         */ 
     250         
    236251         
    237252         
     
    288303                                break; 
    289304                                 
     305                        case 'w': 
     306                                $s .= 'D'; 
     307                                break; 
     308                         
     309                        case 'l': 
     310                                $s .= 'DAY'; 
     311                                break; 
     312                         
     313                         case 'W': 
     314                                $s .= 'WW'; 
     315                                break; 
     316 
    290317                        default: 
    291318                        // handle escape characters... 
     
    332359        }  
    333360         
     361        /* 
     362        * Deletes/Unlinks a Blob from the database, otherwise it  
     363        * will be left behind 
     364        * 
     365        * Returns TRUE on success or FALSE on failure. 
     366        * 
     367        * contributed by Todd Rogers todd#windfox.net 
     368        */ 
     369        function BlobDelete( $blob ) 
     370        { 
     371                pg_exec ($this->_connectionID, "begin"); 
     372                $result = @pg_lo_unlink($blob); 
     373                pg_exec ($this->_connectionID, "commit"); 
     374                return( $result ); 
     375        } 
     376 
     377        /* 
     378                Hueristic - not guaranteed to work. 
     379        */ 
     380        function GuessOID($oid) 
     381        { 
     382                if (strlen($oid)>16) return false; 
     383                return is_numeric($oid); 
     384        } 
     385         
    334386        /*  
    335387        * If an OID is detected, then we use pg_lo_* to open the oid file and read the 
     
    340392        * 
    341393        * see http://www.postgresql.org/idocs/index.php?largeobjects.html 
     394        * 
     395        * Since adodb 4.54, this returns the blob, instead of sending it to stdout. Also 
     396        * added maxsize parameter, which defaults to $db->maxblobsize if not defined. 
    342397        */  
    343         function BlobDecode( $blob)  
    344         {  
    345                 if (strlen($blob) > 24) return $blob; 
    346                  
    347                 @pg_exec($this->_connectionID,"begin");  
     398        function BlobDecode($blob,$maxsize=false,$hastrans=true)  
     399        { 
     400                if (!$this->GuessOID($blob)) return $blob; 
     401                 
     402                if ($hastrans) @pg_exec($this->_connectionID,"begin");  
    348403                $fd = @pg_lo_open($this->_connectionID,$blob,"r"); 
    349404                if ($fd === false) { 
    350                         @pg_exec($this->_connectionID,"commit"); 
     405                        if ($hastrans) @pg_exec($this->_connectionID,"commit"); 
    351406                        return $blob; 
    352407                } 
    353                 $realblob = @pg_loreadall($fd);  
     408                if (!$maxsize) $maxsize = $this->maxblobsize; 
     409                $realblob = @pg_loread($fd,$maxsize);  
    354410                @pg_loclose($fd);  
    355                 @pg_exec($this->_connectionID,"commit");  
     411                if ($hastrans) @pg_exec($this->_connectionID,"commit");  
    356412                return $realblob; 
    357         }  
     413        } 
    358414         
    359415        /*  
     
    366422        function BlobEncode($blob) 
    367423        { 
    368                 if (ADODB_PHPVER >= 0x4200 && ADODB_PHPVER < 0x5000) return pg_escape_bytea($blob); 
    369424                 
    370425                /*92=backslash, 0=null, 39=single-quote*/ 
     
    376431        } 
    377432         
     433        // assumes bytea for blob, and varchar for clob 
    378434        function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB') 
    379435        { 
     436         
     437                if ($blobtype == 'CLOB') { 
     438                return $this->Execute("UPDATE $table SET $column=" . $this->qstr($val) . " WHERE $where"); 
     439                } 
    380440                // do not use bind params which uses qstr(), as blobencode() already quotes data 
    381441                return $this->Execute("UPDATE $table SET $column='".$this->BlobEncode($val)."'::bytea WHERE $where"); 
     
    385445        {                
    386446                if (!$date) $date = $this->sysDate; 
     447                else if (strncmp($date,"'",1) == 0) { 
     448                        $len = strlen($date); 
     449                        if (10 <= $len && $len <= 12) $date = 'date '.$date; 
     450                        else $date = 'timestamp '.$date; 
     451                } 
    387452                return "($date+interval'$dayFraction days')"; 
    388453        } 
     
    391456        // for schema support, pass in the $table param "$schema.$tabname". 
    392457        // converts field names to lowercase, $upper is ignored 
     458        // see http://phplens.com/lens/lensforum/msgs.php?id=14018 for more info 
    393459        function &MetaColumns($table,$normalize=true)  
    394460        { 
     
    396462         
    397463                $schema = false; 
     464                $false = false; 
    398465                $this->_findschema($table,$schema); 
    399466                 
     
    409476                $ADODB_FETCH_MODE = $save; 
    410477                 
    411                 if ($rs === false) return false; 
    412                  
     478                if ($rs === false) { 
     479                        return $false; 
     480                } 
    413481                if (!empty($this->metaKeySQL)) { 
    414482                        // If we want the primary keys, we have to issue a separate query 
     
    461529                        $fld->type = $rs->fields[1]; 
    462530                        $fld->max_length = $rs->fields[2]; 
     531                        $fld->attnum = $rs->fields[6]; 
     532                         
    463533                        if ($fld->max_length <= 0) $fld->max_length = $rs->fields[3]-4; 
    464534                        if ($fld->max_length <= 0) $fld->max_length = -1; 
    465                          
     535                        if ($fld->type == 'numeric') { 
     536                                $fld->scale = $fld->max_length & 0xFFFF; 
     537                                $fld->max_length >>= 16; 
     538                        } 
    466539                        // dannym 
    467540                        // 5 hasdefault; 6 num-of-column 
     
    472545 
    473546                        //Freek 
    474                         if ($rs->fields[4] == $this->true) { 
    475                                 $fld->not_null = true; 
    476                         } 
     547                        $fld->not_null = $rs->fields[4] == 't'; 
     548                         
    477549                         
    478550                        // Freek 
    479551                        if (is_array($keys)) { 
    480552                                foreach($keys as $key) { 
    481                                         if ($fld->name == $key['column_name'] AND $key['primary_key'] == $this->true)  
     553                                        if ($fld->name == $key['column_name'] AND $key['primary_key'] == 't')  
    482554                                                $fld->primary_key = true; 
    483                                         if ($fld->name == $key['column_name'] AND $key['unique_key'] == $this->true)  
     555                                        if ($fld->name == $key['column_name'] AND $key['unique_key'] == 't')  
    484556                                                $fld->unique = true; // What name is more compatible? 
    485557                                } 
     
    492564                } 
    493565                $rs->Close(); 
    494                 return $retarr;  
     566                if (empty($retarr)) 
     567                        return  $false; 
     568                else 
     569                        return $retarr;  
    495570                 
    496571        } 
     
    510585JOIN pg_catalog.pg_class c2 ON c2.oid=i.indrelid 
    511586        ,pg_namespace n  
    512 WHERE (c2.relname=\'%s\' or c2.relname=lower(\'%s\')) and c.relnamespace=c2.relnamespace and c.relnamespace=n.oid and n.nspname=\'%s\' AND i.indisprimary=false'; 
     587WHERE (c2.relname=\'%s\' or c2.relname=lower(\'%s\')) and c.relnamespace=c2.relnamespace and c.relnamespace=n.oid and n.nspname=\'%s\''; 
    513588                                } else { 
    514589                        $sql = ' 
     
    517592JOIN pg_catalog.pg_index i ON i.indexrelid=c.oid 
    518593JOIN pg_catalog.pg_class c2 ON c2.oid=i.indrelid 
    519 WHERE c2.relname=\'%s\' or c2.relname=lower(\'%s\')'; 
     594WHERE (c2.relname=\'%s\' or c2.relname=lower(\'%s\'))'; 
    520595                        } 
    521596                                             
     
    537612 
    538613                if (!is_object($rs)) { 
    539                         return FALSE; 
     614                        $false = false; 
     615                                        return $false; 
    540616                } 
    541617                                 
    542                 $col_names = $this->MetaColumnNames($table,true); 
     618                $col_names = $this->MetaColumnNames($table,true,true);  
     619                                //3rd param is use attnum,  
     620                                // see http://sourceforge.net/tracker/index.php?func=detail&aid=1451245&group_id=42718&atid=433976 
    543621                $indexes = array(); 
    544622                while ($row = $rs->FetchRow()) { 
    545623                        $columns = array(); 
    546624                        foreach (explode(' ', $row[2]) as $col) { 
    547                                 $columns[] = $col_names[$col - 1]; 
     625                                $columns[] = $col_names[$col]; 
    548626                        } 
    549627                         
     
    564642        { 
    565643                 
    566                 if (!function_exists('pg_pconnect')) return null; 
     644                if (!function_exists('pg_connect')) return null; 
    567645                 
    568646                $this->_errorMsg = false; 
     
    576654                                $host = split(":", $str); 
    577655                                if ($host[0]) $str = "host=".adodb_addslashes($host[0]); 
    578                                 else $str = 'host=localhost'; 
     656                                else $str = ''; 
    579657                                if (isset($host[1])) $str .= " port=$host[1]"; 
     658                                else if (!empty($this->port)) $str .= " port=".$this->port; 
    580659                        } 
    581660                                if ($user) $str .= " user=".$user; 
     
    601680                if ($this->_connectionID === false) return false; 
    602681                $this->Execute("set datestyle='ISO'"); 
     682                 
     683                $info = $this->ServerInfo(); 
     684                $this->pgVersion = (float) substr($info['version'],0,3); 
     685                if ($this->pgVersion >= 7.1) { // good till version 999 
     686                        $this->_nestedSQL = true; 
     687                } 
    603688                return true; 
    604689        } 
     
    623708        function _query($sql,$inputarr) 
    624709        { 
    625                  
     710                $this->_errorMsg = false; 
    626711                if ($inputarr) { 
    627712                /* 
     
    652737                        if ($execp) $exsql = "EXECUTE $plan ($execp)"; 
    653738                        else $exsql = "EXECUTE $plan"; 
     739                         
    654740                         
    655741                        $rez = @pg_exec($this->_connectionID,$exsql); 
     
    677763                                $s = "PREPARE $plan ($params) AS ".substr($sql,0,strlen($sql)-2);                
    678764                                //adodb_pr($s); 
    679                                 pg_exec($this->_connectionID,$s); 
    680                                 echo $this->ErrorMsg(); 
    681                         } 
    682                          
    683                         $rez = pg_exec($this->_connectionID,$exsql); 
     765                                $rez = pg_exec($this->_connectionID,$s); 
     766                                //echo $this->ErrorMsg(); 
     767                        } 
     768                        if ($rez) 
     769                                $rez = pg_exec($this->_connectionID,$exsql); 
    684770                } else { 
    685                         $this->_errorMsg = false; 
    686771                        //adodb_backtrace(); 
    687772                        $rez = pg_exec($this->_connectionID,$sql); 
     
    699784        } 
    700785         
     786        function _errconnect() 
     787        { 
     788                if (defined('DB_ERROR_CONNECT_FAILED')) return DB_ERROR_CONNECT_FAILED; 
     789                else return 'Database connection failed'; 
     790        } 
    701791 
    702792        /*      Returns: the last error message from previous database operation        */       
     
    712802                        if (!empty($this->_connectionID)) { 
    713803                                $this->_errorMsg = @pg_last_error($this->_connectionID); 
    714                         } else $this->_errorMsg = @pg_last_error(); 
     804                        } else $this->_errorMsg = $this->_errconnect(); 
    715805                } else { 
    716                         if (empty($this->_connectionID)) $this->_errorMsg = @pg_errormessage(); 
     806                        if (empty($this->_connectionID)) $this->_errconnect(); 
    717807                        else $this->_errorMsg = @pg_errormessage($this->_connectionID); 
    718808                } 
     
    780870                case ADODB_FETCH_NUM: $this->fetchMode = PGSQL_NUM; break; 
    781871                case ADODB_FETCH_ASSOC:$this->fetchMode = PGSQL_ASSOC; break; 
    782                 default: 
     872                 
    783873                case ADODB_FETCH_DEFAULT: 
    784                 case ADODB_FETCH_BOTH:$this->fetchMode = PGSQL_BOTH; break; 
    785                 } 
     874                case ADODB_FETCH_BOTH: 
     875                default: $this->fetchMode = PGSQL_BOTH; break; 
     876                } 
     877                $this->adodbFetchMode = $mode; 
    786878                $this->ADORecordSet($queryID); 
    787879        } 
     
    802894                 
    803895                // cache types for blob decode check 
     896                // apparently pg_fieldtype actually performs an sql query on the database to get the type. 
     897                if (empty($this->connection->noBlobs)) 
    804898                for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) {   
    805899                        if (pg_fieldtype($qid,$i) == 'bytea') { 
     
    886980                $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode); 
    887981                 
    888         if ($this->fields && isset($this->_blobArr)) $this->_fixblobs(); 
     982                if ($this->fields && isset($this->_blobArr)) $this->_fixblobs(); 
    889983                         
    890984                return (is_array($this->fields)); 
     
    9121006                                case 'BPCHAR': 
    9131007                                case '_VARCHAR': 
     1008                                case 'INET': 
     1009                                case 'MACADDR': 
    9141010                                        if ($len <= $this->blobSize) return 'C'; 
    9151011                                 
     
    9311027                                        return 'D'; 
    9321028                                 
     1029                                 
     1030                                case 'TIMESTAMP WITHOUT TIME ZONE': 
    9331031                                case 'TIME': 
    9341032                                case 'DATETIME': 
Note: See TracChangeset for help on using the changeset viewer.