3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Index, constraint and alter methods for DB_Table usage with
9 * The code in this class was adopted from the MDB2 PEAR package.
11 * PHP versions 4 and 5
15 * Copyright (c) 1997-2007, Lukas Smith <smith@pooteeweet.org>
16 * Paul M. Jones <pmjones@php.net>
17 * David C. Morse <morse@php.net>
18 * Mark Wiesemann <wiesemann@php.net>
19 * All rights reserved.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
25 * * Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * * Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * * The names of the authors may not be used to endorse or promote products
31 * derived from this software without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
34 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
35 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
36 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
37 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
38 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
39 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
40 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
41 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
42 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
43 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 * @author Lukas Smith <smith@pooteeweet.org>
48 * @author Mark Wiesemann <wiesemann@php.net>
49 * @license http://opensource.org/licenses/bsd-license.php New BSD License
50 * @version CVS: $Id: mysql.php,v 1.5 2007/12/13 16:52:15 wiesemann Exp $
51 * @link http://pear.php.net/package/DB_Table
55 * require DB_Table class
57 require_once 'DB/Table.php';
60 * Index, constraint and alter methods for DB_Table usage with
61 * PEAR::DB as backend.
63 * The code in this class was adopted from the MDB2 PEAR package.
67 * @author Lukas Smith <smith@pooteeweet.org>
68 * @author Mark Wiesemann <wiesemann@php.net>
69 * @version Release: 1.5.6
70 * @link http://pear.php.net/package/DB_Table
73 class DB_Table_Manager_mysql
{
77 * The PEAR DB object that connects to the database.
89 * list all indexes in a table
91 * @param string $table name of table that should be used in method
92 * @return mixed data array on success, a PEAR error on failure
95 function listTableIndexes($table)
97 $key_name = 'Key_name';
98 $non_unique = 'Non_unique';
100 $query = "SHOW INDEX FROM $table";
101 $indexes = $this->_db
->getAll($query, null, DB_FETCHMODE_ASSOC
);
102 if (PEAR
::isError($indexes)) {
107 foreach ($indexes as $index_data) {
108 if ($index_data[$non_unique]) {
109 $result[$index_data[$key_name]] = true;
112 $result = array_change_key_case($result, CASE_LOWER
);
114 return array_keys($result);
119 * list all constraints in a table
121 * @param string $table name of table that should be used in method
122 * @return mixed data array on success, a PEAR error on failure
125 function listTableConstraints($table)
127 $key_name = 'Key_name';
128 $non_unique = 'Non_unique';
130 $query = "SHOW INDEX FROM $table";
131 $indexes = $this->_db
->getAll($query, null, DB_FETCHMODE_ASSOC
);
132 if (PEAR
::isError($indexes)) {
137 foreach ($indexes as $index_data) {
138 if (!$index_data[$non_unique]) {
139 if ($index_data[$key_name] !== 'PRIMARY') {
140 $index = $index_data[$key_name];
144 $result[$index] = true;
147 $result = array_change_key_case($result, CASE_LOWER
);
149 return array_keys($result);
154 * get the structure of an index into an array
156 * @param string $table name of table that should be used in method
157 * @param string $index_name name of index that should be used in method
158 * @return mixed data array on success, a PEAR error on failure
161 function getTableIndexDefinition($table, $index_name)
163 $result = $this->_db
->query("SHOW INDEX FROM $table");
164 if (PEAR
::isError($result)) {
168 $definition = array();
169 while (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC
))) {
170 $row = array_change_key_case($row, CASE_LOWER
);
171 $key_name = $row['key_name'];
172 $key_name = strtolower($key_name);
174 if ($index_name == $key_name) {
175 $column_name = $row['column_name'];
176 $column_name = strtolower($column_name);
177 $definition['fields'][$column_name] = array();
178 if (array_key_exists('collation', $row)) {
179 $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
180 ?
'ascending' : 'descending');
192 * get the structure of a constraint into an array
194 * @param string $table name of table that should be used in method
195 * @param string $index_name name of index that should be used in method
196 * @return mixed data array on success, a PEAR error on failure
199 function getTableConstraintDefinition($table, $index_name)
201 $result = $this->_db
->query("SHOW INDEX FROM $table");
202 if (PEAR
::isError($result)) {
206 $definition = array();
207 while (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC
))) {
208 $row = array_change_key_case($row, CASE_LOWER
);
209 $key_name = $row['key_name'];
210 $key_name = strtolower($key_name);
212 if ($index_name == $key_name) {
213 if ($row['key_name'] == 'PRIMARY') {
214 $definition['primary'] = true;
216 $definition['unique'] = true;
218 $column_name = $row['column_name'];
219 $column_name = strtolower($column_name);
220 $definition['fields'][$column_name] = array();
221 if (array_key_exists('collation', $row)) {
222 $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
223 ?
'ascending' : 'descending');
235 * drop existing index
237 * @param string $table name of table that should be used in method
238 * @param string $name name of the index to be dropped
239 * @return mixed DB_OK on success, a PEAR error on failure
242 function dropIndex($table, $name)
244 $table = $this->_db
->quoteIdentifier($table);
245 $name = $this->_db
->quoteIdentifier($name);
246 return $this->_db
->query("DROP INDEX $name ON $table");
251 * drop existing constraint
253 * @param string $table name of table that should be used in method
254 * @param string $name name of the constraint to be dropped
255 * @return mixed DB_OK on success, a PEAR error on failure
258 function dropConstraint($table, $name)
260 $table = $this->_db
->quoteIdentifier($table);
261 if (strtolower($name) == 'primary') {
262 $query = "ALTER TABLE $table DROP PRIMARY KEY";
264 $name = $this->_db
->quoteIdentifier($name);
265 $query = "ALTER TABLE $table DROP INDEX $name";
267 return $this->_db
->query($query);
272 * alter an existing table
274 * @param string $name name of the table that is intended to be changed.
275 * @param array $changes associative array that contains the details of each type
276 * of change that is intended to be performed. The types of
277 * changes that are currently supported are defined as follows:
281 * New name for the table.
285 * Associative array with the names of fields to be added as
286 * indexes of the array. The value of each entry of the array
287 * should be set to another associative array with the properties
288 * of the fields to be added. The properties of the fields should
289 * be the same as defined by the Metabase parser.
294 * Associative array with the names of fields to be removed as indexes
295 * of the array. Currently the values assigned to each entry are ignored.
296 * An empty array should be used for future compatibility.
300 * Associative array with the names of fields to be renamed as indexes
301 * of the array. The value of each entry of the array should be set to
302 * another associative array with the entry named name with the new
303 * field name and the entry named Declaration that is expected to contain
304 * the portion of the field declaration already in DBMS specific SQL code
305 * as it is used in the CREATE TABLE statement.
309 * Associative array with the names of the fields to be changed as indexes
310 * of the array. Keep in mind that if it is intended to change either the
311 * name of a field and any other properties, the change array entries
312 * should have the new names of the fields as array indexes.
314 * The value of each entry of the array should be set to another associative
315 * array with the properties of the fields to that are meant to be changed as
316 * array entries. These entries should be assigned to the new values of the
317 * respective properties. The properties of the fields should be the same
318 * as defined by the Metabase parser.
322 * 'name' => 'userlist',
325 * 'type' => 'integer',
330 * 'file_limit' => array(),
331 * 'time_limit' => array()
336 * 'definition' => array(
344 * 'name' => 'gender',
345 * 'definition' => array(
354 * @param boolean $check (ignored in DB_Table)
357 * @return mixed DB_OK on success, a PEAR error on failure
359 function alterTable($name, $changes, $check)
361 foreach ($changes as $change_name => $change) {
362 switch ($change_name) {
370 return DB_Table
::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS
);
375 if (array_key_exists('name', $changes)) {
376 $change_name = $this->_db
->quoteIdentifier($changes['name']);
377 $query .= 'RENAME TO ' . $change_name;
380 if (array_key_exists('add', $changes)) {
381 foreach ($changes['add'] as $field_name => $field) {
385 $query.= 'ADD ' . $field_name . ' ' . $field;
389 if (array_key_exists('remove', $changes)) {
390 foreach ($changes['remove'] as $field_name => $field) {
394 $field_name = $this->_db
->quoteIdentifier($field_name);
395 $query.= 'DROP ' . $field_name;
400 if (array_key_exists('rename', $changes)) {
401 foreach ($changes['rename'] as $field_name => $field) {
402 $rename[$field['name']] = $field_name;
406 if (array_key_exists('change', $changes)) {
407 foreach ($changes['change'] as $field_name => $field) {
411 if (isset($rename[$field_name])) {
412 $old_field_name = $rename[$field_name];
413 unset($rename[$field_name]);
415 $old_field_name = $field_name;
417 $old_field_name = $this->_db
->quoteIdentifier($old_field_name);
418 $query.= "CHANGE $old_field_name " . $field_name . ' ' . $field['definition'];
422 if (!empty($rename)) {
423 foreach ($rename as $rename_name => $renamed_field) {
427 $field = $changes['rename'][$renamed_field];
428 $renamed_field = $this->_db
->quoteIdentifier($renamed_field);
429 $query.= 'CHANGE ' . $renamed_field . ' ' . $field['name'] . ' ' . $renamed_field['definition'];
437 $name = $this->_db
->quoteIdentifier($name);
438 return $this->_db
->query("ALTER TABLE $name $query");