3 V5.18 3 Sep 2012 (c) 2000-2012 John Lim (jlim#natsoft.com). 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
24 // Richard 3/18/2012 - Modified SQL to return SERIAL type correctly AS old driver no longer return SERIAL as data type.
28 WHEN x.sequence_name != '' THEN 'SERIAL'
31 a.attlen,a.atttypmod,a.attnotnull,a.atthasdef,a.attnum
32 FROM pg_class c, pg_attribute a
33 JOIN pg_type t ON a.atttypid = t.oid
35 (SELECT c.relname as sequence_name,
36 c1.relname as related_table,
37 a.attname as related_column
39 JOIN pg_depend d ON d.objid = c.oid
40 LEFT JOIN pg_class c1 ON d.refobjid = c1.oid
41 LEFT JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
42 WHERE c.relkind = 'S' AND c1.relname = '%s') x
43 ON x.related_column= a.attname
44 WHERE c.relkind in ('r','v') AND
45 (c.relname='%s' or c.relname = lower('%s')) AND
46 a.attname not like '....%%' AND
51 // used when schema defined
52 var $metaColumnsSQL1 = "
55 WHEN x.sequence_name != '' THEN 'SERIAL'
58 a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum
59 FROM pg_class c, pg_namespace n, pg_attribute a
60 JOIN pg_type t ON a.atttypid = t.oid
62 (SELECT c.relname as sequence_name,
63 c1.relname as related_table,
64 a.attname as related_column
66 JOIN pg_depend d ON d.objid = c.oid
67 LEFT JOIN pg_class c1 ON d.refobjid = c1.oid
68 LEFT JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
69 WHERE c.relkind = 'S' AND c1.relname = '%s') x
70 ON x.related_column= a.attname
71 WHERE c.relkind in ('r','v') AND (c.relname='%s' or c.relname = lower('%s'))
72 AND c.relnamespace=n.oid and n.nspname='%s'
73 AND a.attname not like '....%%' AND a.attnum > 0
74 AND a.atttypid = t.oid AND a.attrelid = c.oid
78 function ADODB_postgres7()
80 $this->ADODB_postgres64();
81 if (ADODB_ASSOC_CASE !== 2) {
82 $this->rsPrefix .= 'assoc_';
84 $this->_bindInputArray = PHP_VERSION >= 5.1;
88 // the following should be compat with postgresql 7.2,
89 // which makes obsolete the LIMIT limit,offset syntax
90 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
92 $offsetStr = ($offset >= 0) ? " OFFSET ".((integer)$offset) : '';
93 $limitStr = ($nrows >= 0) ? " LIMIT ".((integer)$nrows) : '';
95 $rs = $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
97 $rs = $this->Execute($sql."$limitStr$offsetStr",$inputarr);
102 function Prepare($sql)
104 $info = $this->ServerInfo();
105 if ($info['version']>=7.3) {
106 return array($sql,false);
113 I discovered that the MetaForeignKeys method no longer worked for Postgres 8.3.
114 I went ahead and modified it to work for both 8.2 and 8.3.
115 Please feel free to include this change in your next release of adodb.
116 William Kolodny [William.Kolodny#gt-t.net]
118 function MetaForeignKeys($table, $owner=false, $upper=false)
121 SELECT fum.ftblname AS lookup_table, split_part(fum.rf, ')'::text, 1) AS lookup_field,
122 fum.ltable AS dep_table, split_part(fum.lf, ')'::text, 1) AS dep_field
124 SELECT fee.ltable, fee.ftblname, fee.consrc, split_part(fee.consrc,'('::text, 2) AS lf,
125 split_part(fee.consrc, '('::text, 3) AS rf
127 SELECT foo.relname AS ltable, foo.ftblname,
128 pg_get_constraintdef(foo.oid) AS consrc
130 SELECT c.oid, c.conname AS name, t.relname, ft.relname AS ftblname
132 JOIN pg_class t ON (t.oid = c.conrelid)
133 JOIN pg_class ft ON (ft.oid = c.confrelid)
134 JOIN pg_namespace nft ON (nft.oid = ft.relnamespace)
135 LEFT JOIN pg_description ds ON (ds.objoid = c.oid)
136 JOIN pg_namespace n ON (n.oid = t.relnamespace)
137 WHERE c.contype = 'f'::\"char\"
138 ORDER BY t.relname, n.nspname, c.conname, c.oid
141 WHERE fum.ltable='".strtolower($table)."'
142 ORDER BY fum.ftblname, fum.ltable, split_part(fum.lf, ')'::text, 1)
144 $rs = $this->Execute($sql);
146 if (!$rs || $rs->EOF) return false;
151 $a[strtoupper($rs->Fields('lookup_table'))][] = strtoupper(str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field')));
153 $a[$rs->Fields('lookup_table')][] = str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field'));
162 // from Edward Jaramilla, improved version - works on pg 7.4
163 function _old_MetaForeignKeys($table, $owner=false, $upper=false)
165 $sql = 'SELECT t.tgargs as args
167 pg_trigger t,pg_class c,pg_proc p
170 t.tgrelid = c.oid AND
172 p.proname = \'RI_FKey_check_ins\' AND
173 c.relname = \''.strtolower($table).'\'
177 $rs = $this->Execute($sql);
179 if (!$rs || $rs->EOF) return false;
181 $arr = $rs->GetArray();
183 foreach($arr as $v) {
184 $data = explode(chr(0), $v['args']);
185 $size = count($data)-1; //-1 because the last node is empty
186 for($i = 4; $i < $size; $i++) {
188 $a[strtoupper($data[2])][] = strtoupper($data[$i].'='.$data[++$i]);
190 $a[$data[2]][] = $data[$i].'='.$data[++$i];
196 function _query($sql,$inputarr=false)
198 if (! $this->_bindInputArray) {
199 // We don't have native support for parameterized queries, so let's emulate it at the parent
200 return ADODB_postgres64::_query($sql, $inputarr);
204 $this->_errorMsg = false;
205 // -- added Cristiano da Cunha Duarte
207 $sqlarr = explode('?',trim($sql));
210 $last = sizeof($sqlarr)-1;
211 foreach($sqlarr as $v) {
212 if ($last < $i) $sql .= $v;
213 else $sql .= $v.' $'.$i;
217 $rez = pg_query_params($this->_connectionID,$sql, $inputarr);
219 $rez = pg_query($this->_connectionID,$sql);
221 // check if no data returned, then no need to create real recordset
222 if ($rez && pg_numfields($rez) <= 0) {
223 if (is_resource($this->_resultid) && get_resource_type($this->_resultid) === 'pgsql result') {
224 pg_freeresult($this->_resultid);
226 $this->_resultid = $rez;
232 // this is a set of functions for managing client encoding - very important if the encodings
233 // of your database and your output target (i.e. HTML) don't match
234 //for instance, you may have UNICODE database and server it on-site as WIN1251 etc.
235 // GetCharSet - get the name of the character set the client is using now
236 // the functions should work with Postgres 7.0 and above, the set of charsets supported
237 // depends on compile flags of postgres distribution - if no charsets were compiled into the server
238 // it will return 'SQL_ANSI' always
239 function GetCharSet()
241 //we will use ADO's builtin property charSet
242 $this->charSet = @pg_client_encoding($this->_connectionID);
243 if (!$this->charSet) {
246 return $this->charSet;
250 // SetCharSet - switch the client encoding
251 function SetCharSet($charset_name)
254 if ($this->charSet !== $charset_name) {
255 $if = pg_set_client_encoding($this->_connectionID, $charset_name);
256 if ($if == "0" & $this->GetCharSet() == $charset_name) {
264 /*--------------------------------------------------------------------------------------
265 Class Name: Recordset
266 --------------------------------------------------------------------------------------*/
268 class ADORecordSet_postgres7 extends ADORecordSet_postgres64{
270 var $databaseType = "postgres7";
273 function ADORecordSet_postgres7($queryID,$mode=false)
275 $this->ADORecordSet_postgres64($queryID,$mode);
278 // 10% speedup to move MoveNext to child class
282 $this->_currentRow++;
283 if ($this->_numOfRows < 0 || $this->_numOfRows > $this->_currentRow) {
284 $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
286 if (is_array($this->fields)) {
287 if ($this->fields && isset($this->_blobArr)) $this->_fixblobs();
291 $this->fields = false;
299 class ADORecordSet_assoc_postgres7 extends ADORecordSet_postgres64{
301 var $databaseType = "postgres7";
304 function ADORecordSet_assoc_postgres7($queryID,$mode=false)
306 $this->ADORecordSet_postgres64($queryID,$mode);
311 if ($this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0)
314 $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
317 if (isset($this->_blobArr)) $this->_fixblobs();
318 $this->_updatefields();
321 return (is_array($this->fields));
324 // Create associative array
325 function _updatefields()
327 if (ADODB_ASSOC_CASE == 2) return; // native
330 $lowercase = (ADODB_ASSOC_CASE == 0);
332 foreach($this->fields as $k => $v) {
333 if (is_integer($k)) $arr[$k] = $v;
336 $arr[strtolower($k)] = $v;
338 $arr[strtoupper($k)] = $v;
341 $this->fields = $arr;
347 $this->_currentRow++;
348 if ($this->_numOfRows < 0 || $this->_numOfRows > $this->_currentRow) {
349 $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
351 if (is_array($this->fields)) {
353 if (isset($this->_blobArr)) $this->_fixblobs();
355 $this->_updatefields();
362 $this->fields = false;