3 V4.22 15 Apr 2004 (c) 2000-2004 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://php.weblogs.com/
11 Informix port by Mitchell T. Young (mitch@youngfamily.org)
13 Further mods by "Samuel CARRIERE" <samuel_carriere@hotmail.com>
17 if (!defined('IFX_SCROLL')) define('IFX_SCROLL', 1);
19 class ADODB_informix72 extends ADOConnection
21 var $databaseType = "informix72";
22 var $dataProvider = "informix";
23 var $replaceQuote = "''"; // string to use to replace quotes
24 var $fmtDate = "'Y-m-d'";
25 var $fmtTimeStamp = "'Y-m-d H:i:s'";
26 var $hasInsertID = true;
27 var $hasAffectedRows = true;
28 var $upperCase = 'upper';
29 var $substr = 'substr';
30 var $metaTablesSQL = "select tabname from systables";
32 "select c.colname, c.coltype, c.collength, d.default
33 from syscolumns c, systables t,sysdefaults d
34 where c.tabid=t.tabid and d.tabid=t.tabid and d.colno=c.colno and tabname='%s'";
36 // var $metaColumnsSQL = "select colname, coltype, collength from syscolumns c, systables t where c.tabid=t.tabid and tabname='%s'";
37 var $concat_operator = '||';
39 var $lastQuery = false;
40 var $has_insertid = true;
42 var $_autocommit = true;
43 var $_bindInputArray = true; // set to true if ADOConnection.Execute() permits binding of array parameters.
44 var $sysDate = 'TODAY';
45 var $sysTimeStamp = 'CURRENT';
46 var $cursorType = IFX_SCROLL; // IFX_SCROLL or IFX_HOLD or 0
48 function ADODB_informix72()
50 // alternatively, use older method:
51 //putenv("DBDATE=Y4MD-");
53 // force ISO date format
54 putenv('GL_DATE=%Y-%m-%d');
56 if (function_exists('ifx_byteasvarchar')) {
57 ifx_byteasvarchar(1); // Mode "0" will return a blob id, and mode "1" will return a varchar with text content.
58 ifx_textasvarchar(1); // Mode "0" will return a blob id, and mode "1" will return a varchar with text content.
59 ifx_blobinfile_mode(0); // Mode "0" means save Byte-Blobs in memory, and mode "1" means save Byte-Blobs in a file.
65 if (isset($this->version)) return $this->version;
67 $arr['description'] = $this->GetOne("select DBINFO('version','full') from systables where tabid = 1");
68 $arr['version'] = $this->GetOne("select DBINFO('version','major')||" . "||DBINFO('version','minor') from systables where tabid = 1");
69 $this->version = $arr;
75 $sqlca = ifx_getsqlca($this->lastQuery);
76 return @$sqlca["sqlerrd1"];
79 function _affectedrows()
81 if ($this->lastQuery) {
82 return @ifx_affected_rows($this->lastQuery);
89 if ($this->transOff) return true;
91 $this->Execute('BEGIN');
92 $this->_autocommit = false;
96 function CommitTrans($ok = true)
98 if (!$ok) return $this->RollbackTrans();
99 if ($this->transOff) return true;
100 if ($this->transCnt) $this->transCnt -= 1;
101 $this->Execute('COMMIT');
102 $this->_autocommit = true;
106 function RollbackTrans()
108 if ($this->transOff) return true;
109 if ($this->transCnt) $this->transCnt -= 1;
110 $this->Execute('ROLLBACK');
111 $this->_autocommit = true;
115 function RowLock($tables, $where)
117 if ($this->_autocommit) $this->BeginTrans();
118 return $this->GetOne("select 1 as ignore from $tables where $where for update");
121 /* Returns: the last error message from previous database operation
122 Note: This function is NOT available for Microsoft SQL Server. */
126 if (!empty($this->_logsql)) return $this->_errorMsg;
127 $this->_errorMsg = ifx_errormsg();
128 return $this->_errorMsg;
133 preg_match("/.*SQLCODE=([^\]]*)/", ifx_error(), $parse); //!EOS
134 if (is_array($parse) && isset($parse[1])) return (int)$parse[1];
138 function &MetaColumns($table)
140 global $ADODB_FETCH_MODE;
142 if (!empty($this->metaColumnsSQL)) {
143 $save = $ADODB_FETCH_MODE;
144 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
145 if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
146 $rs = $this->Execute(sprintf($this->metaColumnsSQL, $table));
147 if (isset($savem)) $this->SetFetchMode($savem);
148 $ADODB_FETCH_MODE = $save;
149 if ($rs === false) return false;
152 while (!$rs->EOF) { //print_r($rs->fields);
153 $fld = new ADOFieldObject();
154 $fld->name = $rs->fields[0];
155 $fld->type = $rs->fields[1];
156 $fld->max_length = $rs->fields[2];
157 if (trim($rs->fields[3]) != "AAAAAA 0") {
158 $fld->has_default = 1;
159 $fld->default_value = $rs->fields[3];
161 $fld->has_default = 0;
164 $retarr[strtolower($fld->name)] = $fld;
175 function &xMetaColumns($table)
177 return ADOConnection::MetaColumns($table, false);
180 function UpdateBlob($table, $column, $val, $where, $blobtype = 'BLOB')
182 $type = ($blobtype == 'TEXT') ? 1 : 0;
183 $blobid = ifx_create_blob($type, 0, $val);
184 return $this->Execute("UPDATE $table SET $column=(?) WHERE $where", array($blobid));
187 function BlobDecode($blobid)
189 return function_exists('ifx_byteasvarchar') ? $blobid : @ifx_get_blob($blobid);
192 // returns true or false
193 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
195 if (!function_exists('ifx_connect')) return false;
197 $dbs = $argDatabasename . "@" . $argHostname;
198 if ($argHostname) putenv("INFORMIXSERVER=$argHostname");
199 putenv("INFORMIXSERVER=" . trim($argHostname));
200 $this->_connectionID = ifx_connect($dbs, $argUsername, $argPassword);
201 if ($this->_connectionID === false) return false;
202 #if ($argDatabasename) return $this->SelectDB($argDatabasename);
206 // returns true or false
207 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
209 if (!function_exists('ifx_connect')) return false;
211 $dbs = $argDatabasename . "@" . $argHostname;
212 putenv("INFORMIXSERVER=" . trim($argHostname));
213 $this->_connectionID = ifx_pconnect($dbs, $argUsername, $argPassword);
214 if ($this->_connectionID === false) return false;
215 #if ($argDatabasename) return $this->SelectDB($argDatabasename);
220 // ifx_do does not accept bind parameters - weird ???
221 function Prepare($sql)
223 $stmt = ifx_prepare($sql);
224 if (!$stmt) return $sql;
225 else return array($sql,$stmt);
228 // returns query ID if successful, otherwise false
229 function _query($sql, $inputarr)
231 global $ADODB_COUNTRECS;
233 // String parameters have to be converted using ifx_create_char
235 foreach ($inputarr as $v) {
236 if (gettype($v) == 'string') {
237 $tab[] = ifx_create_char($v);
244 // In case of select statement, we use a scroll cursor in order
245 // to be able to call "move", or "movefirst" statements
246 if (!$ADODB_COUNTRECS && preg_match("/^\s*select/is", $sql)) {
248 $this->lastQuery = ifx_query($sql, $this->_connectionID, $this->cursorType, $tab);
250 $this->lastQuery = ifx_query($sql, $this->_connectionID, $this->cursorType);
254 $this->lastQuery = ifx_query($sql, $this->_connectionID, $tab);
256 $this->lastQuery = ifx_query($sql, $this->_connectionID);
260 // Following line have been commented because autocommit mode is
261 // not supported by informix SE 7.2
263 //if ($this->_autocommit) ifx_query('COMMIT',$this->_connectionID);
265 return $this->lastQuery;
268 // returns true or false
271 $this->lastQuery = false;
272 return ifx_close($this->_connectionID);
276 /*--------------------------------------------------------------------------------------
277 Class Name: Recordset
278 --------------------------------------------------------------------------------------*/
280 class ADORecordset_informix72 extends ADORecordSet
283 var $databaseType = "informix72";
285 var $_fieldprops = false;
287 function ADORecordset_informix72($id, $mode = false)
289 if ($mode === false) {
290 global $ADODB_FETCH_MODE;
291 $mode = $ADODB_FETCH_MODE;
293 $this->fetchMode = $mode;
294 return $this->ADORecordSet($id);
297 /* Returns: an object containing field information.
298 Get column information in the Recordset object. fetchField() can be used in order to obtain information about
299 fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
300 fetchField() is retrieved. */
301 function &FetchField($fieldOffset = -1)
303 if (empty($this->_fieldprops)) {
304 $fp = ifx_fieldproperties($this->_queryID);
305 foreach ($fp as $k => $v) {
306 $o = new ADOFieldObject;
308 $arr = explode(';', $v); //"SQLTYPE;length;precision;scale;ISNULLABLE"
310 $o->max_length = $arr[1];
311 $this->_fieldprops[] = $o;
312 $o->not_null = $arr[4] == "N";
315 return $this->_fieldprops[$fieldOffset];
320 $this->_numOfRows = -1; // ifx_affected_rows not reliable, only returns estimate -- ($ADODB_COUNTRECS)? ifx_affected_rows($this->_queryID):-1;
321 $this->_numOfFields = ifx_num_fields($this->_queryID);
326 return @ifx_fetch_row($this->_queryID, $row);
331 $this->fields = @ifx_fetch_row($this->_queryID, "LAST");
332 if ($this->fields) $this->EOF = false;
333 $this->_currentRow = -1;
335 if ($this->fetchMode == ADODB_FETCH_NUM) {
336 foreach ($this->fields as $v) {
339 $this->fields = $arr;
347 $this->fields = @ifx_fetch_row($this->_queryID, "FIRST");
348 if ($this->fields) $this->EOF = false;
349 $this->_currentRow = 0;
351 if ($this->fetchMode == ADODB_FETCH_NUM) {
352 foreach ($this->fields as $v) {
355 $this->fields = $arr;
361 function _fetch($ignore_fields = false)
364 $this->fields = @ifx_fetch_row($this->_queryID);
366 if (!is_array($this->fields)) return false;
368 if ($this->fetchMode == ADODB_FETCH_NUM) {
369 foreach ($this->fields as $v) {
372 $this->fields = $arr;
377 /* close() only needs to be called if you are worried about using too much memory while your script
378 is running. All associated result memory for the specified result identifier will automatically be freed. */
381 return ifx_free_result($this->_queryID);