2 /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2004 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.02 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Author: Tomas V.V.Cox <cox@idecnet.com> |
17 // | Maintainer: Daniel Convissor <danielc@php.net> |
18 // +----------------------------------------------------------------------+
20 // $Id: ifx.php 7703 2010-09-21 06:28:07Z rurban $
24 // For more info on Informix errors see:
25 // http://www.informix.com/answers/english/ierrors.htm
28 // - set needed env Informix vars on connect
29 // - implement native prepare/execute
32 require_once 'DB/common.php';
35 * Database independent query interface definition for PHP's Informix
39 * @version $Id: ifx.php 7703 2010-09-21 06:28:07Z rurban $
41 * @author Tomas V.V.Cox <cox@idecnet.com>
43 class DB_ifx extends DB_common
50 var $transaction_opcount = 0;
51 var $autocommit = true;
52 var $fetchmode = DB_FETCHMODE_ORDERED; /* Default fetch mode */
59 $this->phptype = 'ifx';
60 $this->dbsyntax = 'ifx';
61 $this->features = array(
64 'transactions' => true,
67 $this->errorcode_map = array(
68 '-201' => DB_ERROR_SYNTAX,
69 '-206' => DB_ERROR_NOSUCHTABLE,
70 '-217' => DB_ERROR_NOSUCHFIELD,
71 '-239' => DB_ERROR_CONSTRAINT,
72 '-253' => DB_ERROR_SYNTAX,
73 '-292' => DB_ERROR_CONSTRAINT_NOT_NULL,
74 '-310' => DB_ERROR_ALREADY_EXISTS,
75 '-329' => DB_ERROR_NODBSELECTED,
76 '-346' => DB_ERROR_CONSTRAINT,
77 '-386' => DB_ERROR_CONSTRAINT_NOT_NULL,
78 '-391' => DB_ERROR_CONSTRAINT_NOT_NULL,
79 '-554' => DB_ERROR_SYNTAX,
80 '-691' => DB_ERROR_CONSTRAINT,
81 '-703' => DB_ERROR_CONSTRAINT_NOT_NULL,
82 '-1204' => DB_ERROR_INVALID_DATE,
83 '-1205' => DB_ERROR_INVALID_DATE,
84 '-1206' => DB_ERROR_INVALID_DATE,
85 '-1209' => DB_ERROR_INVALID_DATE,
86 '-1210' => DB_ERROR_INVALID_DATE,
87 '-1212' => DB_ERROR_INVALID_DATE,
88 '-1213' => DB_ERROR_INVALID_NUMBER,
96 * Connect to a database and log in as the specified user.
98 * @param $dsn the data source name (see DB::parseDSN for syntax)
99 * @param $persistent (optional) whether the connection should
102 * @return int DB_OK on success, a DB error code on failure
104 function connect($dsninfo, $persistent = false)
106 if (!DB::assertExtension('informix') &&
107 !DB::assertExtension('Informix'))
109 return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
111 $this->dsn = $dsninfo;
112 $dbhost = $dsninfo['hostspec'] ? '@' . $dsninfo['hostspec'] : '';
113 $dbname = $dsninfo['database'] ? $dsninfo['database'] . $dbhost : '';
114 $user = $dsninfo['username'] ? $dsninfo['username'] : '';
115 $pw = $dsninfo['password'] ? $dsninfo['password'] : '';
117 $connect_function = $persistent ? 'ifx_pconnect' : 'ifx_connect';
119 $this->connection = @$connect_function($dbname, $user, $pw);
120 if (!is_resource($this->connection)) {
121 return $this->ifxraiseError(DB_ERROR_CONNECT_FAILED);
130 * Log out and disconnect from the database.
132 * @return bool true on success, false if not connected.
134 function disconnect()
136 $ret = @ifx_close($this->connection);
137 $this->connection = null;
145 * Send a query to Informix and return the results as a
146 * Informix resource identifier.
148 * @param $query the SQL query
150 * @return int returns a valid Informix result for successful SELECT
151 * queries, DB_OK for other successful queries. A DB error code
152 * is returned on failure.
154 function simpleQuery($query)
156 $ismanip = DB::isManip($query);
157 $this->last_query = $query;
158 $this->affected = null;
159 if (preg_match('/(SELECT)/i', $query)) { //TESTME: Use !DB::isManip()?
160 // the scroll is needed for fetching absolute row numbers
161 // in a select query result
162 $result = @ifx_query($query, $this->connection, IFX_SCROLL);
164 if (!$this->autocommit && $ismanip) {
165 if ($this->transaction_opcount == 0) {
166 $result = @ifx_query('BEGIN WORK', $this->connection);
168 return $this->ifxraiseError();
171 $this->transaction_opcount++;
173 $result = @ifx_query($query, $this->connection);
176 return $this->ifxraiseError();
178 $this->affected = @ifx_affected_rows($result);
179 // Determine which queries should return data, and which
180 // should return an error code only.
181 if (preg_match('/(SELECT)/i', $query)) {
184 // XXX Testme: free results inside a transaction
185 // may cause to stop it and commit the work?
187 // Result has to be freed even with a insert or update
188 @ifx_free_result($result);
197 * Move the internal ifx result pointer to the next available result
199 * @param a valid fbsql result resource
203 * @return true if a result is available otherwise return false
205 function nextResult($result)
211 // {{{ affectedRows()
214 * Gets the number of rows affected by the last query.
215 * if the last query was a select, returns 0.
217 * @return number of rows affected by the last query
219 function affectedRows()
221 if (DB::isManip($this->last_query)) {
222 return $this->affected;
233 * Fetch a row and insert the data into an existing array.
235 * Formating of the array and the data therein are configurable.
236 * See DB_result::fetchInto() for more information.
238 * @param resource $result query result identifier
239 * @param array $arr (reference) array where data from the row
241 * @param int $fetchmode how the resulting array should be indexed
242 * @param int $rownum the row number to fetch
244 * @return mixed DB_OK on success, null when end of result set is
245 * reached or on failure
247 * @see DB_result::fetchInto()
250 function fetchInto($result, &$arr, $fetchmode, $rownum=null)
252 if (($rownum !== null) && ($rownum < 0)) {
255 if ($rownum === null) {
257 * Even though fetch_row() should return the next row if
258 * $rownum is null, it doesn't in all cases. Bug 598.
262 // Index starts at row 1, unlike most DBMS's starting at 0.
265 if (!$arr = @ifx_fetch_row($result, $rownum)) {
268 if ($fetchmode !== DB_FETCHMODE_ASSOC) {
271 foreach ($arr as $val) {
275 } elseif ($fetchmode == DB_FETCHMODE_ASSOC &&
276 $this->options['portability'] & DB_PORTABILITY_LOWERCASE)
278 $arr = array_change_key_case($arr, CASE_LOWER);
280 if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
281 $this->_rtrimArrayValues($arr);
283 if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
284 $this->_convertNullArrayValuesToEmpty($arr);
292 function numRows($result)
294 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
301 * Get the number of columns in a result set.
303 * @param $result Informix result identifier
305 * @return int the number of columns per row in $result
307 function numCols($result)
309 if (!$cols = @ifx_num_fields($result)) {
310 return $this->ifxraiseError();
319 * Free the internal resources associated with $result.
321 * @param $result Informix result identifier
323 * @return bool true on success, false if $result is invalid
325 function freeResult($result)
327 return @ifx_free_result($result);
334 * Enable/disable automatic commits
336 function autoCommit($onoff = true)
338 // XXX if $this->transaction_opcount > 0, we should probably
339 // issue a warning here.
340 $this->autocommit = $onoff ? true : false;
348 * Commit the current transaction.
352 if ($this->transaction_opcount > 0) {
353 $result = @ifx_query('COMMIT WORK', $this->connection);
354 $this->transaction_opcount = 0;
356 return $this->ifxRaiseError();
366 * Roll back (undo) the current transaction.
370 if ($this->transaction_opcount > 0) {
371 $result = @ifx_query('ROLLBACK WORK', $this->connection);
372 $this->transaction_opcount = 0;
374 return $this->ifxRaiseError();
381 // {{{ ifxraiseError()
384 * Gather information about an error, then use that info to create a
385 * DB error object and finally return that object.
387 * @param integer $errno PEAR error number (usually a DB constant) if
388 * manually raising an error
389 * @return object DB error object
392 * @see DB_common::raiseError()
394 function ifxraiseError($errno = null)
396 if ($errno === null) {
397 $errno = $this->errorCode(ifx_error());
400 return $this->raiseError($errno, null, null, null,
401 $this->errorNative());
408 * Map native error codes to DB's portable ones.
410 * Requires that the DB implementation's constructor fills
411 * in the <var>$errorcode_map</var> property.
413 * @param string $nativecode error code returned by the database
414 * @return int a portable DB error code, or DB_ERROR if this DB
415 * implementation has no mapping for the given error code.
417 function errorCode($nativecode)
419 if (ereg('SQLCODE=(.*)]', $nativecode, $match)) {
421 if (isset($this->errorcode_map[$code])) {
422 return $this->errorcode_map[$code];
432 * Get the native error message of the last error (if any) that
433 * occured on the current connection.
435 * @return int native Informix error code
437 function errorNative()
439 return @ifx_error() . ' ' . @ifx_errormsg();
443 // {{{ getSpecialQuery()
446 * Returns the query needed to get some backend info
447 * @param string $type What kind of info you want to retrieve
448 * @return string The SQL query string
450 function getSpecialQuery($type)
454 return 'select tabname from systables where tabid >= 100';
464 * Returns information about a table or a result set.
466 * NOTE: only supports 'table' if <var>$result</var> is a table name.
468 * If analyzing a query result and the result has duplicate field names,
469 * an error will be raised saying
470 * <samp>can't distinguish duplicate field names</samp>.
472 * @param object|string $result DB_result object from a query or a
473 * string containing the name of a table
474 * @param int $mode a valid tableInfo mode
475 * @return array an associative array with the information requested
476 * or an error object if something is wrong
480 * @see DB_common::tableInfo()
482 function tableInfo($result, $mode = null)
484 if (isset($result->result)) {
486 * Probably received a result object.
487 * Extract the result resource identifier.
489 $id = $result->result;
491 } elseif (is_string($result)) {
493 * Probably received a table name.
494 * Create a result resource identifier.
496 $id = @ifx_query("SELECT * FROM $result WHERE 1=0",
501 * Probably received a result resource identifier.
508 if (!is_resource($id)) {
509 return $this->ifxRaiseError(DB_ERROR_NEED_MORE_DATA);
512 $flds = @ifx_fieldproperties($id);
513 $count = @ifx_num_fields($id);
515 if (count($flds) != $count) {
516 return $this->raiseError("can't distinguish duplicate field names");
519 if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
520 $case_func = 'strtolower';
522 $case_func = 'strval';
526 // made this IF due to performance (one if is faster than $count if's)
528 foreach ($flds as $key => $value) {
529 $props = explode(';', $value);
531 $res[$i]['table'] = $got_string ? $case_func($result) : '';
532 $res[$i]['name'] = $case_func($key);
533 $res[$i]['type'] = $props[0];
534 $res[$i]['len'] = $props[1];
535 $res[$i]['flags'] = $props[4] == 'N' ? 'not_null' : '';
540 $res['num_fields'] = $count;
542 foreach ($flds as $key => $value) {
543 $props = explode(';', $value);
545 $res[$i]['table'] = $got_string ? $case_func($result) : '';
546 $res[$i]['name'] = $case_func($key);
547 $res[$i]['type'] = $props[0];
548 $res[$i]['len'] = $props[1];
549 $res[$i]['flags'] = $props[4] == 'N' ? 'not_null' : '';
551 if ($mode & DB_TABLEINFO_ORDER) {
552 $res['order'][$res[$i]['name']] = $i;
554 if ($mode & DB_TABLEINFO_ORDERTABLE) {
555 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
561 // free the result only if we were called on a table
563 @ifx_free_result($id);