ifx.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * The PEAR DB driver for PHP's ifx extension
  5. * for interacting with Informix databases
  6. *
  7. * PHP version 5
  8. *
  9. * LICENSE: This source file is subject to version 3.0 of the PHP license
  10. * that is available through the world-wide-web at the following URI:
  11. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  12. * the PHP License and are unable to obtain it through the web, please
  13. * send a note to license@php.net so we can mail you a copy immediately.
  14. *
  15. * @category Database
  16. * @package DB
  17. * @author Tomas V.V.Cox <cox@idecnet.com>
  18. * @author Daniel Convissor <danielc@php.net>
  19. * @copyright 1997-2007 The PHP Group
  20. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  21. * @version CVS: $Id$
  22. * @link http://pear.php.net/package/DB
  23. */
  24. /**
  25. * Obtain the DB_common class so it can be extended from
  26. */
  27. require_once 'DB/common.php';
  28. /**
  29. * The methods PEAR DB uses to interact with PHP's ifx extension
  30. * for interacting with Informix databases
  31. *
  32. * These methods overload the ones declared in DB_common.
  33. *
  34. * More info on Informix errors can be found at:
  35. * http://www.informix.com/answers/english/ierrors.htm
  36. *
  37. * TODO:
  38. * - set needed env Informix vars on connect
  39. * - implement native prepare/execute
  40. *
  41. * @category Database
  42. * @package DB
  43. * @author Tomas V.V.Cox <cox@idecnet.com>
  44. * @author Daniel Convissor <danielc@php.net>
  45. * @copyright 1997-2007 The PHP Group
  46. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  47. * @version Release: 1.9.2
  48. * @link http://pear.php.net/package/DB
  49. */
  50. class DB_ifx extends DB_common
  51. {
  52. // {{{ properties
  53. /**
  54. * The DB driver type (mysql, oci8, odbc, etc.)
  55. * @var string
  56. */
  57. var $phptype = 'ifx';
  58. /**
  59. * The database syntax variant to be used (db2, access, etc.), if any
  60. * @var string
  61. */
  62. var $dbsyntax = 'ifx';
  63. /**
  64. * The capabilities of this DB implementation
  65. *
  66. * The 'new_link' element contains the PHP version that first provided
  67. * new_link support for this DBMS. Contains false if it's unsupported.
  68. *
  69. * Meaning of the 'limit' element:
  70. * + 'emulate' = emulate with fetch row by number
  71. * + 'alter' = alter the query
  72. * + false = skip rows
  73. *
  74. * @var array
  75. */
  76. var $features = array(
  77. 'limit' => 'emulate',
  78. 'new_link' => false,
  79. 'numrows' => 'emulate',
  80. 'pconnect' => true,
  81. 'prepare' => false,
  82. 'ssl' => false,
  83. 'transactions' => true,
  84. );
  85. /**
  86. * A mapping of native error codes to DB error codes
  87. * @var array
  88. */
  89. var $errorcode_map = array(
  90. '-201' => DB_ERROR_SYNTAX,
  91. '-206' => DB_ERROR_NOSUCHTABLE,
  92. '-217' => DB_ERROR_NOSUCHFIELD,
  93. '-236' => DB_ERROR_VALUE_COUNT_ON_ROW,
  94. '-239' => DB_ERROR_CONSTRAINT,
  95. '-253' => DB_ERROR_SYNTAX,
  96. '-268' => DB_ERROR_CONSTRAINT,
  97. '-292' => DB_ERROR_CONSTRAINT_NOT_NULL,
  98. '-310' => DB_ERROR_ALREADY_EXISTS,
  99. '-316' => DB_ERROR_ALREADY_EXISTS,
  100. '-319' => DB_ERROR_NOT_FOUND,
  101. '-329' => DB_ERROR_NODBSELECTED,
  102. '-346' => DB_ERROR_CONSTRAINT,
  103. '-386' => DB_ERROR_CONSTRAINT_NOT_NULL,
  104. '-391' => DB_ERROR_CONSTRAINT_NOT_NULL,
  105. '-554' => DB_ERROR_SYNTAX,
  106. '-691' => DB_ERROR_CONSTRAINT,
  107. '-692' => DB_ERROR_CONSTRAINT,
  108. '-703' => DB_ERROR_CONSTRAINT_NOT_NULL,
  109. '-1202' => DB_ERROR_DIVZERO,
  110. '-1204' => DB_ERROR_INVALID_DATE,
  111. '-1205' => DB_ERROR_INVALID_DATE,
  112. '-1206' => DB_ERROR_INVALID_DATE,
  113. '-1209' => DB_ERROR_INVALID_DATE,
  114. '-1210' => DB_ERROR_INVALID_DATE,
  115. '-1212' => DB_ERROR_INVALID_DATE,
  116. '-1213' => DB_ERROR_INVALID_NUMBER,
  117. );
  118. /**
  119. * The raw database connection created by PHP
  120. * @var resource
  121. */
  122. var $connection;
  123. /**
  124. * The DSN information for connecting to a database
  125. * @var array
  126. */
  127. var $dsn = array();
  128. /**
  129. * Should data manipulation queries be committed automatically?
  130. * @var bool
  131. * @access private
  132. */
  133. var $autocommit = true;
  134. /**
  135. * The quantity of transactions begun
  136. *
  137. * {@internal While this is private, it can't actually be designated
  138. * private in PHP 5 because it is directly accessed in the test suite.}}
  139. *
  140. * @var integer
  141. * @access private
  142. */
  143. var $transaction_opcount = 0;
  144. /**
  145. * The number of rows affected by a data manipulation query
  146. * @var integer
  147. * @access private
  148. */
  149. var $affected = 0;
  150. // }}}
  151. // {{{ constructor
  152. /**
  153. * This constructor calls <kbd>parent::__construct()</kbd>
  154. *
  155. * @return void
  156. */
  157. function __construct()
  158. {
  159. parent::__construct();
  160. }
  161. // }}}
  162. // {{{ connect()
  163. /**
  164. * Connect to the database server, log in and open the database
  165. *
  166. * Don't call this method directly. Use DB::connect() instead.
  167. *
  168. * @param array $dsn the data source name
  169. * @param bool $persistent should the connection be persistent?
  170. *
  171. * @return int DB_OK on success. A DB_Error object on failure.
  172. */
  173. function connect($dsn, $persistent = false)
  174. {
  175. if (!PEAR::loadExtension('informix') &&
  176. !PEAR::loadExtension('Informix'))
  177. {
  178. return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
  179. }
  180. $this->dsn = $dsn;
  181. if ($dsn['dbsyntax']) {
  182. $this->dbsyntax = $dsn['dbsyntax'];
  183. }
  184. $dbhost = $dsn['hostspec'] ? '@' . $dsn['hostspec'] : '';
  185. $dbname = $dsn['database'] ? $dsn['database'] . $dbhost : '';
  186. $user = $dsn['username'] ? $dsn['username'] : '';
  187. $pw = $dsn['password'] ? $dsn['password'] : '';
  188. $connect_function = $persistent ? 'ifx_pconnect' : 'ifx_connect';
  189. $this->connection = @$connect_function($dbname, $user, $pw);
  190. if (!is_resource($this->connection)) {
  191. return $this->ifxRaiseError(DB_ERROR_CONNECT_FAILED);
  192. }
  193. return DB_OK;
  194. }
  195. // }}}
  196. // {{{ disconnect()
  197. /**
  198. * Disconnects from the database server
  199. *
  200. * @return bool TRUE on success, FALSE on failure
  201. */
  202. function disconnect()
  203. {
  204. $ret = @ifx_close($this->connection);
  205. $this->connection = null;
  206. return $ret;
  207. }
  208. // }}}
  209. // {{{ simpleQuery()
  210. /**
  211. * Sends a query to the database server
  212. *
  213. * @param string the SQL query string
  214. *
  215. * @return mixed + a PHP result resrouce for successful SELECT queries
  216. * + the DB_OK constant for other successful queries
  217. * + a DB_Error object on failure
  218. */
  219. function simpleQuery($query)
  220. {
  221. $ismanip = $this->_checkManip($query);
  222. $this->last_query = $query;
  223. $this->affected = null;
  224. if (preg_match('/(SELECT|EXECUTE)/i', $query)) { //TESTME: Use !DB::isManip()?
  225. // the scroll is needed for fetching absolute row numbers
  226. // in a select query result
  227. $result = @ifx_query($query, $this->connection, IFX_SCROLL);
  228. } else {
  229. if (!$this->autocommit && $ismanip) {
  230. if ($this->transaction_opcount == 0) {
  231. $result = @ifx_query('BEGIN WORK', $this->connection);
  232. if (!$result) {
  233. return $this->ifxRaiseError();
  234. }
  235. }
  236. $this->transaction_opcount++;
  237. }
  238. $result = @ifx_query($query, $this->connection);
  239. }
  240. if (!$result) {
  241. return $this->ifxRaiseError();
  242. }
  243. $this->affected = @ifx_affected_rows($result);
  244. // Determine which queries should return data, and which
  245. // should return an error code only.
  246. if (preg_match('/(SELECT|EXECUTE)/i', $query)) {
  247. return $result;
  248. }
  249. // XXX Testme: free results inside a transaction
  250. // may cause to stop it and commit the work?
  251. // Result has to be freed even with a insert or update
  252. @ifx_free_result($result);
  253. return DB_OK;
  254. }
  255. // }}}
  256. // {{{ nextResult()
  257. /**
  258. * Move the internal ifx result pointer to the next available result
  259. *
  260. * @param a valid fbsql result resource
  261. *
  262. * @access public
  263. *
  264. * @return true if a result is available otherwise return false
  265. */
  266. function nextResult($result)
  267. {
  268. return false;
  269. }
  270. // }}}
  271. // {{{ affectedRows()
  272. /**
  273. * Determines the number of rows affected by a data maniuplation query
  274. *
  275. * 0 is returned for queries that don't manipulate data.
  276. *
  277. * @return int the number of rows. A DB_Error object on failure.
  278. */
  279. function affectedRows()
  280. {
  281. if ($this->_last_query_manip) {
  282. return $this->affected;
  283. } else {
  284. return 0;
  285. }
  286. }
  287. // }}}
  288. // {{{ fetchInto()
  289. /**
  290. * Places a row from the result set into the given array
  291. *
  292. * Formating of the array and the data therein are configurable.
  293. * See DB_result::fetchInto() for more information.
  294. *
  295. * This method is not meant to be called directly. Use
  296. * DB_result::fetchInto() instead. It can't be declared "protected"
  297. * because DB_result is a separate object.
  298. *
  299. * @param resource $result the query result resource
  300. * @param array $arr the referenced array to put the data in
  301. * @param int $fetchmode how the resulting array should be indexed
  302. * @param int $rownum the row number to fetch (0 = first row)
  303. *
  304. * @return mixed DB_OK on success, NULL when the end of a result set is
  305. * reached or on failure
  306. *
  307. * @see DB_result::fetchInto()
  308. */
  309. function fetchInto($result, &$arr, $fetchmode, $rownum = null)
  310. {
  311. if (($rownum !== null) && ($rownum < 0)) {
  312. return null;
  313. }
  314. if ($rownum === null) {
  315. /*
  316. * Even though fetch_row() should return the next row if
  317. * $rownum is null, it doesn't in all cases. Bug 598.
  318. */
  319. $rownum = 'NEXT';
  320. } else {
  321. // Index starts at row 1, unlike most DBMS's starting at 0.
  322. $rownum++;
  323. }
  324. if (!$arr = @ifx_fetch_row($result, $rownum)) {
  325. return null;
  326. }
  327. if ($fetchmode !== DB_FETCHMODE_ASSOC) {
  328. $i=0;
  329. $order = array();
  330. foreach ($arr as $val) {
  331. $order[$i++] = $val;
  332. }
  333. $arr = $order;
  334. } elseif ($fetchmode == DB_FETCHMODE_ASSOC &&
  335. $this->options['portability'] & DB_PORTABILITY_LOWERCASE)
  336. {
  337. $arr = array_change_key_case($arr, CASE_LOWER);
  338. }
  339. if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
  340. $this->_rtrimArrayValues($arr);
  341. }
  342. if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
  343. $this->_convertNullArrayValuesToEmpty($arr);
  344. }
  345. return DB_OK;
  346. }
  347. // }}}
  348. // {{{ numCols()
  349. /**
  350. * Gets the number of columns in a result set
  351. *
  352. * This method is not meant to be called directly. Use
  353. * DB_result::numCols() instead. It can't be declared "protected"
  354. * because DB_result is a separate object.
  355. *
  356. * @param resource $result PHP's query result resource
  357. *
  358. * @return int the number of columns. A DB_Error object on failure.
  359. *
  360. * @see DB_result::numCols()
  361. */
  362. function numCols($result)
  363. {
  364. if (!$cols = @ifx_num_fields($result)) {
  365. return $this->ifxRaiseError();
  366. }
  367. return $cols;
  368. }
  369. // }}}
  370. // {{{ freeResult()
  371. /**
  372. * Deletes the result set and frees the memory occupied by the result set
  373. *
  374. * This method is not meant to be called directly. Use
  375. * DB_result::free() instead. It can't be declared "protected"
  376. * because DB_result is a separate object.
  377. *
  378. * @param resource $result PHP's query result resource
  379. *
  380. * @return bool TRUE on success, FALSE if $result is invalid
  381. *
  382. * @see DB_result::free()
  383. */
  384. function freeResult($result)
  385. {
  386. return is_resource($result) ? ifx_free_result($result) : false;
  387. }
  388. // }}}
  389. // {{{ autoCommit()
  390. /**
  391. * Enables or disables automatic commits
  392. *
  393. * @param bool $onoff true turns it on, false turns it off
  394. *
  395. * @return int DB_OK on success. A DB_Error object if the driver
  396. * doesn't support auto-committing transactions.
  397. */
  398. function autoCommit($onoff = true)
  399. {
  400. // XXX if $this->transaction_opcount > 0, we should probably
  401. // issue a warning here.
  402. $this->autocommit = $onoff ? true : false;
  403. return DB_OK;
  404. }
  405. // }}}
  406. // {{{ commit()
  407. /**
  408. * Commits the current transaction
  409. *
  410. * @return int DB_OK on success. A DB_Error object on failure.
  411. */
  412. function commit()
  413. {
  414. if ($this->transaction_opcount > 0) {
  415. $result = @ifx_query('COMMIT WORK', $this->connection);
  416. $this->transaction_opcount = 0;
  417. if (!$result) {
  418. return $this->ifxRaiseError();
  419. }
  420. }
  421. return DB_OK;
  422. }
  423. // }}}
  424. // {{{ rollback()
  425. /**
  426. * Reverts the current transaction
  427. *
  428. * @return int DB_OK on success. A DB_Error object on failure.
  429. */
  430. function rollback()
  431. {
  432. if ($this->transaction_opcount > 0) {
  433. $result = @ifx_query('ROLLBACK WORK', $this->connection);
  434. $this->transaction_opcount = 0;
  435. if (!$result) {
  436. return $this->ifxRaiseError();
  437. }
  438. }
  439. return DB_OK;
  440. }
  441. // }}}
  442. // {{{ ifxRaiseError()
  443. /**
  444. * Produces a DB_Error object regarding the current problem
  445. *
  446. * @param int $errno if the error is being manually raised pass a
  447. * DB_ERROR* constant here. If this isn't passed
  448. * the error information gathered from the DBMS.
  449. *
  450. * @return object the DB_Error object
  451. *
  452. * @see DB_common::raiseError(),
  453. * DB_ifx::errorNative(), DB_ifx::errorCode()
  454. */
  455. function ifxRaiseError($errno = null)
  456. {
  457. if ($errno === null) {
  458. $errno = $this->errorCode(ifx_error());
  459. }
  460. return $this->raiseError($errno, null, null, null,
  461. $this->errorNative());
  462. }
  463. // }}}
  464. // {{{ errorNative()
  465. /**
  466. * Gets the DBMS' native error code and message produced by the last query
  467. *
  468. * @return string the DBMS' error code and message
  469. */
  470. function errorNative()
  471. {
  472. return @ifx_error() . ' ' . @ifx_errormsg();
  473. }
  474. // }}}
  475. // {{{ errorCode()
  476. /**
  477. * Maps native error codes to DB's portable ones.
  478. *
  479. * Requires that the DB implementation's constructor fills
  480. * in the <var>$errorcode_map</var> property.
  481. *
  482. * @param string $nativecode error code returned by the database
  483. * @return int a portable DB error code, or DB_ERROR if this DB
  484. * implementation has no mapping for the given error code.
  485. */
  486. function errorCode($nativecode)
  487. {
  488. if (preg_match('/SQLCODE=(.*)]/', $nativecode, $match)) {
  489. $code = $match[1];
  490. if (isset($this->errorcode_map[$code])) {
  491. return $this->errorcode_map[$code];
  492. }
  493. }
  494. return DB_ERROR;
  495. }
  496. // }}}
  497. // {{{ tableInfo()
  498. /**
  499. * Returns information about a table or a result set
  500. *
  501. * NOTE: only supports 'table' if <var>$result</var> is a table name.
  502. *
  503. * If analyzing a query result and the result has duplicate field names,
  504. * an error will be raised saying
  505. * <samp>can't distinguish duplicate field names</samp>.
  506. *
  507. * @param object|string $result DB_result object from a query or a
  508. * string containing the name of a table.
  509. * While this also accepts a query result
  510. * resource identifier, this behavior is
  511. * deprecated.
  512. * @param int $mode a valid tableInfo mode
  513. *
  514. * @return array an associative array with the information requested.
  515. * A DB_Error object on failure.
  516. *
  517. * @see DB_common::tableInfo()
  518. * @since Method available since Release 1.6.0
  519. */
  520. function tableInfo($result, $mode = null)
  521. {
  522. if (is_string($result)) {
  523. /*
  524. * Probably received a table name.
  525. * Create a result resource identifier.
  526. */
  527. $id = @ifx_query("SELECT * FROM $result WHERE 1=0",
  528. $this->connection);
  529. $got_string = true;
  530. } elseif (isset($result->result)) {
  531. /*
  532. * Probably received a result object.
  533. * Extract the result resource identifier.
  534. */
  535. $id = $result->result;
  536. $got_string = false;
  537. } else {
  538. /*
  539. * Probably received a result resource identifier.
  540. * Copy it.
  541. */
  542. $id = $result;
  543. $got_string = false;
  544. }
  545. if (!is_resource($id)) {
  546. return $this->ifxRaiseError(DB_ERROR_NEED_MORE_DATA);
  547. }
  548. $flds = @ifx_fieldproperties($id);
  549. $count = @ifx_num_fields($id);
  550. if (count($flds) != $count) {
  551. return $this->raiseError("can't distinguish duplicate field names");
  552. }
  553. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  554. $case_func = 'strtolower';
  555. } else {
  556. $case_func = 'strval';
  557. }
  558. $i = 0;
  559. $res = array();
  560. if ($mode) {
  561. $res['num_fields'] = $count;
  562. }
  563. foreach ($flds as $key => $value) {
  564. $props = explode(';', $value);
  565. $res[$i] = array(
  566. 'table' => $got_string ? $case_func($result) : '',
  567. 'name' => $case_func($key),
  568. 'type' => $props[0],
  569. 'len' => $props[1],
  570. 'flags' => $props[4] == 'N' ? 'not_null' : '',
  571. );
  572. if ($mode & DB_TABLEINFO_ORDER) {
  573. $res['order'][$res[$i]['name']] = $i;
  574. }
  575. if ($mode & DB_TABLEINFO_ORDERTABLE) {
  576. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  577. }
  578. $i++;
  579. }
  580. // free the result only if we were called on a table
  581. if ($got_string) {
  582. @ifx_free_result($id);
  583. }
  584. return $res;
  585. }
  586. // }}}
  587. // {{{ getSpecialQuery()
  588. /**
  589. * Obtains the query string needed for listing a given type of objects
  590. *
  591. * @param string $type the kind of objects you want to retrieve
  592. *
  593. * @return string the SQL query string or null if the driver doesn't
  594. * support the object type requested
  595. *
  596. * @access protected
  597. * @see DB_common::getListOf()
  598. */
  599. function getSpecialQuery($type)
  600. {
  601. switch ($type) {
  602. case 'tables':
  603. return 'SELECT tabname FROM systables WHERE tabid >= 100';
  604. default:
  605. return null;
  606. }
  607. }
  608. // }}}
  609. }
  610. /*
  611. * Local variables:
  612. * tab-width: 4
  613. * c-basic-offset: 4
  614. * End:
  615. */
  616. ?>