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 // +----------------------------------------------------------------------+
21 // For more info on Informix errors see:
22 // http://www.informix.com/answers/english/ierrors.htm
25 // - set needed env Informix vars on connect
26 // - implement native prepare/execute
28 require_once 'DB/common.php';
31 * Database independent query interface definition for PHP's Informix
37 * @author Tomas V.V.Cox <cox@idecnet.com>
39 class DB_ifx extends DB_common
46 var $transaction_opcount = 0;
47 var $autocommit = true;
48 var $fetchmode = DB_FETCHMODE_ORDERED; /* Default fetch mode */
55 $this->phptype = 'ifx';
56 $this->dbsyntax = 'ifx';
57 $this->features = array(
60 'transactions' => true,
63 $this->errorcode_map = array(
64 '-201' => DB_ERROR_SYNTAX,
65 '-206' => DB_ERROR_NOSUCHTABLE,
66 '-217' => DB_ERROR_NOSUCHFIELD,
67 '-239' => DB_ERROR_CONSTRAINT,
68 '-253' => DB_ERROR_SYNTAX,
69 '-292' => DB_ERROR_CONSTRAINT_NOT_NULL,
70 '-310' => DB_ERROR_ALREADY_EXISTS,
71 '-329' => DB_ERROR_NODBSELECTED,
72 '-346' => DB_ERROR_CONSTRAINT,
73 '-386' => DB_ERROR_CONSTRAINT_NOT_NULL,
74 '-391' => DB_ERROR_CONSTRAINT_NOT_NULL,
75 '-554' => DB_ERROR_SYNTAX,
76 '-691' => DB_ERROR_CONSTRAINT,
77 '-703' => DB_ERROR_CONSTRAINT_NOT_NULL,
78 '-1204' => DB_ERROR_INVALID_DATE,
79 '-1205' => DB_ERROR_INVALID_DATE,
80 '-1206' => DB_ERROR_INVALID_DATE,
81 '-1209' => DB_ERROR_INVALID_DATE,
82 '-1210' => DB_ERROR_INVALID_DATE,
83 '-1212' => DB_ERROR_INVALID_DATE,
84 '-1213' => DB_ERROR_INVALID_NUMBER,
92 * Connect to a database and log in as the specified user.
94 * @param $dsn the data source name (see DB::parseDSN for syntax)
95 * @param $persistent (optional) whether the connection should
98 * @return int DB_OK on success, a DB error code on failure
100 function connect($dsninfo, $persistent = false)
102 if (!DB::assertExtension('informix') &&
103 !DB::assertExtension('Informix'))
105 return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
107 $this->dsn = $dsninfo;
108 $dbhost = $dsninfo['hostspec'] ? '@' . $dsninfo['hostspec'] : '';
109 $dbname = $dsninfo['database'] ? $dsninfo['database'] . $dbhost : '';
110 $user = $dsninfo['username'] ? $dsninfo['username'] : '';
111 $pw = $dsninfo['password'] ? $dsninfo['password'] : '';
113 $connect_function = $persistent ? 'ifx_pconnect' : 'ifx_connect';
115 $this->connection = @$connect_function($dbname, $user, $pw);
116 if (!is_resource($this->connection)) {
117 return $this->ifxraiseError(DB_ERROR_CONNECT_FAILED);
126 * Log out and disconnect from the database.
128 * @return bool true on success, false if not connected.
130 function disconnect()
132 $ret = @ifx_close($this->connection);
133 $this->connection = null;
141 * Send a query to Informix and return the results as a
142 * Informix resource identifier.
144 * @param $query the SQL query
146 * @return int returns a valid Informix result for successful SELECT
147 * queries, DB_OK for other successful queries. A DB error code
148 * is returned on failure.
150 function simpleQuery($query)
152 $ismanip = DB::isManip($query);
153 $this->last_query = $query;
154 $this->affected = null;
155 if (preg_match('/(SELECT)/i', $query)) { //TESTME: Use !DB::isManip()?
156 // the scroll is needed for fetching absolute row numbers
157 // in a select query result
158 $result = @ifx_query($query, $this->connection, IFX_SCROLL);
160 if (!$this->autocommit && $ismanip) {
161 if ($this->transaction_opcount == 0) {
162 $result = @ifx_query('BEGIN WORK', $this->connection);
164 return $this->ifxraiseError();
167 $this->transaction_opcount++;
169 $result = @ifx_query($query, $this->connection);
172 return $this->ifxraiseError();
174 $this->affected = @ifx_affected_rows($result);
175 // Determine which queries should return data, and which
176 // should return an error code only.
177 if (preg_match('/(SELECT)/i', $query)) {
180 // XXX Testme: free results inside a transaction
181 // may cause to stop it and commit the work?
183 // Result has to be freed even with a insert or update
184 @ifx_free_result($result);
193 * Move the internal ifx result pointer to the next available result
195 * @param a valid fbsql result resource
199 * @return true if a result is available otherwise return false
201 function nextResult($result)
207 // {{{ affectedRows()
210 * Gets the number of rows affected by the last query.
211 * if the last query was a select, returns 0.
213 * @return number of rows affected by the last query
215 function affectedRows()
217 if (DB::isManip($this->last_query)) {
218 return $this->affected;
229 * Fetch a row and insert the data into an existing array.
231 * Formating of the array and the data therein are configurable.
232 * See DB_result::fetchInto() for more information.
234 * @param resource $result query result identifier
235 * @param array $arr (reference) array where data from the row
237 * @param int $fetchmode how the resulting array should be indexed
238 * @param int $rownum the row number to fetch
240 * @return mixed DB_OK on success, null when end of result set is
241 * reached or on failure
243 * @see DB_result::fetchInto()
246 function fetchInto($result, &$arr, $fetchmode, $rownum=null)
248 if (($rownum !== null) && ($rownum < 0)) {
251 if ($rownum === null) {
253 * Even though fetch_row() should return the next row if
254 * $rownum is null, it doesn't in all cases. Bug 598.
258 // Index starts at row 1, unlike most DBMS's starting at 0.
261 if (!$arr = @ifx_fetch_row($result, $rownum)) {
264 if ($fetchmode !== DB_FETCHMODE_ASSOC) {
267 foreach ($arr as $val) {
271 } elseif ($fetchmode == DB_FETCHMODE_ASSOC &&
272 $this->options['portability'] & DB_PORTABILITY_LOWERCASE)
274 $arr = array_change_key_case($arr, CASE_LOWER);
276 if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
277 $this->_rtrimArrayValues($arr);
279 if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
280 $this->_convertNullArrayValuesToEmpty($arr);
288 function numRows($result)
290 return $this->raiseError(DB_ERROR_NOT_CAPABLE);
297 * Get the number of columns in a result set.
299 * @param $result Informix result identifier
301 * @return int the number of columns per row in $result
303 function numCols($result)
305 if (!$cols = @ifx_num_fields($result)) {
306 return $this->ifxraiseError();
315 * Free the internal resources associated with $result.
317 * @param $result Informix result identifier
319 * @return bool true on success, false if $result is invalid
321 function freeResult($result)
323 return @ifx_free_result($result);
330 * Enable/disable automatic commits
332 function autoCommit($onoff = true)
334 // XXX if $this->transaction_opcount > 0, we should probably
335 // issue a warning here.
336 $this->autocommit = $onoff ? true : false;
344 * Commit the current transaction.
348 if ($this->transaction_opcount > 0) {
349 $result = @ifx_query('COMMIT WORK', $this->connection);
350 $this->transaction_opcount = 0;
352 return $this->ifxRaiseError();
362 * Roll back (undo) the current transaction.
366 if ($this->transaction_opcount > 0) {
367 $result = @ifx_query('ROLLBACK WORK', $this->connection);
368 $this->transaction_opcount = 0;
370 return $this->ifxRaiseError();
377 // {{{ ifxraiseError()
380 * Gather information about an error, then use that info to create a
381 * DB error object and finally return that object.
383 * @param integer $errno PEAR error number (usually a DB constant) if
384 * manually raising an error
385 * @return object DB error object
388 * @see DB_common::raiseError()
390 function ifxraiseError($errno = null)
392 if ($errno === null) {
393 $errno = $this->errorCode(ifx_error());
396 return $this->raiseError($errno, null, null, null,
397 $this->errorNative());
404 * Map native error codes to DB's portable ones.
406 * Requires that the DB implementation's constructor fills
407 * in the <var>$errorcode_map</var> property.
409 * @param string $nativecode error code returned by the database
410 * @return int a portable DB error code, or DB_ERROR if this DB
411 * implementation has no mapping for the given error code.
413 function errorCode($nativecode)
415 if (ereg('SQLCODE=(.*)]', $nativecode, $match)) {
417 if (isset($this->errorcode_map[$code])) {
418 return $this->errorcode_map[$code];
428 * Get the native error message of the last error (if any) that
429 * occured on the current connection.
431 * @return int native Informix error code
433 function errorNative()
435 return @ifx_error() . ' ' . @ifx_errormsg();
439 // {{{ getSpecialQuery()
442 * Returns the query needed to get some backend info
443 * @param string $type What kind of info you want to retrieve
444 * @return string The SQL query string
446 function getSpecialQuery($type)
450 return 'select tabname from systables where tabid >= 100';
460 * Returns information about a table or a result set.
462 * NOTE: only supports 'table' if <var>$result</var> is a table name.
464 * If analyzing a query result and the result has duplicate field names,
465 * an error will be raised saying
466 * <samp>can't distinguish duplicate field names</samp>.
468 * @param object|string $result DB_result object from a query or a
469 * string containing the name of a table
470 * @param int $mode a valid tableInfo mode
471 * @return array an associative array with the information requested
472 * or an error object if something is wrong
476 * @see DB_common::tableInfo()
478 function tableInfo($result, $mode = null)
480 if (isset($result->result)) {
482 * Probably received a result object.
483 * Extract the result resource identifier.
485 $id = $result->result;
487 } elseif (is_string($result)) {
489 * Probably received a table name.
490 * Create a result resource identifier.
492 $id = @ifx_query("SELECT * FROM $result WHERE 1=0",
497 * Probably received a result resource identifier.
504 if (!is_resource($id)) {
505 return $this->ifxRaiseError(DB_ERROR_NEED_MORE_DATA);
508 $flds = @ifx_fieldproperties($id);
509 $count = @ifx_num_fields($id);
511 if (count($flds) != $count) {
512 return $this->raiseError("can't distinguish duplicate field names");
515 if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
516 $case_func = 'strtolower';
518 $case_func = 'strval';
522 // made this IF due to performance (one if is faster than $count if's)
524 foreach ($flds as $key => $value) {
525 $props = explode(';', $value);
527 $res[$i]['table'] = $got_string ? $case_func($result) : '';
528 $res[$i]['name'] = $case_func($key);
529 $res[$i]['type'] = $props[0];
530 $res[$i]['len'] = $props[1];
531 $res[$i]['flags'] = $props[4] == 'N' ? 'not_null' : '';
536 $res['num_fields'] = $count;
538 foreach ($flds as $key => $value) {
539 $props = explode(';', $value);
541 $res[$i]['table'] = $got_string ? $case_func($result) : '';
542 $res[$i]['name'] = $case_func($key);
543 $res[$i]['type'] = $props[0];
544 $res[$i]['len'] = $props[1];
545 $res[$i]['flags'] = $props[4] == 'N' ? 'not_null' : '';
547 if ($mode & DB_TABLEINFO_ORDER) {
548 $res['order'][$res[$i]['name']] = $i;
550 if ($mode & DB_TABLEINFO_ORDERTABLE) {
551 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
557 // free the result only if we were called on a table
559 @ifx_free_result($id);