Update to last ADOdb version
authorsoranzo <soranzo>
Mon, 16 Apr 2007 17:47:53 +0000 (17:47 +0000)
committersoranzo <soranzo>
Mon, 16 Apr 2007 17:47:53 +0000 (17:47 +0000)
libraries/adodb/drivers/adodb-postgres64.inc.php

index aabe63ade4b7ab3e8597da69a4d50b37bcb29d62..3133ec2efc982c144d0ced29f4934ea489fac3f1 100644 (file)
@@ -1,6 +1,8 @@
 <?php
 /*
- V4.65 22 July 2005  (c) 2000-2005 John Lim (jlim@natsoft.com.my). All rights reserved.
+ Modified by phpPgAdmin team: functions _connect and _decode
+
+ V4.94 23 Jan 2007  (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved.
   Released under both BSD license and Lesser GPL library license. 
   Whenever there is any discrepancy between the two licenses, 
   the BSD license will take precedence.
@@ -49,7 +51,7 @@ function adodb_addslashes($s)
 {
        $len = strlen($s);
        if ($len == 0) return "''";
-       if (strncmp($s,"'",1) === 0 && substr(s,$len-1) == "'") return $s; // already quoted
+       if (strncmp($s,"'",1) === 0 && substr($s,$len-1) == "'") return $s; // already quoted
        
        return "'".addslashes($s)."'";
 }
@@ -94,7 +96,7 @@ WHERE relkind in ('r','v') AND (c.relname='%s' or c.relname = lower('%s'))
        var $true = 'TRUE';             // string that represents TRUE for a database
        var $false = 'FALSE';           // string that represents FALSE for a database
        var $fmtDate = "'Y-m-d'";       // used by DBDate() as the default date format used by the database
-       var $fmtTimeStamp = "'Y-m-d G:i:s'"; // used by DBTimeStamp as the default timestamp fmt.
+       var $fmtTimeStamp = "'Y-m-d H:i:s'"; // used by DBTimeStamp as the default timestamp fmt.
        var $hasMoveFirst = true;
        var $hasGenID = true;
        var $_genIDSQL = "SELECT NEXTVAL('%s')";
@@ -161,7 +163,7 @@ a different OID if a database must be reloaded. */
        }
 
 // I get this error with PHP before 4.0.6 - jlim
-// Warning: This compilation does not support pg_cmdtuples() in d:/inetpub/wwwroot/php/adodb/adodb-postgres.inc.php on line 44
+// Warning: This compilation does not support pg_cmdtuples() in adodb-postgres.inc.php on line 44
    function _affectedrows()
    {
                if (!is_resource($this->_resultid) || get_resource_type($this->_resultid) !== 'pgsql result') return false;
@@ -174,7 +176,7 @@ a different OID if a database must be reloaded. */
        {
                if ($this->transOff) return true;
                $this->transCnt += 1;
-               return @pg_Exec($this->_connectionID, "begin");
+               return @pg_Exec($this->_connectionID, "begin ".$this->_transmode);
        }
        
        function RowLock($tables,$where,$flds='1 as ignore') 
@@ -232,16 +234,19 @@ select viewname,'V' from pg_views where viewname like $mask";
                return $ret;
        }
        
-       /*
+       
        // if magic quotes disabled, use pg_escape_string()
        function qstr($s,$magic_quotes=false)
        {
                if (!$magic_quotes) {
+                       if (ADODB_PHPVER >= 0x5200) {
+                               return  "'".pg_escape_string($this->_connectionID,$s)."'";
+                       }
                        if (ADODB_PHPVER >= 0x4200) {
                                return  "'".pg_escape_string($s)."'";
                        }
                        if ($this->replaceQuote[0] == '\\'){
-                               $s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\0"),$s);
+                               $s = adodb_str_replace(array('\\',"\0"),array('\\\\',"\\\\000"),$s);
                        }
                        return  "'".str_replace("'",$this->replaceQuote,$s)."'"; 
                }
@@ -250,7 +255,7 @@ select viewname,'V' from pg_views where viewname like $mask";
                $s = str_replace('\\"','"',$s);
                return "'$s'";
        }
-       */
+       
        
        
        // Format date column in sql string given an input format that understands Y M D
@@ -312,7 +317,11 @@ select viewname,'V' from pg_views where viewname like $mask";
                        case 'l':
                                $s .= 'DAY';
                                break;
-                               
+                       
+                       case 'W':
+                               $s .= 'WW';
+                               break;
+
                        default:
                        // handle escape characters...
                                if ($ch == '\\') {
@@ -357,6 +366,22 @@ select viewname,'V' from pg_views where viewname like $mask";
                return $rez; 
        } 
        
+       /*
+       * Deletes/Unlinks a Blob from the database, otherwise it 
+       * will be left behind
+       *
+       * Returns TRUE on success or FALSE on failure.
+       *
+       * contributed by Todd Rogers todd#windfox.net
+       */
+       function BlobDelete( $blob )
+       {
+               pg_exec ($this->_connectionID, "begin");
+               $result = @pg_lo_unlink($blob);
+               pg_exec ($this->_connectionID, "commit");
+               return( $result );
+       }
+
        /*
                Hueristic - not guaranteed to work.
        */
@@ -393,7 +418,7 @@ select viewname,'V' from pg_views where viewname like $mask";
                @pg_loclose($fd); 
                if ($hastrans) @pg_exec($this->_connectionID,"commit"); 
                return $realblob;
-       } 
+       }
        
        /* 
                See http://www.postgresql.org/idocs/index.php?datatype-binary.html
@@ -404,6 +429,7 @@ select viewname,'V' from pg_views where viewname like $mask";
        */
        function BlobEncode($blob)
        {
+               if (ADODB_PHPVER >= 0x5200) return pg_escape_bytea($this->_connectionID, $blob);
                if (ADODB_PHPVER >= 0x4200) return pg_escape_bytea($blob);
                
                /*92=backslash, 0=null, 39=single-quote*/
@@ -428,12 +454,18 @@ select viewname,'V' from pg_views where viewname like $mask";
        function OffsetDate($dayFraction,$date=false)
        {               
                if (!$date) $date = $this->sysDate;
+               else if (strncmp($date,"'",1) == 0) {
+                       $len = strlen($date);
+                       if (10 <= $len && $len <= 12) $date = 'date '.$date;
+                       else $date = 'timestamp '.$date;
+               }
                return "($date+interval'$dayFraction days')";
        }
        
 
        // for schema support, pass in the $table param "$schema.$tabname".
        // converts field names to lowercase, $upper is ignored
+       // see http://phplens.com/lens/lensforum/msgs.php?id=14018 for more info
        function &MetaColumns($table,$normalize=true) 
        {
        global $ADODB_FETCH_MODE;
@@ -506,6 +538,8 @@ select viewname,'V' from pg_views where viewname like $mask";
                        $fld->name = $rs->fields[0];
                        $fld->type = $rs->fields[1];
                        $fld->max_length = $rs->fields[2];
+                       $fld->attnum = $rs->fields[6];
+                       
                        if ($fld->max_length <= 0) $fld->max_length = $rs->fields[3]-4;
                        if ($fld->max_length <= 0) $fld->max_length = -1;
                        if ($fld->type == 'numeric') {
@@ -520,9 +554,7 @@ select viewname,'V' from pg_views where viewname like $mask";
                        }
 
                        //Freek
-                       if ($rs->fields[4] == 't') {
-                               $fld->not_null = true;
-                       }
+                       $fld->not_null = $rs->fields[4] == 't';
                        
                        // Freek
                        if (is_array($keys)) {
@@ -541,7 +573,7 @@ select viewname,'V' from pg_views where viewname like $mask";
                }
                $rs->Close();
                if (empty($retarr))
-                       return  $false;
+                       return $false;
                else
                        return $retarr; 
                
@@ -561,14 +593,14 @@ FROM pg_catalog.pg_class c
 JOIN pg_catalog.pg_index i ON i.indexrelid=c.oid 
 JOIN pg_catalog.pg_class c2 ON c2.oid=i.indrelid
        ,pg_namespace n 
-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';
+WHERE (c2.relname=\'%s\' or c2.relname=lower(\'%s\')) and c.relnamespace=c2.relnamespace and c.relnamespace=n.oid and n.nspname=\'%s\'';
                                } else {
                        $sql = '
 SELECT c.relname as "Name", i.indisunique as "Unique", i.indkey as "Columns"
 FROM pg_catalog.pg_class c
 JOIN pg_catalog.pg_index i ON i.indexrelid=c.oid
 JOIN pg_catalog.pg_class c2 ON c2.oid=i.indrelid
-WHERE c2.relname=\'%s\' or c2.relname=lower(\'%s\')';
+WHERE (c2.relname=\'%s\' or c2.relname=lower(\'%s\'))';
                        }
                                            
                 if ($primary == FALSE) {
@@ -589,15 +621,17 @@ WHERE c2.relname=\'%s\' or c2.relname=lower(\'%s\')';
 
                 if (!is_object($rs)) {
                        $false = false;
-                                       return $false;
+                       return $false;
                 }
                                
-                $col_names = $this->MetaColumnNames($table,true);
+                $col_names = $this->MetaColumnNames($table,true,true); 
+                               //3rd param is use attnum, 
+                               // see http://sourceforge.net/tracker/index.php?func=detail&aid=1451245&group_id=42718&atid=433976
                 $indexes = array();
                 while ($row = $rs->FetchRow()) {
                         $columns = array();
                         foreach (explode(' ', $row[2]) as $col) {
-                                $columns[] = $col_names[$col - 1];
+                                $columns[] = $col_names[$col];
                         }
                         
                         $indexes[$row[0]] = array(
@@ -616,7 +650,7 @@ WHERE c2.relname=\'%s\' or c2.relname=lower(\'%s\')';
        function _connect($str,$user='',$pwd='',$db='',$ctype=0)
        {
                
-               if (!function_exists('pg_pconnect')) return null;
+               if (!function_exists('pg_connect')) return null;
                
                $this->_errorMsg = false;
                
@@ -634,15 +668,15 @@ WHERE c2.relname=\'%s\' or c2.relname=lower(\'%s\')';
                                if (isset($host[2])) $str .= " sslmode=".adodb_addslashes($host[2]);
                                else if (!empty($this->sslmode)) $str .= " sslmode=".$this->sslmode;
                        }
-                               if ($user) $str .= " user=".$user;
-                               if ($pwd)  $str .= " password=".$pwd;
-                               if ($db)   $str .= " dbname=".$db;
+                       if ($user) $str .= " user=".$user;
+                       if ($pwd)  $str .= " password=".$pwd;
+                       if ($db)   $str .= " dbname=".$db;
                }
 
                //if ($user) $linea = "user=$user host=$linea password=$pwd dbname=$db port=5432";
                
                if ($ctype === 1) { // persistent
-                       $this->_connectionID = pg_pconnect($str);
+                       $this->_connectionID = @pg_pconnect($str);
                } else {
                        if ($ctype === -1) { // nconnect, we trick pgsql ext by changing the connection str
                        static $ncnt;
@@ -652,10 +686,16 @@ WHERE c2.relname=\'%s\' or c2.relname=lower(\'%s\')';
                                
                                $str .= str_repeat(' ',$ncnt);
                        }
-                       $this->_connectionID = @pg_connect($str);
+                       $this->_connectionID = pg_connect($str);
                }
                if ($this->_connectionID === false) return false;
                $this->Execute("set datestyle='ISO'");
+               
+               $info = $this->ServerInfo();
+               $this->pgVersion = (float) substr($info['version'],0,3);
+               if ($this->pgVersion >= 7.1) { // good till version 999
+                       $this->_nestedSQL = true;
+               }
                return true;
        }
        
@@ -678,7 +718,7 @@ WHERE c2.relname=\'%s\' or c2.relname=lower(\'%s\')';
        // returns queryID or false
        function _query($sql,$inputarr)
        {
-               
+               $this->_errorMsg = false;
                if ($inputarr) {
                /*
                        It appears that PREPARE/EXECUTE is slower for many queries.
@@ -732,13 +772,12 @@ WHERE c2.relname=\'%s\' or c2.relname=lower(\'%s\')';
                                }
                                $s = "PREPARE $plan ($params) AS ".substr($sql,0,strlen($sql)-2);               
                                //adodb_pr($s);
-                               pg_exec($this->_connectionID,$s);
-                               echo $this->ErrorMsg();
+                               $rez = pg_exec($this->_connectionID,$s);
+                               //echo $this->ErrorMsg();
                        }
-                       
-                       $rez = pg_exec($this->_connectionID,$exsql);
+                       if ($rez)
+                               $rez = pg_exec($this->_connectionID,$exsql);
                } else {
-                       $this->_errorMsg = false;
                        //adodb_backtrace();
                        $rez = pg_exec($this->_connectionID,$sql);
                }
@@ -754,6 +793,11 @@ WHERE c2.relname=\'%s\' or c2.relname=lower(\'%s\')';
                return $rez;
        }
        
+       function _errconnect()
+       {
+               if (defined('DB_ERROR_CONNECT_FAILED')) return DB_ERROR_CONNECT_FAILED;
+               else return 'Database connection failed';
+       }
 
        /*      Returns: the last error message from previous database operation        */      
        function ErrorMsg() 
@@ -767,9 +811,9 @@ WHERE c2.relname=\'%s\' or c2.relname=lower(\'%s\')';
                        
                        if (!empty($this->_connectionID)) {
                                $this->_errorMsg = @pg_last_error($this->_connectionID);
-                       } else $this->_errorMsg = @pg_last_error();
+                       } else $this->_errorMsg = $this->_errconnect();
                } else {
-                       if (empty($this->_connectionID)) $this->_errorMsg = @pg_errormessage();
+                       if (empty($this->_connectionID)) $this->_errconnect();
                        else $this->_errorMsg = @pg_errormessage($this->_connectionID);
                }
                return $this->_errorMsg;
@@ -780,8 +824,8 @@ WHERE c2.relname=\'%s\' or c2.relname=lower(\'%s\')';
                $e = $this->ErrorMsg();
                if (strlen($e)) {
                        return ADOConnection::MetaError($e);
-                }
-                return 0;
+               }
+               return 0;
        }
 
        // returns true or false
@@ -901,9 +945,8 @@ class ADORecordSet_postgres64 extends ADORecordSet{
        
        function _decode($blob)
        {
-               if ($blob === NULL)
+               if (is_null($blob))
                        return NULL;
-
                eval('$realblob="'.adodb_str_replace(array('"','$'),array('\"','\$'),$blob).'";');
                return $realblob;       
        }
@@ -944,7 +987,7 @@ class ADORecordSet_postgres64 extends ADORecordSet{
        {
                                
                if ($this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0)
-               return false;
+                       return false;
 
                $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
                
@@ -975,6 +1018,7 @@ class ADORecordSet_postgres64 extends ADORecordSet{
                                case 'BPCHAR':
                                case '_VARCHAR':
                                case 'INET':
+                               case 'MACADDR':
                                        if ($len <= $this->blobSize) return 'C';
                                
                                case 'TEXT':
@@ -994,6 +1038,7 @@ class ADORecordSet_postgres64 extends ADORecordSet{
                                case 'DATE':
                                        return 'D';
                                
+                               case 'TIMESTAMP WITHOUT TIME ZONE':
                                case 'TIME':
                                case 'DATETIME':
                                case 'TIMESTAMP':