source: trunk/phpgwapi/inc/adodb/drivers/adodb-informix72.inc.php @ 2

Revision 2, 11.0 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

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