001: /*
002: * $Id: CreateIndexCommand.java,v 1.16 2005/12/20 18:32:28 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2003 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine.commands;
042:
043: import java.util.ArrayList;
044: import java.util.List;
045:
046: import org.axiondb.AxionException;
047: import org.axiondb.Column;
048: import org.axiondb.ColumnIdentifier;
049: import org.axiondb.Database;
050: import org.axiondb.Index;
051: import org.axiondb.IndexFactory;
052: import org.axiondb.Table;
053: import org.axiondb.TableIdentifier;
054:
055: /**
056: * A <code>CREATE [UNIQUE] [<i>TYPE</i>] INDEX</code> command.
057: *
058: * @version $Revision: 1.16 $ $Date: 2005/12/20 18:32:28 $
059: * @author Rodney Waldhoff
060: * @author Ahimanikya Satapathy
061: */
062: public class CreateIndexCommand extends CreateCommand {
063: public CreateIndexCommand() {
064: }
065:
066: public TableIdentifier getTable() {
067: return _table;
068: }
069:
070: public void setTable(TableIdentifier table) {
071: _table = table;
072: }
073:
074: public void setTable(String tableName) {
075: _table = new TableIdentifier(tableName);
076: }
077:
078: public void addColumn(String name) {
079: _columns.add(new ColumnIdentifier(name));
080: }
081:
082: public void addColumn(ColumnIdentifier col) {
083: _columns.add(col);
084: }
085:
086: public ColumnIdentifier getColumn(int i) {
087: return (ColumnIdentifier) (_columns.get(i));
088: }
089:
090: public int getColumnCount() {
091: return _columns.size();
092: }
093:
094: public void setUnique(boolean unique) {
095: _unique = unique;
096: }
097:
098: public boolean isUnique() {
099: return _unique;
100: }
101:
102: public void setType(String type) {
103: _type = type;
104: }
105:
106: public String getType() {
107: return _type;
108: }
109:
110: public boolean execute(Database db) throws AxionException {
111: assertNotReadOnly(db);
112: if (!isIfNotExists() || !db.hasIndex(getObjectName())) {
113: if (getColumnCount() > 1) {
114: throw new AxionException(
115: "Multi-column indices are not supported yet.");
116: }
117:
118: Table table = db.getTable(getTable());
119: if (null == table) {
120: throw new AxionException("Table " + getTable()
121: + " not found.");
122: }
123:
124: if (getObjectName().startsWith("SYS")) {
125: throw new AxionException(
126: "Cannot create user index with a name starting with SYS - "
127: + "reserved for internally-generated indexes.");
128: }
129:
130: String columnName = getColumn(0).getName();
131: Column column = table.getColumn(columnName);
132:
133: Index index = null;
134: IndexFactory factory = db
135: .getIndexFactory(null == _type ? "default" : _type);
136: if (null == factory) {
137: throw new AxionException("Index type \"" + _type
138: + "\" not recognized.");
139: }
140: index = factory.makeNewInstance(getObjectName(), column,
141: _unique, db.getDBDirectory() == null);
142:
143: db.addIndex(index, table, true);
144: }
145: return false;
146: }
147:
148: private boolean _unique = false;
149: private String _type;
150: private TableIdentifier _table;
151: private List _columns = new ArrayList(2);
152: }
|