sybase.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Sterling Hughes <sterling@php.net> |
  17. // | Antônio Carlos Venâncio Júnior <floripa@php.net> |
  18. // | Maintainer: Daniel Convissor <danielc@php.net> |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: sybase.php,v 1.48 2004/02/18 23:07:10 danielc Exp $
  22. // TODO
  23. // - This driver may fail with multiple connections under the same
  24. // user/pass/host and different databases
  25. require_once 'DB/common.php';
  26. /**
  27. * Database independent query interface definition for PHP's Sybase
  28. * extension.
  29. *
  30. * @package DB
  31. * @version $Id: sybase.php,v 1.48 2004/02/18 23:07:10 danielc Exp $
  32. * @category Database
  33. * @author Sterling Hughes <sterling@php.net>
  34. * @author Antônio Carlos Venâncio Júnior <floripa@php.net>
  35. */
  36. class DB_sybase extends DB_common
  37. {
  38. // {{{ properties
  39. var $connection;
  40. var $phptype, $dbsyntax;
  41. var $prepare_tokens = array();
  42. var $prepare_types = array();
  43. var $transaction_opcount = 0;
  44. var $autocommit = true;
  45. // }}}
  46. // {{{ constructor
  47. /**
  48. * DB_sybase constructor.
  49. *
  50. * @access public
  51. */
  52. function DB_sybase()
  53. {
  54. $this->DB_common();
  55. $this->phptype = 'sybase';
  56. $this->dbsyntax = 'sybase';
  57. $this->features = array(
  58. 'prepare' => false,
  59. 'pconnect' => true,
  60. 'transactions' => false,
  61. 'limit' => 'emulate'
  62. );
  63. $this->errorcode_map = array(
  64. );
  65. }
  66. // }}}
  67. // {{{ connect()
  68. /**
  69. * Connect to a database and log in as the specified user.
  70. *
  71. * @param $dsn the data source name (see DB::parseDSN for syntax)
  72. * @param $persistent (optional) whether the connection should
  73. * be persistent
  74. * @access public
  75. * @return int DB_OK on success, a DB error on failure
  76. */
  77. function connect($dsninfo, $persistent = false)
  78. {
  79. if (!DB::assertExtension('sybase') &&
  80. !DB::assertExtension('sybase_ct'))
  81. {
  82. return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
  83. }
  84. $this->dsn = $dsninfo;
  85. $interface = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
  86. $connect_function = $persistent ? 'sybase_pconnect' : 'sybase_connect';
  87. if ($interface && $dsninfo['username'] && $dsninfo['password']) {
  88. $conn = @$connect_function($interface, $dsninfo['username'],
  89. $dsninfo['password']);
  90. } elseif ($interface && $dsninfo['username']) {
  91. /*
  92. * Using false for pw as a workaround to avoid segfault.
  93. * See PEAR bug 631
  94. */
  95. $conn = @$connect_function($interface, $dsninfo['username'],
  96. false);
  97. } else {
  98. $conn = FALSE;
  99. }
  100. if (!$conn) {
  101. return $this->raiseError(DB_ERROR_CONNECT_FAILED);
  102. }
  103. if ($dsninfo['database']) {
  104. if (!@sybase_select_db($dsninfo['database'], $conn)) {
  105. return $this->raiseError(DB_ERROR_NODBSELECTED, null,
  106. null, null, sybase_get_last_message());
  107. }
  108. $this->_db = $dsninfo['database'];
  109. }
  110. $this->connection = $conn;
  111. return DB_OK;
  112. }
  113. // }}}
  114. // {{{ disconnect()
  115. /**
  116. * Log out and disconnect from the database.
  117. *
  118. * @access public
  119. *
  120. * @return bool TRUE on success, FALSE if not connected.
  121. */
  122. function disconnect()
  123. {
  124. $ret = @sybase_close($this->connection);
  125. $this->connection = null;
  126. return $ret;
  127. }
  128. // }}}
  129. // {{{ errorNative()
  130. /**
  131. * Get the last server error messge (if any)
  132. *
  133. * @return string sybase last error message
  134. */
  135. function errorNative()
  136. {
  137. return sybase_get_last_message();
  138. }
  139. // }}}
  140. // {{{ errorCode()
  141. /**
  142. * Determine PEAR::DB error code from the database's text error message.
  143. *
  144. * @param string $errormsg error message returned from the database
  145. * @return integer an error number from a DB error constant
  146. */
  147. function errorCode($errormsg)
  148. {
  149. static $error_regexps;
  150. if (!isset($error_regexps)) {
  151. $error_regexps = array(
  152. '/Incorrect syntax near/'
  153. => DB_ERROR_SYNTAX,
  154. '/^Unclosed quote before the character string [\"\'].*[\"\']\./'
  155. => DB_ERROR_SYNTAX,
  156. '/Implicit conversion from datatype [\"\'].+[\"\'] to [\"\'].+[\"\'] is not allowed\./'
  157. => DB_ERROR_INVALID_NUMBER,
  158. '/Cannot drop the table [\"\'].+[\"\'], because it doesn\'t exist in the system catalogs\./'
  159. => DB_ERROR_NOSUCHTABLE,
  160. '/Only the owner of object [\"\'].+[\"\'] or a user with System Administrator \(SA\) role can run this command\./'
  161. => DB_ERROR_ACCESS_VIOLATION,
  162. '/^.+ permission denied on object .+, database .+, owner .+/'
  163. => DB_ERROR_ACCESS_VIOLATION,
  164. '/^.* permission denied, database .+, owner .+/'
  165. => DB_ERROR_ACCESS_VIOLATION,
  166. '/[^.*] not found\./'
  167. => DB_ERROR_NOSUCHTABLE,
  168. '/There is already an object named/'
  169. => DB_ERROR_ALREADY_EXISTS,
  170. '/Invalid column name/'
  171. => DB_ERROR_NOSUCHFIELD,
  172. '/does not allow null values/'
  173. => DB_ERROR_CONSTRAINT_NOT_NULL,
  174. '/Command has been aborted/'
  175. => DB_ERROR_CONSTRAINT,
  176. );
  177. }
  178. foreach ($error_regexps as $regexp => $code) {
  179. if (preg_match($regexp, $errormsg)) {
  180. return $code;
  181. }
  182. }
  183. return DB_ERROR;
  184. }
  185. // }}}
  186. // {{{ sybaseRaiseError()
  187. /**
  188. * Gather information about an error, then use that info to create a
  189. * DB error object and finally return that object.
  190. *
  191. * @param integer $errno PEAR error number (usually a DB constant) if
  192. * manually raising an error
  193. * @return object DB error object
  194. * @see errorNative()
  195. * @see errorCode()
  196. * @see DB_common::raiseError()
  197. */
  198. function sybaseRaiseError($errno = null)
  199. {
  200. $native = $this->errorNative();
  201. if ($errno === null) {
  202. $errno = $this->errorCode($native);
  203. }
  204. return $this->raiseError($errno, null, null, null, $native);
  205. }
  206. // }}}
  207. // {{{ simpleQuery()
  208. /**
  209. * Send a query to Sybase and return the results as a Sybase resource
  210. * identifier.
  211. *
  212. * @param the SQL query
  213. *
  214. * @access public
  215. *
  216. * @return mixed returns a valid Sybase result for successful SELECT
  217. * queries, DB_OK for other successful queries. A DB error is
  218. * returned on failure.
  219. */
  220. function simpleQuery($query)
  221. {
  222. $ismanip = DB::isManip($query);
  223. $this->last_query = $query;
  224. if (!@sybase_select_db($this->_db, $this->connection)) {
  225. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  226. }
  227. $query = $this->modifyQuery($query);
  228. if (!$this->autocommit && $ismanip) {
  229. if ($this->transaction_opcount == 0) {
  230. $result = @sybase_query('BEGIN TRANSACTION', $this->connection);
  231. if (!$result) {
  232. return $this->sybaseRaiseError();
  233. }
  234. }
  235. $this->transaction_opcount++;
  236. }
  237. $result = @sybase_query($query, $this->connection);
  238. if (!$result) {
  239. return $this->sybaseRaiseError();
  240. }
  241. if (is_resource($result)) {
  242. $numrows = $this->numRows($result);
  243. if (is_object($numrows)) {
  244. return $numrows;
  245. }
  246. $this->num_rows[(int)$result] = $numrows;
  247. return $result;
  248. }
  249. // Determine which queries that should return data, and which
  250. // should return an error code only.
  251. return $ismanip ? DB_OK : $result;
  252. }
  253. // }}}
  254. // {{{ nextResult()
  255. /**
  256. * Move the internal sybase result pointer to the next available result
  257. *
  258. * @param a valid sybase result resource
  259. *
  260. * @access public
  261. *
  262. * @return true if a result is available otherwise return false
  263. */
  264. function nextResult($result)
  265. {
  266. return false;
  267. }
  268. // }}}
  269. // {{{ fetchInto()
  270. /**
  271. * Fetch a row and insert the data into an existing array.
  272. *
  273. * Formating of the array and the data therein are configurable.
  274. * See DB_result::fetchInto() for more information.
  275. *
  276. * @param resource $result query result identifier
  277. * @param array $arr (reference) array where data from the row
  278. * should be placed
  279. * @param int $fetchmode how the resulting array should be indexed
  280. * @param int $rownum the row number to fetch
  281. *
  282. * @return mixed DB_OK on success, NULL when end of result set is
  283. * reached or on failure
  284. *
  285. * @see DB_result::fetchInto()
  286. * @access private
  287. */
  288. function fetchInto($result, &$arr, $fetchmode, $rownum=null)
  289. {
  290. if ($rownum !== null) {
  291. if (!@sybase_data_seek($result, $rownum)) {
  292. return null;
  293. }
  294. }
  295. if ($fetchmode & DB_FETCHMODE_ASSOC) {
  296. if (function_exists('sybase_fetch_assoc')) {
  297. $arr = @sybase_fetch_assoc($result);
  298. } else {
  299. if ($arr = @sybase_fetch_array($result)) {
  300. foreach ($arr as $key => $value) {
  301. if (is_int($key)) {
  302. unset($arr[$key]);
  303. }
  304. }
  305. }
  306. }
  307. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
  308. $arr = array_change_key_case($arr, CASE_LOWER);
  309. }
  310. } else {
  311. $arr = @sybase_fetch_row($result);
  312. }
  313. if (!$arr) {
  314. // reported not work as seems that sybase_get_last_message()
  315. // always return a message here
  316. //if ($errmsg = sybase_get_last_message()) {
  317. // return $this->sybaseRaiseError($errmsg);
  318. //} else {
  319. return null;
  320. //}
  321. }
  322. if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
  323. $this->_rtrimArrayValues($arr);
  324. }
  325. if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
  326. $this->_convertNullArrayValuesToEmpty($arr);
  327. }
  328. return DB_OK;
  329. }
  330. // }}}
  331. // {{{ freeResult()
  332. /**
  333. * Free the internal resources associated with $result.
  334. *
  335. * @param $result Sybase result identifier
  336. *
  337. * @access public
  338. *
  339. * @return bool TRUE on success, FALSE if $result is invalid
  340. */
  341. function freeResult($result)
  342. {
  343. unset($this->num_rows[(int)$result]);
  344. return @sybase_free_result($result);
  345. }
  346. // }}}
  347. // {{{ numCols()
  348. /**
  349. * Get the number of columns in a result set.
  350. *
  351. * @param $result Sybase result identifier
  352. *
  353. * @access public
  354. *
  355. * @return int the number of columns per row in $result
  356. */
  357. function numCols($result)
  358. {
  359. $cols = @sybase_num_fields($result);
  360. if (!$cols) {
  361. return $this->sybaseRaiseError();
  362. }
  363. return $cols;
  364. }
  365. // }}}
  366. // {{{ numRows()
  367. /**
  368. * Get the number of rows in a result set.
  369. *
  370. * @param $result Sybase result identifier
  371. *
  372. * @access public
  373. *
  374. * @return int the number of rows in $result
  375. */
  376. function numRows($result)
  377. {
  378. $rows = @sybase_num_rows($result);
  379. if ($rows === false) {
  380. return $this->sybaseRaiseError();
  381. }
  382. return $rows;
  383. }
  384. // }}}
  385. // {{{ affectedRows()
  386. /**
  387. * Gets the number of rows affected by the data manipulation
  388. * query. For other queries, this function returns 0.
  389. *
  390. * @return number of rows affected by the last query
  391. */
  392. function affectedRows()
  393. {
  394. if (DB::isManip($this->last_query)) {
  395. $result = @sybase_affected_rows($this->connection);
  396. } else {
  397. $result = 0;
  398. }
  399. return $result;
  400. }
  401. // }}}
  402. // {{{ nextId()
  403. /**
  404. * Returns the next free id in a sequence
  405. *
  406. * @param string $seq_name name of the sequence
  407. * @param boolean $ondemand when true, the seqence is automatically
  408. * created if it does not exist
  409. *
  410. * @return int the next id number in the sequence. DB_Error if problem.
  411. *
  412. * @internal
  413. * @see DB_common::nextID()
  414. * @access public
  415. */
  416. function nextId($seq_name, $ondemand = true)
  417. {
  418. $seqname = $this->getSequenceName($seq_name);
  419. if (!@sybase_select_db($this->_db, $this->connection)) {
  420. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  421. }
  422. $repeat = 0;
  423. do {
  424. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  425. $result = $this->query("INSERT INTO $seqname (vapor) VALUES (0)");
  426. $this->popErrorHandling();
  427. if ($ondemand && DB::isError($result) &&
  428. ($result->getCode() == DB_ERROR || $result->getCode() == DB_ERROR_NOSUCHTABLE))
  429. {
  430. $repeat = 1;
  431. $result = $this->createSequence($seq_name);
  432. if (DB::isError($result)) {
  433. return $this->raiseError($result);
  434. }
  435. } elseif (!DB::isError($result)) {
  436. $result =& $this->query("SELECT @@IDENTITY FROM $seqname");
  437. $repeat = 0;
  438. } else {
  439. $repeat = false;
  440. }
  441. } while ($repeat);
  442. if (DB::isError($result)) {
  443. return $this->raiseError($result);
  444. }
  445. $result = $result->fetchRow(DB_FETCHMODE_ORDERED);
  446. return $result[0];
  447. }
  448. /**
  449. * Creates a new sequence
  450. *
  451. * @param string $seq_name name of the new sequence
  452. *
  453. * @return int DB_OK on success. A DB_Error object is returned if
  454. * problems arise.
  455. *
  456. * @internal
  457. * @see DB_common::createSequence()
  458. * @access public
  459. */
  460. function createSequence($seq_name)
  461. {
  462. $seqname = $this->getSequenceName($seq_name);
  463. return $this->query("CREATE TABLE $seqname ".
  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. DB_Error if problems.
  475. *
  476. * @internal
  477. * @see DB_common::dropSequence()
  478. * @access public
  479. */
  480. function dropSequence($seq_name)
  481. {
  482. $seqname = $this->getSequenceName($seq_name);
  483. return $this->query("DROP TABLE $seqname");
  484. }
  485. // }}}
  486. // {{{ getSpecialQuery()
  487. /**
  488. * Returns the query needed to get some backend info
  489. * @param string $type What kind of info you want to retrieve
  490. * @return string The SQL query string
  491. */
  492. function getSpecialQuery($type)
  493. {
  494. switch ($type) {
  495. case 'tables':
  496. return "select name from sysobjects where type = 'U' order by name";
  497. case 'views':
  498. return "select name from sysobjects where type = 'V'";
  499. default:
  500. return null;
  501. }
  502. }
  503. // }}}
  504. // {{{ autoCommit()
  505. /**
  506. * Enable/disable automatic commits
  507. */
  508. function autoCommit($onoff = false)
  509. {
  510. // XXX if $this->transaction_opcount > 0, we should probably
  511. // issue a warning here.
  512. $this->autocommit = $onoff ? true : false;
  513. return DB_OK;
  514. }
  515. // }}}
  516. // {{{ commit()
  517. /**
  518. * Commit the current transaction.
  519. */
  520. function commit()
  521. {
  522. if ($this->transaction_opcount > 0) {
  523. if (!@sybase_select_db($this->_db, $this->connection)) {
  524. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  525. }
  526. $result = @sybase_query('COMMIT', $this->connection);
  527. $this->transaction_opcount = 0;
  528. if (!$result) {
  529. return $this->sybaseRaiseError();
  530. }
  531. }
  532. return DB_OK;
  533. }
  534. // }}}
  535. // {{{ rollback()
  536. /**
  537. * Roll back (undo) the current transaction.
  538. */
  539. function rollback()
  540. {
  541. if ($this->transaction_opcount > 0) {
  542. if (!@sybase_select_db($this->_db, $this->connection)) {
  543. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  544. }
  545. $result = @sybase_query('ROLLBACK', $this->connection);
  546. $this->transaction_opcount = 0;
  547. if (!$result) {
  548. return $this->sybaseRaiseError();
  549. }
  550. }
  551. return DB_OK;
  552. }
  553. // }}}
  554. // {{{ tableInfo()
  555. /**
  556. * Returns information about a table or a result set.
  557. *
  558. * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  559. * is a table name.
  560. *
  561. * @param object|string $result DB_result object from a query or a
  562. * string containing the name of a table
  563. * @param int $mode a valid tableInfo mode
  564. * @return array an associative array with the information requested
  565. * or an error object if something is wrong
  566. * @access public
  567. * @internal
  568. * @since 1.6.0
  569. * @see DB_common::tableInfo()
  570. */
  571. function tableInfo($result, $mode = null)
  572. {
  573. if (isset($result->result)) {
  574. /*
  575. * Probably received a result object.
  576. * Extract the result resource identifier.
  577. */
  578. $id = $result->result;
  579. $got_string = false;
  580. } elseif (is_string($result)) {
  581. /*
  582. * Probably received a table name.
  583. * Create a result resource identifier.
  584. */
  585. if (!@sybase_select_db($this->_db, $this->connection)) {
  586. return $this->sybaseRaiseError(DB_ERROR_NODBSELECTED);
  587. }
  588. $id = @sybase_query("SELECT * FROM $result WHERE 1=0",
  589. $this->connection);
  590. $got_string = true;
  591. } else {
  592. /*
  593. * Probably received a result resource identifier.
  594. * Copy it.
  595. * Depricated. Here for compatibility only.
  596. */
  597. $id = $result;
  598. $got_string = false;
  599. }
  600. if (!is_resource($id)) {
  601. return $this->sybaseRaiseError(DB_ERROR_NEED_MORE_DATA);
  602. }
  603. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  604. $case_func = 'strtolower';
  605. } else {
  606. $case_func = 'strval';
  607. }
  608. $count = @sybase_num_fields($id);
  609. // made this IF due to performance (one if is faster than $count if's)
  610. if (!$mode) {
  611. for ($i=0; $i<$count; $i++) {
  612. $f = @sybase_fetch_field($id, $i);
  613. // column_source is often blank
  614. if ($got_string) {
  615. $res[$i]['table'] = $case_func($result);
  616. } else {
  617. $res[$i]['table'] = $case_func($f->column_source);
  618. }
  619. $res[$i]['name'] = $case_func($f->name);
  620. $res[$i]['type'] = $f->type;
  621. $res[$i]['len'] = $f->max_length;
  622. if ($res[$i]['table']) {
  623. $res[$i]['flags'] = $this->_sybase_field_flags(
  624. $res[$i]['table'], $res[$i]['name']);
  625. } else {
  626. $res[$i]['flags'] = '';
  627. }
  628. }
  629. } else {
  630. // get full info
  631. $res['num_fields'] = $count;
  632. for ($i=0; $i<$count; $i++) {
  633. $f = @sybase_fetch_field($id, $i);
  634. // column_source is often blank
  635. if ($got_string) {
  636. $res[$i]['table'] = $case_func($result);
  637. } else {
  638. $res[$i]['table'] = $case_func($f->column_source);
  639. }
  640. $res[$i]['name'] = $case_func($f->name);
  641. $res[$i]['type'] = $f->type;
  642. $res[$i]['len'] = $f->max_length;
  643. if ($res[$i]['table']) {
  644. $res[$i]['flags'] = $this->_sybase_field_flags(
  645. $res[$i]['table'], $res[$i]['name']);
  646. } else {
  647. $res[$i]['flags'] = '';
  648. }
  649. if ($mode & DB_TABLEINFO_ORDER) {
  650. $res['order'][$res[$i]['name']] = $i;
  651. }
  652. if ($mode & DB_TABLEINFO_ORDERTABLE) {
  653. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  654. }
  655. }
  656. }
  657. // free the result only if we were called on a table
  658. if ($got_string) {
  659. @sybase_free_result($id);
  660. }
  661. return $res;
  662. }
  663. // }}}
  664. // {{{ _sybase_field_flags()
  665. /**
  666. * Get the flags for a field.
  667. *
  668. * Currently supports:
  669. * + <samp>unique_key</samp> (unique index, unique check or primary_key)
  670. * + <samp>multiple_key</samp> (multi-key index)
  671. *
  672. * @param string $table table name
  673. * @param string $column field name
  674. * @return string space delimited string of flags. Empty string if none.
  675. * @access private
  676. */
  677. function _sybase_field_flags($table, $column)
  678. {
  679. static $tableName = null;
  680. static $flags = array();
  681. if ($table != $tableName) {
  682. $flags = array();
  683. $tableName = $table;
  684. // get unique/primary keys
  685. $res = $this->getAll("sp_helpindex $table", DB_FETCHMODE_ASSOC);
  686. if (!isset($res[0]['index_description'])) {
  687. return '';
  688. }
  689. foreach ($res as $val) {
  690. $keys = explode(', ', trim($val['index_keys']));
  691. if (sizeof($keys) > 1) {
  692. foreach ($keys as $key) {
  693. $this->_add_flag($flags[$key], 'multiple_key');
  694. }
  695. }
  696. if (strpos($val['index_description'], 'unique')) {
  697. foreach ($keys as $key) {
  698. $this->_add_flag($flags[$key], 'unique_key');
  699. }
  700. }
  701. }
  702. }
  703. if (array_key_exists($column, $flags)) {
  704. return(implode(' ', $flags[$column]));
  705. }
  706. return '';
  707. }
  708. // }}}
  709. // {{{ _add_flag()
  710. /**
  711. * Adds a string to the flags array if the flag is not yet in there
  712. * - if there is no flag present the array is created.
  713. *
  714. * @param array $array reference of flags array to add a value to
  715. * @param mixed $value value to add to the flag array
  716. * @access private
  717. */
  718. function _add_flag(&$array, $value)
  719. {
  720. if (!is_array($array)) {
  721. $array = array($value);
  722. } elseif (!in_array($value, $array)) {
  723. array_push($array, $value);
  724. }
  725. }
  726. // }}}
  727. // {{{ quoteIdentifier()
  728. /**
  729. * Quote a string so it can be safely used as a table / column name
  730. *
  731. * Quoting style depends on which database driver is being used.
  732. *
  733. * @param string $str identifier name to be quoted
  734. *
  735. * @return string quoted identifier string
  736. *
  737. * @since 1.6.0
  738. * @access public
  739. */
  740. function quoteIdentifier($str)
  741. {
  742. return '[' . str_replace(']', ']]', $str) . ']';
  743. }
  744. // }}}
  745. }
  746. /*
  747. * Local variables:
  748. * tab-width: 4
  749. * c-basic-offset: 4
  750. * End:
  751. */
  752. ?>