001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.util.Vector;
042:
043: import java.io.IOException;
044:
045: import java.sql.SQLException;
046:
047: import com.quadcap.util.Debug;
048:
049: /**
050: * Implementation of the SQL <B>CREATE INDEX</B> statement.
051: *
052: * @author Stan Bailes
053: */
054:
055: public class StmtCreateIndex implements Stmt {
056: String indexName;
057: String tableName;
058: Vector columns;
059: boolean unique;
060: boolean text;
061:
062: public StmtCreateIndex() {
063: }
064:
065: public StmtCreateIndex(String indexName, String tableName,
066: Vector columns, boolean unique, boolean text) {
067: this .indexName = indexName;
068: this .tableName = tableName;
069: this .columns = columns;
070: this .unique = unique;
071: this .text = text;
072: }
073:
074: public void execute(Session session) throws IOException,
075: SQLException {
076: session.getTableWriteLock("#Schema");
077: session.getTableWriteLock(tableName);
078: Database db = session.getDatabase();
079: Relation r = db.getRelation(tableName);
080: if (r == null) {
081: throw new SQLException("No such table: " + tableName,
082: "42000");
083: }
084: if (!(r instanceof Table)) {
085: throw new SQLException("Not a base table: " + tableName,
086: "42000");
087: }
088: if (db.getTableForIndex(indexName) != null) {
089: throw new SQLException("Index: " + indexName + " exists",
090: "42000");
091: }
092: Table t = (Table) r;
093: Constraint c = null;
094: if (unique) {
095: c = new UniqueConstraint(indexName, columns);
096: //} else if (text) {
097: // c = new TextIndexConstraint(indexName, columns);
098: } else {
099: c = new NonUniqueIndexConstraint(indexName, columns);
100: }
101: c.setGlobal(true);
102: c.setTable(t);
103: t.nameConstraint(c);
104: session.doStep(new AddConstraint(session, t, c, true));
105: }
106: }
|