3 V4.94 23 Jan 2007 (c) 2000-2007 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.
10 28 Feb 2001: Currently indicate that we support LIMIT
11 01 Dec 2001: dannym added support for default values
14 // security - hide paths
15 if (!defined('ADODB_DIR')) die();
17 include_once(ADODB_DIR
."/drivers/adodb-postgres64.inc.php");
19 class ADODB_postgres7
extends ADODB_postgres64
{
20 var $databaseType = 'postgres7';
21 var $hasLimit = true; // set to true for pgsql 6.5+ only. support pgsql/mysql SELECT * FROM TABLE LIMIT 10
22 var $ansiOuter = true;
23 var $charSet = true; //set to true for Postgres 7 and above - PG client supports encodings
25 function ADODB_postgres7()
27 $this->ADODB_postgres64();
28 if (ADODB_ASSOC_CASE
!== 2) {
29 $this->rsPrefix
.= 'assoc_';
31 $this->_bindInputArray
= PHP_VERSION
>= 5.1;
35 // the following should be compat with postgresql 7.2,
36 // which makes obsolete the LIMIT limit,offset syntax
37 function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
39 $offsetStr = ($offset >= 0) ?
" OFFSET ".((integer)$offset) : '';
40 $limitStr = ($nrows >= 0) ?
" LIMIT ".((integer)$nrows) : '';
42 $rs =& $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
44 $rs =& $this->Execute($sql."$limitStr$offsetStr",$inputarr);
49 function Prepare($sql)
51 $info = $this->ServerInfo();
52 if ($info['version']>=7.3) {
53 return array($sql,false);
60 // from Edward Jaramilla, improved version - works on pg 7.4
61 function MetaForeignKeys($table, $owner=false, $upper=false)
63 $sql = 'SELECT t.tgargs as args
65 pg_trigger t,pg_class c,pg_proc p
70 p.proname = \'RI_FKey_check_ins\' AND
71 c.relname = \''.strtolower($table).'\'
75 $rs =& $this->Execute($sql);
77 if (!$rs ||
$rs->EOF
) return false;
79 $arr =& $rs->GetArray();
82 $data = explode(chr(0), $v['args']);
83 $size = count($data)-1; //-1 because the last node is empty
84 for($i = 4; $i < $size; $i++
) {
86 $a[strtoupper($data[2])][] = strtoupper($data[$i].'='.$data[++
$i]);
88 $a[$data[2]][] = $data[$i].'='.$data[++
$i];
94 function _query($sql,$inputarr)
96 if (! $this->_bindInputArray
) {
97 // We don't have native support for parameterized queries, so let's emulate it at the parent
98 return ADODB_postgres64
::_query($sql, $inputarr);
100 $this->_errorMsg
= false;
101 // -- added Cristiano da Cunha Duarte
103 $sqlarr = explode('?',trim($sql));
106 $last = sizeof($sqlarr)-1;
107 foreach($sqlarr as $v) {
108 if ($last < $i) $sql .= $v;
109 else $sql .= $v.' $'.$i;
113 $rez = pg_query_params($this->_connectionID
,$sql, $inputarr);
115 $rez = pg_query($this->_connectionID
,$sql);
117 // check if no data returned, then no need to create real recordset
118 if ($rez && pg_numfields($rez) <= 0) {
119 if (is_resource($this->_resultid
) && get_resource_type($this->_resultid
) === 'pgsql result') {
120 pg_freeresult($this->_resultid
);
122 $this->_resultid
= $rez;
128 // this is a set of functions for managing client encoding - very important if the encodings
129 // of your database and your output target (i.e. HTML) don't match
130 //for instance, you may have UNICODE database and server it on-site as WIN1251 etc.
131 // GetCharSet - get the name of the character set the client is using now
132 // the functions should work with Postgres 7.0 and above, the set of charsets supported
133 // depends on compile flags of postgres distribution - if no charsets were compiled into the server
134 // it will return 'SQL_ANSI' always
135 function GetCharSet()
137 //we will use ADO's builtin property charSet
138 $this->charSet
= @pg_client_encoding
($this->_connectionID
);
139 if (!$this->charSet
) {
142 return $this->charSet
;
146 // SetCharSet - switch the client encoding
147 function SetCharSet($charset_name)
150 if ($this->charSet
!== $charset_name) {
151 $if = pg_set_client_encoding($this->_connectionID
, $charset_name);
152 if ($if == "0" & $this->GetCharSet() == $charset_name) {
160 /*--------------------------------------------------------------------------------------
161 Class Name: Recordset
162 --------------------------------------------------------------------------------------*/
164 class ADORecordSet_postgres7
extends ADORecordSet_postgres64
{
166 var $databaseType = "postgres7";
169 function ADORecordSet_postgres7($queryID,$mode=false)
171 $this->ADORecordSet_postgres64($queryID,$mode);
174 // 10% speedup to move MoveNext to child class
178 $this->_currentRow++
;
179 if ($this->_numOfRows
< 0 ||
$this->_numOfRows
> $this->_currentRow
) {
180 $this->fields
= @pg_fetch_array
($this->_queryID
,$this->_currentRow
,$this->fetchMode
);
182 if (is_array($this->fields
)) {
183 if ($this->fields
&& isset($this->_blobArr
)) $this->_fixblobs();
187 $this->fields
= false;
195 class ADORecordSet_assoc_postgres7
extends ADORecordSet_postgres64
{
197 var $databaseType = "postgres7";
200 function ADORecordSet_assoc_postgres7($queryID,$mode=false)
202 $this->ADORecordSet_postgres64($queryID,$mode);
207 if ($this->_currentRow
>= $this->_numOfRows
&& $this->_numOfRows
>= 0)
210 $this->fields
= @pg_fetch_array
($this->_queryID
,$this->_currentRow
,$this->fetchMode
);
213 if (isset($this->_blobArr
)) $this->_fixblobs();
214 $this->_updatefields();
217 return (is_array($this->fields
));
220 // Create associative array
221 function _updatefields()
223 if (ADODB_ASSOC_CASE
== 2) return; // native
226 $lowercase = (ADODB_ASSOC_CASE
== 0);
228 foreach($this->fields
as $k => $v) {
229 if (is_integer($k)) $arr[$k] = $v;
232 $arr[strtolower($k)] = $v;
234 $arr[strtoupper($k)] = $v;
237 $this->fields
= $arr;
243 $this->_currentRow++
;
244 if ($this->_numOfRows
< 0 ||
$this->_numOfRows
> $this->_currentRow
) {
245 $this->fields
= @pg_fetch_array
($this->_queryID
,$this->_currentRow
,$this->fetchMode
);
247 if (is_array($this->fields
)) {
249 if (isset($this->_blobArr
)) $this->_fixblobs();
251 $this->_updatefields();
258 $this->fields
= false;