Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / lib / adodb / drivers / adodb-postgres7.inc.php
blobb5c64dcb12636ceef7eef440015904d6c326e035
1 <?php
2 /*
3 V4.98 13 Feb 2008 (c) 2000-2008 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.
7 Set tabs to 4.
9 Postgres7 support.
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) : '';
41 if ($secs2cache)
42 $rs =& $this->CacheExecute($secs2cache,$sql."$limitStr$offsetStr",$inputarr);
43 else
44 $rs =& $this->Execute($sql."$limitStr$offsetStr",$inputarr);
46 return $rs;
49 function Prepare($sql)
51 $info = $this->ServerInfo();
52 if ($info['version']>=7.3) {
53 return array($sql,false);
55 return $sql;
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
64 FROM
65 pg_trigger t,pg_class c,pg_proc p
66 WHERE
67 t.tgenabled AND
68 t.tgrelid = c.oid AND
69 t.tgfoid = p.oid AND
70 p.proname = \'RI_FKey_check_ins\' AND
71 c.relname = \''.strtolower($table).'\'
72 ORDER BY
73 t.tgrelid';
75 $rs =& $this->Execute($sql);
77 if (!$rs || $rs->EOF) return false;
79 $arr =& $rs->GetArray();
80 $a = array();
81 foreach($arr as $v) {
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++) {
85 if ($upper)
86 $a[strtoupper($data[2])][] = strtoupper($data[$i].'='.$data[++$i]);
87 else
88 $a[$data[2]][] = $data[$i].'='.$data[++$i];
91 return $a;
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
102 if ($inputarr) {
103 $sqlarr = explode('?',trim($sql));
104 $sql = '';
105 $i = 1;
106 $last = sizeof($sqlarr)-1;
107 foreach($sqlarr as $v) {
108 if ($last < $i) $sql .= $v;
109 else $sql .= $v.' $'.$i;
110 $i++;
113 $rez = pg_query_params($this->_connectionID,$sql, $inputarr);
114 } else {
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;
123 return true;
125 return $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) {
140 return false;
141 } else {
142 return $this->charSet;
146 // SetCharSet - switch the client encoding
147 function SetCharSet($charset_name)
149 $this->GetCharSet();
150 if ($this->charSet !== $charset_name) {
151 $if = pg_set_client_encoding($this->_connectionID, $charset_name);
152 if ($if == "0" & $this->GetCharSet() == $charset_name) {
153 return true;
154 } else return false;
155 } else return true;
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
175 function MoveNext()
177 if (!$this->EOF) {
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();
184 return true;
187 $this->fields = false;
188 $this->EOF = true;
190 return 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);
205 function _fetch()
207 if ($this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0)
208 return false;
210 $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
212 if ($this->fields) {
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
225 $arr = array();
226 $lowercase = (ADODB_ASSOC_CASE == 0);
228 foreach($this->fields as $k => $v) {
229 if (is_integer($k)) $arr[$k] = $v;
230 else {
231 if ($lowercase)
232 $arr[strtolower($k)] = $v;
233 else
234 $arr[strtoupper($k)] = $v;
237 $this->fields = $arr;
240 function MoveNext()
242 if (!$this->EOF) {
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)) {
248 if ($this->fields) {
249 if (isset($this->_blobArr)) $this->_fixblobs();
251 $this->_updatefields();
253 return true;
258 $this->fields = false;
259 $this->EOF = true;
261 return false;