ifx.php 19 KB

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