3 V4.94 23 Jan 2007 (c) 2000-2007 John Lim. 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 for best viewing.
9 Latest version is available at http://adodb.sourceforge.net
11 Informix port by Mitchell T. Young (mitch@youngfamily.org)
13 Further mods by "Samuel CARRIERE" <samuel_carriere@hotmail.com>
17 // security - hide paths
18 if (!defined('ADODB_DIR')) die();
20 if (!defined('IFX_SCROLL')) define('IFX_SCROLL',1);
22 class ADODB_informix72
extends ADOConnection
{
23 var $databaseType = "informix72";
24 var $dataProvider = "informix";
25 var $replaceQuote = "''"; // string to use to replace quotes
26 var $fmtDate = "'Y-m-d'";
27 var $fmtTimeStamp = "'Y-m-d H:i:s'";
28 var $hasInsertID = true;
29 var $hasAffectedRows = true;
30 var $substr = 'substr';
31 var $metaTablesSQL="select tabname,tabtype from systables where tabtype in ('T','V') and owner!='informix'"; //Don't get informix tables and pseudo-tables
35 "select c.colname, c.coltype, c.collength, d.default,c.colno
36 from syscolumns c, systables t,outer sysdefaults d
37 where c.tabid=t.tabid and d.tabid=t.tabid and d.colno=c.colno
38 and tabname='%s' order by c.colno";
40 var $metaPrimaryKeySQL =
41 "select part1,part2,part3,part4,part5,part6,part7,part8 from
42 systables t,sysconstraints s,sysindexes i where t.tabname='%s'
43 and s.tabid=t.tabid and s.constrtype='P'
44 and i.idxname=s.idxname";
46 var $concat_operator = '||';
48 var $lastQuery = false;
49 var $has_insertid = true;
51 var $_autocommit = true;
52 var $_bindInputArray = true; // set to true if ADOConnection.Execute() permits binding of array parameters.
53 var $sysDate = 'TODAY';
54 var $sysTimeStamp = 'CURRENT';
55 var $cursorType = IFX_SCROLL
; // IFX_SCROLL or IFX_HOLD or 0
57 function ADODB_informix72()
59 // alternatively, use older method:
60 //putenv("DBDATE=Y4MD-");
62 // force ISO date format
63 putenv('GL_DATE=%Y-%m-%d');
65 if (function_exists('ifx_byteasvarchar')) {
66 ifx_byteasvarchar(1); // Mode "0" will return a blob id, and mode "1" will return a varchar with text content.
67 ifx_textasvarchar(1); // Mode "0" will return a blob id, and mode "1" will return a varchar with text content.
68 ifx_blobinfile_mode(0); // Mode "0" means save Byte-Blobs in memory, and mode "1" means save Byte-Blobs in a file.
74 if (isset($this->version
)) return $this->version
;
76 $arr['description'] = $this->GetOne("select DBINFO('version','full') from systables where tabid = 1");
77 $arr['version'] = $this->GetOne("select DBINFO('version','major') || DBINFO('version','minor') from systables where tabid = 1");
78 $this->version
= $arr;
86 $sqlca =ifx_getsqlca($this->lastQuery
);
87 return @$sqlca["sqlerrd1"];
90 function _affectedrows()
92 if ($this->lastQuery
) {
93 return @ifx_affected_rows
($this->lastQuery
);
100 if ($this->transOff
) return true;
101 $this->transCnt +
= 1;
102 $this->Execute('BEGIN');
103 $this->_autocommit
= false;
107 function CommitTrans($ok=true)
109 if (!$ok) return $this->RollbackTrans();
110 if ($this->transOff
) return true;
111 if ($this->transCnt
) $this->transCnt
-= 1;
112 $this->Execute('COMMIT');
113 $this->_autocommit
= true;
117 function RollbackTrans()
119 if ($this->transOff
) return true;
120 if ($this->transCnt
) $this->transCnt
-= 1;
121 $this->Execute('ROLLBACK');
122 $this->_autocommit
= true;
126 function RowLock($tables,$where,$flds='1 as ignore')
128 if ($this->_autocommit
) $this->BeginTrans();
129 return $this->GetOne("select $flds from $tables where $where for update");
132 /* Returns: the last error message from previous database operation
133 Note: This function is NOT available for Microsoft SQL Server. */
137 if (!empty($this->_logsql
)) return $this->_errorMsg
;
138 $this->_errorMsg
= ifx_errormsg();
139 return $this->_errorMsg
;
144 preg_match("/.*SQLCODE=([^\]]*)/",ifx_error(),$parse);
145 if (is_array($parse) && isset($parse[1])) return (int)$parse[1];
150 function &MetaColumns($table)
152 global $ADODB_FETCH_MODE;
155 if (!empty($this->metaColumnsSQL
)) {
156 $save = $ADODB_FETCH_MODE;
157 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
158 if ($this->fetchMode
!== false) $savem = $this->SetFetchMode(false);
159 $rs = $this->Execute(sprintf($this->metaColumnsSQL
,$table));
160 if (isset($savem)) $this->SetFetchMode($savem);
161 $ADODB_FETCH_MODE = $save;
162 if ($rs === false) return $false;
163 $rspkey = $this->Execute(sprintf($this->metaPrimaryKeySQL
,$table)); //Added to get primary key colno items
166 while (!$rs->EOF
) { //print_r($rs->fields);
167 $fld = new ADOFieldObject();
168 $fld->name
= $rs->fields
[0];
170 $rs->fields[1] is not the correct adodb type
171 $rs->fields[2] is not correct max_length, because can include not-null bit
173 $fld->type = $rs->fields[1];
174 $fld->primary_key=$rspkey->fields && array_search($rs->fields[4],$rspkey->fields); //Added to set primary key flag
175 $fld->max_length = $rs->fields[2];*/
176 $pr=ifx_props($rs->fields
[1],$rs->fields
[2]); //!eos
177 $fld->type
= $pr[0] ;//!eos
178 $fld->primary_key
=$rspkey->fields
&& array_search($rs->fields
[4],$rspkey->fields
);
179 $fld->max_length
= $pr[1]; //!eos
180 $fld->precision
= $pr[2] ;//!eos
181 $fld->not_null
= $pr[3]=="N"; //!eos
183 if (trim($rs->fields
[3]) != "AAAAAA 0") {
184 $fld->has_default
= 1;
185 $fld->default_value
= $rs->fields
[3];
187 $fld->has_default
= 0;
190 $retarr[strtolower($fld->name
)] = $fld;
195 $rspkey->Close(); //!eos
202 function &xMetaColumns($table)
204 return ADOConnection
::MetaColumns($table,false);
207 function MetaForeignKeys($table, $owner=false, $upper=false) //!Eos
210 select tr.tabname,updrule,delrule,
211 i.part1 o1,i2.part1 d1,i.part2 o2,i2.part2 d2,i.part3 o3,i2.part3 d3,i.part4 o4,i2.part4 d4,
212 i.part5 o5,i2.part5 d5,i.part6 o6,i2.part6 d6,i.part7 o7,i2.part7 d7,i.part8 o8,i2.part8 d8
213 from systables t,sysconstraints s,sysindexes i,
214 sysreferences r,systables tr,sysconstraints s2,sysindexes i2
215 where t.tabname='$table'
216 and s.tabid=t.tabid and s.constrtype='R' and r.constrid=s.constrid
217 and i.idxname=s.idxname and tr.tabid=r.ptabid
218 and s2.constrid=r.primary and i2.idxname=s2.idxname";
220 $rs = $this->Execute($sql);
221 if (!$rs ||
$rs->EOF
) return false;
222 $arr =& $rs->GetArray();
224 foreach($arr as $v) {
225 $coldest=$this->metaColumnNames($v["tabname"]);
226 $colorig=$this->metaColumnNames($table);
228 for($i=1;$i<=8 && $v["o$i"] ;$i++
) {
229 $colnames[]=$coldest[$v["d$i"]-1]."=".$colorig[$v["o$i"]-1];
232 $a[strtoupper($v["tabname"])] = $colnames;
234 $a[$v["tabname"]] = $colnames;
239 function UpdateBlob($table, $column, $val, $where, $blobtype = 'BLOB')
241 $type = ($blobtype == 'TEXT') ?
1 : 0;
242 $blobid = ifx_create_blob($type,0,$val);
243 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blobid));
246 function BlobDecode($blobid)
248 return function_exists('ifx_byteasvarchar') ?
$blobid : @ifx_get_blob
($blobid);
251 // returns true or false
252 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
254 if (!function_exists('ifx_connect')) return null;
256 $dbs = $argDatabasename . "@" . $argHostname;
257 if ($argHostname) putenv("INFORMIXSERVER=$argHostname");
258 putenv("INFORMIXSERVER=".trim($argHostname));
259 $this->_connectionID
= ifx_connect($dbs,$argUsername,$argPassword);
260 if ($this->_connectionID
=== false) return false;
261 #if ($argDatabasename) return $this->SelectDB($argDatabasename);
265 // returns true or false
266 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
268 if (!function_exists('ifx_connect')) return null;
270 $dbs = $argDatabasename . "@" . $argHostname;
271 putenv("INFORMIXSERVER=".trim($argHostname));
272 $this->_connectionID
= ifx_pconnect($dbs,$argUsername,$argPassword);
273 if ($this->_connectionID
=== false) return false;
274 #if ($argDatabasename) return $this->SelectDB($argDatabasename);
278 // ifx_do does not accept bind parameters - weird ???
279 function Prepare($sql)
281 $stmt = ifx_prepare($sql);
282 if (!$stmt) return $sql;
283 else return array($sql,$stmt);
286 // returns query ID if successful, otherwise false
287 function _query($sql,$inputarr)
289 global $ADODB_COUNTRECS;
291 // String parameters have to be converted using ifx_create_char
293 foreach($inputarr as $v) {
294 if (gettype($v) == 'string') {
295 $tab[] = ifx_create_char($v);
303 // In case of select statement, we use a scroll cursor in order
304 // to be able to call "move", or "movefirst" statements
305 if (!$ADODB_COUNTRECS && preg_match("/^\s*select/is", $sql)) {
307 $this->lastQuery
= ifx_query($sql,$this->_connectionID
, $this->cursorType
, $tab);
310 $this->lastQuery
= ifx_query($sql,$this->_connectionID
, $this->cursorType
);
315 $this->lastQuery
= ifx_query($sql,$this->_connectionID
, $tab);
318 $this->lastQuery
= ifx_query($sql,$this->_connectionID
);
322 // Following line have been commented because autocommit mode is
323 // not supported by informix SE 7.2
325 //if ($this->_autocommit) ifx_query('COMMIT',$this->_connectionID);
327 return $this->lastQuery
;
330 // returns true or false
333 $this->lastQuery
= false;
334 return ifx_close($this->_connectionID
);
339 /*--------------------------------------------------------------------------------------
340 Class Name: Recordset
341 --------------------------------------------------------------------------------------*/
343 class ADORecordset_informix72
extends ADORecordSet
{
345 var $databaseType = "informix72";
347 var $_fieldprops = false;
349 function ADORecordset_informix72($id,$mode=false)
351 if ($mode === false) {
352 global $ADODB_FETCH_MODE;
353 $mode = $ADODB_FETCH_MODE;
355 $this->fetchMode
= $mode;
356 return $this->ADORecordSet($id);
361 /* Returns: an object containing field information.
362 Get column information in the Recordset object. fetchField() can be used in order to obtain information about
363 fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
364 fetchField() is retrieved. */
365 function &FetchField($fieldOffset = -1)
367 if (empty($this->_fieldprops
)) {
368 $fp = ifx_fieldproperties($this->_queryID
);
369 foreach($fp as $k => $v) {
370 $o = new ADOFieldObject
;
372 $arr = split(';',$v); //"SQLTYPE;length;precision;scale;ISNULLABLE"
374 $o->max_length
= $arr[1];
375 $this->_fieldprops
[] = $o;
376 $o->not_null
= $arr[4]=="N";
379 $ret = $this->_fieldprops
[$fieldOffset];
385 $this->_numOfRows
= -1; // ifx_affected_rows not reliable, only returns estimate -- ($ADODB_COUNTRECS)? ifx_affected_rows($this->_queryID):-1;
386 $this->_numOfFields
= ifx_num_fields($this->_queryID
);
391 return @ifx_fetch_row
($this->_queryID
, (int) $row);
396 $this->fields
= @ifx_fetch_row
($this->_queryID
, "LAST");
397 if ($this->fields
) $this->EOF
= false;
398 $this->_currentRow
= -1;
400 if ($this->fetchMode
== ADODB_FETCH_NUM
) {
401 foreach($this->fields
as $v) {
404 $this->fields
= $arr;
412 $this->fields
= @ifx_fetch_row
($this->_queryID
, "FIRST");
413 if ($this->fields
) $this->EOF
= false;
414 $this->_currentRow
= 0;
416 if ($this->fetchMode
== ADODB_FETCH_NUM
) {
417 foreach($this->fields
as $v) {
420 $this->fields
= $arr;
426 function _fetch($ignore_fields=false)
429 $this->fields
= @ifx_fetch_row
($this->_queryID
);
431 if (!is_array($this->fields
)) return false;
433 if ($this->fetchMode
== ADODB_FETCH_NUM
) {
434 foreach($this->fields
as $v) {
437 $this->fields
= $arr;
442 /* close() only needs to be called if you are worried about using too much memory while your script
443 is running. All associated result memory for the specified result identifier will automatically be freed. */
446 return ifx_free_result($this->_queryID
);
451 * Auxiliar function to Parse coltype,collength. Used by Metacolumns
452 * return: array ($mtype,$length,$precision,$nullable) (similar to ifx_fieldpropierties)
454 function ifx_props($coltype,$collength){
455 $itype=fmod($coltype+
1,256);
456 $nullable=floor(($coltype+
1) /256) ?
"N":"Y";
457 $mtype=substr(" CIIFFNNDN TBXCC ",$itype,1);
464 $length=floor($collength/256);
465 $precision=fmod($collength,256);
471 return array($mtype,$length,$precision,$nullable);