sybase.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * The PEAR DB driver for PHP's sybase extension
  5. * for interacting with Sybase 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 Sterling Hughes <sterling@php.net>
  18. * @author Antônio Carlos Venâncio Júnior <floripa@php.net>
  19. * @author Daniel Convissor <danielc@php.net>
  20. * @copyright 1997-2007 The PHP Group
  21. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  22. * @version CVS: $Id$
  23. * @link http://pear.php.net/package/DB
  24. */
  25. /**
  26. * Obtain the DB_common class so it can be extended from
  27. */
  28. require_once 'DB/common.php';
  29. /**
  30. * The methods PEAR DB uses to interact with PHP's sybase extension
  31. * for interacting with Sybase databases
  32. *
  33. * These methods overload the ones declared in DB_common.
  34. *
  35. * WARNING: This driver may fail with multiple connections under the
  36. * same user/pass/host and different databases.
  37. *
  38. * @category Database
  39. * @package DB
  40. * @author Sterling Hughes <sterling@php.net>
  41. * @author Antônio Carlos Venâncio Júnior <floripa@php.net>
  42. * @author Daniel Convissor <danielc@php.net>
  43. * @copyright 1997-2007 The PHP Group
  44. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  45. * @version Release: 1.9.2
  46. * @link http://pear.php.net/package/DB
  47. */
  48. class DB_sybase extends DB_common
  49. {
  50. // {{{ properties
  51. /**
  52. * The DB driver type (mysql, oci8, odbc, etc.)
  53. * @var string
  54. */
  55. public $phptype = 'sybase';
  56. /**
  57. * The database syntax variant to be used (db2, access, etc.), if any
  58. * @var string
  59. */
  60. public $dbsyntax = 'sybase';
  61. /**
  62. * The capabilities of this DB implementation
  63. *
  64. * The 'new_link' element contains the PHP version that first provided
  65. * new_link support for this DBMS. Contains false if it's unsupported.
  66. *
  67. * Meaning of the 'limit' element:
  68. * + 'emulate' = emulate with fetch row by number
  69. * + 'alter' = alter the query
  70. * + false = skip rows
  71. *
  72. * @var array
  73. */
  74. public $features = array(
  75. 'limit' => 'emulate',
  76. 'new_link' => false,
  77. 'numrows' => true,
  78. 'pconnect' => true,
  79. 'prepare' => false,
  80. 'ssl' => false,
  81. 'transactions' => true,
  82. );
  83. /**
  84. * A mapping of native error codes to DB error codes
  85. * @var array
  86. */
  87. public $errorcode_map = array(
  88. );
  89. /**
  90. * The raw database connection created by PHP
  91. * @var resource
  92. */
  93. public $connection;
  94. /**
  95. * The DSN information for connecting to a database
  96. * @var array
  97. */
  98. public $dsn = array();
  99. /**
  100. * Should data manipulation queries be committed automatically?
  101. * @var bool
  102. * @access private
  103. */
  104. public $autocommit = true;
  105. /**
  106. * The quantity of transactions begun
  107. *
  108. * {@internal While this is private, it can't actually be designated
  109. * private in PHP 5 because it is directly accessed in the test suite.}}
  110. *
  111. * @var integer
  112. * @access private
  113. */
  114. public $transaction_opcount = 0;
  115. /**
  116. * The database specified in the DSN
  117. *
  118. * It's a fix to allow calls to different databases in the same script.
  119. *
  120. * @var string
  121. * @access private
  122. */
  123. public $_db = '';
  124. // }}}
  125. // {{{ constructor
  126. /**
  127. * This constructor calls <kbd>parent::__construct()</kbd>
  128. *
  129. * @return void
  130. */
  131. public function __construct()
  132. {
  133. parent::__construct();
  134. }
  135. // }}}
  136. // {{{ connect()
  137. /**
  138. * Connect to the database server, log in and open the database
  139. *
  140. * Don't call this method directly. Use DB::connect() instead.
  141. *
  142. * PEAR DB's sybase driver supports the following extra DSN options:
  143. * + appname The application name to use on this connection.
  144. * Available since PEAR DB 1.7.0.
  145. * + charset The character set to use on this connection.
  146. * Available since PEAR DB 1.7.0.
  147. *
  148. * @param array $dsn the data source name
  149. * @param bool $persistent should the connection be persistent?
  150. *
  151. * @return int DB_OK on success. A DB_Error object on failure.
  152. */
  153. public function connect($dsn, $persistent = false)
  154. {
  155. if (!PEAR::loadExtension('sybase') &&
  156. !PEAR::loadExtension('sybase_ct')) {
  157. return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
  158. }
  159. $this->dsn = $dsn;
  160. if ($dsn['dbsyntax']) {
  161. $this->dbsyntax = $dsn['dbsyntax'];
  162. }
  163. $dsn['hostspec'] = $dsn['hostspec'] ? $dsn['hostspec'] : 'localhost';
  164. $dsn['password'] = !empty($dsn['password']) ? $dsn['password'] : false;
  165. $dsn['charset'] = isset($dsn['charset']) ? $dsn['charset'] : false;
  166. $dsn['appname'] = isset($dsn['appname']) ? $dsn['appname'] : false;
  167. $connect_function = $persistent ? 'sybase_pconnect' : 'sybase_connect';
  168. if ($dsn['username']) {
  169. $this->connection = @$connect_function(
  170. $dsn['hostspec'],
  171. $dsn['username'],
  172. $dsn['password'],
  173. $dsn['charset'],
  174. $dsn['appname']
  175. );
  176. } else {
  177. return $this->raiseError(
  178. DB_ERROR_CONNECT_FAILED,
  179. null,
  180. null,
  181. null,
  182. 'The DSN did not contain a username.'
  183. );
  184. }
  185. if (!$this->connection) {
  186. return $this->raiseError(
  187. DB_ERROR_CONNECT_FAILED,
  188. null,
  189. null,
  190. null,
  191. @sybase_get_last_message()
  192. );
  193. }
  194. if ($dsn['database']) {
  195. if (!@sybase_select_db($dsn['database'], $this->connection)) {
  196. return $this->raiseError(
  197. DB_ERROR_NODBSELECTED,
  198. null,
  199. null,
  200. null,
  201. @sybase_get_last_message()
  202. );
  203. }
  204. $this->_db = $dsn['database'];
  205. }
  206. return DB_OK;
  207. }
  208. // }}}
  209. // {{{ disconnect()
  210. /**
  211. * Disconnects from the database server
  212. *
  213. * @return bool TRUE on success, FALSE on failure
  214. */
  215. public function disconnect()
  216. {
  217. $ret = @sybase_close($this->connection);
  218. $this->connection = null;
  219. return $ret;
  220. }
  221. // }}}
  222. // {{{ simpleQuery()
  223. /**
  224. * Sends a query to the database server
  225. *
  226. * @param string the SQL query string
  227. *
  228. * @return mixed + a PHP result resrouce for successful SELECT queries
  229. * + the DB_OK constant for other successful queries
  230. * + a DB_Error object on failure
  231. */
  232. public function simpleQuery($query)
  233. {
  234. $ismanip = $this->_checkManip($query);
  235. $this->last_query = $query;
  236. if ($this->_db && !@sybase_select_db($this->_db, $this->connection)) {
  237. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  238. }
  239. $query = $this->modifyQuery($query);
  240. if (!$this->autocommit && $ismanip) {
  241. if ($this->transaction_opcount == 0) {
  242. $result = @sybase_query('BEGIN TRANSACTION', $this->connection);
  243. if (!$result) {
  244. return $this->sybaseRaiseError();
  245. }
  246. }
  247. $this->transaction_opcount++;
  248. }
  249. $result = @sybase_query($query, $this->connection);
  250. if (!$result) {
  251. return $this->sybaseRaiseError();
  252. }
  253. if (is_resource($result)) {
  254. return $result;
  255. }
  256. // Determine which queries that should return data, and which
  257. // should return an error code only.
  258. return $ismanip ? DB_OK : $result;
  259. }
  260. // }}}
  261. // {{{ nextResult()
  262. /**
  263. * Move the internal sybase result pointer to the next available result
  264. *
  265. * @param a valid sybase result resource
  266. *
  267. * @access public
  268. *
  269. * @return true if a result is available otherwise return false
  270. */
  271. public function nextResult($result)
  272. {
  273. return false;
  274. }
  275. // }}}
  276. // {{{ fetchInto()
  277. /**
  278. * Places a row from the result set into the given array
  279. *
  280. * Formating of the array and the data therein are configurable.
  281. * See DB_result::fetchInto() for more information.
  282. *
  283. * This method is not meant to be called directly. Use
  284. * DB_result::fetchInto() instead. It can't be declared "protected"
  285. * because DB_result is a separate object.
  286. *
  287. * @param resource $result the query result resource
  288. * @param array $arr the referenced array to put the data in
  289. * @param int $fetchmode how the resulting array should be indexed
  290. * @param int $rownum the row number to fetch (0 = first row)
  291. *
  292. * @return mixed DB_OK on success, NULL when the end of a result set is
  293. * reached or on failure
  294. *
  295. * @see DB_result::fetchInto()
  296. */
  297. public function fetchInto($result, &$arr, $fetchmode, $rownum = null)
  298. {
  299. if ($rownum !== null) {
  300. if (!@sybase_data_seek($result, $rownum)) {
  301. return null;
  302. }
  303. }
  304. if ($fetchmode & DB_FETCHMODE_ASSOC) {
  305. if (function_exists('sybase_fetch_assoc')) {
  306. $arr = @sybase_fetch_assoc($result);
  307. } else {
  308. if ($arr = @sybase_fetch_array($result)) {
  309. foreach ($arr as $key => $value) {
  310. if (is_int($key)) {
  311. unset($arr[$key]);
  312. }
  313. }
  314. }
  315. }
  316. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
  317. $arr = array_change_key_case($arr, CASE_LOWER);
  318. }
  319. } else {
  320. $arr = @sybase_fetch_row($result);
  321. }
  322. if (!$arr) {
  323. return null;
  324. }
  325. if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
  326. $this->_rtrimArrayValues($arr);
  327. }
  328. if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
  329. $this->_convertNullArrayValuesToEmpty($arr);
  330. }
  331. return DB_OK;
  332. }
  333. // }}}
  334. // {{{ freeResult()
  335. /**
  336. * Deletes the result set and frees the memory occupied by the result set
  337. *
  338. * This method is not meant to be called directly. Use
  339. * DB_result::free() instead. It can't be declared "protected"
  340. * because DB_result is a separate object.
  341. *
  342. * @param resource $result PHP's query result resource
  343. *
  344. * @return bool TRUE on success, FALSE if $result is invalid
  345. *
  346. * @see DB_result::free()
  347. */
  348. public function freeResult($result)
  349. {
  350. return is_resource($result) ? sybase_free_result($result) : false;
  351. }
  352. // }}}
  353. // {{{ numCols()
  354. /**
  355. * Gets the number of columns in a result set
  356. *
  357. * This method is not meant to be called directly. Use
  358. * DB_result::numCols() instead. It can't be declared "protected"
  359. * because DB_result is a separate object.
  360. *
  361. * @param resource $result PHP's query result resource
  362. *
  363. * @return int the number of columns. A DB_Error object on failure.
  364. *
  365. * @see DB_result::numCols()
  366. */
  367. public function numCols($result)
  368. {
  369. $cols = @sybase_num_fields($result);
  370. if (!$cols) {
  371. return $this->sybaseRaiseError();
  372. }
  373. return $cols;
  374. }
  375. // }}}
  376. // {{{ numRows()
  377. /**
  378. * Gets the number of rows in a result set
  379. *
  380. * This method is not meant to be called directly. Use
  381. * DB_result::numRows() instead. It can't be declared "protected"
  382. * because DB_result is a separate object.
  383. *
  384. * @param resource $result PHP's query result resource
  385. *
  386. * @return int the number of rows. A DB_Error object on failure.
  387. *
  388. * @see DB_result::numRows()
  389. */
  390. public function numRows($result)
  391. {
  392. $rows = @sybase_num_rows($result);
  393. if ($rows === false) {
  394. return $this->sybaseRaiseError();
  395. }
  396. return $rows;
  397. }
  398. // }}}
  399. // {{{ affectedRows()
  400. /**
  401. * Determines the number of rows affected by a data maniuplation query
  402. *
  403. * 0 is returned for queries that don't manipulate data.
  404. *
  405. * @return int the number of rows. A DB_Error object on failure.
  406. */
  407. public function affectedRows()
  408. {
  409. if ($this->_last_query_manip) {
  410. $result = @sybase_affected_rows($this->connection);
  411. } else {
  412. $result = 0;
  413. }
  414. return $result;
  415. }
  416. // }}}
  417. // {{{ nextId()
  418. /**
  419. * Returns the next free id in a sequence
  420. *
  421. * @param string $seq_name name of the sequence
  422. * @param boolean $ondemand when true, the seqence is automatically
  423. * created if it does not exist
  424. *
  425. * @return int the next id number in the sequence.
  426. * A DB_Error object on failure.
  427. *
  428. * @see DB_common::nextID(), DB_common::getSequenceName(),
  429. * DB_sybase::createSequence(), DB_sybase::dropSequence()
  430. */
  431. public function nextId($seq_name, $ondemand = true)
  432. {
  433. $seqname = $this->getSequenceName($seq_name);
  434. if ($this->_db && !@sybase_select_db($this->_db, $this->connection)) {
  435. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  436. }
  437. $repeat = 0;
  438. do {
  439. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  440. $result = $this->query("INSERT INTO $seqname (vapor) VALUES (0)");
  441. $this->popErrorHandling();
  442. if ($ondemand && DB::isError($result) &&
  443. ($result->getCode() == DB_ERROR || $result->getCode() == DB_ERROR_NOSUCHTABLE)) {
  444. $repeat = 1;
  445. $result = $this->createSequence($seq_name);
  446. if (DB::isError($result)) {
  447. return $this->raiseError($result);
  448. }
  449. } elseif (!DB::isError($result)) {
  450. $result = $this->query("SELECT @@IDENTITY FROM $seqname");
  451. $repeat = 0;
  452. } else {
  453. $repeat = false;
  454. }
  455. } while ($repeat);
  456. if (DB::isError($result)) {
  457. return $this->raiseError($result);
  458. }
  459. $result = $result->fetchRow(DB_FETCHMODE_ORDERED);
  460. return $result[0];
  461. }
  462. /**
  463. * Creates a new sequence
  464. *
  465. * @param string $seq_name name of the new sequence
  466. *
  467. * @return int DB_OK on success. A DB_Error object on failure.
  468. *
  469. * @see DB_common::createSequence(), DB_common::getSequenceName(),
  470. * DB_sybase::nextID(), DB_sybase::dropSequence()
  471. */
  472. public function createSequence($seq_name)
  473. {
  474. return $this->query('CREATE TABLE '
  475. . $this->getSequenceName($seq_name)
  476. . ' (id numeric(10, 0) IDENTITY NOT NULL,'
  477. . ' vapor int NULL)');
  478. }
  479. // }}}
  480. // {{{ dropSequence()
  481. /**
  482. * Deletes a sequence
  483. *
  484. * @param string $seq_name name of the sequence to be deleted
  485. *
  486. * @return int DB_OK on success. A DB_Error object on failure.
  487. *
  488. * @see DB_common::dropSequence(), DB_common::getSequenceName(),
  489. * DB_sybase::nextID(), DB_sybase::createSequence()
  490. */
  491. public function dropSequence($seq_name)
  492. {
  493. return $this->query('DROP TABLE ' . $this->getSequenceName($seq_name));
  494. }
  495. // }}}
  496. // {{{ quoteFloat()
  497. /**
  498. * Formats a float value for use within a query in a locale-independent
  499. * manner.
  500. *
  501. * @param float the float value to be quoted.
  502. * @return string the quoted string.
  503. * @see DB_common::quoteSmart()
  504. * @since Method available since release 1.7.8.
  505. */
  506. public function quoteFloat($float)
  507. {
  508. return $this->escapeSimple(str_replace(',', '.', strval(floatval($float))));
  509. }
  510. // }}}
  511. // {{{ autoCommit()
  512. /**
  513. * Enables or disables automatic commits
  514. *
  515. * @param bool $onoff true turns it on, false turns it off
  516. *
  517. * @return int DB_OK on success. A DB_Error object if the driver
  518. * doesn't support auto-committing transactions.
  519. */
  520. public function autoCommit($onoff = false)
  521. {
  522. // XXX if $this->transaction_opcount > 0, we should probably
  523. // issue a warning here.
  524. $this->autocommit = $onoff ? true : false;
  525. return DB_OK;
  526. }
  527. // }}}
  528. // {{{ commit()
  529. /**
  530. * Commits the current transaction
  531. *
  532. * @return int DB_OK on success. A DB_Error object on failure.
  533. */
  534. public function commit()
  535. {
  536. if ($this->transaction_opcount > 0) {
  537. if ($this->_db && !@sybase_select_db($this->_db, $this->connection)) {
  538. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  539. }
  540. $result = @sybase_query('COMMIT', $this->connection);
  541. $this->transaction_opcount = 0;
  542. if (!$result) {
  543. return $this->sybaseRaiseError();
  544. }
  545. }
  546. return DB_OK;
  547. }
  548. // }}}
  549. // {{{ rollback()
  550. /**
  551. * Reverts the current transaction
  552. *
  553. * @return int DB_OK on success. A DB_Error object on failure.
  554. */
  555. public function rollback()
  556. {
  557. if ($this->transaction_opcount > 0) {
  558. if ($this->_db && !@sybase_select_db($this->_db, $this->connection)) {
  559. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  560. }
  561. $result = @sybase_query('ROLLBACK', $this->connection);
  562. $this->transaction_opcount = 0;
  563. if (!$result) {
  564. return $this->sybaseRaiseError();
  565. }
  566. }
  567. return DB_OK;
  568. }
  569. // }}}
  570. // {{{ sybaseRaiseError()
  571. /**
  572. * Produces a DB_Error object regarding the current problem
  573. *
  574. * @param int $errno if the error is being manually raised pass a
  575. * DB_ERROR* constant here. If this isn't passed
  576. * the error information gathered from the DBMS.
  577. *
  578. * @return object the DB_Error object
  579. *
  580. * @see DB_common::raiseError(),
  581. * DB_sybase::errorNative(), DB_sybase::errorCode()
  582. */
  583. public function sybaseRaiseError($errno = null)
  584. {
  585. $native = $this->errorNative();
  586. if ($errno === null) {
  587. $errno = $this->errorCode($native);
  588. }
  589. return $this->raiseError($errno, null, null, null, $native);
  590. }
  591. // }}}
  592. // {{{ errorNative()
  593. /**
  594. * Gets the DBMS' native error message produced by the last query
  595. *
  596. * @return string the DBMS' error message
  597. */
  598. public function errorNative()
  599. {
  600. return @sybase_get_last_message();
  601. }
  602. // }}}
  603. // {{{ errorCode()
  604. /**
  605. * Determines PEAR::DB error code from the database's text error message.
  606. *
  607. * @param string $errormsg error message returned from the database
  608. * @return integer an error number from a DB error constant
  609. */
  610. public function errorCode($errormsg)
  611. {
  612. static $error_regexps;
  613. // PHP 5.2+ prepends the function name to $php_errormsg, so we need
  614. // this hack to work around it, per bug #9599.
  615. $errormsg = preg_replace('/^sybase[a-z_]+\(\): /', '', $errormsg);
  616. if (!isset($error_regexps)) {
  617. $error_regexps = array(
  618. '/Incorrect syntax near/'
  619. => DB_ERROR_SYNTAX,
  620. '/^Unclosed quote before the character string [\"\'].*[\"\']\./'
  621. => DB_ERROR_SYNTAX,
  622. '/Implicit conversion (from datatype|of NUMERIC value)/i'
  623. => DB_ERROR_INVALID_NUMBER,
  624. '/Cannot drop the table [\"\'].+[\"\'], because it doesn\'t exist in the system catalogs\./'
  625. => DB_ERROR_NOSUCHTABLE,
  626. '/Only the owner of object [\"\'].+[\"\'] or a user with System Administrator \(SA\) role can run this command\./'
  627. => DB_ERROR_ACCESS_VIOLATION,
  628. '/^.+ permission denied on object .+, database .+, owner .+/'
  629. => DB_ERROR_ACCESS_VIOLATION,
  630. '/^.* permission denied, database .+, owner .+/'
  631. => DB_ERROR_ACCESS_VIOLATION,
  632. '/[^.*] not found\./'
  633. => DB_ERROR_NOSUCHTABLE,
  634. '/There is already an object named/'
  635. => DB_ERROR_ALREADY_EXISTS,
  636. '/Invalid column name/'
  637. => DB_ERROR_NOSUCHFIELD,
  638. '/does not allow null values/'
  639. => DB_ERROR_CONSTRAINT_NOT_NULL,
  640. '/Command has been aborted/'
  641. => DB_ERROR_CONSTRAINT,
  642. '/^Cannot drop the index .* because it doesn\'t exist/i'
  643. => DB_ERROR_NOT_FOUND,
  644. '/^There is already an index/i'
  645. => DB_ERROR_ALREADY_EXISTS,
  646. '/^There are fewer columns in the INSERT statement than values specified/i'
  647. => DB_ERROR_VALUE_COUNT_ON_ROW,
  648. '/Divide by zero/i'
  649. => DB_ERROR_DIVZERO,
  650. );
  651. }
  652. foreach ($error_regexps as $regexp => $code) {
  653. if (preg_match($regexp, $errormsg)) {
  654. return $code;
  655. }
  656. }
  657. return DB_ERROR;
  658. }
  659. // }}}
  660. // {{{ tableInfo()
  661. /**
  662. * Returns information about a table or a result set
  663. *
  664. * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  665. * is a table name.
  666. *
  667. * @param object|string $result DB_result object from a query or a
  668. * string containing the name of a table.
  669. * While this also accepts a query result
  670. * resource identifier, this behavior is
  671. * deprecated.
  672. * @param int $mode a valid tableInfo mode
  673. *
  674. * @return array an associative array with the information requested.
  675. * A DB_Error object on failure.
  676. *
  677. * @see DB_common::tableInfo()
  678. * @since Method available since Release 1.6.0
  679. */
  680. public function tableInfo($result, $mode = null)
  681. {
  682. if (is_string($result)) {
  683. /*
  684. * Probably received a table name.
  685. * Create a result resource identifier.
  686. */
  687. if ($this->_db && !@sybase_select_db($this->_db, $this->connection)) {
  688. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  689. }
  690. $id = @sybase_query(
  691. "SELECT * FROM $result WHERE 1=0",
  692. $this->connection
  693. );
  694. $got_string = true;
  695. } elseif (isset($result->result)) {
  696. /*
  697. * Probably received a result object.
  698. * Extract the result resource identifier.
  699. */
  700. $id = $result->result;
  701. $got_string = false;
  702. } else {
  703. /*
  704. * Probably received a result resource identifier.
  705. * Copy it.
  706. * Deprecated. Here for compatibility only.
  707. */
  708. $id = $result;
  709. $got_string = false;
  710. }
  711. if (!is_resource($id)) {
  712. return $this->sybaseRaiseError(DB_ERROR_NEED_MORE_DATA);
  713. }
  714. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  715. $case_func = 'strtolower';
  716. } else {
  717. $case_func = 'strval';
  718. }
  719. $count = @sybase_num_fields($id);
  720. $res = array();
  721. if ($mode) {
  722. $res['num_fields'] = $count;
  723. }
  724. for ($i = 0; $i < $count; $i++) {
  725. $f = @sybase_fetch_field($id, $i);
  726. // column_source is often blank
  727. $res[$i] = array(
  728. 'table' => $got_string
  729. ? $case_func($result)
  730. : $case_func($f->column_source),
  731. 'name' => $case_func($f->name),
  732. 'type' => $f->type,
  733. 'len' => $f->max_length,
  734. 'flags' => '',
  735. );
  736. if ($res[$i]['table']) {
  737. $res[$i]['flags'] = $this->_sybase_field_flags(
  738. $res[$i]['table'],
  739. $res[$i]['name']
  740. );
  741. }
  742. if ($mode & DB_TABLEINFO_ORDER) {
  743. $res['order'][$res[$i]['name']] = $i;
  744. }
  745. if ($mode & DB_TABLEINFO_ORDERTABLE) {
  746. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  747. }
  748. }
  749. // free the result only if we were called on a table
  750. if ($got_string) {
  751. @sybase_free_result($id);
  752. }
  753. return $res;
  754. }
  755. // }}}
  756. // {{{ _sybase_field_flags()
  757. /**
  758. * Get the flags for a field
  759. *
  760. * Currently supports:
  761. * + <samp>unique_key</samp> (unique index, unique check or primary_key)
  762. * + <samp>multiple_key</samp> (multi-key index)
  763. *
  764. * @param string $table the table name
  765. * @param string $column the field name
  766. *
  767. * @return string space delimited string of flags. Empty string if none.
  768. *
  769. * @access private
  770. */
  771. public function _sybase_field_flags($table, $column)
  772. {
  773. static $tableName = null;
  774. static $flags = array();
  775. if ($table != $tableName) {
  776. $flags = array();
  777. $tableName = $table;
  778. /* We're running sp_helpindex directly because it doesn't exist in
  779. * older versions of ASE -- unfortunately, we can't just use
  780. * DB::isError() because the user may be using callback error
  781. * handling. */
  782. $res = @sybase_query("sp_helpindex $table", $this->connection);
  783. if ($res === false || $res === true) {
  784. // Fake a valid response for BC reasons.
  785. return '';
  786. }
  787. while (($val = sybase_fetch_assoc($res)) !== false) {
  788. if (!isset($val['index_keys'])) {
  789. /* No useful information returned. Break and be done with
  790. * it, which preserves the pre-1.7.9 behaviour. */
  791. break;
  792. }
  793. $keys = explode(', ', trim($val['index_keys']));
  794. if (sizeof($keys) > 1) {
  795. foreach ($keys as $key) {
  796. $this->_add_flag($flags[$key], 'multiple_key');
  797. }
  798. }
  799. if (strpos($val['index_description'], 'unique')) {
  800. foreach ($keys as $key) {
  801. $this->_add_flag($flags[$key], 'unique_key');
  802. }
  803. }
  804. }
  805. sybase_free_result($res);
  806. }
  807. if (array_key_exists($column, $flags)) {
  808. return(implode(' ', $flags[$column]));
  809. }
  810. return '';
  811. }
  812. // }}}
  813. // {{{ _add_flag()
  814. /**
  815. * Adds a string to the flags array if the flag is not yet in there
  816. * - if there is no flag present the array is created
  817. *
  818. * @param array $array reference of flags array to add a value to
  819. * @param mixed $value value to add to the flag array
  820. *
  821. * @return void
  822. *
  823. * @access private
  824. */
  825. public function _add_flag(&$array, $value)
  826. {
  827. if (!is_array($array)) {
  828. $array = array($value);
  829. } elseif (!in_array($value, $array)) {
  830. array_push($array, $value);
  831. }
  832. }
  833. // }}}
  834. // {{{ getSpecialQuery()
  835. /**
  836. * Obtains the query string needed for listing a given type of objects
  837. *
  838. * @param string $type the kind of objects you want to retrieve
  839. *
  840. * @return string the SQL query string or null if the driver doesn't
  841. * support the object type requested
  842. *
  843. * @access protected
  844. * @see DB_common::getListOf()
  845. */
  846. public function getSpecialQuery($type)
  847. {
  848. switch ($type) {
  849. case 'tables':
  850. return "SELECT name FROM sysobjects WHERE type = 'U'"
  851. . ' ORDER BY name';
  852. case 'views':
  853. return "SELECT name FROM sysobjects WHERE type = 'V'";
  854. default:
  855. return null;
  856. }
  857. }
  858. // }}}
  859. }
  860. /*
  861. * Local variables:
  862. * tab-width: 4
  863. * c-basic-offset: 4
  864. * End:
  865. */