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