PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/MDB2/Driver/Manager/mysql.php

https://github.com/chregu/fluxcms
PHP | 1001 lines | 502 code | 99 blank | 400 comment | 113 complexity | 07cc4b2c8ed18d4630737ee71436c6a8 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, Apache-2.0, LGPL-2.1
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2007 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. // | Author: Lukas Smith <smith@pooteeweet.org> |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id$
  46. //
  47. require_once 'MDB2/Driver/Manager/Common.php';
  48. /**
  49. * MDB2 MySQL driver for the management modules
  50. *
  51. * @package MDB2
  52. * @category Database
  53. * @author Lukas Smith <smith@pooteeweet.org>
  54. */
  55. class MDB2_Driver_Manager_mysql extends MDB2_Driver_Manager_Common
  56. {
  57. // }}}
  58. // {{{ createDatabase()
  59. /**
  60. * create a new database
  61. *
  62. * @param string $name name of the database that should be created
  63. * @return mixed MDB2_OK on success, a MDB2 error on failure
  64. * @access public
  65. */
  66. function createDatabase($name)
  67. {
  68. $db =& $this->getDBInstance();
  69. if (PEAR::isError($db)) {
  70. return $db;
  71. }
  72. $name = $db->quoteIdentifier($name, true);
  73. $query = "CREATE DATABASE $name";
  74. $result = $db->exec($query);
  75. if (PEAR::isError($result)) {
  76. return $result;
  77. }
  78. return MDB2_OK;
  79. }
  80. // }}}
  81. // {{{ dropDatabase()
  82. /**
  83. * drop an existing database
  84. *
  85. * @param string $name name of the database that should be dropped
  86. * @return mixed MDB2_OK on success, a MDB2 error on failure
  87. * @access public
  88. */
  89. function dropDatabase($name)
  90. {
  91. $db =& $this->getDBInstance();
  92. if (PEAR::isError($db)) {
  93. return $db;
  94. }
  95. $name = $db->quoteIdentifier($name, true);
  96. $query = "DROP DATABASE $name";
  97. $result = $db->exec($query);
  98. if (PEAR::isError($result)) {
  99. return $result;
  100. }
  101. return MDB2_OK;
  102. }
  103. // }}}
  104. // {{{ createTable()
  105. /**
  106. * create a new table
  107. *
  108. * @param string $name Name of the database that should be created
  109. * @param array $fields Associative array that contains the definition of each field of the new table
  110. * The indexes of the array entries are the names of the fields of the table an
  111. * the array entry values are associative arrays like those that are meant to be
  112. * passed with the field definitions to get[Type]Declaration() functions.
  113. * array(
  114. * 'id' => array(
  115. * 'type' => 'integer',
  116. * 'unsigned' => 1
  117. * 'notnull' => 1
  118. * 'default' => 0
  119. * ),
  120. * 'name' => array(
  121. * 'type' => 'text',
  122. * 'length' => 12
  123. * ),
  124. * 'password' => array(
  125. * 'type' => 'text',
  126. * 'length' => 12
  127. * )
  128. * );
  129. * @param array $options An associative array of table options:
  130. * array(
  131. * 'comment' => 'Foo',
  132. * 'charset' => 'utf8',
  133. * 'collate' => 'utf8_unicode_ci',
  134. * 'type' => 'innodb',
  135. * );
  136. *
  137. * @return mixed MDB2_OK on success, a MDB2 error on failure
  138. * @access public
  139. */
  140. function createTable($name, $fields, $options = array())
  141. {
  142. $db =& $this->getDBInstance();
  143. if (PEAR::isError($db)) {
  144. return $db;
  145. }
  146. $query = $this->_getCreateTableQuery($name, $fields, $options);
  147. if (PEAR::isError($query)) {
  148. return $query;
  149. }
  150. $options_strings = array();
  151. if (!empty($options['comment'])) {
  152. $options_strings['comment'] = 'COMMENT = '.$db->quote($options['comment'], 'text');
  153. }
  154. if (!empty($options['charset'])) {
  155. $options_strings['charset'] = 'DEFAULT CHARACTER SET '.$options['charset'];
  156. if (!empty($options['collate'])) {
  157. $options_strings['charset'].= ' COLLATE '.$options['collate'];
  158. }
  159. }
  160. $type = false;
  161. if (!empty($options['type'])) {
  162. $type = $options['type'];
  163. } elseif ($db->options['default_table_type']) {
  164. $type = $db->options['default_table_type'];
  165. }
  166. if ($type) {
  167. $options_strings[] = "ENGINE = $type";
  168. }
  169. if (!empty($options_strings)) {
  170. $query .= ' '.implode(' ', $options_strings);
  171. }
  172. return $db->exec($query);
  173. }
  174. // }}}
  175. // {{{ alterTable()
  176. /**
  177. * alter an existing table
  178. *
  179. * @param string $name name of the table that is intended to be changed.
  180. * @param array $changes associative array that contains the details of each type
  181. * of change that is intended to be performed. The types of
  182. * changes that are currently supported are defined as follows:
  183. *
  184. * name
  185. *
  186. * New name for the table.
  187. *
  188. * add
  189. *
  190. * Associative array with the names of fields to be added as
  191. * indexes of the array. The value of each entry of the array
  192. * should be set to another associative array with the properties
  193. * of the fields to be added. The properties of the fields should
  194. * be the same as defined by the MDB2 parser.
  195. *
  196. *
  197. * remove
  198. *
  199. * Associative array with the names of fields to be removed as indexes
  200. * of the array. Currently the values assigned to each entry are ignored.
  201. * An empty array should be used for future compatibility.
  202. *
  203. * rename
  204. *
  205. * Associative array with the names of fields to be renamed as indexes
  206. * of the array. The value of each entry of the array should be set to
  207. * another associative array with the entry named name with the new
  208. * field name and the entry named Declaration that is expected to contain
  209. * the portion of the field declaration already in DBMS specific SQL code
  210. * as it is used in the CREATE TABLE statement.
  211. *
  212. * change
  213. *
  214. * Associative array with the names of the fields to be changed as indexes
  215. * of the array. Keep in mind that if it is intended to change either the
  216. * name of a field and any other properties, the change array entries
  217. * should have the new names of the fields as array indexes.
  218. *
  219. * The value of each entry of the array should be set to another associative
  220. * array with the properties of the fields to that are meant to be changed as
  221. * array entries. These entries should be assigned to the new values of the
  222. * respective properties. The properties of the fields should be the same
  223. * as defined by the MDB2 parser.
  224. *
  225. * Example
  226. * array(
  227. * 'name' => 'userlist',
  228. * 'add' => array(
  229. * 'quota' => array(
  230. * 'type' => 'integer',
  231. * 'unsigned' => 1
  232. * )
  233. * ),
  234. * 'remove' => array(
  235. * 'file_limit' => array(),
  236. * 'time_limit' => array()
  237. * ),
  238. * 'change' => array(
  239. * 'name' => array(
  240. * 'length' => '20',
  241. * 'definition' => array(
  242. * 'type' => 'text',
  243. * 'length' => 20,
  244. * ),
  245. * )
  246. * ),
  247. * 'rename' => array(
  248. * 'sex' => array(
  249. * 'name' => 'gender',
  250. * 'definition' => array(
  251. * 'type' => 'text',
  252. * 'length' => 1,
  253. * 'default' => 'M',
  254. * ),
  255. * )
  256. * )
  257. * )
  258. *
  259. * @param boolean $check indicates whether the function should just check if the DBMS driver
  260. * can perform the requested table alterations if the value is true or
  261. * actually perform them otherwise.
  262. * @access public
  263. *
  264. * @return mixed MDB2_OK on success, a MDB2 error on failure
  265. */
  266. function alterTable($name, $changes, $check)
  267. {
  268. $db =& $this->getDBInstance();
  269. if (PEAR::isError($db)) {
  270. return $db;
  271. }
  272. foreach ($changes as $change_name => $change) {
  273. switch ($change_name) {
  274. case 'add':
  275. case 'remove':
  276. case 'change':
  277. case 'rename':
  278. case 'name':
  279. break;
  280. default:
  281. return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
  282. 'change type "'.$change_name.'" not yet supported', __FUNCTION__);
  283. }
  284. }
  285. if ($check) {
  286. return MDB2_OK;
  287. }
  288. $query = '';
  289. if (!empty($changes['name'])) {
  290. $change_name = $db->quoteIdentifier($changes['name'], true);
  291. $query .= 'RENAME TO ' . $change_name;
  292. }
  293. if (!empty($changes['add']) && is_array($changes['add'])) {
  294. foreach ($changes['add'] as $field_name => $field) {
  295. if ($query) {
  296. $query.= ', ';
  297. }
  298. $query.= 'ADD ' . $db->getDeclaration($field['type'], $field_name, $field);
  299. }
  300. }
  301. if (!empty($changes['remove']) && is_array($changes['remove'])) {
  302. foreach ($changes['remove'] as $field_name => $field) {
  303. if ($query) {
  304. $query.= ', ';
  305. }
  306. $field_name = $db->quoteIdentifier($field_name, true);
  307. $query.= 'DROP ' . $field_name;
  308. }
  309. }
  310. $rename = array();
  311. if (!empty($changes['rename']) && is_array($changes['rename'])) {
  312. foreach ($changes['rename'] as $field_name => $field) {
  313. $rename[$field['name']] = $field_name;
  314. }
  315. }
  316. if (!empty($changes['change']) && is_array($changes['change'])) {
  317. foreach ($changes['change'] as $field_name => $field) {
  318. if ($query) {
  319. $query.= ', ';
  320. }
  321. if (isset($rename[$field_name])) {
  322. $old_field_name = $rename[$field_name];
  323. unset($rename[$field_name]);
  324. } else {
  325. $old_field_name = $field_name;
  326. }
  327. $old_field_name = $db->quoteIdentifier($old_field_name, true);
  328. $query.= "CHANGE $old_field_name " . $db->getDeclaration($field['definition']['type'], $field_name, $field['definition']);
  329. }
  330. }
  331. if (!empty($rename) && is_array($rename)) {
  332. foreach ($rename as $rename_name => $renamed_field) {
  333. if ($query) {
  334. $query.= ', ';
  335. }
  336. $field = $changes['rename'][$renamed_field];
  337. $renamed_field = $db->quoteIdentifier($renamed_field, true);
  338. $query.= 'CHANGE ' . $renamed_field . ' ' . $db->getDeclaration($field['definition']['type'], $field['name'], $field['definition']);
  339. }
  340. }
  341. if (!$query) {
  342. return MDB2_OK;
  343. }
  344. $name = $db->quoteIdentifier($name, true);
  345. return $db->exec("ALTER TABLE $name $query");
  346. }
  347. // }}}
  348. // {{{ listDatabases()
  349. /**
  350. * list all databases
  351. *
  352. * @return mixed array of database names on success, a MDB2 error on failure
  353. * @access public
  354. */
  355. function listDatabases()
  356. {
  357. $db =& $this->getDBInstance();
  358. if (PEAR::isError($db)) {
  359. return $db;
  360. }
  361. $result = $db->queryCol('SHOW DATABASES');
  362. if (PEAR::isError($result)) {
  363. return $result;
  364. }
  365. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  366. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  367. }
  368. return $result;
  369. }
  370. // }}}
  371. // {{{ listUsers()
  372. /**
  373. * list all users
  374. *
  375. * @return mixed array of user names on success, a MDB2 error on failure
  376. * @access public
  377. */
  378. function listUsers()
  379. {
  380. $db =& $this->getDBInstance();
  381. if (PEAR::isError($db)) {
  382. return $db;
  383. }
  384. return $db->queryCol('SELECT DISTINCT USER FROM mysql.USER');
  385. }
  386. // }}}
  387. // {{{ listFunctions()
  388. /**
  389. * list all functions in the current database
  390. *
  391. * @return mixed array of function names on success, a MDB2 error on failure
  392. * @access public
  393. */
  394. function listFunctions()
  395. {
  396. $db =& $this->getDBInstance();
  397. if (PEAR::isError($db)) {
  398. return $db;
  399. }
  400. $query = "SELECT name FROM mysql.proc";
  401. /*
  402. SELECT ROUTINE_NAME
  403. FROM INFORMATION_SCHEMA.ROUTINES
  404. WHERE ROUTINE_TYPE = 'FUNCTION'
  405. */
  406. $result = $db->queryCol($query);
  407. if (PEAR::isError($result)) {
  408. return $result;
  409. }
  410. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  411. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  412. }
  413. return $result;
  414. }
  415. // }}}
  416. // {{{ listTableTriggers()
  417. /**
  418. * list all triggers in the database that reference a given table
  419. *
  420. * @param string table for which all referenced triggers should be found
  421. * @return mixed array of trigger names on success, a MDB2 error on failure
  422. * @access public
  423. */
  424. function listTableTriggers($table = null)
  425. {
  426. $db =& $this->getDBInstance();
  427. if (PEAR::isError($db)) {
  428. return $db;
  429. }
  430. $query = 'SHOW TRIGGERS';
  431. if (!is_null($table)) {
  432. $table = $db->quote($table, 'text');
  433. $query .= " LIKE $table";
  434. }
  435. $result = $db->queryCol($query);
  436. if (PEAR::isError($result)) {
  437. return $result;
  438. }
  439. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  440. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  441. }
  442. return $result;
  443. }
  444. // }}}
  445. // {{{ listTables()
  446. /**
  447. * list all tables in the current database
  448. *
  449. * @param string database, the current is default
  450. * @return mixed array of table names on success, a MDB2 error on failure
  451. * @access public
  452. */
  453. function listTables($database = null)
  454. {
  455. $db =& $this->getDBInstance();
  456. if (PEAR::isError($db)) {
  457. return $db;
  458. }
  459. $query = "SHOW /*!50002 FULL*/ TABLES";
  460. if (!is_null($database)) {
  461. $query .= " FROM $database";
  462. }
  463. $query.= "/*!50002 WHERE Table_type = 'BASE TABLE'*/";
  464. $table_names = $db->queryAll($query, null, MDB2_FETCHMODE_ORDERED);
  465. if (PEAR::isError($table_names)) {
  466. return $table_names;
  467. }
  468. $result = array();
  469. foreach ($table_names as $table) {
  470. if (!$this->_fixSequenceName($table[0], true)) {
  471. $result[] = $table[0];
  472. }
  473. }
  474. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  475. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  476. }
  477. return $result;
  478. }
  479. // }}}
  480. // {{{ listViews()
  481. /**
  482. * list all views in the current database
  483. *
  484. * @param string database, the current is default
  485. * @return mixed array of view names on success, a MDB2 error on failure
  486. * @access public
  487. */
  488. function listViews($database = null)
  489. {
  490. $db =& $this->getDBInstance();
  491. if (PEAR::isError($db)) {
  492. return $db;
  493. }
  494. $query = 'SHOW FULL TABLES';
  495. if (!is_null($database)) {
  496. $query.= " FROM $database";
  497. }
  498. $query.= " WHERE Table_type = 'VIEW'";
  499. $result = $db->queryCol($query);
  500. if (PEAR::isError($result)) {
  501. return $result;
  502. }
  503. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  504. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  505. }
  506. return $result;
  507. }
  508. // }}}
  509. // {{{ listTableFields()
  510. /**
  511. * list all fields in a table in the current database
  512. *
  513. * @param string $table name of table that should be used in method
  514. * @return mixed array of field names on success, a MDB2 error on failure
  515. * @access public
  516. */
  517. function listTableFields($table)
  518. {
  519. $db =& $this->getDBInstance();
  520. if (PEAR::isError($db)) {
  521. return $db;
  522. }
  523. $table = $db->quoteIdentifier($table, true);
  524. $result = $db->queryCol("SHOW COLUMNS FROM $table");
  525. if (PEAR::isError($result)) {
  526. return $result;
  527. }
  528. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  529. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  530. }
  531. return $result;
  532. }
  533. // }}}
  534. // {{{ createIndex()
  535. /**
  536. * Get the stucture of a field into an array
  537. *
  538. * @author Leoncx
  539. * @param string $table name of the table on which the index is to be created
  540. * @param string $name name of the index to be created
  541. * @param array $definition associative array that defines properties of the index to be created.
  542. * Currently, only one property named FIELDS is supported. This property
  543. * is also an associative with the names of the index fields as array
  544. * indexes. Each entry of this array is set to another type of associative
  545. * array that specifies properties of the index that are specific to
  546. * each field.
  547. *
  548. * Currently, only the sorting property is supported. It should be used
  549. * to define the sorting direction of the index. It may be set to either
  550. * ascending or descending.
  551. *
  552. * Not all DBMS support index sorting direction configuration. The DBMS
  553. * drivers of those that do not support it ignore this property. Use the
  554. * function supports() to determine whether the DBMS driver can manage indexes.
  555. *
  556. * Example
  557. * array(
  558. * 'fields' => array(
  559. * 'user_name' => array(
  560. * 'sorting' => 'ascending'
  561. * 'length' => 10
  562. * ),
  563. * 'last_login' => array()
  564. * )
  565. * )
  566. * @return mixed MDB2_OK on success, a MDB2 error on failure
  567. * @access public
  568. */
  569. function createIndex($table, $name, $definition)
  570. {
  571. $db =& $this->getDBInstance();
  572. if (PEAR::isError($db)) {
  573. return $db;
  574. }
  575. $table = $db->quoteIdentifier($table, true);
  576. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  577. $query = "CREATE INDEX $name ON $table";
  578. $fields = array();
  579. foreach ($definition['fields'] as $field => $fieldinfo) {
  580. if (!empty($fieldinfo['length'])) {
  581. $fields[] = $db->quoteIdentifier($field, true) . '(' . $fieldinfo['length'] . ')';
  582. } else {
  583. $fields[] = $db->quoteIdentifier($field, true);
  584. }
  585. }
  586. $query .= ' ('. implode(', ', $fields) . ')';
  587. return $db->exec($query);
  588. }
  589. // }}}
  590. // {{{ dropIndex()
  591. /**
  592. * drop existing index
  593. *
  594. * @param string $table name of table that should be used in method
  595. * @param string $name name of the index to be dropped
  596. * @return mixed MDB2_OK on success, a MDB2 error on failure
  597. * @access public
  598. */
  599. function dropIndex($table, $name)
  600. {
  601. $db =& $this->getDBInstance();
  602. if (PEAR::isError($db)) {
  603. return $db;
  604. }
  605. $table = $db->quoteIdentifier($table, true);
  606. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  607. return $db->exec("DROP INDEX $name ON $table");
  608. }
  609. // }}}
  610. // {{{ listTableIndexes()
  611. /**
  612. * list all indexes in a table
  613. *
  614. * @param string $table name of table that should be used in method
  615. * @return mixed array of index names on success, a MDB2 error on failure
  616. * @access public
  617. */
  618. function listTableIndexes($table)
  619. {
  620. $db =& $this->getDBInstance();
  621. if (PEAR::isError($db)) {
  622. return $db;
  623. }
  624. $key_name = 'Key_name';
  625. $non_unique = 'Non_unique';
  626. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  627. if ($db->options['field_case'] == CASE_LOWER) {
  628. $key_name = strtolower($key_name);
  629. $non_unique = strtolower($non_unique);
  630. } else {
  631. $key_name = strtoupper($key_name);
  632. $non_unique = strtoupper($non_unique);
  633. }
  634. }
  635. $table = $db->quoteIdentifier($table, true);
  636. $query = "SHOW INDEX FROM $table";
  637. $indexes = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
  638. if (PEAR::isError($indexes)) {
  639. return $indexes;
  640. }
  641. $result = array();
  642. foreach ($indexes as $index_data) {
  643. if ($index_data[$non_unique] && ($index = $this->_fixIndexName($index_data[$key_name]))) {
  644. $result[$index] = true;
  645. }
  646. }
  647. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  648. $result = array_change_key_case($result, $db->options['field_case']);
  649. }
  650. return array_keys($result);
  651. }
  652. // }}}
  653. // {{{ createConstraint()
  654. /**
  655. * create a constraint on a table
  656. *
  657. * @param string $table name of the table on which the constraint is to be created
  658. * @param string $name name of the constraint to be created
  659. * @param array $definition associative array that defines properties of the constraint to be created.
  660. * Currently, only one property named FIELDS is supported. This property
  661. * is also an associative with the names of the constraint fields as array
  662. * constraints. Each entry of this array is set to another type of associative
  663. * array that specifies properties of the constraint that are specific to
  664. * each field.
  665. *
  666. * Example
  667. * array(
  668. * 'fields' => array(
  669. * 'user_name' => array(),
  670. * 'last_login' => array()
  671. * )
  672. * )
  673. * @return mixed MDB2_OK on success, a MDB2 error on failure
  674. * @access public
  675. */
  676. function createConstraint($table, $name, $definition)
  677. {
  678. $db =& $this->getDBInstance();
  679. if (PEAR::isError($db)) {
  680. return $db;
  681. }
  682. $type = '';
  683. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  684. if (!empty($definition['primary'])) {
  685. $type = 'PRIMARY';
  686. $name = 'KEY';
  687. } elseif (!empty($definition['unique'])) {
  688. $type = 'UNIQUE';
  689. }
  690. if (empty($type)) {
  691. return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  692. 'invalid definition, could not create constraint', __FUNCTION__);
  693. }
  694. $table = $db->quoteIdentifier($table, true);
  695. $query = "ALTER TABLE $table ADD $type $name";
  696. $fields = array();
  697. foreach (array_keys($definition['fields']) as $field) {
  698. $fields[] = $db->quoteIdentifier($field, true);
  699. }
  700. $query .= ' ('. implode(', ', $fields) . ')';
  701. return $db->exec($query);
  702. }
  703. // }}}
  704. // {{{ dropConstraint()
  705. /**
  706. * drop existing constraint
  707. *
  708. * @param string $table name of table that should be used in method
  709. * @param string $name name of the constraint to be dropped
  710. * @param string $primary hint if the constraint is primary
  711. * @return mixed MDB2_OK on success, a MDB2 error on failure
  712. * @access public
  713. */
  714. function dropConstraint($table, $name, $primary = false)
  715. {
  716. $db =& $this->getDBInstance();
  717. if (PEAR::isError($db)) {
  718. return $db;
  719. }
  720. $table = $db->quoteIdentifier($table, true);
  721. if ($primary || strtolower($name) == 'primary') {
  722. $query = "ALTER TABLE $table DROP PRIMARY KEY";
  723. } else {
  724. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  725. $query = "ALTER TABLE $table DROP INDEX $name";
  726. }
  727. return $db->exec($query);
  728. }
  729. // }}}
  730. // {{{ listTableConstraints()
  731. /**
  732. * list all constraints in a table
  733. *
  734. * @param string $table name of table that should be used in method
  735. * @return mixed array of constraint names on success, a MDB2 error on failure
  736. * @access public
  737. */
  738. function listTableConstraints($table)
  739. {
  740. $db =& $this->getDBInstance();
  741. if (PEAR::isError($db)) {
  742. return $db;
  743. }
  744. $key_name = 'Key_name';
  745. $non_unique = 'Non_unique';
  746. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  747. if ($db->options['field_case'] == CASE_LOWER) {
  748. $key_name = strtolower($key_name);
  749. $non_unique = strtolower($non_unique);
  750. } else {
  751. $key_name = strtoupper($key_name);
  752. $non_unique = strtoupper($non_unique);
  753. }
  754. }
  755. $table = $db->quoteIdentifier($table, true);
  756. $query = "SHOW INDEX FROM $table";
  757. $indexes = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
  758. if (PEAR::isError($indexes)) {
  759. return $indexes;
  760. }
  761. $result = array();
  762. foreach ($indexes as $index_data) {
  763. if (!$index_data[$non_unique]) {
  764. if ($index_data[$key_name] !== 'PRIMARY') {
  765. $index = $this->_fixIndexName($index_data[$key_name]);
  766. } else {
  767. $index = 'PRIMARY';
  768. }
  769. if (!empty($index)) {
  770. $result[$index] = true;
  771. }
  772. }
  773. }
  774. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  775. $result = array_change_key_case($result, $db->options['field_case']);
  776. }
  777. return array_keys($result);
  778. }
  779. // }}}
  780. // {{{ createSequence()
  781. /**
  782. * create sequence
  783. *
  784. * @param string $seq_name name of the sequence to be created
  785. * @param string $start start value of the sequence; default is 1
  786. * @param array $options An associative array of table options:
  787. * array(
  788. * 'comment' => 'Foo',
  789. * 'charset' => 'utf8',
  790. * 'collate' => 'utf8_unicode_ci',
  791. * 'type' => 'innodb',
  792. * );
  793. * @return mixed MDB2_OK on success, a MDB2 error on failure
  794. * @access public
  795. */
  796. function createSequence($seq_name, $start = 1, $options = array())
  797. {
  798. $db =& $this->getDBInstance();
  799. if (PEAR::isError($db)) {
  800. return $db;
  801. }
  802. $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
  803. $seqcol_name = $db->quoteIdentifier($db->options['seqcol_name'], true);
  804. $options_strings = array();
  805. if (!empty($options['comment'])) {
  806. $options_strings['comment'] = 'COMMENT = '.$db->quote($options['comment'], 'text');
  807. }
  808. if (!empty($options['charset'])) {
  809. $options_strings['charset'] = 'DEFAULT CHARACTER SET '.$options['charset'];
  810. if (!empty($options['collate'])) {
  811. $options_strings['charset'].= ' COLLATE '.$options['collate'];
  812. }
  813. }
  814. $type = false;
  815. if (!empty($options['type'])) {
  816. $type = $options['type'];
  817. } elseif ($db->options['default_table_type']) {
  818. $type = $db->options['default_table_type'];
  819. }
  820. if ($type) {
  821. $options_strings[] = "ENGINE = $type";
  822. }
  823. $query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
  824. if (!empty($options_strings)) {
  825. $query .= ' '.implode(' ', $options_strings);
  826. }
  827. $res = $db->exec($query);
  828. if (PEAR::isError($res)) {
  829. return $res;
  830. }
  831. if ($start == 1) {
  832. return MDB2_OK;
  833. }
  834. $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')';
  835. $res = $db->exec($query);
  836. if (!PEAR::isError($res)) {
  837. return MDB2_OK;
  838. }
  839. // Handle error
  840. $result = $db->exec("DROP TABLE $sequence_name");
  841. if (PEAR::isError($result)) {
  842. return $db->raiseError($result, null, null,
  843. 'could not drop inconsistent sequence table', __FUNCTION__);
  844. }
  845. return $db->raiseError($res, null, null,
  846. 'could not create sequence table', __FUNCTION__);
  847. }
  848. // }}}
  849. // {{{ dropSequence()
  850. /**
  851. * drop existing sequence
  852. *
  853. * @param string $seq_name name of the sequence to be dropped
  854. * @return mixed MDB2_OK on success, a MDB2 error on failure
  855. * @access public
  856. */
  857. function dropSequence($seq_name)
  858. {
  859. $db =& $this->getDBInstance();
  860. if (PEAR::isError($db)) {
  861. return $db;
  862. }
  863. $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
  864. return $db->exec("DROP TABLE $sequence_name");
  865. }
  866. // }}}
  867. // {{{ listSequences()
  868. /**
  869. * list all sequences in the current database
  870. *
  871. * @param string database, the current is default
  872. * @return mixed array of sequence names on success, a MDB2 error on failure
  873. * @access public
  874. */
  875. function listSequences($database = null)
  876. {
  877. $db =& $this->getDBInstance();
  878. if (PEAR::isError($db)) {
  879. return $db;
  880. }
  881. $query = "SHOW TABLES";
  882. if (!is_null($database)) {
  883. $query .= " FROM $database";
  884. }
  885. $table_names = $db->queryCol($query);
  886. if (PEAR::isError($table_names)) {
  887. return $table_names;
  888. }
  889. $result = array();
  890. foreach ($table_names as $table_name) {
  891. if ($sqn = $this->_fixSequenceName($table_name, true)) {
  892. $result[] = $sqn;
  893. }
  894. }
  895. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  896. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  897. }
  898. return $result;
  899. }
  900. // }}}
  901. }
  902. ?>