]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/adodb/drivers/adodb-informix72.inc.php
Reformat code
[SourceForge/phpwiki.git] / lib / WikiDB / adodb / drivers / adodb-informix72.inc.php
1 <?php
2 /*
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.
8
9   Latest version is available at http://php.weblogs.com/
10
11   Informix port by Mitchell T. Young (mitch@youngfamily.org)
12
13   Further mods by "Samuel CARRIERE" <samuel_carriere@hotmail.com>
14
15 */
16
17 if (!defined('IFX_SCROLL')) define('IFX_SCROLL', 1);
18
19 class ADODB_informix72 extends ADOConnection
20 {
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";
31     var $metaColumnsSQL =
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'";
35
36 //      var $metaColumnsSQL = "select colname, coltype, collength from syscolumns c, systables t where c.tabid=t.tabid and tabname='%s'";
37     var $concat_operator = '||';
38
39     var $lastQuery = false;
40     var $has_insertid = true;
41
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
47
48     function ADODB_informix72()
49     {
50         // alternatively, use older method:
51         //putenv("DBDATE=Y4MD-");
52
53         // force ISO date format
54         putenv('GL_DATE=%Y-%m-%d');
55
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.
60         }
61     }
62
63     function ServerInfo()
64     {
65         if (isset($this->version)) return $this->version;
66
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;
70         return $arr;
71     }
72
73     function _insertid()
74     {
75         $sqlca = ifx_getsqlca($this->lastQuery);
76         return @$sqlca["sqlerrd1"];
77     }
78
79     function _affectedrows()
80     {
81         if ($this->lastQuery) {
82             return @ifx_affected_rows($this->lastQuery);
83         }
84         return 0;
85     }
86
87     function BeginTrans()
88     {
89         if ($this->transOff) return true;
90         $this->transCnt += 1;
91         $this->Execute('BEGIN');
92         $this->_autocommit = false;
93         return true;
94     }
95
96     function CommitTrans($ok = true)
97     {
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;
103         return true;
104     }
105
106     function RollbackTrans()
107     {
108         if ($this->transOff) return true;
109         if ($this->transCnt) $this->transCnt -= 1;
110         $this->Execute('ROLLBACK');
111         $this->_autocommit = true;
112         return true;
113     }
114
115     function RowLock($tables, $where)
116     {
117         if ($this->_autocommit) $this->BeginTrans();
118         return $this->GetOne("select 1 as ignore from $tables where $where for update");
119     }
120
121     /*  Returns: the last error message from previous database operation
122         Note: This function is NOT available for Microsoft SQL Server.  */
123
124     function ErrorMsg()
125     {
126         if (!empty($this->_logsql)) return $this->_errorMsg;
127         $this->_errorMsg = ifx_errormsg();
128         return $this->_errorMsg;
129     }
130
131     function ErrorNo()
132     {
133         preg_match("/.*SQLCODE=([^\]]*)/", ifx_error(), $parse); //!EOS
134         if (is_array($parse) && isset($parse[1])) return (int)$parse[1];
135         return 0;
136     }
137
138     function &MetaColumns($table)
139     {
140         global $ADODB_FETCH_MODE;
141
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;
150
151             $retarr = array();
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];
160                 } else {
161                     $fld->has_default = 0;
162                 }
163
164                 $retarr[strtolower($fld->name)] = $fld;
165                 $rs->MoveNext();
166             }
167
168             $rs->Close();
169             return $retarr;
170         }
171
172         return false;
173     }
174
175     function &xMetaColumns($table)
176     {
177         return ADOConnection::MetaColumns($table, false);
178     }
179
180     function UpdateBlob($table, $column, $val, $where, $blobtype = 'BLOB')
181     {
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));
185     }
186
187     function BlobDecode($blobid)
188     {
189         return function_exists('ifx_byteasvarchar') ? $blobid : @ifx_get_blob($blobid);
190     }
191
192     // returns true or false
193     function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
194     {
195         if (!function_exists('ifx_connect')) return false;
196
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);
203         return true;
204     }
205
206     // returns true or false
207     function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
208     {
209         if (!function_exists('ifx_connect')) return false;
210
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);
216         return true;
217     }
218
219     /*
220         // ifx_do does not accept bind parameters - weird ???
221         function Prepare($sql)
222         {
223             $stmt = ifx_prepare($sql);
224             if (!$stmt) return $sql;
225             else return array($sql,$stmt);
226         }
227     */
228     // returns query ID if successful, otherwise false
229     function _query($sql, $inputarr)
230     {
231         global $ADODB_COUNTRECS;
232
233         // String parameters have to be converted using ifx_create_char
234         if ($inputarr) {
235             foreach ($inputarr as $v) {
236                 if (gettype($v) == 'string') {
237                     $tab[] = ifx_create_char($v);
238                 } else {
239                     $tab[] = $v;
240                 }
241             }
242         }
243
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)) {
247             if ($inputarr) {
248                 $this->lastQuery = ifx_query($sql, $this->_connectionID, $this->cursorType, $tab);
249             } else {
250                 $this->lastQuery = ifx_query($sql, $this->_connectionID, $this->cursorType);
251             }
252         } else {
253             if ($inputarr) {
254                 $this->lastQuery = ifx_query($sql, $this->_connectionID, $tab);
255             } else {
256                 $this->lastQuery = ifx_query($sql, $this->_connectionID);
257             }
258         }
259
260         // Following line have been commented because autocommit mode is
261         // not supported by informix SE 7.2
262
263         //if ($this->_autocommit) ifx_query('COMMIT',$this->_connectionID);
264
265         return $this->lastQuery;
266     }
267
268     // returns true or false
269     function _close()
270     {
271         $this->lastQuery = false;
272         return ifx_close($this->_connectionID);
273     }
274 }
275
276 /*--------------------------------------------------------------------------------------
277      Class Name: Recordset
278 --------------------------------------------------------------------------------------*/
279
280 class ADORecordset_informix72 extends ADORecordSet
281 {
282
283     var $databaseType = "informix72";
284     var $canSeek = true;
285     var $_fieldprops = false;
286
287     function ADORecordset_informix72($id, $mode = false)
288     {
289         if ($mode === false) {
290             global $ADODB_FETCH_MODE;
291             $mode = $ADODB_FETCH_MODE;
292         }
293         $this->fetchMode = $mode;
294         return $this->ADORecordSet($id);
295     }
296
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)
302     {
303         if (empty($this->_fieldprops)) {
304             $fp = ifx_fieldproperties($this->_queryID);
305             foreach ($fp as $k => $v) {
306                 $o = new ADOFieldObject;
307                 $o->name = $k;
308                 $arr = explode(';', $v); //"SQLTYPE;length;precision;scale;ISNULLABLE"
309                 $o->type = $arr[0];
310                 $o->max_length = $arr[1];
311                 $this->_fieldprops[] = $o;
312                 $o->not_null = $arr[4] == "N";
313             }
314         }
315         return $this->_fieldprops[$fieldOffset];
316     }
317
318     function _initrs()
319     {
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);
322     }
323
324     function _seek($row)
325     {
326         return @ifx_fetch_row($this->_queryID, $row);
327     }
328
329     function MoveLast()
330     {
331         $this->fields = @ifx_fetch_row($this->_queryID, "LAST");
332         if ($this->fields) $this->EOF = false;
333         $this->_currentRow = -1;
334
335         if ($this->fetchMode == ADODB_FETCH_NUM) {
336             foreach ($this->fields as $v) {
337                 $arr[] = $v;
338             }
339             $this->fields = $arr;
340         }
341
342         return true;
343     }
344
345     function MoveFirst()
346     {
347         $this->fields = @ifx_fetch_row($this->_queryID, "FIRST");
348         if ($this->fields) $this->EOF = false;
349         $this->_currentRow = 0;
350
351         if ($this->fetchMode == ADODB_FETCH_NUM) {
352             foreach ($this->fields as $v) {
353                 $arr[] = $v;
354             }
355             $this->fields = $arr;
356         }
357
358         return true;
359     }
360
361     function _fetch($ignore_fields = false)
362     {
363
364         $this->fields = @ifx_fetch_row($this->_queryID);
365
366         if (!is_array($this->fields)) return false;
367
368         if ($this->fetchMode == ADODB_FETCH_NUM) {
369             foreach ($this->fields as $v) {
370                 $arr[] = $v;
371             }
372             $this->fields = $arr;
373         }
374         return true;
375     }
376
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.       */
379     function _close()
380     {
381         return ifx_free_result($this->_queryID);
382     }
383
384 }