Automatically exported from code.google.com/p/planningalerts
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

mysql.php 34 KiB

17 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
  6. // | Stig. S. Bakken, Lukas Smith |
  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: mysql.php,v 1.84 2006/08/21 16:39:37 lsmith Exp $
  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. * 'character_set' => 'utf8',
  133. * 'collate' => 'utf8_unicode_ci',
  134. * 'collate' => 'utf8_unicode_ci',
  135. * 'type' => 'innodb',
  136. * );
  137. *
  138. * @return mixed MDB2_OK on success, a MDB2 error on failure
  139. * @access public
  140. */
  141. function createTable($name, $fields, $options = array())
  142. {
  143. $db =& $this->getDBInstance();
  144. if (PEAR::isError($db)) {
  145. return $db;
  146. }
  147. if (!$name) {
  148. return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
  149. 'no valid table name specified', __FUNCTION__);
  150. }
  151. if (empty($fields)) {
  152. return $db->raiseError(MDB2_ERROR_CANNOT_CREATE, null, null,
  153. 'no fields specified for table "'.$name.'"', __FUNCTION__);
  154. }
  155. $query_fields = $this->getFieldDeclarationList($fields);
  156. if (PEAR::isError($query_fields)) {
  157. return $query_fields;
  158. }
  159. if (!empty($options['primary'])) {
  160. $query_fields.= ', PRIMARY KEY ('.implode(', ', array_keys($options['primary'])).')';
  161. }
  162. $name = $db->quoteIdentifier($name, true);
  163. $query = "CREATE TABLE $name ($query_fields)";
  164. $options_strings = array();
  165. if (!empty($options['comment'])) {
  166. $options_strings['comment'] = 'COMMENT = '.$db->quote($options['comment'], 'text');
  167. }
  168. if (!empty($options['charset'])) {
  169. $options_strings['charset'] = 'DEFAULT CHARACTER SET '.$options['charset'];
  170. if (!empty($options['collate'])) {
  171. $options_strings['charset'].= ' COLLATE '.$options['collate'];
  172. }
  173. }
  174. $type = false;
  175. if (!empty($options['type'])) {
  176. $type = $options['type'];
  177. } elseif ($db->options['default_table_type']) {
  178. $type = $db->options['default_table_type'];
  179. }
  180. if ($type) {
  181. $options_strings[] = "ENGINE = $type";
  182. }
  183. if (!empty($options_strings)) {
  184. $query.= ' '.implode(' ', $options_strings);
  185. }
  186. return $db->exec($query);
  187. }
  188. // }}}
  189. // {{{ alterTable()
  190. /**
  191. * alter an existing table
  192. *
  193. * @param string $name name of the table that is intended to be changed.
  194. * @param array $changes associative array that contains the details of each type
  195. * of change that is intended to be performed. The types of
  196. * changes that are currently supported are defined as follows:
  197. *
  198. * name
  199. *
  200. * New name for the table.
  201. *
  202. * add
  203. *
  204. * Associative array with the names of fields to be added as
  205. * indexes of the array. The value of each entry of the array
  206. * should be set to another associative array with the properties
  207. * of the fields to be added. The properties of the fields should
  208. * be the same as defined by the Metabase parser.
  209. *
  210. *
  211. * remove
  212. *
  213. * Associative array with the names of fields to be removed as indexes
  214. * of the array. Currently the values assigned to each entry are ignored.
  215. * An empty array should be used for future compatibility.
  216. *
  217. * rename
  218. *
  219. * Associative array with the names of fields to be renamed as indexes
  220. * of the array. The value of each entry of the array should be set to
  221. * another associative array with the entry named name with the new
  222. * field name and the entry named Declaration that is expected to contain
  223. * the portion of the field declaration already in DBMS specific SQL code
  224. * as it is used in the CREATE TABLE statement.
  225. *
  226. * change
  227. *
  228. * Associative array with the names of the fields to be changed as indexes
  229. * of the array. Keep in mind that if it is intended to change either the
  230. * name of a field and any other properties, the change array entries
  231. * should have the new names of the fields as array indexes.
  232. *
  233. * The value of each entry of the array should be set to another associative
  234. * array with the properties of the fields to that are meant to be changed as
  235. * array entries. These entries should be assigned to the new values of the
  236. * respective properties. The properties of the fields should be the same
  237. * as defined by the Metabase parser.
  238. *
  239. * Example
  240. * array(
  241. * 'name' => 'userlist',
  242. * 'add' => array(
  243. * 'quota' => array(
  244. * 'type' => 'integer',
  245. * 'unsigned' => 1
  246. * )
  247. * ),
  248. * 'remove' => array(
  249. * 'file_limit' => array(),
  250. * 'time_limit' => array()
  251. * ),
  252. * 'change' => array(
  253. * 'name' => array(
  254. * 'length' => '20',
  255. * 'definition' => array(
  256. * 'type' => 'text',
  257. * 'length' => 20,
  258. * ),
  259. * )
  260. * ),
  261. * 'rename' => array(
  262. * 'sex' => array(
  263. * 'name' => 'gender',
  264. * 'definition' => array(
  265. * 'type' => 'text',
  266. * 'length' => 1,
  267. * 'default' => 'M',
  268. * ),
  269. * )
  270. * )
  271. * )
  272. *
  273. * @param boolean $check indicates whether the function should just check if the DBMS driver
  274. * can perform the requested table alterations if the value is true or
  275. * actually perform them otherwise.
  276. * @access public
  277. *
  278. * @return mixed MDB2_OK on success, a MDB2 error on failure
  279. */
  280. function alterTable($name, $changes, $check)
  281. {
  282. $db =& $this->getDBInstance();
  283. if (PEAR::isError($db)) {
  284. return $db;
  285. }
  286. foreach ($changes as $change_name => $change) {
  287. switch ($change_name) {
  288. case 'add':
  289. case 'remove':
  290. case 'change':
  291. case 'rename':
  292. case 'name':
  293. break;
  294. default:
  295. return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
  296. 'change type "'.$change_name.'" not yet supported', __FUNCTION__);
  297. }
  298. }
  299. if ($check) {
  300. return MDB2_OK;
  301. }
  302. $query = '';
  303. if (!empty($changes['name'])) {
  304. $change_name = $db->quoteIdentifier($changes['name'], true);
  305. $query .= 'RENAME TO ' . $change_name;
  306. }
  307. if (!empty($changes['add']) && is_array($changes['add'])) {
  308. foreach ($changes['add'] as $field_name => $field) {
  309. if ($query) {
  310. $query.= ', ';
  311. }
  312. $query.= 'ADD ' . $db->getDeclaration($field['type'], $field_name, $field);
  313. }
  314. }
  315. if (!empty($changes['remove']) && is_array($changes['remove'])) {
  316. foreach ($changes['remove'] as $field_name => $field) {
  317. if ($query) {
  318. $query.= ', ';
  319. }
  320. $field_name = $db->quoteIdentifier($field_name, true);
  321. $query.= 'DROP ' . $field_name;
  322. }
  323. }
  324. $rename = array();
  325. if (!empty($changes['rename']) && is_array($changes['rename'])) {
  326. foreach ($changes['rename'] as $field_name => $field) {
  327. $rename[$field['name']] = $field_name;
  328. }
  329. }
  330. if (!empty($changes['change']) && is_array($changes['change'])) {
  331. foreach ($changes['change'] as $field_name => $field) {
  332. if ($query) {
  333. $query.= ', ';
  334. }
  335. if (isset($rename[$field_name])) {
  336. $old_field_name = $rename[$field_name];
  337. unset($rename[$field_name]);
  338. } else {
  339. $old_field_name = $field_name;
  340. }
  341. $old_field_name = $db->quoteIdentifier($old_field_name, true);
  342. $query.= "CHANGE $old_field_name " . $db->getDeclaration($field['definition']['type'], $field_name, $field['definition']);
  343. }
  344. }
  345. if (!empty($rename) && is_array($rename)) {
  346. foreach ($rename as $rename_name => $renamed_field) {
  347. if ($query) {
  348. $query.= ', ';
  349. }
  350. $field = $changes['rename'][$renamed_field];
  351. $renamed_field = $db->quoteIdentifier($renamed_field, true);
  352. $query.= 'CHANGE ' . $renamed_field . ' ' . $db->getDeclaration($field['definition']['type'], $field['name'], $field['definition']);
  353. }
  354. }
  355. if (!$query) {
  356. return MDB2_OK;
  357. }
  358. $name = $db->quoteIdentifier($name, true);
  359. return $db->exec("ALTER TABLE $name $query");
  360. }
  361. // }}}
  362. // {{{ listDatabases()
  363. /**
  364. * list all databases
  365. *
  366. * @return mixed data array on success, a MDB2 error on failure
  367. * @access public
  368. */
  369. function listDatabases()
  370. {
  371. $db =& $this->getDBInstance();
  372. if (PEAR::isError($db)) {
  373. return $db;
  374. }
  375. $result = $db->queryCol('SHOW DATABASES');
  376. if (PEAR::isError($result)) {
  377. return $result;
  378. }
  379. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  380. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  381. }
  382. return $result;
  383. }
  384. // }}}
  385. // {{{ listUsers()
  386. /**
  387. * list all users
  388. *
  389. * @return mixed data array on success, a MDB2 error on failure
  390. * @access public
  391. */
  392. function listUsers()
  393. {
  394. $db =& $this->getDBInstance();
  395. if (PEAR::isError($db)) {
  396. return $db;
  397. }
  398. return $db->queryCol('SELECT DISTINCT USER FROM USER');
  399. }
  400. // }}}
  401. // {{{ listTables()
  402. /**
  403. * list all tables in the current database
  404. *
  405. * @param string database, the current is default
  406. * @return mixed data array on success, a MDB2 error on failure
  407. * @access public
  408. */
  409. function listTables($database = null)
  410. {
  411. $db =& $this->getDBInstance();
  412. if (PEAR::isError($db)) {
  413. return $db;
  414. }
  415. $query = "SHOW /*!50002 FULL*/ TABLES";
  416. if (!is_null($database)) {
  417. $query .= " FROM $database";
  418. }
  419. $query.= "/*!50002 WHERE Table_type = 'BASE TABLE'*/";
  420. $table_names = $db->queryAll($query, null, MDB2_FETCHMODE_ORDERED);
  421. if (PEAR::isError($table_names)) {
  422. return $table_names;
  423. }
  424. $result = array();
  425. foreach ($table_names as $table) {
  426. if (!$this->_fixSequenceName($table[0], true)) {
  427. $result[] = $table[0];
  428. }
  429. }
  430. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  431. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  432. }
  433. return $result;
  434. }
  435. // }}}
  436. // {{{ listViews()
  437. /**
  438. * list the views in the database
  439. *
  440. * @param string database, the current is default
  441. * @return mixed MDB2_OK on success, a MDB2 error on failure
  442. * @access public
  443. **/
  444. function listViews($database = null)
  445. {
  446. $db =& $this->getDBInstance();
  447. if (PEAR::isError($db)) {
  448. return $db;
  449. }
  450. $query = 'SHOW FULL TABLES';
  451. if (!is_null($database)) {
  452. $query.= " FROM $database";
  453. }
  454. $query.= " WHERE Table_type = 'VIEW'";
  455. $result = $db->queryCol($query);
  456. if (PEAR::isError($result)) {
  457. return $result;
  458. }
  459. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  460. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  461. }
  462. return $result;
  463. }
  464. // }}}
  465. // {{{ listTableFields()
  466. /**
  467. * list all fields in a tables in the current database
  468. *
  469. * @param string $table name of table that should be used in method
  470. * @return mixed data array on success, a MDB2 error on failure
  471. * @access public
  472. */
  473. function listTableFields($table)
  474. {
  475. $db =& $this->getDBInstance();
  476. if (PEAR::isError($db)) {
  477. return $db;
  478. }
  479. $table = $db->quoteIdentifier($table, true);
  480. $result = $db->queryCol("SHOW COLUMNS FROM $table");
  481. if (PEAR::isError($result)) {
  482. return $result;
  483. }
  484. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  485. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  486. }
  487. return $result;
  488. }
  489. // }}}
  490. // {{{ createIndex()
  491. /**
  492. * Get the stucture of a field into an array
  493. *
  494. * @author Leoncx
  495. * @param string $table name of the table on which the index is to be created
  496. * @param string $name name of the index to be created
  497. * @param array $definition associative array that defines properties of the index to be created.
  498. * Currently, only one property named FIELDS is supported. This property
  499. * is also an associative with the names of the index fields as array
  500. * indexes. Each entry of this array is set to another type of associative
  501. * array that specifies properties of the index that are specific to
  502. * each field.
  503. *
  504. * Currently, only the sorting property is supported. It should be used
  505. * to define the sorting direction of the index. It may be set to either
  506. * ascending or descending.
  507. *
  508. * Not all DBMS support index sorting direction configuration. The DBMS
  509. * drivers of those that do not support it ignore this property. Use the
  510. * function supports() to determine whether the DBMS driver can manage indexes.
  511. *
  512. * Example
  513. * array(
  514. * 'fields' => array(
  515. * 'user_name' => array(
  516. * 'sorting' => 'ascending'
  517. * 'length' => 10
  518. * ),
  519. * 'last_login' => array()
  520. * )
  521. * )
  522. * @return mixed MDB2_OK on success, a MDB2 error on failure
  523. * @access public
  524. */
  525. function createIndex($table, $name, $definition)
  526. {
  527. $db =& $this->getDBInstance();
  528. if (PEAR::isError($db)) {
  529. return $db;
  530. }
  531. $table = $db->quoteIdentifier($table, true);
  532. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  533. $query = "CREATE INDEX $name ON $table";
  534. $fields = array();
  535. foreach ($definition['fields'] as $field => $fieldinfo) {
  536. if (!empty($fieldinfo['length'])) {
  537. $fields[] = $db->quoteIdentifier($field, true) . '(' . $fieldinfo['length'] . ')';
  538. } else {
  539. $fields[] = $db->quoteIdentifier($field, true);
  540. }
  541. }
  542. $query .= ' ('. implode(', ', $fields) . ')';
  543. return $db->exec($query);
  544. }
  545. // }}}
  546. // {{{ dropIndex()
  547. /**
  548. * drop existing index
  549. *
  550. * @param string $table name of table that should be used in method
  551. * @param string $name name of the index to be dropped
  552. * @return mixed MDB2_OK on success, a MDB2 error on failure
  553. * @access public
  554. */
  555. function dropIndex($table, $name)
  556. {
  557. $db =& $this->getDBInstance();
  558. if (PEAR::isError($db)) {
  559. return $db;
  560. }
  561. $table = $db->quoteIdentifier($table, true);
  562. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  563. return $db->exec("DROP INDEX $name ON $table");
  564. }
  565. // }}}
  566. // {{{ listTableIndexes()
  567. /**
  568. * list all indexes in a table
  569. *
  570. * @param string $table name of table that should be used in method
  571. * @return mixed data array on success, a MDB2 error on failure
  572. * @access public
  573. */
  574. function listTableIndexes($table)
  575. {
  576. $db =& $this->getDBInstance();
  577. if (PEAR::isError($db)) {
  578. return $db;
  579. }
  580. $key_name = 'Key_name';
  581. $non_unique = 'Non_unique';
  582. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  583. if ($db->options['field_case'] == CASE_LOWER) {
  584. $key_name = strtolower($key_name);
  585. $non_unique = strtolower($non_unique);
  586. } else {
  587. $key_name = strtoupper($key_name);
  588. $non_unique = strtoupper($non_unique);
  589. }
  590. }
  591. $table = $db->quoteIdentifier($table, true);
  592. $query = "SHOW INDEX FROM $table";
  593. $indexes = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
  594. if (PEAR::isError($indexes)) {
  595. return $indexes;
  596. }
  597. $result = array();
  598. foreach ($indexes as $index_data) {
  599. if ($index_data[$non_unique] && ($index = $this->_fixIndexName($index_data[$key_name]))) {
  600. $result[$index] = true;
  601. }
  602. }
  603. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  604. $result = array_change_key_case($result, $db->options['field_case']);
  605. }
  606. return array_keys($result);
  607. }
  608. // }}}
  609. // {{{ createConstraint()
  610. /**
  611. * create a constraint on a table
  612. *
  613. * @param string $table name of the table on which the constraint is to be created
  614. * @param string $name name of the constraint to be created
  615. * @param array $definition associative array that defines properties of the constraint to be created.
  616. * Currently, only one property named FIELDS is supported. This property
  617. * is also an associative with the names of the constraint fields as array
  618. * constraints. Each entry of this array is set to another type of associative
  619. * array that specifies properties of the constraint that are specific to
  620. * each field.
  621. *
  622. * Example
  623. * array(
  624. * 'fields' => array(
  625. * 'user_name' => array(),
  626. * 'last_login' => array()
  627. * )
  628. * )
  629. * @return mixed MDB2_OK on success, a MDB2 error on failure
  630. * @access public
  631. */
  632. function createConstraint($table, $name, $definition)
  633. {
  634. $db =& $this->getDBInstance();
  635. if (PEAR::isError($db)) {
  636. return $db;
  637. }
  638. $type = '';
  639. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  640. if (!empty($definition['primary'])) {
  641. $type = 'PRIMARY';
  642. $name = 'KEY';
  643. } elseif (!empty($definition['unique'])) {
  644. $type = 'UNIQUE';
  645. }
  646. $table = $db->quoteIdentifier($table, true);
  647. $query = "ALTER TABLE $table ADD $type $name";
  648. $fields = array();
  649. foreach (array_keys($definition['fields']) as $field) {
  650. $fields[] = $db->quoteIdentifier($field, true);
  651. }
  652. $query .= ' ('. implode(', ', $fields) . ')';
  653. return $db->exec($query);
  654. }
  655. // }}}
  656. // {{{ dropConstraint()
  657. /**
  658. * drop existing constraint
  659. *
  660. * @param string $table name of table that should be used in method
  661. * @param string $name name of the constraint to be dropped
  662. * @param string $primary hint if the constraint is primary
  663. * @return mixed MDB2_OK on success, a MDB2 error on failure
  664. * @access public
  665. */
  666. function dropConstraint($table, $name, $primary = false)
  667. {
  668. $db =& $this->getDBInstance();
  669. if (PEAR::isError($db)) {
  670. return $db;
  671. }
  672. $table = $db->quoteIdentifier($table, true);
  673. if ($primary || strtolower($name) == 'primary') {
  674. $query = "ALTER TABLE $table DROP PRIMARY KEY";
  675. } else {
  676. $name = $db->quoteIdentifier($db->getIndexName($name), true);
  677. $query = "ALTER TABLE $table DROP INDEX $name";
  678. }
  679. return $db->exec($query);
  680. }
  681. // }}}
  682. // {{{ listTableConstraints()
  683. /**
  684. * list all constraints in a table
  685. *
  686. * @param string $table name of table that should be used in method
  687. * @return mixed data array on success, a MDB2 error on failure
  688. * @access public
  689. */
  690. function listTableConstraints($table)
  691. {
  692. $db =& $this->getDBInstance();
  693. if (PEAR::isError($db)) {
  694. return $db;
  695. }
  696. $key_name = 'Key_name';
  697. $non_unique = 'Non_unique';
  698. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  699. if ($db->options['field_case'] == CASE_LOWER) {
  700. $key_name = strtolower($key_name);
  701. $non_unique = strtolower($non_unique);
  702. } else {
  703. $key_name = strtoupper($key_name);
  704. $non_unique = strtoupper($non_unique);
  705. }
  706. }
  707. $table = $db->quoteIdentifier($table, true);
  708. $query = "SHOW INDEX FROM $table";
  709. $indexes = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
  710. if (PEAR::isError($indexes)) {
  711. return $indexes;
  712. }
  713. $result = array();
  714. foreach ($indexes as $index_data) {
  715. if (!$index_data[$non_unique]) {
  716. if ($index_data[$key_name] !== 'PRIMARY') {
  717. $index = $this->_fixIndexName($index_data[$key_name]);
  718. } else {
  719. $index = 'PRIMARY';
  720. }
  721. if (!empty($index)) {
  722. $result[$index] = true;
  723. }
  724. }
  725. }
  726. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  727. $result = array_change_key_case($result, $db->options['field_case']);
  728. }
  729. return array_keys($result);
  730. }
  731. // }}}
  732. // {{{ createSequence()
  733. /**
  734. * create sequence
  735. *
  736. * @param string $seq_name name of the sequence to be created
  737. * @param string $start start value of the sequence; default is 1
  738. * @return mixed MDB2_OK on success, a MDB2 error on failure
  739. * @access public
  740. */
  741. function createSequence($seq_name, $start = 1)
  742. {
  743. $db =& $this->getDBInstance();
  744. if (PEAR::isError($db)) {
  745. return $db;
  746. }
  747. $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
  748. $seqcol_name = $db->quoteIdentifier($db->options['seqcol_name'], true);
  749. $query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
  750. $query.= strlen($db->options['default_table_type']) ? ' TYPE='.$db->options['default_table_type'] : '';
  751. $res = $db->exec($query);
  752. if (PEAR::isError($res)) {
  753. return $res;
  754. }
  755. if ($start == 1) {
  756. return MDB2_OK;
  757. }
  758. $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')';
  759. $res = $db->exec($query);
  760. if (!PEAR::isError($res)) {
  761. return MDB2_OK;
  762. }
  763. // Handle error
  764. $result = $db->exec("DROP TABLE $sequence_name");
  765. if (PEAR::isError($result)) {
  766. return $db->raiseError($result, null, null,
  767. 'could not drop inconsistent sequence table', __FUNCTION__);
  768. }
  769. return $db->raiseError($res, null, null,
  770. 'could not create sequence table', __FUNCTION__);
  771. }
  772. // }}}
  773. // {{{ dropSequence()
  774. /**
  775. * drop existing sequence
  776. *
  777. * @param string $seq_name name of the sequence to be dropped
  778. * @return mixed MDB2_OK on success, a MDB2 error on failure
  779. * @access public
  780. */
  781. function dropSequence($seq_name)
  782. {
  783. $db =& $this->getDBInstance();
  784. if (PEAR::isError($db)) {
  785. return $db;
  786. }
  787. $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
  788. return $db->exec("DROP TABLE $sequence_name");
  789. }
  790. // }}}
  791. // {{{ listSequences()
  792. /**
  793. * list all sequences in the current database
  794. *
  795. * @param string database, the current is default
  796. * @return mixed data array on success, a MDB2 error on failure
  797. * @access public
  798. */
  799. function listSequences($database = null)
  800. {
  801. $db =& $this->getDBInstance();
  802. if (PEAR::isError($db)) {
  803. return $db;
  804. }
  805. $query = "SHOW TABLES";
  806. if (!is_null($database)) {
  807. $query .= " FROM $database";
  808. }
  809. $table_names = $db->queryCol($query);
  810. if (PEAR::isError($table_names)) {
  811. return $table_names;
  812. }
  813. $result = array();
  814. foreach ($table_names as $table_name) {
  815. if ($sqn = $this->_fixSequenceName($table_name, true)) {
  816. $result[] = $sqn;
  817. }
  818. }
  819. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  820. $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  821. }
  822. return $result;
  823. }
  824. // }}}
  825. }
  826. ?>