]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/adodb/drivers/adodb-informix72.inc.php
extra_empty_lines
[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     var $databaseType = "informix72";
21     var $dataProvider = "informix";
22     var $replaceQuote = "''"; // string to use to replace quotes
23     var $fmtDate = "'Y-m-d'";
24     var $fmtTimeStamp = "'Y-m-d H:i:s'";
25     var $hasInsertID = true;
26     var $hasAffectedRows = true;
27     var $upperCase = 'upper';
28     var $substr = 'substr';
29     var $metaTablesSQL="select tabname from systables";
30     var $metaColumnsSQL =
31 "select c.colname, c.coltype, c.collength, d.default
32     from syscolumns c, systables t,sysdefaults d
33     where c.tabid=t.tabid and d.tabid=t.tabid and d.colno=c.colno and tabname='%s'";
34
35 //      var $metaColumnsSQL = "select colname, coltype, collength from syscolumns c, systables t where c.tabid=t.tabid and tabname='%s'";
36     var $concat_operator = '||';
37
38     var $lastQuery = false;
39     var $has_insertid = true;
40
41     var $_autocommit = true;
42     var $_bindInputArray = true;  // set to true if ADOConnection.Execute() permits binding of array parameters.
43     var $sysDate = 'TODAY';
44     var $sysTimeStamp = 'CURRENT';
45     var $cursorType = IFX_SCROLL; // IFX_SCROLL or IFX_HOLD or 0
46
47     function ADODB_informix72()
48     {
49         // alternatively, use older method:
50         //putenv("DBDATE=Y4MD-");
51
52         // force ISO date format
53         putenv('GL_DATE=%Y-%m-%d');
54
55         if (function_exists('ifx_byteasvarchar')) {
56             ifx_byteasvarchar(1); // Mode "0" will return a blob id, and mode "1" will return a varchar with text content.
57             ifx_textasvarchar(1); // Mode "0" will return a blob id, and mode "1" will return a varchar with text content.
58             ifx_blobinfile_mode(0); // Mode "0" means save Byte-Blobs in memory, and mode "1" means save Byte-Blobs in a file.
59         }
60     }
61
62     function ServerInfo()
63     {
64         if (isset($this->version)) return $this->version;
65
66         $arr['description'] = $this->GetOne("select DBINFO('version','full') from systables where tabid = 1");
67         $arr['version'] = $this->GetOne("select DBINFO('version','major')||"."||DBINFO('version','minor') from systables where tabid = 1");
68         $this->version = $arr;
69         return $arr;
70     }
71
72     function _insertid()
73     {
74         $sqlca =ifx_getsqlca($this->lastQuery);
75         return @$sqlca["sqlerrd1"];
76     }
77
78     function _affectedrows()
79     {
80         if ($this->lastQuery) {
81            return @ifx_affected_rows ($this->lastQuery);
82         }
83         return 0;
84     }
85
86     function BeginTrans()
87     {
88         if ($this->transOff) return true;
89         $this->transCnt += 1;
90         $this->Execute('BEGIN');
91         $this->_autocommit = false;
92         return true;
93     }
94
95     function CommitTrans($ok=true)
96     {
97         if (!$ok) return $this->RollbackTrans();
98         if ($this->transOff) return true;
99         if ($this->transCnt) $this->transCnt -= 1;
100         $this->Execute('COMMIT');
101         $this->_autocommit = true;
102         return true;
103     }
104
105     function RollbackTrans()
106     {
107         if ($this->transOff) return true;
108         if ($this->transCnt) $this->transCnt -= 1;
109         $this->Execute('ROLLBACK');
110         $this->_autocommit = true;
111         return true;
112     }
113
114     function RowLock($tables,$where)
115     {
116         if ($this->_autocommit) $this->BeginTrans();
117         return $this->GetOne("select 1 as ignore from $tables where $where for update");
118     }
119
120     /*  Returns: the last error message from previous database operation
121         Note: This function is NOT available for Microsoft SQL Server.  */
122
123     function ErrorMsg()
124     {
125         if (!empty($this->_logsql)) return $this->_errorMsg;
126         $this->_errorMsg = ifx_errormsg();
127         return $this->_errorMsg;
128     }
129
130     function ErrorNo()
131     {
132         preg_match("/.*SQLCODE=([^\]]*)/",ifx_error(),$parse); //!EOS
133         if (is_array($parse) && isset($parse[1])) return (int)$parse[1];
134         return 0;
135     }
136
137     function &MetaColumns($table)
138     {
139     global $ADODB_FETCH_MODE;
140
141         if (!empty($this->metaColumnsSQL)) {
142             $save = $ADODB_FETCH_MODE;
143             $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
144             if ($this->fetchMode !== false) $savem = $this->SetFetchMode(false);
145                   $rs = $this->Execute(sprintf($this->metaColumnsSQL,$table));
146             if (isset($savem)) $this->SetFetchMode($savem);
147             $ADODB_FETCH_MODE = $save;
148             if ($rs === false) return false;
149
150             $retarr = array();
151             while (!$rs->EOF) { //print_r($rs->fields);
152                 $fld = new ADOFieldObject();
153                 $fld->name = $rs->fields[0];
154                 $fld->type = $rs->fields[1];
155                 $fld->max_length = $rs->fields[2];
156                 if (trim($rs->fields[3]) != "AAAAAA 0") {
157                                 $fld->has_default = 1;
158                                 $fld->default_value = $rs->fields[3];
159                 } else {
160                     $fld->has_default = 0;
161                 }
162
163                 $retarr[strtolower($fld->name)] = $fld;
164                 $rs->MoveNext();
165             }
166
167             $rs->Close();
168             return $retarr;
169         }
170
171         return false;
172     }
173
174    function &xMetaColumns($table)
175    {
176         return ADOConnection::MetaColumns($table,false);
177    }
178
179    function UpdateBlob($table, $column, $val, $where, $blobtype = 'BLOB')
180    {
181            $type = ($blobtype == 'TEXT') ? 1 : 0;
182         $blobid = ifx_create_blob($type,0,$val);
183         return $this->Execute("UPDATE $table SET $column=(?) WHERE $where",array($blobid));
184    }
185
186    function BlobDecode($blobid)
187    {
188            return function_exists('ifx_byteasvarchar') ? $blobid : @ifx_get_blob($blobid);
189    }
190
191     // returns true or false
192    function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
193     {
194         if (!function_exists('ifx_connect')) return false;
195
196         $dbs = $argDatabasename . "@" . $argHostname;
197         if ($argHostname) putenv("INFORMIXSERVER=$argHostname");
198         putenv("INFORMIXSERVER=".trim($argHostname));
199         $this->_connectionID = ifx_connect($dbs,$argUsername,$argPassword);
200         if ($this->_connectionID === false) return false;
201         #if ($argDatabasename) return $this->SelectDB($argDatabasename);
202         return true;
203     }
204
205     // returns true or false
206    function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
207     {
208         if (!function_exists('ifx_connect')) return false;
209
210         $dbs = $argDatabasename . "@" . $argHostname;
211         putenv("INFORMIXSERVER=".trim($argHostname));
212         $this->_connectionID = ifx_pconnect($dbs,$argUsername,$argPassword);
213         if ($this->_connectionID === false) return false;
214         #if ($argDatabasename) return $this->SelectDB($argDatabasename);
215         return true;
216     }
217 /*
218     // ifx_do does not accept bind parameters - weird ???
219     function Prepare($sql)
220     {
221         $stmt = ifx_prepare($sql);
222         if (!$stmt) return $sql;
223         else return array($sql,$stmt);
224     }
225 */
226     // returns query ID if successful, otherwise false
227     function _query($sql,$inputarr)
228     {
229     global $ADODB_COUNTRECS;
230
231       // String parameters have to be converted using ifx_create_char
232       if ($inputarr) {
233          foreach($inputarr as $v) {
234             if (gettype($v) == 'string') {
235                $tab[] = ifx_create_char($v);
236             }
237             else {
238                $tab[] = $v;
239             }
240          }
241       }
242
243       // In case of select statement, we use a scroll cursor in order
244       // to be able to call "move", or "movefirst" statements
245       if (!$ADODB_COUNTRECS && preg_match("/^\s*select/is", $sql)) {
246          if ($inputarr) {
247             $this->lastQuery = ifx_query($sql,$this->_connectionID, $this->cursorType, $tab);
248          }
249          else {
250             $this->lastQuery = ifx_query($sql,$this->_connectionID, $this->cursorType);
251          }
252       }
253       else {
254          if ($inputarr) {
255             $this->lastQuery = ifx_query($sql,$this->_connectionID, $tab);
256          }
257          else {
258             $this->lastQuery = ifx_query($sql,$this->_connectionID);
259          }
260       }
261
262       // Following line have been commented because autocommit mode is
263       // not supported by informix SE 7.2
264
265       //if ($this->_autocommit) ifx_query('COMMIT',$this->_connectionID);
266
267         return $this->lastQuery;
268     }
269
270     // returns true or false
271     function _close()
272     {
273         $this->lastQuery = false;
274         return ifx_close($this->_connectionID);
275     }
276 }
277
278 /*--------------------------------------------------------------------------------------
279      Class Name: Recordset
280 --------------------------------------------------------------------------------------*/
281
282 class ADORecordset_informix72 extends ADORecordSet {
283
284     var $databaseType = "informix72";
285     var $canSeek = true;
286     var $_fieldprops = false;
287
288     function ADORecordset_informix72($id,$mode=false)
289     {
290         if ($mode === false) {
291             global $ADODB_FETCH_MODE;
292             $mode = $ADODB_FETCH_MODE;
293         }
294         $this->fetchMode = $mode;
295         return $this->ADORecordSet($id);
296     }
297
298     /*  Returns: an object containing field information.
299         Get column information in the Recordset object. fetchField() can be used in order to obtain information about
300         fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
301         fetchField() is retrieved.      */
302     function &FetchField($fieldOffset = -1)
303     {
304         if (empty($this->_fieldprops)) {
305             $fp = ifx_fieldproperties($this->_queryID);
306             foreach($fp as $k => $v) {
307                 $o = new ADOFieldObject;
308                 $o->name = $k;
309                 $arr = explode(';', $v); //"SQLTYPE;length;precision;scale;ISNULLABLE"
310                 $o->type = $arr[0];
311                 $o->max_length = $arr[1];
312                 $this->_fieldprops[] = $o;
313                 $o->not_null = $arr[4]=="N";
314             }
315         }
316         return $this->_fieldprops[$fieldOffset];
317     }
318
319     function _initrs()
320     {
321         $this->_numOfRows = -1; // ifx_affected_rows not reliable, only returns estimate -- ($ADODB_COUNTRECS)? ifx_affected_rows($this->_queryID):-1;
322         $this->_numOfFields = ifx_num_fields($this->_queryID);
323     }
324
325     function _seek($row)
326     {
327         return @ifx_fetch_row($this->_queryID, $row);
328     }
329
330    function MoveLast()
331    {
332       $this->fields = @ifx_fetch_row($this->_queryID, "LAST");
333       if ($this->fields) $this->EOF = false;
334       $this->_currentRow = -1;
335
336       if ($this->fetchMode == ADODB_FETCH_NUM) {
337          foreach($this->fields as $v) {
338             $arr[] = $v;
339          }
340          $this->fields = $arr;
341       }
342
343       return true;
344    }
345
346    function MoveFirst()
347     {
348       $this->fields = @ifx_fetch_row($this->_queryID, "FIRST");
349       if ($this->fields) $this->EOF = false;
350       $this->_currentRow = 0;
351
352       if ($this->fetchMode == ADODB_FETCH_NUM) {
353          foreach($this->fields as $v) {
354             $arr[] = $v;
355          }
356          $this->fields = $arr;
357       }
358
359       return true;
360    }
361
362    function _fetch($ignore_fields=false)
363    {
364
365         $this->fields = @ifx_fetch_row($this->_queryID);
366
367         if (!is_array($this->fields)) return false;
368
369         if ($this->fetchMode == ADODB_FETCH_NUM) {
370             foreach($this->fields as $v) {
371                 $arr[] = $v;
372             }
373             $this->fields = $arr;
374         }
375         return true;
376     }
377
378     /*  close() only needs to be called if you are worried about using too much memory while your script
379         is running. All associated result memory for the specified result identifier will automatically be freed.       */
380     function _close()
381     {
382         return ifx_free_result($this->_queryID);
383     }
384
385 }
386 ?>