Common.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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 |
  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. // | Authors: Lukas Smith <smith@pooteeweet.org> |
  43. // | Lorenzo Alberton <l.alberton@quipo.it> |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id$
  47. //
  48. /**
  49. * @package MDB2
  50. * @category Database
  51. * @author Lukas Smith <smith@pooteeweet.org>
  52. * @author Lorenzo Alberton <l.alberton@quipo.it>
  53. */
  54. /**
  55. * Base class for the management modules that is extended by each MDB2 driver
  56. *
  57. * To load this module in the MDB2 object:
  58. * $mdb->loadModule('Manager');
  59. *
  60. * @package MDB2
  61. * @category Database
  62. * @author Lukas Smith <smith@pooteeweet.org>
  63. */
  64. class MDB2_Driver_Manager_Common extends MDB2_Module_Common
  65. {
  66. // {{{ splitTableSchema()
  67. /**
  68. * Split the "[owner|schema].table" notation into an array
  69. *
  70. * @param string $table [schema and] table name
  71. *
  72. * @return array array(schema, table)
  73. * @access private
  74. */
  75. function splitTableSchema($table)
  76. {
  77. $ret = array();
  78. if (strpos($table, '.') !== false) {
  79. return explode('.', $table);
  80. }
  81. return array(null, $table);
  82. }
  83. // }}}
  84. // {{{ getFieldDeclarationList()
  85. /**
  86. * Get declaration of a number of field in bulk
  87. *
  88. * @param array $fields a multidimensional associative array.
  89. * The first dimension determines the field name, while the second
  90. * dimension is keyed with the name of the properties
  91. * of the field being declared as array indexes. Currently, the types
  92. * of supported field properties are as follows:
  93. *
  94. * default
  95. * Boolean value to be used as default for this field.
  96. *
  97. * notnull
  98. * Boolean flag that indicates whether this field is constrained
  99. * to not be set to null.
  100. *
  101. * @return mixed string on success, a MDB2 error on failure
  102. * @access public
  103. */
  104. function getFieldDeclarationList($fields)
  105. {
  106. $db = $this->getDBInstance();
  107. if (MDB2::isError($db)) {
  108. return $db;
  109. }
  110. if (!is_array($fields) || empty($fields)) {
  111. return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  112. 'missing any fields', __FUNCTION__);
  113. }
  114. foreach ($fields as $field_name => $field) {
  115. $query = $db->getDeclaration($field['type'], $field_name, $field);
  116. if (MDB2::isError($query)) {
  117. return $query;
  118. }
  119. $query_fields[] = $query;
  120. }
  121. return implode(', ', $query_fields);
  122. }
  123. // }}}
  124. // {{{ _fixSequenceName()
  125. /**
  126. * Removes any formatting in an sequence name using the 'seqname_format' option
  127. *
  128. * @param string $sqn string that containts name of a potential sequence
  129. * @param bool $check if only formatted sequences should be returned
  130. * @return string name of the sequence with possible formatting removed
  131. * @access protected
  132. */
  133. function _fixSequenceName($sqn, $check = false)
  134. {
  135. $db = $this->getDBInstance();
  136. if (MDB2::isError($db)) {
  137. return $db;
  138. }
  139. $seq_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['seqname_format']).'$/i';
  140. $seq_name = preg_replace($seq_pattern, '\\1', $sqn);
  141. if ($seq_name && !strcasecmp($sqn, $db->getSequenceName($seq_name))) {
  142. return $seq_name;
  143. }
  144. if ($check) {
  145. return false;
  146. }
  147. return $sqn;
  148. }
  149. // }}}
  150. // {{{ _fixIndexName()
  151. /**
  152. * Removes any formatting in an index name using the 'idxname_format' option
  153. *
  154. * @param string $idx string that containts name of anl index
  155. * @return string name of the index with eventual formatting removed
  156. * @access protected
  157. */
  158. function _fixIndexName($idx)
  159. {
  160. $db = $this->getDBInstance();
  161. if (MDB2::isError($db)) {
  162. return $db;
  163. }
  164. $idx_pattern = '/^'.preg_replace('/%s/', '([a-z0-9_]+)', $db->options['idxname_format']).'$/i';
  165. $idx_name = preg_replace($idx_pattern, '\\1', $idx);
  166. if ($idx_name && !strcasecmp($idx, $db->getIndexName($idx_name))) {
  167. return $idx_name;
  168. }
  169. return $idx;
  170. }
  171. // }}}
  172. // {{{ createDatabase()
  173. /**
  174. * create a new database
  175. *
  176. * @param string $name name of the database that should be created
  177. * @param array $options array with charset, collation info
  178. *
  179. * @return mixed MDB2_OK on success, a MDB2 error on failure
  180. * @access public
  181. */
  182. function createDatabase($database, $options = array())
  183. {
  184. $db = $this->getDBInstance();
  185. if (MDB2::isError($db)) {
  186. return $db;
  187. }
  188. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  189. 'method not implemented', __FUNCTION__);
  190. }
  191. // }}}
  192. // {{{ alterDatabase()
  193. /**
  194. * alter an existing database
  195. *
  196. * @param string $name name of the database that should be created
  197. * @param array $options array with charset, collation info
  198. *
  199. * @return mixed MDB2_OK on success, a MDB2 error on failure
  200. * @access public
  201. */
  202. function alterDatabase($database, $options = array())
  203. {
  204. $db = $this->getDBInstance();
  205. if (MDB2::isError($db)) {
  206. return $db;
  207. }
  208. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  209. 'method not implemented', __FUNCTION__);
  210. }
  211. // }}}
  212. // {{{ dropDatabase()
  213. /**
  214. * drop an existing database
  215. *
  216. * @param string $name name of the database that should be dropped
  217. * @return mixed MDB2_OK on success, a MDB2 error on failure
  218. * @access public
  219. */
  220. function dropDatabase($database)
  221. {
  222. $db = $this->getDBInstance();
  223. if (MDB2::isError($db)) {
  224. return $db;
  225. }
  226. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  227. 'method not implemented', __FUNCTION__);
  228. }
  229. // }}}
  230. // {{{ _getCreateTableQuery()
  231. /**
  232. * Create a basic SQL query for a new table creation
  233. *
  234. * @param string $name Name of the database that should be created
  235. * @param array $fields Associative array that contains the definition of each field of the new table
  236. * @param array $options An associative array of table options
  237. *
  238. * @return mixed string (the SQL query) on success, a MDB2 error on failure
  239. * @see createTable()
  240. */
  241. function _getCreateTableQuery($name, $fields, $options = array())
  242. {
  243. $db = $this->getDBInstance();
  244. if (MDB2::isError($db)) {
  245. return $db;
  246. }
  247. if (!$name) {
  248. return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
  249. 'no valid table name specified', __FUNCTION__);
  250. }
  251. if (empty($fields)) {
  252. return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
  253. 'no fields specified for table "'.$name.'"', __FUNCTION__);
  254. }
  255. $query_fields = $this->getFieldDeclarationList($fields);
  256. if (MDB2::isError($query_fields)) {
  257. return $query_fields;
  258. }
  259. if (!empty($options['primary'])) {
  260. $query_fields.= ', PRIMARY KEY ('.implode(', ', array_keys($options['primary'])).')';
  261. }
  262. $name = $db->quoteIdentifier($name, true);
  263. $result = 'CREATE ';
  264. if (!empty($options['temporary'])) {
  265. $result .= $this->_getTemporaryTableQuery();
  266. }
  267. $result .= " TABLE $name ($query_fields)";
  268. return $result;
  269. }
  270. // }}}
  271. // {{{ _getTemporaryTableQuery()
  272. /**
  273. * A method to return the required SQL string that fits between CREATE ... TABLE
  274. * to create the table as a temporary table.
  275. *
  276. * Should be overridden in driver classes to return the correct string for the
  277. * specific database type.
  278. *
  279. * The default is to return the string "TEMPORARY" - this will result in a
  280. * SQL error for any database that does not support temporary tables, or that
  281. * requires a different SQL command from "CREATE TEMPORARY TABLE".
  282. *
  283. * @return string The string required to be placed between "CREATE" and "TABLE"
  284. * to generate a temporary table, if possible.
  285. */
  286. function _getTemporaryTableQuery()
  287. {
  288. return 'TEMPORARY';
  289. }
  290. // }}}
  291. // {{{ createTable()
  292. /**
  293. * create a new table
  294. *
  295. * @param string $name Name of the database that should be created
  296. * @param array $fields Associative array that contains the definition of each field of the new table
  297. * The indexes of the array entries are the names of the fields of the table an
  298. * the array entry values are associative arrays like those that are meant to be
  299. * passed with the field definitions to get[Type]Declaration() functions.
  300. * array(
  301. * 'id' => array(
  302. * 'type' => 'integer',
  303. * 'unsigned' => 1
  304. * 'notnull' => 1
  305. * 'default' => 0
  306. * ),
  307. * 'name' => array(
  308. * 'type' => 'text',
  309. * 'length' => 12
  310. * ),
  311. * 'password' => array(
  312. * 'type' => 'text',
  313. * 'length' => 12
  314. * )
  315. * );
  316. * @param array $options An associative array of table options:
  317. * array(
  318. * 'comment' => 'Foo',
  319. * 'temporary' => true|false,
  320. * );
  321. * @return mixed MDB2_OK on success, a MDB2 error on failure
  322. * @access public
  323. */
  324. function createTable($name, $fields, $options = array())
  325. {
  326. $query = $this->_getCreateTableQuery($name, $fields, $options);
  327. if (MDB2::isError($query)) {
  328. return $query;
  329. }
  330. $db = $this->getDBInstance();
  331. if (MDB2::isError($db)) {
  332. return $db;
  333. }
  334. $result = $db->exec($query);
  335. if (MDB2::isError($result)) {
  336. return $result;
  337. }
  338. return MDB2_OK;
  339. }
  340. // }}}
  341. // {{{ dropTable()
  342. /**
  343. * drop an existing table
  344. *
  345. * @param string $name name of the table that should be dropped
  346. * @return mixed MDB2_OK on success, a MDB2 error on failure
  347. * @access public
  348. */
  349. function dropTable($name)
  350. {
  351. $db = $this->getDBInstance();
  352. if (MDB2::isError($db)) {
  353. return $db;
  354. }
  355. $name = $db->quoteIdentifier($name, true);
  356. $result = $db->exec("DROP TABLE $name");
  357. if (MDB2::isError($result)) {
  358. return $result;
  359. }
  360. return MDB2_OK;
  361. }
  362. // }}}
  363. // {{{ truncateTable()
  364. /**
  365. * Truncate an existing table (if the TRUNCATE TABLE syntax is not supported,
  366. * it falls back to a DELETE FROM TABLE query)
  367. *
  368. * @param string $name name of the table that should be truncated
  369. * @return mixed MDB2_OK on success, a MDB2 error on failure
  370. * @access public
  371. */
  372. function truncateTable($name)
  373. {
  374. $db = $this->getDBInstance();
  375. if (MDB2::isError($db)) {
  376. return $db;
  377. }
  378. $name = $db->quoteIdentifier($name, true);
  379. $result = $db->exec("DELETE FROM $name");
  380. if (MDB2::isError($result)) {
  381. return $result;
  382. }
  383. return MDB2_OK;
  384. }
  385. // }}}
  386. // {{{ vacuum()
  387. /**
  388. * Optimize (vacuum) all the tables in the db (or only the specified table)
  389. * and optionally run ANALYZE.
  390. *
  391. * @param string $table table name (all the tables if empty)
  392. * @param array $options an array with driver-specific options:
  393. * - timeout [int] (in seconds) [mssql-only]
  394. * - analyze [boolean] [pgsql and mysql]
  395. * - full [boolean] [pgsql-only]
  396. * - freeze [boolean] [pgsql-only]
  397. *
  398. * @return mixed MDB2_OK success, a MDB2 error on failure
  399. * @access public
  400. */
  401. function vacuum($table = null, $options = array())
  402. {
  403. $db = $this->getDBInstance();
  404. if (MDB2::isError($db)) {
  405. return $db;
  406. }
  407. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  408. 'method not implemented', __FUNCTION__);
  409. }
  410. // }}}
  411. // {{{ alterTable()
  412. /**
  413. * alter an existing table
  414. *
  415. * @param string $name name of the table that is intended to be changed.
  416. * @param array $changes associative array that contains the details of each type
  417. * of change that is intended to be performed. The types of
  418. * changes that are currently supported are defined as follows:
  419. *
  420. * name
  421. *
  422. * New name for the table.
  423. *
  424. * add
  425. *
  426. * Associative array with the names of fields to be added as
  427. * indexes of the array. The value of each entry of the array
  428. * should be set to another associative array with the properties
  429. * of the fields to be added. The properties of the fields should
  430. * be the same as defined by the MDB2 parser.
  431. *
  432. *
  433. * remove
  434. *
  435. * Associative array with the names of fields to be removed as indexes
  436. * of the array. Currently the values assigned to each entry are ignored.
  437. * An empty array should be used for future compatibility.
  438. *
  439. * rename
  440. *
  441. * Associative array with the names of fields to be renamed as indexes
  442. * of the array. The value of each entry of the array should be set to
  443. * another associative array with the entry named name with the new
  444. * field name and the entry named Declaration that is expected to contain
  445. * the portion of the field declaration already in DBMS specific SQL code
  446. * as it is used in the CREATE TABLE statement.
  447. *
  448. * change
  449. *
  450. * Associative array with the names of the fields to be changed as indexes
  451. * of the array. Keep in mind that if it is intended to change either the
  452. * name of a field and any other properties, the change array entries
  453. * should have the new names of the fields as array indexes.
  454. *
  455. * The value of each entry of the array should be set to another associative
  456. * array with the properties of the fields to that are meant to be changed as
  457. * array entries. These entries should be assigned to the new values of the
  458. * respective properties. The properties of the fields should be the same
  459. * as defined by the MDB2 parser.
  460. *
  461. * Example
  462. * array(
  463. * 'name' => 'userlist',
  464. * 'add' => array(
  465. * 'quota' => array(
  466. * 'type' => 'integer',
  467. * 'unsigned' => 1
  468. * )
  469. * ),
  470. * 'remove' => array(
  471. * 'file_limit' => array(),
  472. * 'time_limit' => array()
  473. * ),
  474. * 'change' => array(
  475. * 'name' => array(
  476. * 'length' => '20',
  477. * 'definition' => array(
  478. * 'type' => 'text',
  479. * 'length' => 20,
  480. * ),
  481. * )
  482. * ),
  483. * 'rename' => array(
  484. * 'sex' => array(
  485. * 'name' => 'gender',
  486. * 'definition' => array(
  487. * 'type' => 'text',
  488. * 'length' => 1,
  489. * 'default' => 'M',
  490. * ),
  491. * )
  492. * )
  493. * )
  494. *
  495. * @param boolean $check indicates whether the function should just check if the DBMS driver
  496. * can perform the requested table alterations if the value is true or
  497. * actually perform them otherwise.
  498. * @access public
  499. *
  500. * @return mixed MDB2_OK on success, a MDB2 error on failure
  501. */
  502. function alterTable($name, $changes, $check)
  503. {
  504. $db = $this->getDBInstance();
  505. if (MDB2::isError($db)) {
  506. return $db;
  507. }
  508. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  509. 'method not implemented', __FUNCTION__);
  510. }
  511. // }}}
  512. // {{{ listDatabases()
  513. /**
  514. * list all databases
  515. *
  516. * @return mixed array of database names on success, a MDB2 error on failure
  517. * @access public
  518. */
  519. function listDatabases()
  520. {
  521. $db = $this->getDBInstance();
  522. if (MDB2::isError($db)) {
  523. return $db;
  524. }
  525. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  526. 'method not implementedd', __FUNCTION__);
  527. }
  528. // }}}
  529. // {{{ listUsers()
  530. /**
  531. * list all users
  532. *
  533. * @return mixed array of user names on success, a MDB2 error on failure
  534. * @access public
  535. */
  536. function listUsers()
  537. {
  538. $db = $this->getDBInstance();
  539. if (MDB2::isError($db)) {
  540. return $db;
  541. }
  542. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  543. 'method not implemented', __FUNCTION__);
  544. }
  545. // }}}
  546. // {{{ listViews()
  547. /**
  548. * list all views in the current database
  549. *
  550. * @param string database, the current is default
  551. * NB: not all the drivers can get the view names from
  552. * a database other than the current one
  553. * @return mixed array of view names on success, a MDB2 error on failure
  554. * @access public
  555. */
  556. function listViews($database = null)
  557. {
  558. $db = $this->getDBInstance();
  559. if (MDB2::isError($db)) {
  560. return $db;
  561. }
  562. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  563. 'method not implemented', __FUNCTION__);
  564. }
  565. // }}}
  566. // {{{ listTableViews()
  567. /**
  568. * list the views in the database that reference a given table
  569. *
  570. * @param string table for which all referenced views should be found
  571. * @return mixed array of view names on success, a MDB2 error on failure
  572. * @access public
  573. */
  574. function listTableViews($table)
  575. {
  576. $db = $this->getDBInstance();
  577. if (MDB2::isError($db)) {
  578. return $db;
  579. }
  580. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  581. 'method not implemented', __FUNCTION__);
  582. }
  583. // }}}
  584. // {{{ listTableTriggers()
  585. /**
  586. * list all triggers in the database that reference a given table
  587. *
  588. * @param string table for which all referenced triggers should be found
  589. * @return mixed array of trigger names on success, a MDB2 error on failure
  590. * @access public
  591. */
  592. function listTableTriggers($table = null)
  593. {
  594. $db = $this->getDBInstance();
  595. if (MDB2::isError($db)) {
  596. return $db;
  597. }
  598. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  599. 'method not implemented', __FUNCTION__);
  600. }
  601. // }}}
  602. // {{{ listFunctions()
  603. /**
  604. * list all functions in the current database
  605. *
  606. * @return mixed array of function names on success, a MDB2 error on failure
  607. * @access public
  608. */
  609. function listFunctions()
  610. {
  611. $db = $this->getDBInstance();
  612. if (MDB2::isError($db)) {
  613. return $db;
  614. }
  615. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  616. 'method not implemented', __FUNCTION__);
  617. }
  618. // }}}
  619. // {{{ listTables()
  620. /**
  621. * list all tables in the current database
  622. *
  623. * @param string database, the current is default.
  624. * NB: not all the drivers can get the table names from
  625. * a database other than the current one
  626. * @return mixed array of table names on success, a MDB2 error on failure
  627. * @access public
  628. */
  629. function listTables($database = null)
  630. {
  631. $db = $this->getDBInstance();
  632. if (MDB2::isError($db)) {
  633. return $db;
  634. }
  635. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  636. 'method not implemented', __FUNCTION__);
  637. }
  638. // }}}
  639. // {{{ listTableFields()
  640. /**
  641. * list all fields in a table in the current database
  642. *
  643. * @param string $table name of table that should be used in method
  644. * @return mixed array of field names on success, a MDB2 error on failure
  645. * @access public
  646. */
  647. function listTableFields($table)
  648. {
  649. $db = $this->getDBInstance();
  650. if (MDB2::isError($db)) {
  651. return $db;
  652. }
  653. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  654. 'method not implemented', __FUNCTION__);
  655. }
  656. // }}}
  657. // {{{ createIndex()
  658. /**
  659. * Get the stucture of a field into an array
  660. *
  661. * @param string $table name of the table on which the index is to be created
  662. * @param string $name name of the index to be created
  663. * @param array $definition associative array that defines properties of the index to be created.
  664. * Currently, only one property named FIELDS is supported. This property
  665. * is also an associative with the names of the index fields as array
  666. * indexes. Each entry of this array is set to another type of associative
  667. * array that specifies properties of the index that are specific to
  668. * each field.
  669. *
  670. * Currently, only the sorting property is supported. It should be used
  671. * to define the sorting direction of the index. It may be set to either
  672. * ascending or descending.
  673. *
  674. * Not all DBMS support index sorting direction configuration. The DBMS
  675. * drivers of those that do not support it ignore this property. Use the
  676. * function supports() to determine whether the DBMS driver can manage indexes.
  677. *
  678. * Example
  679. * array(
  680. * 'fields' => array(
  681. * 'user_name' => array(
  682. * 'sorting' => 'ascending'
  683. * ),
  684. * 'last_login' => array()
  685. * )
  686. * )
  687. * @return mixed MDB2_OK on success, a MDB2 error on failure
  688. * @access public
  689. */
  690. function createIndex($table, $name, $definition)
  691. {
  692. $db = $this->getDBInstance();
  693. if (MDB2::isError($db)) {
  694. return $db;
  695. }
  696. $table = $db->quoteIdentifier($table, true);
  697. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  698. $query = "CREATE INDEX $name ON $table";
  699. $fields = array();
  700. foreach (array_keys($definition['fields']) as $field) {
  701. $fields[] = $db->quoteIdentifier($field, true);
  702. }
  703. $query .= ' ('. implode(', ', $fields) . ')';
  704. $result = $db->exec($query);
  705. if (MDB2::isError($result)) {
  706. return $result;
  707. }
  708. return MDB2_OK;
  709. }
  710. // }}}
  711. // {{{ dropIndex()
  712. /**
  713. * drop existing index
  714. *
  715. * @param string $table name of table that should be used in method
  716. * @param string $name name of the index to be dropped
  717. * @return mixed MDB2_OK on success, a MDB2 error on failure
  718. * @access public
  719. */
  720. function dropIndex($table, $name)
  721. {
  722. $db = $this->getDBInstance();
  723. if (MDB2::isError($db)) {
  724. return $db;
  725. }
  726. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  727. $result = $db->exec("DROP INDEX $name");
  728. if (MDB2::isError($result)) {
  729. return $result;
  730. }
  731. return MDB2_OK;
  732. }
  733. // }}}
  734. // {{{ listTableIndexes()
  735. /**
  736. * list all indexes in a table
  737. *
  738. * @param string $table name of table that should be used in method
  739. * @return mixed array of index names on success, a MDB2 error on failure
  740. * @access public
  741. */
  742. function listTableIndexes($table)
  743. {
  744. $db = $this->getDBInstance();
  745. if (MDB2::isError($db)) {
  746. return $db;
  747. }
  748. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  749. 'method not implemented', __FUNCTION__);
  750. }
  751. // }}}
  752. // {{{ _getAdvancedFKOptions()
  753. /**
  754. * Return the FOREIGN KEY query section dealing with non-standard options
  755. * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
  756. *
  757. * @param array $definition
  758. * @return string
  759. * @access protected
  760. */
  761. function _getAdvancedFKOptions($definition)
  762. {
  763. return '';
  764. }
  765. // }}}
  766. // {{{ createConstraint()
  767. /**
  768. * create a constraint on a table
  769. *
  770. * @param string $table name of the table on which the constraint is to be created
  771. * @param string $name name of the constraint to be created
  772. * @param array $definition associative array that defines properties of the constraint to be created.
  773. * The full structure of the array looks like this:
  774. * <pre>
  775. * array (
  776. * [primary] => 0
  777. * [unique] => 0
  778. * [foreign] => 1
  779. * [check] => 0
  780. * [fields] => array (
  781. * [field1name] => array() // one entry per each field covered
  782. * [field2name] => array() // by the index
  783. * [field3name] => array(
  784. * [sorting] => ascending
  785. * [position] => 3
  786. * )
  787. * )
  788. * [references] => array(
  789. * [table] => name
  790. * [fields] => array(
  791. * [field1name] => array( //one entry per each referenced field
  792. * [position] => 1
  793. * )
  794. * )
  795. * )
  796. * [deferrable] => 0
  797. * [initiallydeferred] => 0
  798. * [onupdate] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION
  799. * [ondelete] => CASCADE|RESTRICT|SET NULL|SET DEFAULT|NO ACTION
  800. * [match] => SIMPLE|PARTIAL|FULL
  801. * );
  802. * </pre>
  803. * @return mixed MDB2_OK on success, a MDB2 error on failure
  804. * @access public
  805. */
  806. function createConstraint($table, $name, $definition)
  807. {
  808. $db = $this->getDBInstance();
  809. if (MDB2::isError($db)) {
  810. return $db;
  811. }
  812. $table = $db->quoteIdentifier($table, true);
  813. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  814. $query = "ALTER TABLE $table ADD CONSTRAINT $name";
  815. if (!empty($definition['primary'])) {
  816. $query.= ' PRIMARY KEY';
  817. } elseif (!empty($definition['unique'])) {
  818. $query.= ' UNIQUE';
  819. } elseif (!empty($definition['foreign'])) {
  820. $query.= ' FOREIGN KEY';
  821. }
  822. $fields = array();
  823. foreach (array_keys($definition['fields']) as $field) {
  824. $fields[] = $db->quoteIdentifier($field, true);
  825. }
  826. $query .= ' ('. implode(', ', $fields) . ')';
  827. if (!empty($definition['foreign'])) {
  828. $query.= ' REFERENCES ' . $db->quoteIdentifier($definition['references']['table'], true);
  829. $referenced_fields = array();
  830. foreach (array_keys($definition['references']['fields']) as $field) {
  831. $referenced_fields[] = $db->quoteIdentifier($field, true);
  832. }
  833. $query .= ' ('. implode(', ', $referenced_fields) . ')';
  834. $query .= $this->_getAdvancedFKOptions($definition);
  835. }
  836. $result = $db->exec($query);
  837. if (MDB2::isError($result)) {
  838. return $result;
  839. }
  840. return MDB2_OK;
  841. }
  842. // }}}
  843. // {{{ dropConstraint()
  844. /**
  845. * drop existing constraint
  846. *
  847. * @param string $table name of table that should be used in method
  848. * @param string $name name of the constraint to be dropped
  849. * @param string $primary hint if the constraint is primary
  850. * @return mixed MDB2_OK on success, a MDB2 error on failure
  851. * @access public
  852. */
  853. function dropConstraint($table, $name, $primary = false)
  854. {
  855. $db = $this->getDBInstance();
  856. if (MDB2::isError($db)) {
  857. return $db;
  858. }
  859. $table = $db->quoteIdentifier($table, true);
  860. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  861. $result = $db->exec("ALTER TABLE $table DROP CONSTRAINT $name");
  862. if (MDB2::isError($result)) {
  863. return $result;
  864. }
  865. return MDB2_OK;
  866. }
  867. // }}}
  868. // {{{ listTableConstraints()
  869. /**
  870. * list all constraints in a table
  871. *
  872. * @param string $table name of table that should be used in method
  873. * @return mixed array of constraint names on success, a MDB2 error on failure
  874. * @access public
  875. */
  876. function listTableConstraints($table)
  877. {
  878. $db = $this->getDBInstance();
  879. if (MDB2::isError($db)) {
  880. return $db;
  881. }
  882. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  883. 'method not implemented', __FUNCTION__);
  884. }
  885. // }}}
  886. // {{{ createSequence()
  887. /**
  888. * create sequence
  889. *
  890. * @param string $seq_name name of the sequence to be created
  891. * @param string $start start value of the sequence; default is 1
  892. * @return mixed MDB2_OK on success, a MDB2 error on failure
  893. * @access public
  894. */
  895. function createSequence($seq_name, $start = 1)
  896. {
  897. $db = $this->getDBInstance();
  898. if (MDB2::isError($db)) {
  899. return $db;
  900. }
  901. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  902. 'method not implemented', __FUNCTION__);
  903. }
  904. // }}}
  905. // {{{ dropSequence()
  906. /**
  907. * drop existing sequence
  908. *
  909. * @param string $seq_name name of the sequence to be dropped
  910. * @return mixed MDB2_OK on success, a MDB2 error on failure
  911. * @access public
  912. */
  913. function dropSequence($name)
  914. {
  915. $db = $this->getDBInstance();
  916. if (MDB2::isError($db)) {
  917. return $db;
  918. }
  919. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  920. 'method not implemented', __FUNCTION__);
  921. }
  922. // }}}
  923. // {{{ listSequences()
  924. /**
  925. * list all sequences in the current database
  926. *
  927. * @param string database, the current is default
  928. * NB: not all the drivers can get the sequence names from
  929. * a database other than the current one
  930. * @return mixed array of sequence names on success, a MDB2 error on failure
  931. * @access public
  932. */
  933. function listSequences($database = null)
  934. {
  935. $db = $this->getDBInstance();
  936. if (MDB2::isError($db)) {
  937. return $db;
  938. }
  939. return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  940. 'method not implemented', __FUNCTION__);
  941. }
  942. // }}}
  943. }
  944. ?>