|
| |
1 |
|
<?php |
|
| |
2 |
|
|
|
| |
3 |
|
|
|
| |
4 |
|
|
|
| |
5 |
|
|
|
| |
6 |
|
|
|
| |
7 |
|
|
|
| |
8 |
|
|
|
| |
9 |
|
|
|
| |
10 |
|
|
|
| |
11 |
|
|
|
| |
12 |
|
|
|
| |
13 |
|
|
|
| |
14 |
|
|
|
| |
15 |
|
|
|
| |
16 |
|
|
|
| |
17 |
|
|
|
| |
18 |
|
|
|
| |
19 |
|
|
|
| |
20 |
|
|
|
| |
21 |
|
class ADODB_postgres extends ADODBConnection{ |
|
| |
22 |
|
var $databaseType = 'postgres'; |
|
| |
23 |
|
var $hasInsertID = true; |
|
| |
24 |
|
var $_resultid = false; |
|
| |
25 |
|
var $concat_operator='||'; |
|
| |
26 |
|
var $metaTablesSQL = "select tablename from pg_tables where tablename not like 'pg_%' order by 1"; |
|
| |
27 |
|
|
|
| |
28 |
|
|
|
| |
29 |
|
|
|
| |
30 |
|
|
|
| |
31 |
|
|
|
| |
32 |
|
|
|
| |
33 |
|
|
|
| |
34 |
|
|
|
| |
35 |
|
|
|
| |
36 |
|
|
|
| |
37 |
|
var $metaColumnsSQL = "SELECT a.attname,t.typname,a.attlen,a.atttypmod,a.attnotnull FROM pg_class c, pg_attribute a,pg_type t WHERE relkind = 'r' AND c.relname='%s' AND a.attnum > 0 AND a.atttypid = t.oid AND a.attrelid = c.oid ORDER BY a.attnum"; |
|
| |
38 |
|
|
|
| |
39 |
|
var $metaKeySQL = "SELECT ic.relname AS index_name, a.attname AS column_name,i.indisunique AS unique_key, i.indisprimary AS primary_key FROM pg_class bc, pg_class ic, pg_index i, pg_attribute a WHERE bc.oid = i.indrelid AND ic.oid = i.indexrelid AND (i.indkey[0] = a.attnum OR i.indkey[1] = a.attnum OR i.indkey[2] = a.attnum OR i.indkey[3] = a.attnum OR i.indkey[4] = a.attnum OR i.indkey[5] = a.attnum OR i.indkey[6] = a.attnum OR i.indkey[7] = a.attnum) AND a.attrelid = bc.oid AND bc.relname = '%s'"; |
|
| |
40 |
|
|
|
| |
41 |
|
var $_hastrans = false; |
|
| |
42 |
|
var $hasAffectedRows = false; |
|
| |
43 |
|
var $hasTop = false; |
|
| |
44 |
|
var $hasLimit = false; |
|
| |
45 |
|
|
|
| |
46 |
|
var $true = 't'; |
|
| |
47 |
|
var $false = 'f'; |
|
| |
48 |
|
var $fmtDate = "'Y-m-d'"; |
|
| |
49 |
|
var $fmtTimeStamp = "'Y-m-d h:i:s'"; |
|
| |
50 |
|
var $hasMoveFirst = true; |
|
| |
51 |
|
var $hasGenID = true; |
|
| |
52 |
|
var $_genIDSQL = "SELECT NEXTVAL('%s')"; |
|
| |
53 |
|
var $_genSeqSQL = "CREATE SEQUENCE %s"; |
|
| |
54 |
|
|
|
| |
55 |
|
|
|
| |
56 |
|
|
|
| |
57 |
|
|
|
| |
58 |
|
|
|
| |
59 |
|
|
|
| |
60 |
|
|
|
| |
61 |
|
|
|
| |
62 |
|
|
|
| |
63 |
|
function ADODB_postgres() |
|
| |
64 |
|
{ |
|
| |
65 |
|
} |
|
| |
66 |
|
|
|
| |
67 |
|
|
|
| |
68 |
|
|
|
| |
69 |
|
|
|
| |
70 |
|
|
|
| |
71 |
|
function _insertid() |
|
| |
72 |
|
{ |
|
| |
73 |
|
return pg_getlastoid($this->_resultid); |
|
| |
74 |
|
} |
|
| |
75 |
|
|
|
| |
76 |
|
|
|
| |
77 |
|
|
|
| |
78 |
|
function _Affected_Rows() |
|
| |
79 |
|
{ |
|
| |
80 |
|
|
|
| |
81 |
|
return pg_cmdtuples($this->_resultid); |
|
| |
82 |
|
} |
|
| |
83 |
|
|
|
| |
84 |
|
|
|
| |
85 |
|
|
|
| |
86 |
|
function BeginTrans() |
|
| |
87 |
|
{ |
|
| |
88 |
|
$this->_hastrans = true; |
|
| |
89 |
|
return @pg_Exec($this->_connectionID, "begin"); |
|
| |
90 |
|
} |
|
| |
91 |
|
|
|
| |
92 |
|
|
|
| |
93 |
|
function CommitTrans() |
|
| |
94 |
|
{ |
|
| |
95 |
|
$this->_hastrans = false; |
|
| |
96 |
|
return @pg_Exec($this->_connectionID, "commit"); |
|
| |
97 |
|
} |
|
| |
98 |
|
|
|
| |
99 |
|
|
|
| |
100 |
|
function RollbackTrans() |
|
| |
101 |
|
{ |
|
| |
102 |
|
$this->_hastrans = false; |
|
| |
103 |
|
return @pg_Exec($this->_connectionID, "rollback"); |
|
| |
104 |
|
} |
|
| |
105 |
|
|
|
| |
106 |
|
|
|
| |
107 |
|
function &MetaColumns($table) |
|
| |
108 |
|
{ |
|
| |
109 |
|
if (!empty($this->metaColumnsSQL)) { |
|
| |
110 |
|
|
|
| |
111 |
|
$rs = $this->Execute(sprintf($this->metaColumnsSQL,strtolower($table))); |
|
| |
112 |
|
if ($rs === false) return false; |
|
| |
113 |
|
|
|
| |
114 |
|
if (!empty($this->metaKeySQL)) { |
|
| |
115 |
|
|
|
| |
116 |
|
|
|
| |
117 |
|
|
|
| |
118 |
|
|
|
| |
119 |
|
$rskey = $this->Execute(sprintf($this->metaKeySQL,strtolower($table))); |
|
| |
120 |
|
|
|
| |
121 |
|
$keys = $rskey->GetArray(); |
|
| |
122 |
|
$rskey->Close(); |
|
| |
123 |
|
unset($rskey); |
|
| |
124 |
|
} |
|
| |
125 |
|
|
|
| |
126 |
|
$retarr = array(); |
|
| |
127 |
|
while (!$rs->EOF) { |
|
| |
128 |
|
$fld = new ADODBFieldObject(); |
|
| |
129 |
|
$fld->name = $rs->fields[0]; |
|
| |
130 |
|
$fld->type = $rs->fields[1]; |
|
| |
131 |
|
$fld->max_length = $rs->fields[2]; |
|
| |
132 |
|
if ($fld->max_length <= 0) $fld->max_length = $rs->fields[3]-4; |
|
| |
133 |
|
if ($fld->max_length <= 0) $fld->max_length = -1; |
|
| |
134 |
|
|
|
| |
135 |
|
|
|
| |
136 |
|
if ($rs->fields[4] == $this->true) { |
|
| |
137 |
|
$fld->not_null = true; |
|
| |
138 |
|
} |
|
| |
139 |
|
|
|
| |
140 |
|
|
|
| |
141 |
|
if (is_array($keys)) { |
|
| |
142 |
|
reset ($keys); |
|
| |
143 |
|
while (list($x,$key) = each($keys)) { |
|
| |
144 |
|
if ($fld->name == $key['column_name'] AND $key['primary_key'] == $this->true) |
|
| |
145 |
|
$fld->primary_key = true; |
|
| |
146 |
|
if ($fld->name == $key['column_name'] AND $key['unique_key'] == $this->true) |
|
| |
147 |
|
$fld->unique = true; |
|
| |
148 |
|
} |
|
| |
149 |
|
} |
|
| |
150 |
|
|
|
| |
151 |
|
$retarr[strtoupper($fld->name)] = $fld; |
|
| |
152 |
|
|
|
| |
153 |
|
$rs->MoveNext(); |
|
| |
154 |
|
} |
|
| |
155 |
|
$rs->Close(); |
|
| |
156 |
|
return $retarr; |
|
| |
157 |
|
} |
|
| |
158 |
|
return false; |
|
| |
159 |
|
} |
|
| |
160 |
|
|
|
| |
161 |
|
|
|
| |
162 |
|
|
|
| |
163 |
|
|
|
| |
164 |
|
|
|
| |
165 |
|
|
|
| |
166 |
|
|
|
| |
167 |
|
function _connect($str,$user='',$pwd='',$db='') |
|
| |
168 |
|
{ |
|
| |
169 |
|
if ($user || $pwd || $db) { |
|
| |
170 |
|
if ($str) { |
|
| |
171 |
|
$host = split(":", $str); |
|
| |
172 |
|
if ($host[0]) $str = "host=$host[0]"; |
|
| |
173 |
|
else $str = 'localhost'; |
|
| |
174 |
|
if (isset($host[1])) $str .= " port=$host[1]"; |
|
| |
175 |
|
} |
|
| |
176 |
|
if ($user) $str .= " user=".$user; |
|
| |
177 |
|
if ($pwd) $str .= " password=".$pwd; |
|
| |
178 |
|
if ($db) $str .= " dbname=".$db; |
|
| |
179 |
|
} |
|
| |
180 |
|
|
|
| |
181 |
|
|
|
| |
182 |
|
$this->_connectionID = pg_connect($str); |
|
| |
183 |
|
if ($this->_connectionID === false) return false; |
|
| |
184 |
|
$this->Execute("set datestyle='ISO'"); |
|
| |
185 |
|
return true; |
|
| |
186 |
|
} |
|
| |
187 |
|
|
|
| |
188 |
|
|
|
| |
189 |
|
|
|
| |
190 |
|
|
|
| |
191 |
|
|
|
| |
192 |
|
|
|
| |
193 |
|
function _pconnect($str,$user='',$pwd='',$db='') |
|
| |
194 |
|
{ |
|
| |
195 |
|
if ($user || $pwd || $db) { |
|
| |
196 |
|
if ($str) { |
|
| |
197 |
|
$host = split(":", $str); |
|
| |
198 |
|
if ($host[0]) $str = "host=$host[0]"; |
|
| |
199 |
|
else $str = 'localhost'; |
|
| |
200 |
|
if (isset($host[1])) $str .= " port=$host[1]"; |
|
| |
201 |
|
} |
|
| |
202 |
|
if ($user) $str .= " user=".$user; |
|
| |
203 |
|
if ($pwd) $str .= " password=".$pwd; |
|
| |
204 |
|
if ($db) $str .= " dbname=".$db; |
|
| |
205 |
|
} |
|
| |
206 |
|
$this->_connectionID = pg_pconnect($str); |
|
| |
207 |
|
if ($this->_connectionID === false) return false; |
|
| |
208 |
|
$this->Execute("set datestyle='ISO'"); |
|
| |
209 |
|
return true; |
|
| |
210 |
|
} |
|
| |
211 |
|
|
|
| |
212 |
|
|
|
| |
213 |
|
function _query($sql,$inputarr) |
|
| |
214 |
|
{ |
|
| |
215 |
|
$this->_resultid= pg_Exec($this->_connectionID,$sql); |
|
| |
216 |
|
return $this->_resultid; |
|
| |
217 |
|
} |
|
| |
218 |
|
|
|
| |
219 |
|
|
|
| |
220 |
|
|
|
| |
221 |
|
function ErrorMsg() { |
|
| |
222 |
|
$this->_errorMsg = @pg_errormessage($this->_connectionID); |
|
| |
223 |
|
return $this->_errorMsg; |
|
| |
224 |
|
} |
|
| |
225 |
|
|
|
| |
226 |
|
|
|
| |
227 |
|
function _close() |
|
| |
228 |
|
{ |
|
| |
229 |
|
if ($this->_hastrans) $this->RollbackTrans(); |
|
| |
230 |
|
@pg_close($this->_connectionID); |
|
| |
231 |
|
return true; |
|
| |
232 |
|
} |
|
| |
233 |
|
|
|
| |
234 |
|
} |
|
| |
235 |
|
|
|
| |
236 |
|
|
|
| |
237 |
|
|
|
| |
238 |
|
|
|
| |
239 |
|
|
|
| |
240 |
|
class ADORecordSet_postgres extends ADORecordSet{ |
|
| |
241 |
|
|
|
| |
242 |
|
var $databaseType = "postgres"; |
|
| |
243 |
|
var $canSeek = true; |
|
| |
244 |
|
var $fetchMode; |
|
| |
245 |
|
function ADORecordSet_postgres($queryID) { |
|
| |
246 |
|
global $ADODB_FETCH_MODE; |
|
| |
247 |
|
|
|
| |
248 |
|
switch ($ADODB_FETCH_MODE) |
|
| |
249 |
|
{ |
|
| |
250 |
|
case ADODB_FETCH_NUM: $this->fetchMode = PGSQL_NUM; break; |
|
| |
251 |
|
case ADODB_FETCH_ASSOC:$this->fetchMode = PGSQL_ASSOC; break; |
|
| |
252 |
|
default: |
|
| |
253 |
|
case ADODB_FETCH_DEFAULT: |
|
| |
254 |
|
case ADODB_FETCH_BOTH:$this->fetchMode = PGSQL_BOTH; break; |
|
| |
255 |
|
} |
|
| |
256 |
|
|
|
| |
257 |
|
$this->ADORecordSet($queryID); |
|
| |
258 |
|
} |
|
| |
259 |
|
|
|
| |
260 |
|
function _initrs() |
|
| |
261 |
|
{ |
|
| |
262 |
|
global $ADODB_COUNTRECS; |
|
| |
263 |
|
$this->_numOfRows = ($ADODB_COUNTRECS)? @pg_numrows($this->_queryID):-1; |
|
| |
264 |
|
$this->_numOfFields = @pg_numfields($this->_queryID); |
|
| |
265 |
|
} |
|
| |
266 |
|
|
|
| |
267 |
|
function &FetchField($fieldOffset = 0) |
|
| |
268 |
|
{ |
|
| |
269 |
|
$off=$fieldOffset; |
|
| |
270 |
|
|
|
| |
271 |
|
$o= new ADODBFieldObject(); |
|
| |
272 |
|
$o->name = @pg_fieldname($this->_queryID,$off); |
|
| |
273 |
|
$o->type = @pg_fieldtype($this->_queryID,$off); |
|
| |
274 |
|
$o->max_length = @pg_fieldsize($this->_queryID,$off); |
|
| |
275 |
|
|
|
| |
276 |
|
|
|
| |
277 |
|
return $o; |
|
| |
278 |
|
} |
|
| |
279 |
|
|
|
| |
280 |
|
function _seek($row) |
|
| |
281 |
|
{ |
|
| |
282 |
|
return @pg_fetch_row($this->_queryID,$row); |
|
| |
283 |
|
} |
|
| |
284 |
|
|
|
| |
285 |
|
|
|
| |
286 |
|
function MoveNext($ignore_fields=false) |
|
| |
287 |
|
{ |
|
| |
288 |
|
if ($this->_numOfRows != 0 && !$this->EOF) { |
|
| |
289 |
|
$this->_currentRow++; |
|
| |
290 |
|
$this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode); |
|
| |
291 |
|
if (is_array($this->fields)) return true; |
|
| |
292 |
|
} |
|
| |
293 |
|
$this->EOF = true; |
|
| |
294 |
|
return false; |
|
| |
295 |
|
} |
|
| |
296 |
|
function _fetch($ignore_fields=false) |
|
| |
297 |
|
{ |
|
| |
298 |
|
$this->fields = @pg_fetch_array($this->_queryID,$this->_currentRow,$this->fetchMode); |
|
| |
299 |
|
return (is_array($this->fields)); |
|
| |
300 |
|
} |
|
| |
301 |
|
|
|
| |
302 |
|
function _close() { |
|
| |
303 |
|
return @pg_freeresult($this->_queryID); |
|
| |
304 |
|
} |
|
| |
305 |
|
|
|
| |
306 |
|
function MetaType($t,$len=-1,$fieldobj=false) |
|
| |
307 |
|
{ |
|
| |
308 |
|
switch (strtoupper($t)) { |
|
| |
309 |
|
case 'CHAR': |
|
| |
310 |
|
case 'CHARACTER': |
|
| |
311 |
|
case 'VARCHAR': |
|
| |
312 |
|
case 'NAME': |
|
| |
313 |
|
case 'BPCHAR': |
|
| |
314 |
|
if ($len <= $this->blobSize) return 'C'; |
|
| |
315 |
|
|
|
| |
316 |
|
case 'TEXT': |
|
| |
317 |
|
return 'X'; |
|
| |
318 |
|
|
|
| |
319 |
|
case 'IMAGE': |
|
| |
320 |
|
case 'BLOB': |
|
| |
321 |
|
case 'BIT': |
|
| |
322 |
|
case 'VARBIT': |
|
| |
323 |
|
case 'BYTEA': |
|
| |
324 |
|
return 'B'; |
|
| |
325 |
|
|
|
| |
326 |
|
case 'BOOL': |
|
| |
327 |
|
case 'BOOLEAN': |
|
| |
328 |
|
return 'L'; |
|
| |
329 |
|
|
|
| |
330 |
|
case 'DATE': |
|
| |
331 |
|
return 'D'; |
|
| |
332 |
|
|
|
| |
333 |
|
case 'TIME': |
|
| |
334 |
|
case 'DATETIME': |
|
| |
335 |
|
case 'TIMESTAMP': |
|
| |
336 |
|
return 'T'; |
|
| |
337 |
|
|
|
| |
338 |
|
case 'SMALLINT': |
|
| |
339 |
|
case 'BIGINT': |
|
| |
340 |
|
case 'INTEGER': |
|
| |
341 |
|
case 'INT8': |
|
| |
342 |
|
case 'INT4': |
|
| |
343 |
|
case 'INT2': |
|
| |
344 |
|
if (isset($fieldobj) && |
|
| |
345 |
|
empty($fieldobj->primary_key) && empty($fieldobj->unique)) return 'I'; |
|
| |
346 |
|
|
|
| |
347 |
|
case 'OID': |
|
| |
348 |
|
case 'SERIAL': |
|
| |
349 |
|
return 'R'; |
|
| |
350 |
|
|
|
| |
351 |
|
default: |
|
| |
352 |
|
return 'N'; |
|
| |
353 |
|
} |
|
| |
354 |
|
} |
|
| |
355 |
|
|
|
| |
356 |
|
} |
|
| |
357 |
|
?> |