<?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.
{
$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)."'";
}
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')";
}
// 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;
{
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')
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)."'";
}
$s = str_replace('\\"','"',$s);
return "'$s'";
}
- */
+
// Format date column in sql string given an input format that understands Y M D
case 'l':
$s .= 'DAY';
break;
-
+
+ case 'W':
+ $s .= 'WW';
+ break;
+
default:
// handle escape characters...
if ($ch == '\\') {
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.
*/
@pg_loclose($fd);
if ($hastrans) @pg_exec($this->_connectionID,"commit");
return $realblob;
- }
+ }
/*
See http://www.postgresql.org/idocs/index.php?datatype-binary.html
*/
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*/
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;
$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') {
}
//Freek
- if ($rs->fields[4] == 't') {
- $fld->not_null = true;
- }
+ $fld->not_null = $rs->fields[4] == 't';
// Freek
if (is_array($keys)) {
}
$rs->Close();
if (empty($retarr))
- return $false;
+ return $false;
else
return $retarr;
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) {
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(
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;
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;
$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;
}
// returns queryID or false
function _query($sql,$inputarr)
{
-
+ $this->_errorMsg = false;
if ($inputarr) {
/*
It appears that PREPARE/EXECUTE is slower for many queries.
}
$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);
}
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()
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;
$e = $this->ErrorMsg();
if (strlen($e)) {
return ADOConnection::MetaError($e);
- }
- return 0;
+ }
+ return 0;
}
// returns true or false
function _decode($blob)
{
- if ($blob === NULL)
+ if (is_null($blob))
return NULL;
-
eval('$realblob="'.adodb_str_replace(array('"','$'),array('\"','\$'),$blob).'";');
return $realblob;
}
{
if ($this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0)
- return false;
+ return false;
$this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
case 'BPCHAR':
case '_VARCHAR':
case 'INET':
+ case 'MACADDR':
if ($len <= $this->blobSize) return 'C';
case 'TEXT':
case 'DATE':
return 'D';
+ case 'TIMESTAMP WITHOUT TIME ZONE':
case 'TIME':
case 'DATETIME':
case 'TIMESTAMP':