Updated adodb syysext to upstream version 5.0.8a.
[Packages/TYPO3.CMS.git] / typo3 / sysext / adodb / adodb / drivers / adodb-postgres7.inc.php
1 <?php
2 /*
3 V5.08 6 Apr 2009 (c) 2000-2009 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.
7 Set tabs to 4.
8
9 Postgres7 support.
10 28 Feb 2001: Currently indicate that we support LIMIT
11 01 Dec 2001: dannym added support for default values
12 */
13
14 // security - hide paths
15 if (!defined('ADODB_DIR')) die();
16
17 include_once(ADODB_DIR."/drivers/adodb-postgres64.inc.php");
18
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
25 function ADODB_postgres7()
26 {
27 $this->ADODB_postgres64();
28 if (ADODB_ASSOC_CASE !== 2) {
29 $this->rsPrefix .= 'assoc_';
30 }
31 $this->_bindInputArray = PHP_VERSION >= 5.1;
32 }
33
34
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)
38 {
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);
45
46 return $rs;
47 }
48 /*
49 function Prepare($sql)
50 {
51 $info = $this->ServerInfo();
52 if ($info['version']>=7.3) {
53 return array($sql,false);
54 }
55 return $sql;
56 }
57 */
58
59 /*
60 I discovered that the MetaForeignKeys method no longer worked for Postgres 8.3.
61 I went ahead and modified it to work for both 8.2 and 8.3.
62 Please feel free to include this change in your next release of adodb.
63 William Kolodny [William.Kolodny#gt-t.net]
64 */
65 function MetaForeignKeys($table, $owner=false, $upper=false)
66 {
67 $sql="
68 SELECT fum.ftblname AS lookup_table, split_part(fum.rf, ')'::text, 1) AS lookup_field,
69 fum.ltable AS dep_table, split_part(fum.lf, ')'::text, 1) AS dep_field
70 FROM (
71 SELECT fee.ltable, fee.ftblname, fee.consrc, split_part(fee.consrc,'('::text, 2) AS lf,
72 split_part(fee.consrc, '('::text, 3) AS rf
73 FROM (
74 SELECT foo.relname AS ltable, foo.ftblname,
75 pg_get_constraintdef(foo.oid) AS consrc
76 FROM (
77 SELECT c.oid, c.conname AS name, t.relname, ft.relname AS ftblname
78 FROM pg_constraint c
79 JOIN pg_class t ON (t.oid = c.conrelid)
80 JOIN pg_class ft ON (ft.oid = c.confrelid)
81 JOIN pg_namespace nft ON (nft.oid = ft.relnamespace)
82 LEFT JOIN pg_description ds ON (ds.objoid = c.oid)
83 JOIN pg_namespace n ON (n.oid = t.relnamespace)
84 WHERE c.contype = 'f'::\"char\"
85 ORDER BY t.relname, n.nspname, c.conname, c.oid
86 ) foo
87 ) fee) fum
88 WHERE fum.ltable='".strtolower($table)."'
89 ORDER BY fum.ftblname, fum.ltable, split_part(fum.lf, ')'::text, 1)
90 ";
91 $rs = $this->Execute($sql);
92
93 if (!$rs || $rs->EOF) return false;
94
95 $a = array();
96 while (!$rs->EOF) {
97 if ($upper) {
98 $a[strtoupper($rs->Fields('lookup_table'))][] = strtoupper(str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field')));
99 } else {
100 $a[$rs->Fields('lookup_table')][] = str_replace('"','',$rs->Fields('dep_field').'='.$rs->Fields('lookup_field'));
101 }
102 $rs->MoveNext();
103 }
104
105 return $a;
106
107 }
108
109 // from Edward Jaramilla, improved version - works on pg 7.4
110 function _old_MetaForeignKeys($table, $owner=false, $upper=false)
111 {
112 $sql = 'SELECT t.tgargs as args
113 FROM
114 pg_trigger t,pg_class c,pg_proc p
115 WHERE
116 t.tgenabled AND
117 t.tgrelid = c.oid AND
118 t.tgfoid = p.oid AND
119 p.proname = \'RI_FKey_check_ins\' AND
120 c.relname = \''.strtolower($table).'\'
121 ORDER BY
122 t.tgrelid';
123
124 $rs = $this->Execute($sql);
125
126 if (!$rs || $rs->EOF) return false;
127
128 $arr = $rs->GetArray();
129 $a = array();
130 foreach($arr as $v) {
131 $data = explode(chr(0), $v['args']);
132 $size = count($data)-1; //-1 because the last node is empty
133 for($i = 4; $i < $size; $i++) {
134 if ($upper)
135 $a[strtoupper($data[2])][] = strtoupper($data[$i].'='.$data[++$i]);
136 else
137 $a[$data[2]][] = $data[$i].'='.$data[++$i];
138 }
139 }
140 return $a;
141 }
142
143 function _query($sql,$inputarr=false)
144 {
145 if (! $this->_bindInputArray) {
146 // We don't have native support for parameterized queries, so let's emulate it at the parent
147 return ADODB_postgres64::_query($sql, $inputarr);
148 }
149 $this->_errorMsg = false;
150 // -- added Cristiano da Cunha Duarte
151 if ($inputarr) {
152 $sqlarr = explode('?',trim($sql));
153 $sql = '';
154 $i = 1;
155 $last = sizeof($sqlarr)-1;
156 foreach($sqlarr as $v) {
157 if ($last < $i) $sql .= $v;
158 else $sql .= $v.' $'.$i;
159 $i++;
160 }
161
162 $rez = pg_query_params($this->_connectionID,$sql, $inputarr);
163 } else {
164 $rez = pg_query($this->_connectionID,$sql);
165 }
166 // check if no data returned, then no need to create real recordset
167 if ($rez && pg_numfields($rez) <= 0) {
168 if (is_resource($this->_resultid) && get_resource_type($this->_resultid) === 'pgsql result') {
169 pg_freeresult($this->_resultid);
170 }
171 $this->_resultid = $rez;
172 return true;
173 }
174 return $rez;
175 }
176
177 // this is a set of functions for managing client encoding - very important if the encodings
178 // of your database and your output target (i.e. HTML) don't match
179 //for instance, you may have UNICODE database and server it on-site as WIN1251 etc.
180 // GetCharSet - get the name of the character set the client is using now
181 // the functions should work with Postgres 7.0 and above, the set of charsets supported
182 // depends on compile flags of postgres distribution - if no charsets were compiled into the server
183 // it will return 'SQL_ANSI' always
184 function GetCharSet()
185 {
186 //we will use ADO's builtin property charSet
187 $this->charSet = @pg_client_encoding($this->_connectionID);
188 if (!$this->charSet) {
189 return false;
190 } else {
191 return $this->charSet;
192 }
193 }
194
195 // SetCharSet - switch the client encoding
196 function SetCharSet($charset_name)
197 {
198 $this->GetCharSet();
199 if ($this->charSet !== $charset_name) {
200 $if = pg_set_client_encoding($this->_connectionID, $charset_name);
201 if ($if == "0" & $this->GetCharSet() == $charset_name) {
202 return true;
203 } else return false;
204 } else return true;
205 }
206
207 }
208
209 /*--------------------------------------------------------------------------------------
210 Class Name: Recordset
211 --------------------------------------------------------------------------------------*/
212
213 class ADORecordSet_postgres7 extends ADORecordSet_postgres64{
214
215 var $databaseType = "postgres7";
216
217
218 function ADORecordSet_postgres7($queryID,$mode=false)
219 {
220 $this->ADORecordSet_postgres64($queryID,$mode);
221 }
222
223 // 10% speedup to move MoveNext to child class
224 function MoveNext()
225 {
226 if (!$this->EOF) {
227 $this->_currentRow++;
228 if ($this->_numOfRows < 0 || $this->_numOfRows > $this->_currentRow) {
229 $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
230
231 if (is_array($this->fields)) {
232 if ($this->fields && isset($this->_blobArr)) $this->_fixblobs();
233 return true;
234 }
235 }
236 $this->fields = false;
237 $this->EOF = true;
238 }
239 return false;
240 }
241
242 }
243
244 class ADORecordSet_assoc_postgres7 extends ADORecordSet_postgres64{
245
246 var $databaseType = "postgres7";
247
248
249 function ADORecordSet_assoc_postgres7($queryID,$mode=false)
250 {
251 $this->ADORecordSet_postgres64($queryID,$mode);
252 }
253
254 function _fetch()
255 {
256 if ($this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0)
257 return false;
258
259 $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
260
261 if ($this->fields) {
262 if (isset($this->_blobArr)) $this->_fixblobs();
263 $this->_updatefields();
264 }
265
266 return (is_array($this->fields));
267 }
268
269 // Create associative array
270 function _updatefields()
271 {
272 if (ADODB_ASSOC_CASE == 2) return; // native
273
274 $arr = array();
275 $lowercase = (ADODB_ASSOC_CASE == 0);
276
277 foreach($this->fields as $k => $v) {
278 if (is_integer($k)) $arr[$k] = $v;
279 else {
280 if ($lowercase)
281 $arr[strtolower($k)] = $v;
282 else
283 $arr[strtoupper($k)] = $v;
284 }
285 }
286 $this->fields = $arr;
287 }
288
289 function MoveNext()
290 {
291 if (!$this->EOF) {
292 $this->_currentRow++;
293 if ($this->_numOfRows < 0 || $this->_numOfRows > $this->_currentRow) {
294 $this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
295
296 if (is_array($this->fields)) {
297 if ($this->fields) {
298 if (isset($this->_blobArr)) $this->_fixblobs();
299
300 $this->_updatefields();
301 }
302 return true;
303 }
304 }
305
306
307 $this->fields = false;
308 $this->EOF = true;
309 }
310 return false;
311 }
312 }
313 ?>