01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.command.ddl;
07:
08: import java.sql.SQLException;
09: import org.h2.constant.ErrorCode;
10: import org.h2.constraint.Constraint;
11: import org.h2.engine.Database;
12: import org.h2.engine.Right;
13: import org.h2.engine.Session;
14: import org.h2.index.Index;
15: import org.h2.message.Message;
16: import org.h2.schema.Schema;
17: import org.h2.table.Table;
18: import org.h2.util.ObjectArray;
19:
20: /**
21: * This class represents the statement
22: * DROP INDEX
23: */
24: public class DropIndex extends SchemaCommand {
25:
26: private String indexName;
27: private boolean ifExists;
28:
29: public DropIndex(Session session, Schema schema) {
30: super (session, schema);
31: }
32:
33: public void setIfExists(boolean b) {
34: ifExists = b;
35: }
36:
37: public void setIndexName(String indexName) {
38: this .indexName = indexName;
39: }
40:
41: public int update() throws SQLException {
42: session.commit(true);
43: Database db = session.getDatabase();
44: Index index = getSchema().findIndex(indexName);
45: if (index == null) {
46: if (!ifExists) {
47: throw Message.getSQLException(
48: ErrorCode.INDEX_NOT_FOUND_1, indexName);
49: }
50: } else {
51: Table table = index.getTable();
52: session.getUser().checkRight(index.getTable(), Right.ALL);
53: Constraint pkConstraint = null;
54: ObjectArray constraints = table.getConstraints();
55: for (int i = 0; constraints != null
56: && i < constraints.size(); i++) {
57: Constraint cons = (Constraint) constraints.get(i);
58: if (cons.usesIndex(index)) {
59: // can drop primary key index (for compatibility)
60: if (Constraint.PRIMARY_KEY.equals(cons
61: .getConstraintType())) {
62: pkConstraint = cons;
63: } else {
64: throw Message
65: .getSQLException(
66: ErrorCode.INDEX_BELONGS_TO_CONSTRAINT_1,
67: indexName);
68: }
69: }
70: }
71: index.getTable().setModified();
72: if (pkConstraint != null) {
73: db.removeSchemaObject(session, pkConstraint);
74: } else {
75: db.removeSchemaObject(session, index);
76: }
77: }
78: return 0;
79: }
80:
81: }
|