ifx.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. public $phptype = 'ifx';
  58. /**
  59. * The database syntax variant to be used (db2, access, etc.), if any
  60. * @var string
  61. */
  62. public $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. public $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. public $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. public $connection;
  123. /**
  124. * The DSN information for connecting to a database
  125. * @var array
  126. */
  127. public $dsn = array();
  128. /**
  129. * Should data manipulation queries be committed automatically?
  130. * @var bool
  131. * @access private
  132. */
  133. public $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. public $transaction_opcount = 0;
  144. /**
  145. * The number of rows affected by a data manipulation query
  146. * @var integer
  147. * @access private
  148. */
  149. public $affected = 0;
  150. // }}}
  151. // {{{ constructor
  152. /**
  153. * This constructor calls <kbd>parent::__construct()</kbd>
  154. *
  155. * @return void
  156. */
  157. public 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. public function connect($dsn, $persistent = false)
  174. {
  175. if (!PEAR::loadExtension('informix') &&
  176. !PEAR::loadExtension('Informix')) {
  177. return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
  178. }
  179. $this->dsn = $dsn;
  180. if ($dsn['dbsyntax']) {
  181. $this->dbsyntax = $dsn['dbsyntax'];
  182. }
  183. $dbhost = $dsn['hostspec'] ? '@' . $dsn['hostspec'] : '';
  184. $dbname = $dsn['database'] ? $dsn['database'] . $dbhost : '';
  185. $user = $dsn['username'] ? $dsn['username'] : '';
  186. $pw = $dsn['password'] ? $dsn['password'] : '';
  187. $connect_function = $persistent ? 'ifx_pconnect' : 'ifx_connect';
  188. $this->connection = @$connect_function($dbname, $user, $pw);
  189. if (!is_resource($this->connection)) {
  190. return $this->ifxRaiseError(DB_ERROR_CONNECT_FAILED);
  191. }
  192. return DB_OK;
  193. }
  194. // }}}
  195. // {{{ disconnect()
  196. /**
  197. * Disconnects from the database server
  198. *
  199. * @return bool TRUE on success, FALSE on failure
  200. */
  201. public function disconnect()
  202. {
  203. $ret = @ifx_close($this->connection);
  204. $this->connection = null;
  205. return $ret;
  206. }
  207. // }}}
  208. // {{{ simpleQuery()
  209. /**
  210. * Sends a query to the database server
  211. *
  212. * @param string the SQL query string
  213. *
  214. * @return mixed + a PHP result resrouce for successful SELECT queries
  215. * + the DB_OK constant for other successful queries
  216. * + a DB_Error object on failure
  217. */
  218. public function simpleQuery($query)
  219. {
  220. $ismanip = $this->_checkManip($query);
  221. $this->last_query = $query;
  222. $this->affected = null;
  223. if (preg_match('/(SELECT|EXECUTE)/i', $query)) { //TESTME: Use !DB::isManip()?
  224. // the scroll is needed for fetching absolute row numbers
  225. // in a select query result
  226. $result = @ifx_query($query, $this->connection, IFX_SCROLL);
  227. } else {
  228. if (!$this->autocommit && $ismanip) {
  229. if ($this->transaction_opcount == 0) {
  230. $result = @ifx_query('BEGIN WORK', $this->connection);
  231. if (!$result) {
  232. return $this->ifxRaiseError();
  233. }
  234. }
  235. $this->transaction_opcount++;
  236. }
  237. $result = @ifx_query($query, $this->connection);
  238. }
  239. if (!$result) {
  240. return $this->ifxRaiseError();
  241. }
  242. $this->affected = @ifx_affected_rows($result);
  243. // Determine which queries should return data, and which
  244. // should return an error code only.
  245. if (preg_match('/(SELECT|EXECUTE)/i', $query)) {
  246. return $result;
  247. }
  248. // XXX Testme: free results inside a transaction
  249. // may cause to stop it and commit the work?
  250. // Result has to be freed even with a insert or update
  251. @ifx_free_result($result);
  252. return DB_OK;
  253. }
  254. // }}}
  255. // {{{ nextResult()
  256. /**
  257. * Move the internal ifx result pointer to the next available result
  258. *
  259. * @param a valid fbsql result resource
  260. *
  261. * @access public
  262. *
  263. * @return true if a result is available otherwise return false
  264. */
  265. public function nextResult($result)
  266. {
  267. return false;
  268. }
  269. // }}}
  270. // {{{ affectedRows()
  271. /**
  272. * Determines the number of rows affected by a data maniuplation query
  273. *
  274. * 0 is returned for queries that don't manipulate data.
  275. *
  276. * @return int the number of rows. A DB_Error object on failure.
  277. */
  278. public function affectedRows()
  279. {
  280. if ($this->_last_query_manip) {
  281. return $this->affected;
  282. } else {
  283. return 0;
  284. }
  285. }
  286. // }}}
  287. // {{{ fetchInto()
  288. /**
  289. * Places a row from the result set into the given array
  290. *
  291. * Formating of the array and the data therein are configurable.
  292. * See DB_result::fetchInto() for more information.
  293. *
  294. * This method is not meant to be called directly. Use
  295. * DB_result::fetchInto() instead. It can't be declared "protected"
  296. * because DB_result is a separate object.
  297. *
  298. * @param resource $result the query result resource
  299. * @param array $arr the referenced array to put the data in
  300. * @param int $fetchmode how the resulting array should be indexed
  301. * @param int $rownum the row number to fetch (0 = first row)
  302. *
  303. * @return mixed DB_OK on success, NULL when the end of a result set is
  304. * reached or on failure
  305. *
  306. * @see DB_result::fetchInto()
  307. */
  308. public function fetchInto($result, &$arr, $fetchmode, $rownum = null)
  309. {
  310. if (($rownum !== null) && ($rownum < 0)) {
  311. return null;
  312. }
  313. if ($rownum === null) {
  314. /*
  315. * Even though fetch_row() should return the next row if
  316. * $rownum is null, it doesn't in all cases. Bug 598.
  317. */
  318. $rownum = 'NEXT';
  319. } else {
  320. // Index starts at row 1, unlike most DBMS's starting at 0.
  321. $rownum++;
  322. }
  323. if (!$arr = @ifx_fetch_row($result, $rownum)) {
  324. return null;
  325. }
  326. if ($fetchmode !== DB_FETCHMODE_ASSOC) {
  327. $i=0;
  328. $order = array();
  329. foreach ($arr as $val) {
  330. $order[$i++] = $val;
  331. }
  332. $arr = $order;
  333. } elseif ($fetchmode == DB_FETCHMODE_ASSOC &&
  334. $this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  335. $arr = array_change_key_case($arr, CASE_LOWER);
  336. }
  337. if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
  338. $this->_rtrimArrayValues($arr);
  339. }
  340. if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
  341. $this->_convertNullArrayValuesToEmpty($arr);
  342. }
  343. return DB_OK;
  344. }
  345. // }}}
  346. // {{{ numCols()
  347. /**
  348. * Gets the number of columns in a result set
  349. *
  350. * This method is not meant to be called directly. Use
  351. * DB_result::numCols() instead. It can't be declared "protected"
  352. * because DB_result is a separate object.
  353. *
  354. * @param resource $result PHP's query result resource
  355. *
  356. * @return int the number of columns. A DB_Error object on failure.
  357. *
  358. * @see DB_result::numCols()
  359. */
  360. public function numCols($result)
  361. {
  362. if (!$cols = @ifx_num_fields($result)) {
  363. return $this->ifxRaiseError();
  364. }
  365. return $cols;
  366. }
  367. // }}}
  368. // {{{ freeResult()
  369. /**
  370. * Deletes the result set and frees the memory occupied by the result set
  371. *
  372. * This method is not meant to be called directly. Use
  373. * DB_result::free() instead. It can't be declared "protected"
  374. * because DB_result is a separate object.
  375. *
  376. * @param resource $result PHP's query result resource
  377. *
  378. * @return bool TRUE on success, FALSE if $result is invalid
  379. *
  380. * @see DB_result::free()
  381. */
  382. public function freeResult($result)
  383. {
  384. return is_resource($result) ? ifx_free_result($result) : false;
  385. }
  386. // }}}
  387. // {{{ autoCommit()
  388. /**
  389. * Enables or disables automatic commits
  390. *
  391. * @param bool $onoff true turns it on, false turns it off
  392. *
  393. * @return int DB_OK on success. A DB_Error object if the driver
  394. * doesn't support auto-committing transactions.
  395. */
  396. public function autoCommit($onoff = true)
  397. {
  398. // XXX if $this->transaction_opcount > 0, we should probably
  399. // issue a warning here.
  400. $this->autocommit = $onoff ? true : false;
  401. return DB_OK;
  402. }
  403. // }}}
  404. // {{{ commit()
  405. /**
  406. * Commits the current transaction
  407. *
  408. * @return int DB_OK on success. A DB_Error object on failure.
  409. */
  410. public function commit()
  411. {
  412. if ($this->transaction_opcount > 0) {
  413. $result = @ifx_query('COMMIT WORK', $this->connection);
  414. $this->transaction_opcount = 0;
  415. if (!$result) {
  416. return $this->ifxRaiseError();
  417. }
  418. }
  419. return DB_OK;
  420. }
  421. // }}}
  422. // {{{ rollback()
  423. /**
  424. * Reverts the current transaction
  425. *
  426. * @return int DB_OK on success. A DB_Error object on failure.
  427. */
  428. public function rollback()
  429. {
  430. if ($this->transaction_opcount > 0) {
  431. $result = @ifx_query('ROLLBACK WORK', $this->connection);
  432. $this->transaction_opcount = 0;
  433. if (!$result) {
  434. return $this->ifxRaiseError();
  435. }
  436. }
  437. return DB_OK;
  438. }
  439. // }}}
  440. // {{{ ifxRaiseError()
  441. /**
  442. * Produces a DB_Error object regarding the current problem
  443. *
  444. * @param int $errno if the error is being manually raised pass a
  445. * DB_ERROR* constant here. If this isn't passed
  446. * the error information gathered from the DBMS.
  447. *
  448. * @return object the DB_Error object
  449. *
  450. * @see DB_common::raiseError(),
  451. * DB_ifx::errorNative(), DB_ifx::errorCode()
  452. */
  453. public function ifxRaiseError($errno = null)
  454. {
  455. if ($errno === null) {
  456. $errno = $this->errorCode(ifx_error());
  457. }
  458. return $this->raiseError(
  459. $errno,
  460. null,
  461. null,
  462. null,
  463. $this->errorNative()
  464. );
  465. }
  466. // }}}
  467. // {{{ errorNative()
  468. /**
  469. * Gets the DBMS' native error code and message produced by the last query
  470. *
  471. * @return string the DBMS' error code and message
  472. */
  473. public function errorNative()
  474. {
  475. return @ifx_error() . ' ' . @ifx_errormsg();
  476. }
  477. // }}}
  478. // {{{ errorCode()
  479. /**
  480. * Maps native error codes to DB's portable ones.
  481. *
  482. * Requires that the DB implementation's constructor fills
  483. * in the <var>$errorcode_map</var> property.
  484. *
  485. * @param string $nativecode error code returned by the database
  486. * @return int a portable DB error code, or DB_ERROR if this DB
  487. * implementation has no mapping for the given error code.
  488. */
  489. public function errorCode($nativecode)
  490. {
  491. if (preg_match('/SQLCODE=(.*)]/', $nativecode, $match)) {
  492. $code = $match[1];
  493. if (isset($this->errorcode_map[$code])) {
  494. return $this->errorcode_map[$code];
  495. }
  496. }
  497. return DB_ERROR;
  498. }
  499. // }}}
  500. // {{{ tableInfo()
  501. /**
  502. * Returns information about a table or a result set
  503. *
  504. * NOTE: only supports 'table' if <var>$result</var> is a table name.
  505. *
  506. * If analyzing a query result and the result has duplicate field names,
  507. * an error will be raised saying
  508. * <samp>can't distinguish duplicate field names</samp>.
  509. *
  510. * @param object|string $result DB_result object from a query or a
  511. * string containing the name of a table.
  512. * While this also accepts a query result
  513. * resource identifier, this behavior is
  514. * deprecated.
  515. * @param int $mode a valid tableInfo mode
  516. *
  517. * @return array an associative array with the information requested.
  518. * A DB_Error object on failure.
  519. *
  520. * @see DB_common::tableInfo()
  521. * @since Method available since Release 1.6.0
  522. */
  523. public function tableInfo($result, $mode = null)
  524. {
  525. if (is_string($result)) {
  526. /*
  527. * Probably received a table name.
  528. * Create a result resource identifier.
  529. */
  530. $id = @ifx_query(
  531. "SELECT * FROM $result WHERE 1=0",
  532. $this->connection
  533. );
  534. $got_string = true;
  535. } elseif (isset($result->result)) {
  536. /*
  537. * Probably received a result object.
  538. * Extract the result resource identifier.
  539. */
  540. $id = $result->result;
  541. $got_string = false;
  542. } else {
  543. /*
  544. * Probably received a result resource identifier.
  545. * Copy it.
  546. */
  547. $id = $result;
  548. $got_string = false;
  549. }
  550. if (!is_resource($id)) {
  551. return $this->ifxRaiseError(DB_ERROR_NEED_MORE_DATA);
  552. }
  553. $flds = @ifx_fieldproperties($id);
  554. $count = @ifx_num_fields($id);
  555. if (count($flds) != $count) {
  556. return $this->raiseError("can't distinguish duplicate field names");
  557. }
  558. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  559. $case_func = 'strtolower';
  560. } else {
  561. $case_func = 'strval';
  562. }
  563. $i = 0;
  564. $res = array();
  565. if ($mode) {
  566. $res['num_fields'] = $count;
  567. }
  568. foreach ($flds as $key => $value) {
  569. $props = explode(';', $value);
  570. $res[$i] = array(
  571. 'table' => $got_string ? $case_func($result) : '',
  572. 'name' => $case_func($key),
  573. 'type' => $props[0],
  574. 'len' => $props[1],
  575. 'flags' => $props[4] == 'N' ? 'not_null' : '',
  576. );
  577. if ($mode & DB_TABLEINFO_ORDER) {
  578. $res['order'][$res[$i]['name']] = $i;
  579. }
  580. if ($mode & DB_TABLEINFO_ORDERTABLE) {
  581. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  582. }
  583. $i++;
  584. }
  585. // free the result only if we were called on a table
  586. if ($got_string) {
  587. @ifx_free_result($id);
  588. }
  589. return $res;
  590. }
  591. // }}}
  592. // {{{ getSpecialQuery()
  593. /**
  594. * Obtains the query string needed for listing a given type of objects
  595. *
  596. * @param string $type the kind of objects you want to retrieve
  597. *
  598. * @return string the SQL query string or null if the driver doesn't
  599. * support the object type requested
  600. *
  601. * @access protected
  602. * @see DB_common::getListOf()
  603. */
  604. public function getSpecialQuery($type)
  605. {
  606. switch ($type) {
  607. case 'tables':
  608. return 'SELECT tabname FROM systables WHERE tabid >= 100';
  609. default:
  610. return null;
  611. }
  612. }
  613. // }}}
  614. }
  615. /*
  616. * Local variables:
  617. * tab-width: 4
  618. * c-basic-offset: 4
  619. * End:
  620. */