Subversion Repositories svnkaklik

Rev

Details | Last modification | View Log

Rev Author Line No. Line
36 kaklik 1
<?php
2
/*
3
 V4.80 8 Mar 2006  (c) 2000-2006 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.
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
	// from  Edward Jaramilla, improved version - works on pg 7.4
60
	function MetaForeignKeys($table, $owner=false, $upper=false)
61
	{
62
		$sql = 'SELECT t.tgargs as args
63
		FROM
64
		pg_trigger t,pg_class c,pg_proc p
65
		WHERE
66
		t.tgenabled AND
67
		t.tgrelid = c.oid AND
68
		t.tgfoid = p.oid AND
69
		p.proname = \'RI_FKey_check_ins\' AND
70
		c.relname = \''.strtolower($table).'\'
71
		ORDER BY
72
			t.tgrelid';
73
 
74
		$rs =& $this->Execute($sql);
75
 
76
		if ($rs && !$rs->EOF) {
77
			$arr =& $rs->GetArray();
78
			$a = array();
79
			foreach($arr as $v)
80
			{
81
				$data = explode(chr(0), $v['args']);
82
				if ($upper) {
83
					$a[strtoupper($data[2])][] = strtoupper($data[4].'='.$data[5]);
84
				} else {
85
				$a[$data[2]][] = $data[4].'='.$data[5];
86
				}
87
			}
88
			return $a;
89
		}
90
		return false;
91
	}
92
 
93
	function _query($sql,$inputarr)
94
	{
95
		if (! $this->_bindInputArray) {
96
			// We don't have native support for parameterized queries, so let's emulate it at the parent
97
			return ADODB_postgres64::_query($sql, $inputarr);
98
		}
99
		// -- added Cristiano da Cunha Duarte
100
		if ($inputarr) {
101
			$sqlarr = explode('?',trim($sql));
102
			$sql = '';
103
			$i = 1;
104
			$last = sizeof($sqlarr)-1;
105
			foreach($sqlarr as $v) {
106
				if ($last < $i) $sql .= $v;
107
				else $sql .= $v.' $'.$i;
108
				$i++;
109
			}
110
 
111
			$rez = pg_query_params($this->_connectionID,$sql, $inputarr);
112
		} else {
113
			$rez = pg_query($this->_connectionID,$sql);
114
		}
115
		// check if no data returned, then no need to create real recordset
116
		if ($rez && pg_numfields($rez) <= 0) {
117
			if (is_resource($this->_resultid) && get_resource_type($this->_resultid) === 'pgsql result') {
118
				pg_freeresult($this->_resultid);
119
			}
120
			$this->_resultid = $rez;
121
			return true;
122
		}		
123
		return $rez;
124
	}
125
 
126
 	 // this is a set of functions for managing client encoding - very important if the encodings
127
	// of your database and your output target (i.e. HTML) don't match
128
	//for instance, you may have UNICODE database and server it on-site as WIN1251 etc.
129
	// GetCharSet - get the name of the character set the client is using now
130
	// the functions should work with Postgres 7.0 and above, the set of charsets supported
131
	// depends on compile flags of postgres distribution - if no charsets were compiled into the server
132
	// it will return 'SQL_ANSI' always
133
	function GetCharSet()
134
	{
135
		//we will use ADO's builtin property charSet
136
		$this->charSet = @pg_client_encoding($this->_connectionID);
137
		if (!$this->charSet) {
138
			return false;
139
		} else {
140
			return $this->charSet;
141
		}
142
	}
143
 
144
	// SetCharSet - switch the client encoding
145
	function SetCharSet($charset_name)
146
	{
147
		$this->GetCharSet();
148
		if ($this->charSet !== $charset_name) {
149
			$if = pg_set_client_encoding($this->_connectionID, $charset_name);
150
			if ($if == "0" & $this->GetCharSet() == $charset_name) {
151
				return true;
152
			} else return false;
153
		} else return true;
154
	}
155
 
156
}
157
 
158
/*--------------------------------------------------------------------------------------
159
	 Class Name: Recordset
160
--------------------------------------------------------------------------------------*/
161
 
162
class ADORecordSet_postgres7 extends ADORecordSet_postgres64{
163
 
164
	var $databaseType = "postgres7";
165
 
166
 
167
	function ADORecordSet_postgres7($queryID,$mode=false) 
168
	{
169
		$this->ADORecordSet_postgres64($queryID,$mode);
170
	}
171
 
172
	 	// 10% speedup to move MoveNext to child class
173
	function MoveNext() 
174
	{
175
		if (!$this->EOF) {
176
			$this->_currentRow++;
177
			if ($this->_numOfRows < 0 || $this->_numOfRows > $this->_currentRow) {
178
				$this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
179
 
180
				if (is_array($this->fields)) {
181
					if ($this->fields && isset($this->_blobArr)) $this->_fixblobs();
182
					return true;
183
				}
184
			}
185
			$this->fields = false;
186
			$this->EOF = true;
187
		}
188
		return false;
189
	}		
190
 
191
}
192
 
193
class ADORecordSet_assoc_postgres7 extends ADORecordSet_postgres64{
194
 
195
	var $databaseType = "postgres7";
196
 
197
 
198
	function ADORecordSet_assoc_postgres7($queryID,$mode=false) 
199
	{
200
		$this->ADORecordSet_postgres64($queryID,$mode);
201
	}
202
 
203
	function _fetch()
204
	{
205
		if ($this->_currentRow >= $this->_numOfRows && $this->_numOfRows >= 0)
206
        	return false;
207
 
208
		$this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
209
 
210
		if ($this->fields) {
211
			if (isset($this->_blobArr)) $this->_fixblobs();
212
			$this->_updatefields();
213
		}
214
 
215
		return (is_array($this->fields));
216
	}
217
 
218
		// Create associative array
219
	function _updatefields()
220
	{
221
		if (ADODB_ASSOC_CASE == 2) return; // native
222
 
223
		$arr = array();
224
		$lowercase = (ADODB_ASSOC_CASE == 0);
225
 
226
		foreach($this->fields as $k => $v) {
227
			if (is_integer($k)) $arr[$k] = $v;
228
			else {
229
				if ($lowercase)
230
					$arr[strtolower($k)] = $v;
231
				else
232
					$arr[strtoupper($k)] = $v;
233
			}
234
		}
235
		$this->fields = $arr;
236
	}
237
 
238
	function MoveNext() 
239
	{
240
		if (!$this->EOF) {
241
			$this->_currentRow++;
242
			if ($this->_numOfRows < 0 || $this->_numOfRows > $this->_currentRow) {
243
				$this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode);
244
 
245
				if (is_array($this->fields)) {
246
					if ($this->fields) {
247
						if (isset($this->_blobArr)) $this->_fixblobs();
248
 
249
						$this->_updatefields();
250
					}
251
					return true;
252
				}
253
			}
254
 
255
 
256
			$this->fields = false;
257
			$this->EOF = true;
258
		}
259
		return false;
260
	}
261
}
262
?>