3 @version v5.20.9 21-Dec-2016
4 @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
5 @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
6 Released under both BSD license and Lesser GPL library license.
7 Whenever there is any discrepancy between the two licenses,
8 the BSD license will take precedence.
12 28 Feb 2001: Currently indicate that we support LIMIT
13 01 Dec 2001: dannym added support for default values
16 // security - hide paths
17 if (!defined('ADODB_DIR')) die();
19 include_once(ADODB_DIR
."/drivers/adodb-postgres64.inc.php");
21 class ADODB_postgres7
extends ADODB_postgres64
{
22 var $databaseType = 'postgres7';
23 var $hasLimit = true; // set to true for pgsql 6.5+ only. support pgsql/mysql SELECT * FROM TABLE LIMIT 10
24 var $ansiOuter = true;
25 var $charSet = true; //set to true for Postgres 7 and above - PG client supports encodings
27 // Richard 3/18/2012 - Modified SQL to return SERIAL type correctly AS old driver no longer return SERIAL as data type.
28 var $metaColumnsSQL = "
32 WHEN x.sequence_name != ''
36 a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum
40 JOIN pg_type t ON a.atttypid = t.oid
43 c.relname as sequence_name,
44 c1.relname as related_table,
45 a.attname as related_column
47 JOIN pg_depend d ON d.objid = c.oid
48 LEFT JOIN pg_class c1 ON d.refobjid = c1.oid
49 LEFT JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
50 WHERE c.relkind = 'S' AND c1.relname = '%s'
51 ) x ON x.related_column= a.attname
53 c.relkind in ('r','v')
54 AND (c.relname='%s' or c.relname = lower('%s'))
55 AND a.attname not like '....%%'
57 AND a.attrelid = c.oid
61 // used when schema defined
62 var $metaColumnsSQL1 = "
66 WHEN x.sequence_name != ''
70 a.attlen, a.atttypmod, a.attnotnull, a.atthasdef, a.attnum
75 JOIN pg_type t ON a.atttypid = t.oid
78 c.relname as sequence_name,
79 c1.relname as related_table,
80 a.attname as related_column
82 JOIN pg_depend d ON d.objid = c.oid
83 LEFT JOIN pg_class c1 ON d.refobjid = c1.oid
84 LEFT JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum)
85 WHERE c.relkind = 'S' AND c1.relname = '%s'
86 ) x ON x.related_column= a.attname
88 c.relkind in ('r','v')
89 AND (c.relname='%s' or c.relname = lower('%s'))
90 AND c.relnamespace=n.oid and n.nspname='%s'
91 AND a.attname not like '....%%'
93 AND a.atttypid = t.oid
94 AND a.attrelid = c.oid
98 function __construct()
100 parent
::__construct();
101 if (ADODB_ASSOC_CASE
!== ADODB_ASSOC_CASE_NATIVE
) {
102 $this->rsPrefix
.= 'assoc_';
104 $this->_bindInputArray
= PHP_VERSION
>= 5.1;
108 // the following should be compat with postgresql 7.2,
109 // which makes obsolete the LIMIT limit,offset syntax
110 function SelectLimit($sql,$nrows=-1,$offset=-1,$inputarr=false,$secs2cache=0)
112 $offsetStr = ($offset >= 0) ?
" OFFSET ".((integer)$offset) : '';
113 $limitStr = ($nrows >= 0) ?
" LIMIT ".((integer)$nrows) : '';
115 $rs = $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
117 $rs = $this->Execute($sql."$limitStr$offsetStr",$inputarr);
123 function Prepare($sql)
125 $info = $this->ServerInfo();
126 if ($info['version']>=7.3) {
127 return array($sql,false);
134 * Generate the SQL to retrieve MetaColumns data
135 * @param string $table Table name
136 * @param string $schema Schema name (can be blank)
137 * @return string SQL statement to execute
139 protected function _generateMetaColumnsSQL($table, $schema)
142 return sprintf($this->metaColumnsSQL1
, $table, $table, $table, $schema);
145 return sprintf($this->metaColumnsSQL
, $table, $table, $schema);
150 * @returns assoc array where keys are tables, and values are foreign keys
152 function MetaForeignKeys($table, $owner=false, $upper=false)
154 # Regex isolates the 2 terms between parenthesis using subexpressions
155 $regex = '^.*\((.*)\).*\((.*)\).*$';
159 regexp_replace(consrc, '$regex', '\\2') AS lookup_field,
161 regexp_replace(consrc, '$regex', '\\1') AS dep_field
164 pg_get_constraintdef(c.oid) AS consrc,
165 t.relname AS dep_table,
166 ft.relname AS lookup_table
168 JOIN pg_class t ON (t.oid = c.conrelid)
169 JOIN pg_class ft ON (ft.oid = c.confrelid)
170 JOIN pg_namespace nft ON (nft.oid = ft.relnamespace)
171 LEFT JOIN pg_description ds ON (ds.objoid = c.oid)
172 JOIN pg_namespace n ON (n.oid = t.relnamespace)
173 WHERE c.contype = 'f'::\"char\"
174 ORDER BY t.relname, n.nspname, c.conname, c.oid
177 dep_table='".strtolower($table)."'
182 $rs = $this->Execute($sql);
184 if (!$rs ||
$rs->EOF
) return false;
189 $a[strtoupper($rs->Fields('lookup_table'))][] = strtoupper(str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field')));
191 $a[$rs->Fields('lookup_table')][] = str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field'));
200 // from Edward Jaramilla, improved version - works on pg 7.4
201 function _old_MetaForeignKeys($table, $owner=false, $upper=false)
203 $sql = 'SELECT t.tgargs as args
205 pg_trigger t,pg_class c,pg_proc p
208 t.tgrelid = c.oid AND
210 p.proname = \'RI_FKey_check_ins\' AND
211 c.relname = \''.strtolower($table).'\'
215 $rs = $this->Execute($sql);
217 if (!$rs ||
$rs->EOF
) return false;
219 $arr = $rs->GetArray();
221 foreach($arr as $v) {
222 $data = explode(chr(0), $v['args']);
223 $size = count($data)-1; //-1 because the last node is empty
224 for($i = 4; $i < $size; $i++
) {
226 $a[strtoupper($data[2])][] = strtoupper($data[$i].'='.$data[++
$i]);
228 $a[$data[2]][] = $data[$i].'='.$data[++
$i];
234 function _query($sql,$inputarr=false)
236 if (! $this->_bindInputArray
) {
237 // We don't have native support for parameterized queries, so let's emulate it at the parent
238 return ADODB_postgres64
::_query($sql, $inputarr);
242 $this->_errorMsg
= false;
243 // -- added Cristiano da Cunha Duarte
245 $sqlarr = explode('?',trim($sql));
248 $last = sizeof($sqlarr)-1;
249 foreach($sqlarr as $v) {
250 if ($last < $i) $sql .= $v;
251 else $sql .= $v.' $'.$i;
255 $rez = pg_query_params($this->_connectionID
,$sql, $inputarr);
257 $rez = pg_query($this->_connectionID
,$sql);
259 // check if no data returned, then no need to create real recordset
260 if ($rez && pg_num_fields($rez) <= 0) {
261 if (is_resource($this->_resultid
) && get_resource_type($this->_resultid
) === 'pgsql result') {
262 pg_free_result($this->_resultid
);
264 $this->_resultid
= $rez;
270 // this is a set of functions for managing client encoding - very important if the encodings
271 // of your database and your output target (i.e. HTML) don't match
272 //for instance, you may have UNICODE database and server it on-site as WIN1251 etc.
273 // GetCharSet - get the name of the character set the client is using now
274 // the functions should work with Postgres 7.0 and above, the set of charsets supported
275 // depends on compile flags of postgres distribution - if no charsets were compiled into the server
276 // it will return 'SQL_ANSI' always
277 function GetCharSet()
279 //we will use ADO's builtin property charSet
280 $this->charSet
= @pg_client_encoding
($this->_connectionID
);
281 if (!$this->charSet
) {
284 return $this->charSet
;
288 // SetCharSet - switch the client encoding
289 function SetCharSet($charset_name)
292 if ($this->charSet
!== $charset_name) {
293 $if = pg_set_client_encoding($this->_connectionID
, $charset_name);
294 if ($if == "0" & $this->GetCharSet() == $charset_name) {
302 /*--------------------------------------------------------------------------------------
303 Class Name: Recordset
304 --------------------------------------------------------------------------------------*/
306 class ADORecordSet_postgres7
extends ADORecordSet_postgres64
{
308 var $databaseType = "postgres7";
311 function __construct($queryID, $mode=false)
313 parent
::__construct($queryID, $mode);
316 // 10% speedup to move MoveNext to child class
320 $this->_currentRow++
;
321 if ($this->_numOfRows
< 0 ||
$this->_numOfRows
> $this->_currentRow
) {
322 $this->fields
= @pg_fetch_array
($this->_queryID
,$this->_currentRow
,$this->fetchMode
);
324 if (is_array($this->fields
)) {
325 if ($this->fields
&& isset($this->_blobArr
)) $this->_fixblobs();
329 $this->fields
= false;
337 class ADORecordSet_assoc_postgres7
extends ADORecordSet_postgres64
{
339 var $databaseType = "postgres7";
342 function __construct($queryID, $mode=false)
344 parent
::__construct($queryID, $mode);
349 if ($this->_currentRow
>= $this->_numOfRows
&& $this->_numOfRows
>= 0) {
353 $this->fields
= @pg_fetch_array
($this->_queryID
,$this->_currentRow
,$this->fetchMode
);
356 if (isset($this->_blobArr
)) $this->_fixblobs();
357 $this->_updatefields();
360 return (is_array($this->fields
));
366 $this->_currentRow++
;
367 if ($this->_numOfRows
< 0 ||
$this->_numOfRows
> $this->_currentRow
) {
368 $this->fields
= @pg_fetch_array
($this->_queryID
,$this->_currentRow
,$this->fetchMode
);
370 if (is_array($this->fields
)) {
372 if (isset($this->_blobArr
)) $this->_fixblobs();
374 $this->_updatefields();
381 $this->fields
= false;