PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/php/DB/sybase.php

https://bitbucket.org/adarshj/convenient_website
PHP | 907 lines | 409 code | 87 blank | 411 comment | 71 complexity | a878f7349a35c6901f972b3a80a29103 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  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 versions 4 and 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-2005 The PHP Group
  21. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  22. * @version CVS: $Id: sybase.php,v 1.78 2005/02/20 00:44:48 danielc Exp $
  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-2005 The PHP Group
  44. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  45. * @version Release: 1.7.6
  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. var $phptype = 'sybase';
  56. /**
  57. * The database syntax variant to be used (db2, access, etc.), if any
  58. * @var string
  59. */
  60. var $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. var $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. var $errorcode_map = array(
  88. );
  89. /**
  90. * The raw database connection created by PHP
  91. * @var resource
  92. */
  93. var $connection;
  94. /**
  95. * The DSN information for connecting to a database
  96. * @var array
  97. */
  98. var $dsn = array();
  99. /**
  100. * Should data manipulation queries be committed automatically?
  101. * @var bool
  102. * @access private
  103. */
  104. var $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. var $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. var $_db = '';
  124. // }}}
  125. // {{{ constructor
  126. /**
  127. * This constructor calls <kbd>$this->DB_common()</kbd>
  128. *
  129. * @return void
  130. */
  131. function DB_sybase()
  132. {
  133. $this->DB_common();
  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. function connect($dsn, $persistent = false)
  154. {
  155. if (!PEAR::loadExtension('sybase') &&
  156. !PEAR::loadExtension('sybase_ct'))
  157. {
  158. return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
  159. }
  160. $this->dsn = $dsn;
  161. if ($dsn['dbsyntax']) {
  162. $this->dbsyntax = $dsn['dbsyntax'];
  163. }
  164. $dsn['hostspec'] = $dsn['hostspec'] ? $dsn['hostspec'] : 'localhost';
  165. $dsn['password'] = !empty($dsn['password']) ? $dsn['password'] : false;
  166. $dsn['charset'] = isset($dsn['charset']) ? $dsn['charset'] : false;
  167. $dsn['appname'] = isset($dsn['appname']) ? $dsn['appname'] : false;
  168. $connect_function = $persistent ? 'sybase_pconnect' : 'sybase_connect';
  169. if ($dsn['username']) {
  170. $this->connection = @$connect_function($dsn['hostspec'],
  171. $dsn['username'],
  172. $dsn['password'],
  173. $dsn['charset'],
  174. $dsn['appname']);
  175. } else {
  176. return $this->raiseError(DB_ERROR_CONNECT_FAILED,
  177. null, null, null,
  178. 'The DSN did not contain a username.');
  179. }
  180. if (!$this->connection) {
  181. return $this->raiseError(DB_ERROR_CONNECT_FAILED,
  182. null, null, null,
  183. @sybase_get_last_message());
  184. }
  185. if ($dsn['database']) {
  186. if (!@sybase_select_db($dsn['database'], $this->connection)) {
  187. return $this->raiseError(DB_ERROR_NODBSELECTED,
  188. null, null, null,
  189. @sybase_get_last_message());
  190. }
  191. $this->_db = $dsn['database'];
  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 = @sybase_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 = DB::isManip($query);
  222. $this->last_query = $query;
  223. if (!@sybase_select_db($this->_db, $this->connection)) {
  224. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  225. }
  226. $query = $this->modifyQuery($query);
  227. if (!$this->autocommit && $ismanip) {
  228. if ($this->transaction_opcount == 0) {
  229. $result = @sybase_query('BEGIN TRANSACTION', $this->connection);
  230. if (!$result) {
  231. return $this->sybaseRaiseError();
  232. }
  233. }
  234. $this->transaction_opcount++;
  235. }
  236. $result = @sybase_query($query, $this->connection);
  237. if (!$result) {
  238. return $this->sybaseRaiseError();
  239. }
  240. if (is_resource($result)) {
  241. return $result;
  242. }
  243. // Determine which queries that should return data, and which
  244. // should return an error code only.
  245. return $ismanip ? DB_OK : $result;
  246. }
  247. // }}}
  248. // {{{ nextResult()
  249. /**
  250. * Move the internal sybase result pointer to the next available result
  251. *
  252. * @param a valid sybase result resource
  253. *
  254. * @access public
  255. *
  256. * @return true if a result is available otherwise return false
  257. */
  258. function nextResult($result)
  259. {
  260. return false;
  261. }
  262. // }}}
  263. // {{{ fetchInto()
  264. /**
  265. * Places a row from the result set into the given array
  266. *
  267. * Formating of the array and the data therein are configurable.
  268. * See DB_result::fetchInto() for more information.
  269. *
  270. * This method is not meant to be called directly. Use
  271. * DB_result::fetchInto() instead. It can't be declared "protected"
  272. * because DB_result is a separate object.
  273. *
  274. * @param resource $result the query result resource
  275. * @param array $arr the referenced array to put the data in
  276. * @param int $fetchmode how the resulting array should be indexed
  277. * @param int $rownum the row number to fetch (0 = first row)
  278. *
  279. * @return mixed DB_OK on success, NULL when the end of a result set is
  280. * reached or on failure
  281. *
  282. * @see DB_result::fetchInto()
  283. */
  284. function fetchInto($result, &$arr, $fetchmode, $rownum = null)
  285. {
  286. if ($rownum !== null) {
  287. if (!@sybase_data_seek($result, $rownum)) {
  288. return null;
  289. }
  290. }
  291. if ($fetchmode & DB_FETCHMODE_ASSOC) {
  292. if (function_exists('sybase_fetch_assoc')) {
  293. $arr = @sybase_fetch_assoc($result);
  294. } else {
  295. if ($arr = @sybase_fetch_array($result)) {
  296. foreach ($arr as $key => $value) {
  297. if (is_int($key)) {
  298. unset($arr[$key]);
  299. }
  300. }
  301. }
  302. }
  303. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
  304. $arr = array_change_key_case($arr, CASE_LOWER);
  305. }
  306. } else {
  307. $arr = @sybase_fetch_row($result);
  308. }
  309. if (!$arr) {
  310. return null;
  311. }
  312. if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
  313. $this->_rtrimArrayValues($arr);
  314. }
  315. if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
  316. $this->_convertNullArrayValuesToEmpty($arr);
  317. }
  318. return DB_OK;
  319. }
  320. // }}}
  321. // {{{ freeResult()
  322. /**
  323. * Deletes the result set and frees the memory occupied by the result set
  324. *
  325. * This method is not meant to be called directly. Use
  326. * DB_result::free() instead. It can't be declared "protected"
  327. * because DB_result is a separate object.
  328. *
  329. * @param resource $result PHP's query result resource
  330. *
  331. * @return bool TRUE on success, FALSE if $result is invalid
  332. *
  333. * @see DB_result::free()
  334. */
  335. function freeResult($result)
  336. {
  337. return @sybase_free_result($result);
  338. }
  339. // }}}
  340. // {{{ numCols()
  341. /**
  342. * Gets the number of columns in a result set
  343. *
  344. * This method is not meant to be called directly. Use
  345. * DB_result::numCols() instead. It can't be declared "protected"
  346. * because DB_result is a separate object.
  347. *
  348. * @param resource $result PHP's query result resource
  349. *
  350. * @return int the number of columns. A DB_Error object on failure.
  351. *
  352. * @see DB_result::numCols()
  353. */
  354. function numCols($result)
  355. {
  356. $cols = @sybase_num_fields($result);
  357. if (!$cols) {
  358. return $this->sybaseRaiseError();
  359. }
  360. return $cols;
  361. }
  362. // }}}
  363. // {{{ numRows()
  364. /**
  365. * Gets the number of rows in a result set
  366. *
  367. * This method is not meant to be called directly. Use
  368. * DB_result::numRows() instead. It can't be declared "protected"
  369. * because DB_result is a separate object.
  370. *
  371. * @param resource $result PHP's query result resource
  372. *
  373. * @return int the number of rows. A DB_Error object on failure.
  374. *
  375. * @see DB_result::numRows()
  376. */
  377. function numRows($result)
  378. {
  379. $rows = @sybase_num_rows($result);
  380. if ($rows === false) {
  381. return $this->sybaseRaiseError();
  382. }
  383. return $rows;
  384. }
  385. // }}}
  386. // {{{ affectedRows()
  387. /**
  388. * Determines the number of rows affected by a data maniuplation query
  389. *
  390. * 0 is returned for queries that don't manipulate data.
  391. *
  392. * @return int the number of rows. A DB_Error object on failure.
  393. */
  394. function affectedRows()
  395. {
  396. if (DB::isManip($this->last_query)) {
  397. $result = @sybase_affected_rows($this->connection);
  398. } else {
  399. $result = 0;
  400. }
  401. return $result;
  402. }
  403. // }}}
  404. // {{{ nextId()
  405. /**
  406. * Returns the next free id in a sequence
  407. *
  408. * @param string $seq_name name of the sequence
  409. * @param boolean $ondemand when true, the seqence is automatically
  410. * created if it does not exist
  411. *
  412. * @return int the next id number in the sequence.
  413. * A DB_Error object on failure.
  414. *
  415. * @see DB_common::nextID(), DB_common::getSequenceName(),
  416. * DB_sybase::createSequence(), DB_sybase::dropSequence()
  417. */
  418. function nextId($seq_name, $ondemand = true)
  419. {
  420. $seqname = $this->getSequenceName($seq_name);
  421. if (!@sybase_select_db($this->_db, $this->connection)) {
  422. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  423. }
  424. $repeat = 0;
  425. do {
  426. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  427. $result = $this->query("INSERT INTO $seqname (vapor) VALUES (0)");
  428. $this->popErrorHandling();
  429. if ($ondemand && DB::isError($result) &&
  430. ($result->getCode() == DB_ERROR || $result->getCode() == DB_ERROR_NOSUCHTABLE))
  431. {
  432. $repeat = 1;
  433. $result = $this->createSequence($seq_name);
  434. if (DB::isError($result)) {
  435. return $this->raiseError($result);
  436. }
  437. } elseif (!DB::isError($result)) {
  438. $result =& $this->query("SELECT @@IDENTITY FROM $seqname");
  439. $repeat = 0;
  440. } else {
  441. $repeat = false;
  442. }
  443. } while ($repeat);
  444. if (DB::isError($result)) {
  445. return $this->raiseError($result);
  446. }
  447. $result = $result->fetchRow(DB_FETCHMODE_ORDERED);
  448. return $result[0];
  449. }
  450. /**
  451. * Creates a new sequence
  452. *
  453. * @param string $seq_name name of the new sequence
  454. *
  455. * @return int DB_OK on success. A DB_Error object on failure.
  456. *
  457. * @see DB_common::createSequence(), DB_common::getSequenceName(),
  458. * DB_sybase::nextID(), DB_sybase::dropSequence()
  459. */
  460. function createSequence($seq_name)
  461. {
  462. return $this->query('CREATE TABLE '
  463. . $this->getSequenceName($seq_name)
  464. . ' (id numeric(10, 0) IDENTITY NOT NULL,'
  465. . ' vapor int NULL)');
  466. }
  467. // }}}
  468. // {{{ dropSequence()
  469. /**
  470. * Deletes a sequence
  471. *
  472. * @param string $seq_name name of the sequence to be deleted
  473. *
  474. * @return int DB_OK on success. A DB_Error object on failure.
  475. *
  476. * @see DB_common::dropSequence(), DB_common::getSequenceName(),
  477. * DB_sybase::nextID(), DB_sybase::createSequence()
  478. */
  479. function dropSequence($seq_name)
  480. {
  481. return $this->query('DROP TABLE ' . $this->getSequenceName($seq_name));
  482. }
  483. // }}}
  484. // {{{ autoCommit()
  485. /**
  486. * Enables or disables automatic commits
  487. *
  488. * @param bool $onoff true turns it on, false turns it off
  489. *
  490. * @return int DB_OK on success. A DB_Error object if the driver
  491. * doesn't support auto-committing transactions.
  492. */
  493. function autoCommit($onoff = false)
  494. {
  495. // XXX if $this->transaction_opcount > 0, we should probably
  496. // issue a warning here.
  497. $this->autocommit = $onoff ? true : false;
  498. return DB_OK;
  499. }
  500. // }}}
  501. // {{{ commit()
  502. /**
  503. * Commits the current transaction
  504. *
  505. * @return int DB_OK on success. A DB_Error object on failure.
  506. */
  507. function commit()
  508. {
  509. if ($this->transaction_opcount > 0) {
  510. if (!@sybase_select_db($this->_db, $this->connection)) {
  511. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  512. }
  513. $result = @sybase_query('COMMIT', $this->connection);
  514. $this->transaction_opcount = 0;
  515. if (!$result) {
  516. return $this->sybaseRaiseError();
  517. }
  518. }
  519. return DB_OK;
  520. }
  521. // }}}
  522. // {{{ rollback()
  523. /**
  524. * Reverts the current transaction
  525. *
  526. * @return int DB_OK on success. A DB_Error object on failure.
  527. */
  528. function rollback()
  529. {
  530. if ($this->transaction_opcount > 0) {
  531. if (!@sybase_select_db($this->_db, $this->connection)) {
  532. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  533. }
  534. $result = @sybase_query('ROLLBACK', $this->connection);
  535. $this->transaction_opcount = 0;
  536. if (!$result) {
  537. return $this->sybaseRaiseError();
  538. }
  539. }
  540. return DB_OK;
  541. }
  542. // }}}
  543. // {{{ sybaseRaiseError()
  544. /**
  545. * Produces a DB_Error object regarding the current problem
  546. *
  547. * @param int $errno if the error is being manually raised pass a
  548. * DB_ERROR* constant here. If this isn't passed
  549. * the error information gathered from the DBMS.
  550. *
  551. * @return object the DB_Error object
  552. *
  553. * @see DB_common::raiseError(),
  554. * DB_sybase::errorNative(), DB_sybase::errorCode()
  555. */
  556. function sybaseRaiseError($errno = null)
  557. {
  558. $native = $this->errorNative();
  559. if ($errno === null) {
  560. $errno = $this->errorCode($native);
  561. }
  562. return $this->raiseError($errno, null, null, null, $native);
  563. }
  564. // }}}
  565. // {{{ errorNative()
  566. /**
  567. * Gets the DBMS' native error message produced by the last query
  568. *
  569. * @return string the DBMS' error message
  570. */
  571. function errorNative()
  572. {
  573. return @sybase_get_last_message();
  574. }
  575. // }}}
  576. // {{{ errorCode()
  577. /**
  578. * Determines PEAR::DB error code from the database's text error message.
  579. *
  580. * @param string $errormsg error message returned from the database
  581. * @return integer an error number from a DB error constant
  582. */
  583. function errorCode($errormsg)
  584. {
  585. static $error_regexps;
  586. if (!isset($error_regexps)) {
  587. $error_regexps = array(
  588. '/Incorrect syntax near/'
  589. => DB_ERROR_SYNTAX,
  590. '/^Unclosed quote before the character string [\"\'].*[\"\']\./'
  591. => DB_ERROR_SYNTAX,
  592. '/Implicit conversion (from datatype|of NUMERIC value)/i'
  593. => DB_ERROR_INVALID_NUMBER,
  594. '/Cannot drop the table [\"\'].+[\"\'], because it doesn\'t exist in the system catalogs\./'
  595. => DB_ERROR_NOSUCHTABLE,
  596. '/Only the owner of object [\"\'].+[\"\'] or a user with System Administrator \(SA\) role can run this command\./'
  597. => DB_ERROR_ACCESS_VIOLATION,
  598. '/^.+ permission denied on object .+, database .+, owner .+/'
  599. => DB_ERROR_ACCESS_VIOLATION,
  600. '/^.* permission denied, database .+, owner .+/'
  601. => DB_ERROR_ACCESS_VIOLATION,
  602. '/[^.*] not found\./'
  603. => DB_ERROR_NOSUCHTABLE,
  604. '/There is already an object named/'
  605. => DB_ERROR_ALREADY_EXISTS,
  606. '/Invalid column name/'
  607. => DB_ERROR_NOSUCHFIELD,
  608. '/does not allow null values/'
  609. => DB_ERROR_CONSTRAINT_NOT_NULL,
  610. '/Command has been aborted/'
  611. => DB_ERROR_CONSTRAINT,
  612. '/^Cannot drop the index .* because it doesn\'t exist/i'
  613. => DB_ERROR_NOT_FOUND,
  614. '/^There is already an index/i'
  615. => DB_ERROR_ALREADY_EXISTS,
  616. '/^There are fewer columns in the INSERT statement than values specified/i'
  617. => DB_ERROR_VALUE_COUNT_ON_ROW,
  618. );
  619. }
  620. foreach ($error_regexps as $regexp => $code) {
  621. if (preg_match($regexp, $errormsg)) {
  622. return $code;
  623. }
  624. }
  625. return DB_ERROR;
  626. }
  627. // }}}
  628. // {{{ tableInfo()
  629. /**
  630. * Returns information about a table or a result set
  631. *
  632. * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  633. * is a table name.
  634. *
  635. * @param object|string $result DB_result object from a query or a
  636. * string containing the name of a table.
  637. * While this also accepts a query result
  638. * resource identifier, this behavior is
  639. * deprecated.
  640. * @param int $mode a valid tableInfo mode
  641. *
  642. * @return array an associative array with the information requested.
  643. * A DB_Error object on failure.
  644. *
  645. * @see DB_common::tableInfo()
  646. * @since Method available since Release 1.6.0
  647. */
  648. function tableInfo($result, $mode = null)
  649. {
  650. if (is_string($result)) {
  651. /*
  652. * Probably received a table name.
  653. * Create a result resource identifier.
  654. */
  655. if (!@sybase_select_db($this->_db, $this->connection)) {
  656. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  657. }
  658. $id = @sybase_query("SELECT * FROM $result WHERE 1=0",
  659. $this->connection);
  660. $got_string = true;
  661. } elseif (isset($result->result)) {
  662. /*
  663. * Probably received a result object.
  664. * Extract the result resource identifier.
  665. */
  666. $id = $result->result;
  667. $got_string = false;
  668. } else {
  669. /*
  670. * Probably received a result resource identifier.
  671. * Copy it.
  672. * Deprecated. Here for compatibility only.
  673. */
  674. $id = $result;
  675. $got_string = false;
  676. }
  677. if (!is_resource($id)) {
  678. return $this->sybaseRaiseError(DB_ERROR_NEED_MORE_DATA);
  679. }
  680. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  681. $case_func = 'strtolower';
  682. } else {
  683. $case_func = 'strval';
  684. }
  685. $count = @sybase_num_fields($id);
  686. $res = array();
  687. if ($mode) {
  688. $res['num_fields'] = $count;
  689. }
  690. for ($i = 0; $i < $count; $i++) {
  691. $f = @sybase_fetch_field($id, $i);
  692. // column_source is often blank
  693. $res[$i] = array(
  694. 'table' => $got_string
  695. ? $case_func($result)
  696. : $case_func($f->column_source),
  697. 'name' => $case_func($f->name),
  698. 'type' => $f->type,
  699. 'len' => $f->max_length,
  700. 'flags' => '',
  701. );
  702. if ($res[$i]['table']) {
  703. $res[$i]['flags'] = $this->_sybase_field_flags(
  704. $res[$i]['table'], $res[$i]['name']);
  705. }
  706. if ($mode & DB_TABLEINFO_ORDER) {
  707. $res['order'][$res[$i]['name']] = $i;
  708. }
  709. if ($mode & DB_TABLEINFO_ORDERTABLE) {
  710. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  711. }
  712. }
  713. // free the result only if we were called on a table
  714. if ($got_string) {
  715. @sybase_free_result($id);
  716. }
  717. return $res;
  718. }
  719. // }}}
  720. // {{{ _sybase_field_flags()
  721. /**
  722. * Get the flags for a field
  723. *
  724. * Currently supports:
  725. * + <samp>unique_key</samp> (unique index, unique check or primary_key)
  726. * + <samp>multiple_key</samp> (multi-key index)
  727. *
  728. * @param string $table the table name
  729. * @param string $column the field name
  730. *
  731. * @return string space delimited string of flags. Empty string if none.
  732. *
  733. * @access private
  734. */
  735. function _sybase_field_flags($table, $column)
  736. {
  737. static $tableName = null;
  738. static $flags = array();
  739. if ($table != $tableName) {
  740. $flags = array();
  741. $tableName = $table;
  742. // get unique/primary keys
  743. $res = $this->getAll("sp_helpindex $table", DB_FETCHMODE_ASSOC);
  744. if (!isset($res[0]['index_description'])) {
  745. return '';
  746. }
  747. foreach ($res as $val) {
  748. $keys = explode(', ', trim($val['index_keys']));
  749. if (sizeof($keys) > 1) {
  750. foreach ($keys as $key) {
  751. $this->_add_flag($flags[$key], 'multiple_key');
  752. }
  753. }
  754. if (strpos($val['index_description'], 'unique')) {
  755. foreach ($keys as $key) {
  756. $this->_add_flag($flags[$key], 'unique_key');
  757. }
  758. }
  759. }
  760. }
  761. if (array_key_exists($column, $flags)) {
  762. return(implode(' ', $flags[$column]));
  763. }
  764. return '';
  765. }
  766. // }}}
  767. // {{{ _add_flag()
  768. /**
  769. * Adds a string to the flags array if the flag is not yet in there
  770. * - if there is no flag present the array is created
  771. *
  772. * @param array $array reference of flags array to add a value to
  773. * @param mixed $value value to add to the flag array
  774. *
  775. * @return void
  776. *
  777. * @access private
  778. */
  779. function _add_flag(&$array, $value)
  780. {
  781. if (!is_array($array)) {
  782. $array = array($value);
  783. } elseif (!in_array($value, $array)) {
  784. array_push($array, $value);
  785. }
  786. }
  787. // }}}
  788. // {{{ getSpecialQuery()
  789. /**
  790. * Obtains the query string needed for listing a given type of objects
  791. *
  792. * @param string $type the kind of objects you want to retrieve
  793. *
  794. * @return string the SQL query string or null if the driver doesn't
  795. * support the object type requested
  796. *
  797. * @access protected
  798. * @see DB_common::getListOf()
  799. */
  800. function getSpecialQuery($type)
  801. {
  802. switch ($type) {
  803. case 'tables':
  804. return "SELECT name FROM sysobjects WHERE type = 'U'"
  805. . ' ORDER BY name';
  806. case 'views':
  807. return "SELECT name FROM sysobjects WHERE type = 'V'";
  808. default:
  809. return null;
  810. }
  811. }
  812. // }}}
  813. }
  814. /*
  815. * Local variables:
  816. * tab-width: 4
  817. * c-basic-offset: 4
  818. * End:
  819. */
  820. ?>