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: oci8.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_oci8
{
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 $table = strtoupper($table);
98 $query = "SELECT index_name name FROM user_indexes WHERE table_name='$table'";
99 $indexes = $this->_db
->getCol($query);
100 if (PEAR
::isError($indexes)) {
105 foreach ($indexes as $index) {
106 $result[$index] = true;
108 $result = array_change_key_case($result, CASE_LOWER
);
110 return array_keys($result);
115 * list all constraints in a table
117 * @param string $table name of table that should be used in method
118 * @return mixed data array on success, a PEAR error on failure
121 function listTableConstraints($table)
123 $table = strtoupper($table);
124 $query = "SELECT index_name name FROM user_constraints WHERE table_name='$table'";
125 $constraints = $this->_db
->getCol($query);
126 if (PEAR
::isError($constraints)) {
131 foreach ($constraints as $constraint) {
132 $result[$constraint] = true;
134 $result = array_change_key_case($result, CASE_LOWER
);
136 return array_keys($result);
141 * get the structure of an index into an array
143 * @param string $table name of table that should be used in method
144 * @param string $index_name name of index that should be used in method
145 * @return mixed data array on success, a PEAR error on failure
148 function getTableIndexDefinition($table, $index_name)
150 $index_name = $this->_db
->quoteSmart(strtoupper($index_name));
151 $table = $this->_db
->quoteSmart(strtoupper($table));
152 $query = "SELECT * FROM user_indexes where table_name = $table AND index_name = $index_name";
153 $row = $this->_db
->getRow($query, null, DB_FETCHMODE_ASSOC
);
154 if (PEAR
::isError($row)) {
158 $definition = array();
160 $row = array_change_key_case($row, CASE_LOWER
);
161 $key_name = $row['index_name'];
162 $key_name = strtolower($key_name);
163 $query = "SELECT * FROM user_ind_columns WHERE index_name = $index_name AND table_name = $table";
164 $result = $this->_db
->query($query);
165 if (PEAR
::isError($result)) {
168 while ($colrow = $result->fetchRow(DB_FETCHMODE_ASSOC
)) {
169 $column_name = $colrow['column_name'];
170 $column_name = strtolower($column_name);
171 $definition['fields'][$column_name] = array();
172 if (array_key_exists('descend', $colrow)) {
173 $definition['fields'][$column_name]['sorting'] =
174 ($colrow['descend'] == 'ASC' ?
'ascending' : 'descending');
185 * get the structure of a constraint into an array
187 * @param string $table name of table that should be used in method
188 * @param string $index_name name of index that should be used in method
189 * @return mixed data array on success, a PEAR error on failure
192 function getTableConstraintDefinition($table, $index_name)
194 $dsn = DB
::parseDSN($this->_db
->dsn
);
196 $dbName = $this->_db
->quoteSmart($dsn['database']);
197 $index_name = $this->_db
->quoteSmart($index_name);
198 $table = $this->_db
->quoteSmart($table);
199 $query = "SELECT * FROM all_contraints WHERE owner = $dbName AND table_name = $table AND index_name = $index_name";
200 $result = $this->_db
->query($query);
201 if (PEAR
::isError($result)) {
205 $definition = array();
206 while (is_array($row = $result->fetchRow(DB_FETCHMODE_ASSOC
))) {
207 $row = array_change_key_case($row, CASE_LOWER
);
208 $key_name = $row['constraint_name'];
209 $key_name = strtolower($key_name);
211 $definition['primary'] = $row['constraint_type'] == 'P';
212 $definition['unique'] = $row['constraint_type'] == 'U';
214 $query = 'SELECT * FROM all_cons_columns WHERE contraint_name = ';
215 $query.= $this->_db
->quoteSmart($key_name).' AND table_name = '.$table;
216 $colres = $this->_db
->query($query);
217 while ($colrow = $colres->fetchRow(DB_FETCHMODE_ASSOC
)) {
218 $column_name = $row['column_name'];
219 $column_name = strtolower($column_name);
220 $definition['fields'][$column_name] = array();
231 * drop existing index
233 * @param string $table name of table that should be used in method
234 * @param string $name name of the index to be dropped
235 * @return mixed DB_OK on success, a PEAR error on failure
238 function dropIndex($table, $name)
240 $name = $this->_db
->quoteIdentifier($name);
241 return $this->_db
->query("DROP INDEX $name");
246 * drop existing constraint
248 * @param string $table name of table that should be used in method
249 * @param string $name name of the constraint to be dropped
250 * @return mixed DB_OK on success, a PEAR error on failure
253 function dropConstraint($table, $name)
255 $table = $this->_db
->quoteIdentifier($table);
256 $name = $this->_db
->quoteIdentifier($name);
257 return $this->_db
->query("ALTER TABLE $table DROP CONSTRAINT $name");
262 * alter an existing table
264 * @param string $name name of the table that is intended to be changed.
265 * @param array $changes associative array that contains the details of each type
266 * of change that is intended to be performed. The types of
267 * changes that are currently supported are defined as follows:
271 * New name for the table.
275 * Associative array with the names of fields to be added as
276 * indexes of the array. The value of each entry of the array
277 * should be set to another associative array with the properties
278 * of the fields to be added. The properties of the fields should
279 * be the same as defined by the Metabase parser.
284 * Associative array with the names of fields to be removed as indexes
285 * of the array. Currently the values assigned to each entry are ignored.
286 * An empty array should be used for future compatibility.
290 * Associative array with the names of fields to be renamed as indexes
291 * of the array. The value of each entry of the array should be set to
292 * another associative array with the entry named name with the new
293 * field name and the entry named Declaration that is expected to contain
294 * the portion of the field declaration already in DBMS specific SQL code
295 * as it is used in the CREATE TABLE statement.
299 * Associative array with the names of the fields to be changed as indexes
300 * of the array. Keep in mind that if it is intended to change either the
301 * name of a field and any other properties, the change array entries
302 * should have the new names of the fields as array indexes.
304 * The value of each entry of the array should be set to another associative
305 * array with the properties of the fields to that are meant to be changed as
306 * array entries. These entries should be assigned to the new values of the
307 * respective properties. The properties of the fields should be the same
308 * as defined by the Metabase parser.
312 * 'name' => 'userlist',
315 * 'type' => 'integer',
320 * 'file_limit' => array(),
321 * 'time_limit' => array()
326 * 'definition' => array(
334 * 'name' => 'gender',
335 * 'definition' => array(
344 * @param boolean $check (ignored in DB_Table)
347 * @return mixed DB_OK on success, a PEAR error on failure
349 function alterTable($name, $changes, $check)
351 foreach ($changes as $change_name => $change) {
352 switch ($change_name) {
360 return DB_Table
::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS
);
364 $name = $this->_db
->quoteIdentifier($name);
366 if (array_key_exists('add', $changes)) {
368 foreach ($changes['add'] as $field_name => $field) {
369 $fields[] = $field_name . ' ' . $field;
371 $result = $this->_db
->query("ALTER TABLE $name ADD (". implode(', ', $fields).')');
372 if (PEAR
::isError($result)) {
377 if (array_key_exists('change', $changes)) {
379 foreach ($changes['change'] as $field_name => $field) {
380 $fields[] = $field_name. ' ' . $field['definition'];
382 $result = $this->_db
->query("ALTER TABLE $name MODIFY (". implode(', ', $fields).')');
383 if (PEAR
::isError($result)) {
388 if (array_key_exists('rename', $changes)) {
389 foreach ($changes['rename'] as $field_name => $field) {
390 $field_name = $this->_db
->quoteIdentifier($field_name);
391 $query = "ALTER TABLE $name RENAME COLUMN $field_name TO ".$this->_db
->quoteIdentifier($field['name']);
392 $result = $this->_db
->query($query);
393 if (PEAR
::isError($result)) {
399 if (array_key_exists('remove', $changes)) {
401 foreach ($changes['remove'] as $field_name => $field) {
402 $fields[] = $this->_db
->quoteIdentifier($field_name);
404 $result = $this->_db
->query("ALTER TABLE $name DROP COLUMN ". implode(', ', $fields));
405 if (PEAR
::isError($result)) {
410 if (array_key_exists('name', $changes)) {
411 $change_name = $this->_db
->quoteIdentifier($changes['name']);
412 $result = $this->_db
->query("ALTER TABLE $name RENAME TO ".$change_name);
413 if (PEAR
::isError($result)) {