fbsql.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
  6. // | Stig. S. Bakken, Lukas Smith, Frank M. Kromann |
  7. // | All rights reserved. |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
  10. // | API as well as database abstraction for PHP applications. |
  11. // | This LICENSE is in the BSD license style. |
  12. // | |
  13. // | Redistribution and use in source and binary forms, with or without |
  14. // | modification, are permitted provided that the following conditions |
  15. // | are met: |
  16. // | |
  17. // | Redistributions of source code must retain the above copyright |
  18. // | notice, this list of conditions and the following disclaimer. |
  19. // | |
  20. // | Redistributions in binary form must reproduce the above copyright |
  21. // | notice, this list of conditions and the following disclaimer in the |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // | |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission. |
  28. // | |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
  40. // | POSSIBILITY OF SUCH DAMAGE. |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@pooteeweet.org> |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id$
  46. //
  47. require_once 'MDB2/Driver/Manager/Common.php';
  48. /**
  49. * MDB2 FrontBase driver for the management modules
  50. *
  51. * @package MDB2
  52. * @category Database
  53. * @author Frank M. Kromann <frank@kromann.info>
  54. */
  55. class MDB2_Driver_Manager_fbsql extends MDB2_Driver_Manager_Common
  56. {
  57. // {{{ createDatabase()
  58. /**
  59. * create a new database
  60. *
  61. * @param string $name name of the database that should be created
  62. * @param array $options array with charset, collation info
  63. *
  64. * @return mixed MDB2_OK on success, a MDB2 error on failure
  65. * @access public
  66. */
  67. function createDatabase($name, $options = array())
  68. {
  69. $db =& $this->getDBInstance();
  70. if (MDB2::isError($db)) {
  71. return $db;
  72. }
  73. $name = $db->quoteIdentifier($name, true);
  74. $query = "CREATE DATABASE $name";
  75. return $db->standaloneQuery($query, null, true);
  76. }
  77. // }}}
  78. // {{{ dropDatabase()
  79. /**
  80. * drop an existing database
  81. *
  82. * @param string $name name of the database that should be dropped
  83. * @return mixed MDB2_OK on success, a MDB2 error on failure
  84. * @access public
  85. */
  86. function dropDatabase($name)
  87. {
  88. $db =& $this->getDBInstance();
  89. if (MDB2::isError($db)) {
  90. return $db;
  91. }
  92. $name = $db->quoteIdentifier($name, true);
  93. $query = "DELETE DATABASE $name";
  94. return $db->standaloneQuery($query, null, true);
  95. }
  96. // }}}
  97. // {{{ dropTable()
  98. /**
  99. * drop an existing table
  100. *
  101. * @param object $dbs database object that is extended by this class
  102. * @param string $name name of the table that should be dropped
  103. * @return mixed MDB2_OK on success, a MDB2 error on failure
  104. * @access public
  105. */
  106. function dropTable($name)
  107. {
  108. $db =& $this->getDBInstance();
  109. if (MDB2::isError($db)) {
  110. return $db;
  111. }
  112. $name = $db->quoteIdentifier($name, true);
  113. $result = $db->exec("DROP TABLE $name CASCADE");
  114. if (MDB2::isError($result)) {
  115. return $result;
  116. }
  117. return MDB2_OK;
  118. }
  119. // }}}
  120. // {{{ alterTable()
  121. /**
  122. * alter an existing table
  123. *
  124. * @param string $name name of the table that is intended to be changed.
  125. * @param array $changes associative array that contains the details of each type
  126. * of change that is intended to be performed. The types of
  127. * changes that are currently supported are defined as follows:
  128. *
  129. * name
  130. *
  131. * New name for the table.
  132. *
  133. * add
  134. *
  135. * Associative array with the names of fields to be added as
  136. * indexes of the array. The value of each entry of the array
  137. * should be set to another associative array with the properties
  138. * of the fields to be added. The properties of the fields should
  139. * be the same as defined by the MDB2 parser.
  140. *
  141. *
  142. * remove
  143. *
  144. * Associative array with the names of fields to be removed as indexes
  145. * of the array. Currently the values assigned to each entry are ignored.
  146. * An empty array should be used for future compatibility.
  147. *
  148. * rename
  149. *
  150. * Associative array with the names of fields to be renamed as indexes
  151. * of the array. The value of each entry of the array should be set to
  152. * another associative array with the entry named name with the new
  153. * field name and the entry named Declaration that is expected to contain
  154. * the portion of the field declaration already in DBMS specific SQL code
  155. * as it is used in the CREATE TABLE statement.
  156. *
  157. * change
  158. *
  159. * Associative array with the names of the fields to be changed as indexes
  160. * of the array. Keep in mind that if it is intended to change either the
  161. * name of a field and any other properties, the change array entries
  162. * should have the new names of the fields as array indexes.
  163. *
  164. * The value of each entry of the array should be set to another associative
  165. * array with the properties of the fields to that are meant to be changed as
  166. * array entries. These entries should be assigned to the new values of the
  167. * respective properties. The properties of the fields should be the same
  168. * as defined by the MDB2 parser.
  169. *
  170. * Example
  171. * array(
  172. * 'name' => 'userlist',
  173. * 'add' => array(
  174. * 'quota' => array(
  175. * 'type' => 'integer',
  176. * 'unsigned' => 1
  177. * )
  178. * ),
  179. * 'remove' => array(
  180. * 'file_limit' => array(),
  181. * 'time_limit' => array()
  182. * ),
  183. * 'change' => array(
  184. * 'name' => array(
  185. * 'length' => '20',
  186. * 'definition' => array(
  187. * 'type' => 'text',
  188. * 'length' => 20,
  189. * ),
  190. * )
  191. * ),
  192. * 'rename' => array(
  193. * 'sex' => array(
  194. * 'name' => 'gender',
  195. * 'definition' => array(
  196. * 'type' => 'text',
  197. * 'length' => 1,
  198. * 'default' => 'M',
  199. * ),
  200. * )
  201. * )
  202. * )
  203. *
  204. * @param boolean $check indicates whether the function should just check if the DBMS driver
  205. * can perform the requested table alterations if the value is true or
  206. * actually perform them otherwise.
  207. * @access public
  208. *
  209. * @return mixed MDB2_OK on success, a MDB2 error on failure
  210. */
  211. function alterTable($name, $changes, $check)
  212. {
  213. $db =& $this->getDBInstance();
  214. if (MDB2::isError($db)) {
  215. return $db;
  216. }
  217. foreach ($changes as $change_name => $change){
  218. switch ($change_name) {
  219. case 'add':
  220. case 'remove':
  221. case 'change':
  222. case 'rename':
  223. case 'name':
  224. break;
  225. default:
  226. return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
  227. 'change type "'.$change_name.'" not yet supported', __FUNCTION__);
  228. }
  229. }
  230. if ($check) {
  231. return MDB2_OK;
  232. }
  233. $query = '';
  234. if (!empty($changes['name'])) {
  235. $change_name = $db->quoteIdentifier($changes['name'], true);
  236. $query .= 'RENAME TO ' . $change_name;
  237. }
  238. if (!empty($changes['add']) && is_array($changes['add'])) {
  239. foreach ($changes['add'] as $field_name => $field) {
  240. if ($query) {
  241. $query.= ', ';
  242. }
  243. $query.= 'ADD ' . $db->getDeclaration($field['type'], $field_name, $field);
  244. }
  245. }
  246. if (!empty($changes['remove']) && is_array($changes['remove'])) {
  247. foreach ($changes['remove'] as $field_name => $field) {
  248. if ($query) {
  249. $query.= ', ';
  250. }
  251. $field_name = $db->quoteIdentifier($field_name, true);
  252. $query.= 'DROP ' . $field_name;
  253. }
  254. }
  255. $rename = array();
  256. if (!empty($changes['rename']) && is_array($changes['rename'])) {
  257. foreach ($changes['rename'] as $field_name => $field) {
  258. $rename[$field['name']] = $field_name;
  259. }
  260. }
  261. if (!empty($changes['change']) && is_array($changes['change'])) {
  262. foreach ($changes['change'] as $field_name => $field) {
  263. if ($query) {
  264. $query.= ', ';
  265. }
  266. if (isset($rename[$field_name])) {
  267. $old_field_name = $rename[$field_name];
  268. unset($rename[$field_name]);
  269. } else {
  270. $old_field_name = $field_name;
  271. }
  272. $old_field_name = $db->quoteIdentifier($old_field_name, true);
  273. $query.= "CHANGE $old_field_name " . $db->getDeclaration($field['definition']['type'], $old_field_name, $field['definition']);
  274. }
  275. }
  276. if (!empty($rename) && is_array($rename)) {
  277. foreach ($rename as $renamed_field_name => $renamed_field) {
  278. if ($query) {
  279. $query.= ', ';
  280. }
  281. $old_field_name = $rename[$renamed_field_name];
  282. $field = $changes['rename'][$old_field_name];
  283. $query.= 'CHANGE ' . $db->getDeclaration($field['definition']['type'], $old_field_name, $field['definition']);
  284. }
  285. }
  286. if (!$query) {
  287. return MDB2_OK;
  288. }
  289. $name = $db->quoteIdentifier($name, true);
  290. $result = $db->exec("ALTER TABLE $name $query");
  291. if (MDB2::isError($result)) {
  292. return $result;
  293. }
  294. return MDB2_OK;
  295. }
  296. // }}}
  297. // {{{ listDatabases()
  298. /**
  299. * list all databases
  300. *
  301. * @return mixed array of database names on success, a MDB2 error on failure
  302. * @access public
  303. */
  304. function listDatabases()
  305. {
  306. $db =& $this->getDBInstance();
  307. if (MDB2::isError($db)) {
  308. return $db;
  309. }
  310. return $db->raiseError(MDB2_ERROR_NOT_CAPABLE, null, null,
  311. 'not capable', __FUNCTION__);
  312. }
  313. // }}}
  314. // {{{ listUsers()
  315. /**
  316. * list all users
  317. *
  318. * @return mixed array of user names on success, a MDB2 error on failure
  319. * @access public
  320. */
  321. function listUsers()
  322. {
  323. $db =& $this->getDBInstance();
  324. if (MDB2::isError($db)) {
  325. return $db;
  326. }
  327. return $db->queryCol('SELECT "user_name" FROM information_schema.users');
  328. }
  329. // }}}
  330. // {{{ listTables()
  331. /**
  332. * list all tables in the current database
  333. *
  334. * @return mixed array of table names on success, a MDB2 error on failure
  335. * @access public
  336. */
  337. function listTables()
  338. {
  339. $db =& $this->getDBInstance();
  340. if (MDB2::isError($db)) {
  341. return $db;
  342. }
  343. $table_names = $db->queryCol('SELECT "table_name"'
  344. . ' FROM information_schema.tables'
  345. . ' t0, information_schema.schemata t1'
  346. . ' WHERE t0.schema_pk=t1.schema_pk AND'
  347. . ' "table_type" = \'BASE TABLE\''
  348. . ' AND "schema_name" = current_schema');
  349. if (MDB2::isError($table_names)) {
  350. return $table_names;
  351. }
  352. $result = array();
  353. for ($i = 0, $j = count($table_names); $i < $j; ++$i) {
  354. if (!$this->_fixSequenceName($table_names[$i], true)) {
  355. $result[] = $table_names[$i];
  356. }
  357. }
  358. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  359. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  360. }
  361. return $result;
  362. }
  363. // }}}
  364. // {{{ listTableFields()
  365. /**
  366. * list all fields in a table in the current database
  367. *
  368. * @param string $table name of table that should be used in method
  369. * @return mixed array of field names on success, a MDB2 error on failure
  370. * @access public
  371. */
  372. function listTableFields($table)
  373. {
  374. $db =& $this->getDBInstance();
  375. if (MDB2::isError($db)) {
  376. return $db;
  377. }
  378. $table = $db->quoteIdentifier($table, true);
  379. $result = $db->queryCol("SHOW COLUMNS FROM $table");
  380. if (MDB2::isError($result)) {
  381. return $result;
  382. }
  383. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  384. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  385. }
  386. return $result;
  387. }
  388. // }}}
  389. // {{{ dropIndex()
  390. /**
  391. * drop existing index
  392. *
  393. * @param string $table name of table that should be used in method
  394. * @param string $name name of the index to be dropped
  395. * @return mixed MDB2_OK on success, a MDB2 error on failure
  396. * @access public
  397. */
  398. function dropIndex($table, $name)
  399. {
  400. $db =& $this->getDBInstance();
  401. if (MDB2::isError($db)) {
  402. return $db;
  403. }
  404. $table = $db->quoteIdentifier($table, true);
  405. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  406. $result = $db->exec("ALTER TABLE $table DROP INDEX $name");
  407. if (MDB2::isError($result)) {
  408. return $result;
  409. }
  410. return MDB2_OK;
  411. }
  412. // }}}
  413. // {{{ listTableIndexes()
  414. /**
  415. * list all indexes in a table
  416. *
  417. * @param string $table name of table that should be used in method
  418. * @return mixed array of index names on success, a MDB2 error on failure
  419. * @access public
  420. */
  421. function listTableIndexes($table)
  422. {
  423. $db =& $this->getDBInstance();
  424. if (MDB2::isError($db)) {
  425. return $db;
  426. }
  427. $key_name = 'Key_name';
  428. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  429. if ($db->options['field_case'] == CASE_LOWER) {
  430. $key_name = strtolower($key_name);
  431. } else {
  432. $key_name = strtoupper($key_name);
  433. }
  434. }
  435. $table = $db->quoteIdentifier($table, true);
  436. $query = "SHOW INDEX FROM $table";
  437. $indexes = $db->queryCol($query, 'text', $key_name);
  438. if (MDB2::isError($indexes)) {
  439. return $indexes;
  440. }
  441. $result = array();
  442. foreach ($indexes as $index) {
  443. if ($index != 'PRIMARY' && ($index = $this->_fixIndexName($index))) {
  444. $result[$index] = true;
  445. }
  446. }
  447. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  448. $result = array_change_key_case($result, $db->options['field_case']);
  449. }
  450. return array_keys($result);
  451. }
  452. // }}}
  453. // {{{ createSequence()
  454. /**
  455. * create sequence
  456. *
  457. * @param string $seq_name name of the sequence to be created
  458. * @param string $start start value of the sequence; default is 1
  459. * @return mixed MDB2_OK on success, a MDB2 error on failure
  460. * @access public
  461. */
  462. function createSequence($seq_name, $start = 1)
  463. {
  464. $db =& $this->getDBInstance();
  465. if (MDB2::isError($db)) {
  466. return $db;
  467. }
  468. $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
  469. $seqcol_name = $db->quoteIdentifier($db->options['seqcol_name'], true);
  470. $query = "CREATE TABLE $sequence_name ($seqcol_name INTEGER DEFAULT UNIQUE, PRIMARY KEY($seqcol_name))";
  471. $res = $db->exec($query);
  472. $res = $db->exec("SET UNIQUE = 1 FOR $sequence_name");
  473. if (MDB2::isError($res)) {
  474. return $res;
  475. }
  476. if ($start == 1) {
  477. return MDB2_OK;
  478. }
  479. $res = $db->exec("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')');
  480. if (!MDB2::isError($res)) {
  481. return MDB2_OK;
  482. }
  483. // Handle error
  484. $result = $db->exec("DROP TABLE $sequence_name");
  485. if (MDB2::isError($result)) {
  486. return $db->raiseError($result, null, null,
  487. 'could not drop inconsistent sequence table', __FUNCTION__);
  488. }
  489. return $db->raiseError($res, null, null,
  490. 'could not create sequence table', __FUNCTION__);
  491. }
  492. // }}}
  493. // {{{ dropSequence()
  494. /**
  495. * drop existing sequence
  496. *
  497. * @param string $seq_name name of the sequence to be dropped
  498. * @return mixed MDB2_OK on success, a MDB2 error on failure
  499. * @access public
  500. */
  501. function dropSequence($seq_name)
  502. {
  503. $db =& $this->getDBInstance();
  504. if (MDB2::isError($db)) {
  505. return $db;
  506. }
  507. $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
  508. $result = $db->exec("DROP TABLE $sequence_name CASCADE");
  509. if (MDB2::isError($result)) {
  510. return $result;
  511. }
  512. return MDB2_OK;
  513. }
  514. // }}}
  515. // {{{ listSequences()
  516. /**
  517. * list all sequences in the current database
  518. *
  519. * @return mixed array of sequence names on success, a MDB2 error on failure
  520. * @access public
  521. */
  522. function listSequences()
  523. {
  524. $db =& $this->getDBInstance();
  525. if (MDB2::isError($db)) {
  526. return $db;
  527. }
  528. $table_names = $db->queryCol('SHOW TABLES', 'text');
  529. if (MDB2::isError($table_names)) {
  530. return $table_names;
  531. }
  532. $result = array();
  533. for ($i = 0, $j = count($table_names); $i < $j; ++$i) {
  534. if ($sqn = $this->_fixSequenceName($table_names[$i], true)) {
  535. $result[] = $sqn;
  536. }
  537. }
  538. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  539. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  540. }
  541. return $result;
  542. }
  543. // }}}
  544. }
  545. ?>