3 @version v5.20.1 06-Dec-2015
4 @copyright (c) 2000-2013 John Lim. All rights reserved.
5 @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
6 Released under both BSD license and Lesser GPL library license.
7 Whenever there is any discrepancy between the two licenses,
8 the BSD license will take precedence.
9 Set tabs to 4 for best viewing.
11 Latest version is available at http://adodb.sourceforge.net
13 Informix port by Mitchell T. Young (mitch@youngfamily.org)
15 Further mods by "Samuel CARRIERE" <samuel_carriere@hotmail.com>
19 // security - hide paths
20 if (!defined('ADODB_DIR')) die();
22 if (!defined('IFX_SCROLL')) define('IFX_SCROLL',1);
24 class ADODB_informix72
extends ADOConnection
{
25 var $databaseType = "informix72";
26 var $dataProvider = "informix";
27 var $replaceQuote = "''"; // string to use to replace quotes
28 var $fmtDate = "'Y-m-d'";
29 var $fmtTimeStamp = "'Y-m-d H:i:s'";
30 var $hasInsertID = true;
31 var $hasAffectedRows = true;
32 var $substr = 'substr';
33 var $metaTablesSQL="select tabname,tabtype from systables where tabtype in ('T','V') and owner!='informix'"; //Don't get informix tables and pseudo-tables
37 "select c.colname, c.coltype, c.collength, d.default,c.colno
38 from syscolumns c, systables t,outer sysdefaults d
39 where c.tabid=t.tabid and d.tabid=t.tabid and d.colno=c.colno
40 and tabname='%s' order by c.colno";
42 var $metaPrimaryKeySQL =
43 "select part1,part2,part3,part4,part5,part6,part7,part8 from
44 systables t,sysconstraints s,sysindexes i where t.tabname='%s'
45 and s.tabid=t.tabid and s.constrtype='P'
46 and i.idxname=s.idxname";
48 var $concat_operator = '||';
50 var $lastQuery = false;
51 var $has_insertid = true;
53 var $_autocommit = true;
54 var $_bindInputArray = true; // set to true if ADOConnection.Execute() permits binding of array parameters.
55 var $sysDate = 'TODAY';
56 var $sysTimeStamp = 'CURRENT';
57 var $cursorType = IFX_SCROLL
; // IFX_SCROLL or IFX_HOLD or 0
59 function __construct()
61 // alternatively, use older method:
62 //putenv("DBDATE=Y4MD-");
64 // force ISO date format
65 putenv('GL_DATE=%Y-%m-%d');
67 if (function_exists('ifx_byteasvarchar')) {
68 ifx_byteasvarchar(1); // Mode "0" will return a blob id, and mode "1" will return a varchar with text content.
69 ifx_textasvarchar(1); // Mode "0" will return a blob id, and mode "1" will return a varchar with text content.
70 ifx_blobinfile_mode(0); // Mode "0" means save Byte-Blobs in memory, and mode "1" means save Byte-Blobs in a file.
76 if (isset($this->version
)) return $this->version
;
78 $arr['description'] = $this->GetOne("select DBINFO('version','full') from systables where tabid = 1");
79 $arr['version'] = $this->GetOne("select DBINFO('version','major') || DBINFO('version','minor') from systables where tabid = 1");
80 $this->version
= $arr;
88 $sqlca =ifx_getsqlca($this->lastQuery
);
89 return @$sqlca["sqlerrd1"];
92 function _affectedrows()
94 if ($this->lastQuery
) {
95 return @ifx_affected_rows
($this->lastQuery
);
100 function BeginTrans()
102 if ($this->transOff
) return true;
103 $this->transCnt +
= 1;
104 $this->Execute('BEGIN');
105 $this->_autocommit
= false;
109 function CommitTrans($ok=true)
111 if (!$ok) return $this->RollbackTrans();
112 if ($this->transOff
) return true;
113 if ($this->transCnt
) $this->transCnt
-= 1;
114 $this->Execute('COMMIT');
115 $this->_autocommit
= true;
119 function RollbackTrans()
121 if ($this->transOff
) return true;
122 if ($this->transCnt
) $this->transCnt
-= 1;
123 $this->Execute('ROLLBACK');
124 $this->_autocommit
= true;
128 function RowLock($tables,$where,$col='1 as adodbignore')
130 if ($this->_autocommit
) $this->BeginTrans();
131 return $this->GetOne("select $col from $tables where $where for update");
134 /* Returns: the last error message from previous database operation
135 Note: This function is NOT available for Microsoft SQL Server. */
139 if (!empty($this->_logsql
)) return $this->_errorMsg
;
140 $this->_errorMsg
= ifx_errormsg();
141 return $this->_errorMsg
;
146 preg_match("/.*SQLCODE=([^\]]*)/",ifx_error(),$parse);
147 if (is_array($parse) && isset($parse[1])) return (int)$parse[1];
152 function MetaProcedures($NamePattern = false, $catalog = null, $schemaPattern = null)
154 // save old fetch mode
155 global $ADODB_FETCH_MODE;
158 $save = $ADODB_FETCH_MODE;
159 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
160 if ($this->fetchMode
!== FALSE) {
161 $savem = $this->SetFetchMode(FALSE);
164 $procedures = array ();
170 $likepattern = " WHERE procname LIKE '".$NamePattern."'";
173 $rs = $this->Execute('SELECT procname, isproc FROM sysprocedures'.$likepattern);
175 if (is_object($rs)) {
176 // parse index data into array
178 while ($row = $rs->FetchRow()) {
179 $procedures[$row[0]] = array(
180 'type' => ($row[1] == 'f' ?
'FUNCTION' : 'PROCEDURE'),
190 $this->SetFetchMode($savem);
192 $ADODB_FETCH_MODE = $save;
197 function MetaColumns($table, $normalize=true)
199 global $ADODB_FETCH_MODE;
202 if (!empty($this->metaColumnsSQL
)) {
203 $save = $ADODB_FETCH_MODE;
204 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
205 if ($this->fetchMode
!== false) $savem = $this->SetFetchMode(false);
206 $rs = $this->Execute(sprintf($this->metaColumnsSQL
,$table));
207 if (isset($savem)) $this->SetFetchMode($savem);
208 $ADODB_FETCH_MODE = $save;
209 if ($rs === false) return $false;
210 $rspkey = $this->Execute(sprintf($this->metaPrimaryKeySQL
,$table)); //Added to get primary key colno items
213 while (!$rs->EOF
) { //print_r($rs->fields);
214 $fld = new ADOFieldObject();
215 $fld->name
= $rs->fields
[0];
217 $rs->fields[1] is not the correct adodb type
218 $rs->fields[2] is not correct max_length, because can include not-null bit
220 $fld->type = $rs->fields[1];
221 $fld->primary_key=$rspkey->fields && array_search($rs->fields[4],$rspkey->fields); //Added to set primary key flag
222 $fld->max_length = $rs->fields[2];*/
223 $pr=ifx_props($rs->fields
[1],$rs->fields
[2]); //!eos
224 $fld->type
= $pr[0] ;//!eos
225 $fld->primary_key
=$rspkey->fields
&& array_search($rs->fields
[4],$rspkey->fields
);
226 $fld->max_length
= $pr[1]; //!eos
227 $fld->precision
= $pr[2] ;//!eos
228 $fld->not_null
= $pr[3]=="N"; //!eos
230 if (trim($rs->fields
[3]) != "AAAAAA 0") {
231 $fld->has_default
= 1;
232 $fld->default_value
= $rs->fields
[3];
234 $fld->has_default
= 0;
237 $retarr[strtolower($fld->name
)] = $fld;
242 $rspkey->Close(); //!eos
249 function xMetaColumns($table)
251 return ADOConnection
::MetaColumns($table,false);
254 function MetaForeignKeys($table, $owner=false, $upper=false) //!Eos
257 select tr.tabname,updrule,delrule,
258 i.part1 o1,i2.part1 d1,i.part2 o2,i2.part2 d2,i.part3 o3,i2.part3 d3,i.part4 o4,i2.part4 d4,
259 i.part5 o5,i2.part5 d5,i.part6 o6,i2.part6 d6,i.part7 o7,i2.part7 d7,i.part8 o8,i2.part8 d8
260 from systables t,sysconstraints s,sysindexes i,
261 sysreferences r,systables tr,sysconstraints s2,sysindexes i2
262 where t.tabname='$table'
263 and s.tabid=t.tabid and s.constrtype='R' and r.constrid=s.constrid
264 and i.idxname=s.idxname and tr.tabid=r.ptabid
265 and s2.constrid=r.primary and i2.idxname=s2.idxname";
267 $rs = $this->Execute($sql);
268 if (!$rs ||
$rs->EOF
) return false;
269 $arr = $rs->GetArray();
271 foreach($arr as $v) {
272 $coldest=$this->metaColumnNames($v["tabname"]);
273 $colorig=$this->metaColumnNames($table);
275 for($i=1;$i<=8 && $v["o$i"] ;$i++
) {
276 $colnames[]=$coldest[$v["d$i"]-1]."=".$colorig[$v["o$i"]-1];
279 $a[strtoupper($v["tabname"])] = $colnames;
281 $a[$v["tabname"]] = $colnames;
286 function UpdateBlob($table, $column, $val, $where, $blobtype = 'BLOB')
288 $type = ($blobtype == 'TEXT') ?
1 : 0;
289 $blobid = ifx_create_blob($type,0,$val);
290 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blobid));
293 function BlobDecode($blobid)
295 return function_exists('ifx_byteasvarchar') ?
$blobid : @ifx_get_blob
($blobid);
298 // returns true or false
299 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
301 if (!function_exists('ifx_connect')) return null;
303 $dbs = $argDatabasename . "@" . $argHostname;
304 if ($argHostname) putenv("INFORMIXSERVER=$argHostname");
305 putenv("INFORMIXSERVER=".trim($argHostname));
306 $this->_connectionID
= ifx_connect($dbs,$argUsername,$argPassword);
307 if ($this->_connectionID
=== false) return false;
308 #if ($argDatabasename) return $this->SelectDB($argDatabasename);
312 // returns true or false
313 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
315 if (!function_exists('ifx_connect')) return null;
317 $dbs = $argDatabasename . "@" . $argHostname;
318 putenv("INFORMIXSERVER=".trim($argHostname));
319 $this->_connectionID
= ifx_pconnect($dbs,$argUsername,$argPassword);
320 if ($this->_connectionID
=== false) return false;
321 #if ($argDatabasename) return $this->SelectDB($argDatabasename);
325 // ifx_do does not accept bind parameters - weird ???
326 function Prepare($sql)
328 $stmt = ifx_prepare($sql);
329 if (!$stmt) return $sql;
330 else return array($sql,$stmt);
333 // returns query ID if successful, otherwise false
334 function _query($sql,$inputarr=false)
336 global $ADODB_COUNTRECS;
338 // String parameters have to be converted using ifx_create_char
340 foreach($inputarr as $v) {
341 if (gettype($v) == 'string') {
342 $tab[] = ifx_create_char($v);
350 // In case of select statement, we use a scroll cursor in order
351 // to be able to call "move", or "movefirst" statements
352 if (!$ADODB_COUNTRECS && preg_match("/^\s*select/is", $sql)) {
354 $this->lastQuery
= ifx_query($sql,$this->_connectionID
, $this->cursorType
, $tab);
357 $this->lastQuery
= ifx_query($sql,$this->_connectionID
, $this->cursorType
);
362 $this->lastQuery
= ifx_query($sql,$this->_connectionID
, $tab);
365 $this->lastQuery
= ifx_query($sql,$this->_connectionID
);
369 // Following line have been commented because autocommit mode is
370 // not supported by informix SE 7.2
372 //if ($this->_autocommit) ifx_query('COMMIT',$this->_connectionID);
374 return $this->lastQuery
;
377 // returns true or false
380 $this->lastQuery
= false;
381 return ifx_close($this->_connectionID
);
386 /*--------------------------------------------------------------------------------------
387 Class Name: Recordset
388 --------------------------------------------------------------------------------------*/
390 class ADORecordset_informix72
extends ADORecordSet
{
392 var $databaseType = "informix72";
394 var $_fieldprops = false;
396 function __construct($id,$mode=false)
398 if ($mode === false) {
399 global $ADODB_FETCH_MODE;
400 $mode = $ADODB_FETCH_MODE;
402 $this->fetchMode
= $mode;
403 return parent
::__construct($id);
408 /* Returns: an object containing field information.
409 Get column information in the Recordset object. fetchField() can be used in order to obtain information about
410 fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
411 fetchField() is retrieved. */
412 function FetchField($fieldOffset = -1)
414 if (empty($this->_fieldprops
)) {
415 $fp = ifx_fieldproperties($this->_queryID
);
416 foreach($fp as $k => $v) {
417 $o = new ADOFieldObject
;
419 $arr = explode(';',$v); //"SQLTYPE;length;precision;scale;ISNULLABLE"
421 $o->max_length
= $arr[1];
422 $this->_fieldprops
[] = $o;
423 $o->not_null
= $arr[4]=="N";
426 $ret = $this->_fieldprops
[$fieldOffset];
432 $this->_numOfRows
= -1; // ifx_affected_rows not reliable, only returns estimate -- ($ADODB_COUNTRECS)? ifx_affected_rows($this->_queryID):-1;
433 $this->_numOfFields
= ifx_num_fields($this->_queryID
);
438 return @ifx_fetch_row
($this->_queryID
, (int) $row);
443 $this->fields
= @ifx_fetch_row
($this->_queryID
, "LAST");
444 if ($this->fields
) $this->EOF
= false;
445 $this->_currentRow
= -1;
447 if ($this->fetchMode
== ADODB_FETCH_NUM
) {
448 foreach($this->fields
as $v) {
451 $this->fields
= $arr;
459 $this->fields
= @ifx_fetch_row
($this->_queryID
, "FIRST");
460 if ($this->fields
) $this->EOF
= false;
461 $this->_currentRow
= 0;
463 if ($this->fetchMode
== ADODB_FETCH_NUM
) {
464 foreach($this->fields
as $v) {
467 $this->fields
= $arr;
473 function _fetch($ignore_fields=false)
476 $this->fields
= @ifx_fetch_row
($this->_queryID
);
478 if (!is_array($this->fields
)) return false;
480 if ($this->fetchMode
== ADODB_FETCH_NUM
) {
481 foreach($this->fields
as $v) {
484 $this->fields
= $arr;
489 /* close() only needs to be called if you are worried about using too much memory while your script
490 is running. All associated result memory for the specified result identifier will automatically be freed. */
493 return ifx_free_result($this->_queryID
);
498 * Auxiliar function to Parse coltype,collength. Used by Metacolumns
499 * return: array ($mtype,$length,$precision,$nullable) (similar to ifx_fieldpropierties)
501 function ifx_props($coltype,$collength){
502 $itype=fmod($coltype+
1,256);
503 $nullable=floor(($coltype+
1) /256) ?
"N":"Y";
504 $mtype=substr(" CIIFFNNDN TBXCC ",$itype,1);
511 $length=floor($collength/256);
512 $precision=fmod($collength,256);
518 return array($mtype,$length,$precision,$nullable);