ibase.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |
  6. // | Stig. S. Bakken, Lukas Smith, Lorenzo Alberton |
  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: Lorenzo Alberton <l.alberton@quipo.it> |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id$
  46. require_once 'MDB2/Driver/Manager/Common.php';
  47. /**
  48. * MDB2 FireBird/InterBase driver for the management modules
  49. *
  50. * @package MDB2
  51. * @category Database
  52. * @author Lorenzo Alberton <l.alberton@quipo.it>
  53. */
  54. class MDB2_Driver_Manager_ibase extends MDB2_Driver_Manager_Common
  55. {
  56. // {{{ createDatabase()
  57. /**
  58. * create a new database
  59. *
  60. * @param string $name name of the database that should be created
  61. * @param array $options array with charset info
  62. *
  63. * @return mixed MDB2_OK on success, a MDB2 error on failure
  64. * @access public
  65. */
  66. function createDatabase($name, $options = array())
  67. {
  68. $db = $this->getDBInstance();
  69. if (MDB2::isError($db)) {
  70. return $db;
  71. }
  72. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, 'Create database',
  73. 'PHP Interbase API does not support direct queries. You have to '.
  74. 'create the db manually by using isql command or a similar program', __FUNCTION__);
  75. }
  76. // }}}
  77. // {{{ dropDatabase()
  78. /**
  79. * drop an existing database
  80. *
  81. * @param string $name name of the database that should be dropped
  82. *
  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. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, 'Drop database',
  93. 'PHP Interbase API does not support direct queries. You have '.
  94. 'to drop the db manually by using isql command or a similar program', __FUNCTION__);
  95. }
  96. // }}}
  97. // {{{ _silentCommit()
  98. /**
  99. * conditional COMMIT query to make changes permanent, when auto
  100. * @access private
  101. */
  102. function _silentCommit()
  103. {
  104. $db = $this->getDBInstance();
  105. if (MDB2::isError($db)) {
  106. return $db;
  107. }
  108. if (!$db->in_transaction) {
  109. @$db->exec('COMMIT');
  110. }
  111. }
  112. // }}}
  113. // {{{ _makeAutoincrement()
  114. /**
  115. * add an autoincrement sequence + trigger
  116. *
  117. * @param string $name name of the PK field
  118. * @param string $table name of the table
  119. * @param string $start start value for the sequence
  120. *
  121. * @return mixed MDB2_OK on success, a MDB2 error on failure
  122. * @access private
  123. */
  124. function _makeAutoincrement($name, $table, $start = null)
  125. {
  126. $db = $this->getDBInstance();
  127. if (MDB2::isError($db)) {
  128. return $db;
  129. }
  130. $table_quoted = $db->quoteIdentifier($table, true);
  131. if (null === $start) {
  132. $db->beginTransaction();
  133. $query = 'SELECT MAX(' . $db->quoteIdentifier($name, true) . ') FROM ' . $table_quoted;
  134. $start = $this->db->queryOne($query, 'integer');
  135. if (MDB2::isError($start)) {
  136. return $start;
  137. }
  138. ++$start;
  139. $result = $db->manager->createSequence($table, $start);
  140. $db->commit();
  141. } else {
  142. $result = $db->manager->createSequence($table, $start);
  143. }
  144. if (MDB2::isError($result)) {
  145. return $db->raiseError(null, null, null,
  146. 'sequence for autoincrement PK could not be created', __FUNCTION__);
  147. }
  148. $sequence_name = $db->getSequenceName($table);
  149. $trigger_name = $db->quoteIdentifier($table . '_AI_PK', true);
  150. $name = $db->quoteIdentifier($name, true);
  151. $trigger_sql = 'CREATE TRIGGER ' . $trigger_name . ' FOR ' . $table_quoted . '
  152. ACTIVE BEFORE INSERT POSITION 0
  153. AS
  154. BEGIN
  155. IF (NEW.' . $name . ' IS NULL OR NEW.' . $name . ' = 0) THEN
  156. NEW.' . $name . ' = GEN_ID('.$sequence_name.', 1);
  157. END';
  158. $result = $db->exec($trigger_sql);
  159. if (MDB2::isError($result)) {
  160. return $result;
  161. }
  162. $this->_silentCommit();
  163. return MDB2_OK;
  164. }
  165. // }}}
  166. // {{{ _dropAutoincrement()
  167. /**
  168. * drop an existing autoincrement sequence + trigger
  169. *
  170. * @param string $table name of the table
  171. *
  172. * @return mixed MDB2_OK on success, a MDB2 error on failure
  173. * @access private
  174. */
  175. function _dropAutoincrement($table)
  176. {
  177. $db = $this->getDBInstance();
  178. if (MDB2::isError($db)) {
  179. return $db;
  180. }
  181. $result = $db->manager->dropSequence($table);
  182. if (MDB2::isError($result)) {
  183. return $db->raiseError(null, null, null,
  184. 'sequence for autoincrement PK could not be dropped', __FUNCTION__);
  185. }
  186. //remove autoincrement trigger associated with the table
  187. $table = $db->quote(strtoupper($table), 'text');
  188. $trigger_name = $db->quote(strtoupper($table) . '_AI_PK', 'text');
  189. $trigger_name_old = $db->quote(strtoupper($table) . '_AUTOINCREMENT_PK', 'text');
  190. $query = "DELETE FROM RDB\$TRIGGERS
  191. WHERE UPPER(RDB\$RELATION_NAME)=$table
  192. AND (UPPER(RDB\$TRIGGER_NAME)=$trigger_name
  193. OR UPPER(RDB\$TRIGGER_NAME)=$trigger_name_old)";
  194. $result = $db->exec($query);
  195. if (MDB2::isError($result)) {
  196. return $db->raiseError(null, null, null,
  197. 'trigger for autoincrement PK could not be dropped', __FUNCTION__);
  198. }
  199. return MDB2_OK;
  200. }
  201. // }}}
  202. // {{{ createTable()
  203. /**
  204. * create a new table
  205. *
  206. * @param string $name Name of the database that should be created
  207. * @param array $fields Associative array that contains the definition of each field of the new table
  208. * The indexes of the array entries are the names of the fields of the table an
  209. * the array entry values are associative arrays like those that are meant to be
  210. * passed with the field definitions to get[Type]Declaration() functions.
  211. *
  212. * Example
  213. * array(
  214. * 'id' => array(
  215. * 'type' => 'integer',
  216. * 'unsigned' => 1,
  217. * 'notnull' => 1,
  218. * 'default' => 0,
  219. * ),
  220. * 'name' => array(
  221. * 'type' => 'text',
  222. * 'length' => 12,
  223. * ),
  224. * 'description' => array(
  225. * 'type' => 'text',
  226. * 'length' => 12,
  227. * )
  228. * );
  229. * @param array $options An associative array of table options:
  230. * array(
  231. * 'comment' => 'Foo',
  232. * 'temporary' => true|false,
  233. * );
  234. *
  235. * @return mixed MDB2_OK on success, a MDB2 error on failure
  236. * @access public
  237. */
  238. function createTable($name, $fields, $options = array())
  239. {
  240. $db = $this->getDBInstance();
  241. if (MDB2::isError($db)) {
  242. return $db;
  243. }
  244. $query = $this->_getCreateTableQuery($name, $fields, $options);
  245. if (MDB2::isError($query)) {
  246. return $query;
  247. }
  248. $options_strings = array();
  249. if (!empty($options['comment'])) {
  250. $options_strings['comment'] = '/* '.$db->quote($options['comment'], 'text'). ' */';
  251. }
  252. if (!empty($options_strings)) {
  253. $query.= ' '.implode(' ', $options_strings);
  254. }
  255. $result = $db->exec($query);
  256. if (MDB2::isError($result)) {
  257. return $result;
  258. }
  259. $this->_silentCommit();
  260. foreach ($fields as $field_name => $field) {
  261. if (!empty($field['autoincrement'])) {
  262. //create PK constraint
  263. $pk_definition = array(
  264. 'fields' => array($field_name => array()),
  265. 'primary' => true,
  266. );
  267. //$pk_name = $name.'_PK';
  268. $pk_name = null;
  269. $result = $this->createConstraint($name, $pk_name, $pk_definition);
  270. if (MDB2::isError($result)) {
  271. return $result;
  272. }
  273. //create autoincrement sequence + trigger
  274. return $this->_makeAutoincrement($field_name, $name, 1);
  275. }
  276. }
  277. return MDB2_OK;
  278. }
  279. // }}}
  280. // {{{ checkSupportedChanges()
  281. /**
  282. * Check if planned changes are supported
  283. *
  284. * @param string $name name of the database that should be dropped
  285. *
  286. * @return mixed MDB2_OK on success, a MDB2 error on failure
  287. * @access public
  288. */
  289. function checkSupportedChanges(&$changes)
  290. {
  291. $db = $this->getDBInstance();
  292. if (MDB2::isError($db)) {
  293. return $db;
  294. }
  295. foreach ($changes as $change_name => $change) {
  296. switch ($change_name) {
  297. case 'notnull':
  298. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  299. 'it is not supported changes to field not null constraint', __FUNCTION__);
  300. case 'default':
  301. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  302. 'it is not supported changes to field default value', __FUNCTION__);
  303. case 'length':
  304. /*
  305. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  306. 'it is not supported changes to field default length', __FUNCTION__);
  307. */
  308. case 'unsigned':
  309. case 'type':
  310. case 'declaration':
  311. case 'definition':
  312. break;
  313. default:
  314. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  315. 'it is not supported change of type' . $change_name, __FUNCTION__);
  316. }
  317. }
  318. return MDB2_OK;
  319. }
  320. // }}}
  321. // {{{ _getTemporaryTableQuery()
  322. /**
  323. * A method to return the required SQL string that fits between CREATE ... TABLE
  324. * to create the table as a temporary table.
  325. *
  326. * @return string The string required to be placed between "CREATE" and "TABLE"
  327. * to generate a temporary table, if possible.
  328. */
  329. function _getTemporaryTableQuery()
  330. {
  331. return 'GLOBAL TEMPORARY';
  332. }
  333. // }}}
  334. // {{{ _getAdvancedFKOptions()
  335. /**
  336. * Return the FOREIGN KEY query section dealing with non-standard options
  337. * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
  338. *
  339. * @param array $definition
  340. *
  341. * @return string
  342. * @access protected
  343. */
  344. function _getAdvancedFKOptions($definition)
  345. {
  346. $query = '';
  347. if (!empty($definition['onupdate'])) {
  348. $query .= ' ON UPDATE '.$definition['onupdate'];
  349. }
  350. if (!empty($definition['ondelete'])) {
  351. $query .= ' ON DELETE '.$definition['ondelete'];
  352. }
  353. return $query;
  354. }
  355. // }}}
  356. // {{{ dropTable()
  357. /**
  358. * drop an existing table
  359. *
  360. * @param string $name name of the table that should be dropped
  361. *
  362. * @return mixed MDB2_OK on success, a MDB2 error on failure
  363. * @access public
  364. */
  365. function dropTable($name)
  366. {
  367. $result = $this->_dropAutoincrement($name);
  368. if (MDB2::isError($result)) {
  369. return $result;
  370. }
  371. $result = parent::dropTable($name);
  372. if (MDB2::isError($result)) {
  373. return $result;
  374. }
  375. $this->_silentCommit();
  376. return $result;
  377. }
  378. // }}}
  379. // {{{ vacuum()
  380. /**
  381. * Optimize (vacuum) all the tables in the db (or only the specified table)
  382. * and optionally run ANALYZE.
  383. *
  384. * @param string $table table name (all the tables if empty)
  385. * @param array $options an array with driver-specific options:
  386. * - timeout [int] (in seconds) [mssql-only]
  387. * - analyze [boolean] [pgsql and mysql]
  388. * - full [boolean] [pgsql-only]
  389. * - freeze [boolean] [pgsql-only]
  390. *
  391. * @return mixed MDB2_OK success, a MDB2 error on failure
  392. * @access public
  393. */
  394. function vacuum($table = null, $options = array())
  395. {
  396. // not needed in Interbase/Firebird
  397. return MDB2_OK;
  398. }
  399. // }}}
  400. // {{{ alterTable()
  401. /**
  402. * alter an existing table
  403. *
  404. * @param string $name name of the table that is intended to be changed.
  405. * @param array $changes associative array that contains the details of each type
  406. * of change that is intended to be performed. The types of
  407. * changes that are currently supported are defined as follows:
  408. *
  409. * name
  410. *
  411. * New name for the table.
  412. *
  413. * add
  414. *
  415. * Associative array with the names of fields to be added as
  416. * indexes of the array. The value of each entry of the array
  417. * should be set to another associative array with the properties
  418. * of the fields to be added. The properties of the fields should
  419. * be the same as defined by the MDB2 parser.
  420. *
  421. *
  422. * remove
  423. *
  424. * Associative array with the names of fields to be removed as indexes
  425. * of the array. Currently the values assigned to each entry are ignored.
  426. * An empty array should be used for future compatibility.
  427. *
  428. * rename
  429. *
  430. * Associative array with the names of fields to be renamed as indexes
  431. * of the array. The value of each entry of the array should be set to
  432. * another associative array with the entry named name with the new
  433. * field name and the entry named Declaration that is expected to contain
  434. * the portion of the field declaration already in DBMS specific SQL code
  435. * as it is used in the CREATE TABLE statement.
  436. *
  437. * change
  438. *
  439. * Associative array with the names of the fields to be changed as indexes
  440. * of the array. Keep in mind that if it is intended to change either the
  441. * name of a field and any other properties, the change array entries
  442. * should have the new names of the fields as array indexes.
  443. *
  444. * The value of each entry of the array should be set to another associative
  445. * array with the properties of the fields to that are meant to be changed as
  446. * array entries. These entries should be assigned to the new values of the
  447. * respective properties. The properties of the fields should be the same
  448. * as defined by the MDB2 parser.
  449. *
  450. * Example
  451. * array(
  452. * 'name' => 'userlist',
  453. * 'add' => array(
  454. * 'quota' => array(
  455. * 'type' => 'integer',
  456. * 'unsigned' => 1
  457. * )
  458. * ),
  459. * 'remove' => array(
  460. * 'file_limit' => array(),
  461. * 'time_limit' => array()
  462. * ),
  463. * 'change' => array(
  464. * 'name' => array(
  465. * 'length' => '20',
  466. * 'definition' => array(
  467. * 'type' => 'text',
  468. * 'length' => 20,
  469. * ),
  470. * )
  471. * ),
  472. * 'rename' => array(
  473. * 'sex' => array(
  474. * 'name' => 'gender',
  475. * 'definition' => array(
  476. * 'type' => 'text',
  477. * 'length' => 1,
  478. * 'default' => 'M',
  479. * ),
  480. * )
  481. * )
  482. * )
  483. *
  484. * @param boolean $check indicates whether the function should just check if the DBMS driver
  485. * can perform the requested table alterations if the value is true or
  486. * actually perform them otherwise.
  487. *
  488. * @return mixed MDB2_OK on success, a MDB2 error on failure
  489. * @access public
  490. */
  491. function alterTable($name, $changes, $check)
  492. {
  493. $db = $this->getDBInstance();
  494. if (MDB2::isError($db)) {
  495. return $db;
  496. }
  497. foreach ($changes as $change_name => $change) {
  498. switch ($change_name) {
  499. case 'add':
  500. case 'remove':
  501. case 'rename':
  502. break;
  503. case 'change':
  504. foreach ($changes['change'] as $field) {
  505. if (MDB2::isError($err = $this->checkSupportedChanges($field))) {
  506. return $err;
  507. }
  508. }
  509. break;
  510. default:
  511. return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
  512. 'change type "' . $change_name . '" not yet supported', __FUNCTION__);
  513. }
  514. }
  515. if ($check) {
  516. return MDB2_OK;
  517. }
  518. $query = '';
  519. if (!empty($changes['add']) && is_array($changes['add'])) {
  520. foreach ($changes['add'] as $field_name => $field) {
  521. if ($query) {
  522. $query.= ', ';
  523. }
  524. $query.= 'ADD ' . $db->getDeclaration($field['type'], $field_name, $field);
  525. }
  526. }
  527. if (!empty($changes['remove']) && is_array($changes['remove'])) {
  528. foreach ($changes['remove'] as $field_name => $field) {
  529. if ($query) {
  530. $query.= ', ';
  531. }
  532. $field_name = $db->quoteIdentifier($field_name, true);
  533. $query.= 'DROP ' . $field_name;
  534. }
  535. }
  536. if (!empty($changes['rename']) && is_array($changes['rename'])) {
  537. foreach ($changes['rename'] as $field_name => $field) {
  538. if ($query) {
  539. $query.= ', ';
  540. }
  541. $field_name = $db->quoteIdentifier($field_name, true);
  542. $query.= 'ALTER ' . $field_name . ' TO ' . $db->quoteIdentifier($field['name'], true);
  543. }
  544. }
  545. if (!empty($changes['change']) && is_array($changes['change'])) {
  546. // missing support to change DEFAULT and NULLability
  547. foreach ($changes['change'] as $field_name => $field) {
  548. if (MDB2::isError($err = $this->checkSupportedChanges($field))) {
  549. return $err;
  550. }
  551. if ($query) {
  552. $query.= ', ';
  553. }
  554. $db->loadModule('Datatype', null, true);
  555. $field_name = $db->quoteIdentifier($field_name, true);
  556. $query.= 'ALTER ' . $field_name.' TYPE ' . $db->datatype->getTypeDeclaration($field['definition']);
  557. }
  558. }
  559. if (!strlen($query)) {
  560. return MDB2_OK;
  561. }
  562. $name = $db->quoteIdentifier($name, true);
  563. $result = $db->exec("ALTER TABLE $name $query");
  564. if (MDB2::isError($result)) {
  565. return $result;
  566. }
  567. $this->_silentCommit();
  568. return MDB2_OK;
  569. }
  570. // }}}
  571. // {{{ listTables()
  572. /**
  573. * list all tables in the current database
  574. *
  575. * @return mixed array of table names on success, a MDB2 error on failure
  576. * @access public
  577. */
  578. function listTables()
  579. {
  580. $db = $this->getDBInstance();
  581. if (MDB2::isError($db)) {
  582. return $db;
  583. }
  584. $query = 'SELECT RDB$RELATION_NAME
  585. FROM RDB$RELATIONS
  586. WHERE (RDB$SYSTEM_FLAG=0 OR RDB$SYSTEM_FLAG IS NULL)
  587. AND RDB$VIEW_BLR IS NULL
  588. ORDER BY RDB$RELATION_NAME';
  589. $result = $db->queryCol($query);
  590. if (MDB2::isError($result)) {
  591. return $result;
  592. }
  593. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  594. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  595. }
  596. return $result;
  597. }
  598. // }}}
  599. // {{{ listTableFields()
  600. /**
  601. * list all fields in a table in the current database
  602. *
  603. * @param string $table name of table that should be used in method
  604. *
  605. * @return mixed array of field names on success, a MDB2 error on failure
  606. * @access public
  607. */
  608. function listTableFields($table)
  609. {
  610. $db = $this->getDBInstance();
  611. if (MDB2::isError($db)) {
  612. return $db;
  613. }
  614. $table = $db->quote(strtoupper($table), 'text');
  615. $query = "SELECT RDB\$FIELD_NAME
  616. FROM RDB\$RELATION_FIELDS
  617. WHERE UPPER(RDB\$RELATION_NAME)=$table
  618. AND (RDB\$SYSTEM_FLAG=0 OR RDB\$SYSTEM_FLAG IS NULL)";
  619. $result = $db->queryCol($query);
  620. if (MDB2::isError($result)) {
  621. return $result;
  622. }
  623. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  624. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  625. }
  626. return $result;
  627. }
  628. // }}}
  629. // {{{ listUsers()
  630. /**
  631. * list all users
  632. *
  633. * @return mixed array of user names on success, a MDB2 error on failure
  634. * @access public
  635. */
  636. function listUsers()
  637. {
  638. $db = $this->getDBInstance();
  639. if (MDB2::isError($db)) {
  640. return $db;
  641. }
  642. return $db->queryCol('SELECT DISTINCT RDB$USER FROM RDB$USER_PRIVILEGES');
  643. }
  644. // }}}
  645. // {{{ listViews()
  646. /**
  647. * list all views in the current database
  648. *
  649. * @return mixed array of view names on success, a MDB2 error on failure
  650. * @access public
  651. */
  652. function listViews()
  653. {
  654. $db = $this->getDBInstance();
  655. if (MDB2::isError($db)) {
  656. return $db;
  657. }
  658. $result = $db->queryCol('SELECT DISTINCT RDB$VIEW_NAME FROM RDB$VIEW_RELATIONS');
  659. if (MDB2::isError($result)) {
  660. return $result;
  661. }
  662. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  663. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  664. }
  665. return $result;
  666. }
  667. // }}}
  668. // {{{ listTableViews()
  669. /**
  670. * list the views in the database that reference a given table
  671. *
  672. * @param string table for which all referenced views should be found
  673. *
  674. * @return mixed array of view names on success, a MDB2 error on failure
  675. * @access public
  676. */
  677. function listTableViews($table)
  678. {
  679. $db = $this->getDBInstance();
  680. if (MDB2::isError($db)) {
  681. return $db;
  682. }
  683. $query = 'SELECT DISTINCT RDB$VIEW_NAME FROM RDB$VIEW_RELATIONS';
  684. $table = $db->quote(strtoupper($table), 'text');
  685. $query .= " WHERE UPPER(RDB\$RELATION_NAME)=$table";
  686. $result = $db->queryCol($query);
  687. if (MDB2::isError($result)) {
  688. return $result;
  689. }
  690. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  691. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  692. }
  693. return $result;
  694. }
  695. // }}}
  696. // {{{ listFunctions()
  697. /**
  698. * list all functions (and stored procedures) in the current database
  699. *
  700. * @return mixed array of function names on success, a MDB2 error on failure
  701. * @access public
  702. */
  703. function listFunctions()
  704. {
  705. $db = $this->getDBInstance();
  706. if (MDB2::isError($db)) {
  707. return $db;
  708. }
  709. $query = 'SELECT RDB$FUNCTION_NAME FROM RDB$FUNCTIONS WHERE (RDB$SYSTEM_FLAG IS NULL OR RDB$SYSTEM_FLAG = 0)
  710. UNION
  711. SELECT RDB$PROCEDURE_NAME FROM RDB$PROCEDURES';
  712. $result = $db->queryCol($query);
  713. if (MDB2::isError($result)) {
  714. return $result;
  715. }
  716. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  717. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  718. }
  719. return $result;
  720. }
  721. // }}}
  722. // {{{ listTableTriggers()
  723. /**
  724. * list all triggers in the database that reference a given table
  725. *
  726. * @param string table for which all referenced triggers should be found
  727. *
  728. * @return mixed array of trigger names on success, a MDB2 error on failure
  729. * @access public
  730. */
  731. function listTableTriggers($table = null)
  732. {
  733. $db = $this->getDBInstance();
  734. if (MDB2::isError($db)) {
  735. return $db;
  736. }
  737. $query = 'SELECT RDB$TRIGGER_NAME
  738. FROM RDB$TRIGGERS
  739. WHERE (RDB$SYSTEM_FLAG IS NULL
  740. OR RDB$SYSTEM_FLAG = 0)';
  741. if (null !== $table) {
  742. $table = $db->quote(strtoupper($table), 'text');
  743. $query .= " AND UPPER(RDB\$RELATION_NAME)=$table";
  744. }
  745. $result = $db->queryCol($query);
  746. if (MDB2::isError($result)) {
  747. return $result;
  748. }
  749. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  750. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  751. }
  752. return $result;
  753. }
  754. // }}}
  755. // {{{ createIndex()
  756. /**
  757. * Get the stucture of a field into an array
  758. *
  759. * @param string $table name of the table on which the index is to be created
  760. * @param string $name name of the index to be created
  761. * @param array $definition associative array that defines properties of the index to be created.
  762. * Currently, only one property named FIELDS is supported. This property
  763. * is also an associative with the names of the index fields as array
  764. * indexes. Each entry of this array is set to another type of associative
  765. * array that specifies properties of the index that are specific to
  766. * each field.
  767. *
  768. * Currently, only the sorting property is supported. It should be used
  769. * to define the sorting direction of the index. It may be set to either
  770. * ascending or descending.
  771. *
  772. * Not all DBMS support index sorting direction configuration. The DBMS
  773. * drivers of those that do not support it ignore this property. Use the
  774. * function support() to determine whether the DBMS driver can manage indexes.
  775. * Example
  776. * array(
  777. * 'fields' => array(
  778. * 'user_name' => array(
  779. * 'sorting' => 'ascending'
  780. * ),
  781. * 'last_login' => array()
  782. * )
  783. * )
  784. *
  785. * @return mixed MDB2_OK on success, a MDB2 error on failure
  786. * @access public
  787. */
  788. function createIndex($table, $name, $definition)
  789. {
  790. $db = $this->getDBInstance();
  791. if (MDB2::isError($db)) {
  792. return $db;
  793. }
  794. $query = 'CREATE';
  795. $query_sort = '';
  796. foreach ($definition['fields'] as $field) {
  797. if (!strcmp($query_sort, '') && isset($field['sorting'])) {
  798. switch ($field['sorting']) {
  799. case 'ascending':
  800. $query_sort = ' ASC';
  801. break;
  802. case 'descending':
  803. $query_sort = ' DESC';
  804. break;
  805. }
  806. }
  807. }
  808. $table = $db->quoteIdentifier($table, true);
  809. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  810. $query .= $query_sort. " INDEX $name ON $table";
  811. $fields = array();
  812. foreach (array_keys($definition['fields']) as $field) {
  813. $fields[] = $db->quoteIdentifier($field, true);
  814. }
  815. $query .= ' ('.implode(', ', $fields) . ')';
  816. $result = $db->exec($query);
  817. if (MDB2::isError($result)) {
  818. return $result;
  819. }
  820. $this->_silentCommit();
  821. return MDB2_OK;
  822. }
  823. // }}}
  824. // {{{ listTableIndexes()
  825. /**
  826. * list all indexes in a table
  827. *
  828. * @param string $table name of table that should be used in method
  829. *
  830. * @return mixed array of index names on success, a MDB2 error on failure
  831. * @access public
  832. */
  833. function listTableIndexes($table)
  834. {
  835. $db = $this->getDBInstance();
  836. if (MDB2::isError($db)) {
  837. return $db;
  838. }
  839. $table = $db->quote(strtoupper($table), 'text');
  840. $query = "SELECT RDB\$INDEX_NAME
  841. FROM RDB\$INDICES
  842. WHERE UPPER(RDB\$RELATION_NAME)=$table
  843. AND (RDB\$SYSTEM_FLAG IS NULL OR RDB\$SYSTEM_FLAG = 0)
  844. AND (RDB\$UNIQUE_FLAG IS NULL OR RDB\$UNIQUE_FLAG = 0)
  845. AND RDB\$FOREIGN_KEY IS NULL";
  846. $indexes = $db->queryCol($query, 'text');
  847. if (MDB2::isError($indexes)) {
  848. return $indexes;
  849. }
  850. $result = array();
  851. foreach ($indexes as $index) {
  852. $index = $this->_fixIndexName($index);
  853. if (!empty($index)) {
  854. $result[$index] = true;
  855. }
  856. }
  857. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  858. $result = array_change_key_case($result, $db->options['field_case']);
  859. }
  860. return array_keys($result);
  861. }
  862. // }}}
  863. // {{{ createConstraint()
  864. /**
  865. * create a constraint on a table
  866. *
  867. * @param string $table name of the table on which the constraint is to be created
  868. * @param string $name name of the constraint to be created
  869. * @param array $definition associative array that defines properties of the constraint to be created.
  870. * Currently, only one property named FIELDS is supported. This property
  871. * is also an associative with the names of the constraint fields as array
  872. * constraints. Each entry of this array is set to another type of associative
  873. * array that specifies properties of the constraint that are specific to
  874. * each field.
  875. *
  876. * Example
  877. * array(
  878. * 'fields' => array(
  879. * 'user_name' => array(),
  880. * 'last_login' => array(),
  881. * )
  882. * )
  883. *
  884. * @return mixed MDB2_OK on success, a MDB2 error on failure
  885. * @access public
  886. */
  887. function createConstraint($table, $name, $definition)
  888. {
  889. $db = $this->getDBInstance();
  890. if (MDB2::isError($db)) {
  891. return $db;
  892. }
  893. $table = $db->quoteIdentifier($table, true);
  894. if (!empty($name)) {
  895. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  896. }
  897. $query = "ALTER TABLE $table ADD";
  898. if (!empty($definition['primary'])) {
  899. if (!empty($name)) {
  900. $query.= ' CONSTRAINT '.$name;
  901. }
  902. $query.= ' PRIMARY KEY';
  903. } else {
  904. $query.= ' CONSTRAINT '. $name;
  905. if (!empty($definition['unique'])) {
  906. $query.= ' UNIQUE';
  907. } elseif (!empty($definition['foreign'])) {
  908. $query.= ' FOREIGN KEY';
  909. }
  910. }
  911. $fields = array();
  912. foreach (array_keys($definition['fields']) as $field) {
  913. $fields[] = $db->quoteIdentifier($field, true);
  914. }
  915. $query .= ' ('. implode(', ', $fields) . ')';
  916. if (!empty($definition['foreign'])) {
  917. $query.= ' REFERENCES ' . $db->quoteIdentifier($definition['references']['table'], true);
  918. $referenced_fields = array();
  919. foreach (array_keys($definition['references']['fields']) as $field) {
  920. $referenced_fields[] = $db->quoteIdentifier($field, true);
  921. }
  922. $query .= ' ('. implode(', ', $referenced_fields) . ')';
  923. $query .= $this->_getAdvancedFKOptions($definition);
  924. }
  925. $result = $db->exec($query);
  926. if (MDB2::isError($result)) {
  927. return $result;
  928. }
  929. $this->_silentCommit();
  930. return MDB2_OK;
  931. }
  932. // }}}
  933. // {{{ listTableConstraints()
  934. /**
  935. * list all constraints in a table
  936. *
  937. * @param string $table name of table that should be used in method
  938. *
  939. * @return mixed array of constraint names on success, a MDB2 error on failure
  940. * @access public
  941. */
  942. function listTableConstraints($table)
  943. {
  944. $db = $this->getDBInstance();
  945. if (MDB2::isError($db)) {
  946. return $db;
  947. }
  948. $table = $db->quote(strtoupper($table), 'text');
  949. $query = "SELECT RDB\$INDEX_NAME
  950. FROM RDB\$INDICES
  951. WHERE UPPER(RDB\$RELATION_NAME)=$table
  952. AND (
  953. (RDB\$UNIQUE_FLAG IS NOT NULL AND RDB\$UNIQUE_FLAG <> 0)
  954. OR RDB\$FOREIGN_KEY IS NOT NULL
  955. )";
  956. $constraints = $db->queryCol($query);
  957. if (MDB2::isError($constraints)) {
  958. return $constraints;
  959. }
  960. $result = array();
  961. foreach ($constraints as $constraint) {
  962. $constraint = $this->_fixIndexName($constraint);
  963. if (!empty($constraint)) {
  964. $result[$constraint] = true;
  965. }
  966. }
  967. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  968. $result = array_change_key_case($result, $db->options['field_case']);
  969. }
  970. return array_keys($result);
  971. }
  972. // }}}
  973. // {{{ createSequence()
  974. /**
  975. * create sequence
  976. *
  977. * @param string $seq_name name of the sequence to be created
  978. * @param string $start start value of the sequence; default is 1
  979. *
  980. * @return mixed MDB2_OK on success, a MDB2 error on failure
  981. * @access public
  982. */
  983. function createSequence($seq_name, $start = 1)
  984. {
  985. $db = $this->getDBInstance();
  986. if (MDB2::isError($db)) {
  987. return $db;
  988. }
  989. $sequence_name = $db->getSequenceName($seq_name);
  990. if (MDB2::isError($result = $db->exec('CREATE GENERATOR '.$sequence_name))) {
  991. return $result;
  992. }
  993. if (MDB2::isError($result = $db->exec('SET GENERATOR '.$sequence_name.' TO '.($start-1)))) {
  994. if (MDB2::isError($err = $db->dropSequence($seq_name))) {
  995. return $db->raiseError($result, null, null,
  996. 'Could not setup sequence start value and then it was not possible to drop it', __FUNCTION__);
  997. }
  998. }
  999. return MDB2_OK;
  1000. }
  1001. // }}}
  1002. // {{{ dropSequence()
  1003. /**
  1004. * drop existing sequence
  1005. *
  1006. * @param string $seq_name name of the sequence to be dropped
  1007. *
  1008. * @return mixed MDB2_OK on success, a MDB2 error on failure
  1009. * @access public
  1010. */
  1011. function dropSequence($seq_name)
  1012. {
  1013. $db = $this->getDBInstance();
  1014. if (MDB2::isError($db)) {
  1015. return $db;
  1016. }
  1017. $sequence_name = $db->getSequenceName($seq_name);
  1018. $sequence_name = $db->quote($sequence_name, 'text');
  1019. $query = "DELETE FROM RDB\$GENERATORS WHERE UPPER(RDB\$GENERATOR_NAME)=$sequence_name";
  1020. $result = $db->exec($query);
  1021. if (MDB2::isError($result)) {
  1022. return $result;
  1023. }
  1024. return MDB2_OK;
  1025. }
  1026. // }}}
  1027. // {{{ listSequences()
  1028. /**
  1029. * list all sequences in the current database
  1030. *
  1031. * @return mixed array of sequence names on success, a MDB2 error on failure
  1032. * @access public
  1033. */
  1034. function listSequences()
  1035. {
  1036. $db = $this->getDBInstance();
  1037. if (MDB2::isError($db)) {
  1038. return $db;
  1039. }
  1040. $query = 'SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS WHERE (RDB$SYSTEM_FLAG IS NULL OR RDB$SYSTEM_FLAG = 0)';
  1041. $table_names = $db->queryCol($query);
  1042. if (MDB2::isError($table_names)) {
  1043. return $table_names;
  1044. }
  1045. $result = array();
  1046. foreach ($table_names as $table_name) {
  1047. $result[] = $this->_fixSequenceName($table_name);
  1048. }
  1049. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  1050. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  1051. }
  1052. return $result;
  1053. }
  1054. }
  1055. ?>