KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > ddl > DropIndex


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.command.ddl;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.constraint.Constraint;
10 import org.h2.engine.Database;
11 import org.h2.engine.Right;
12 import org.h2.engine.Session;
13 import org.h2.index.Index;
14 import org.h2.message.Message;
15 import org.h2.schema.Schema;
16 import org.h2.table.Table;
17 import org.h2.util.ObjectArray;
18
19 /**
20  * @author Thomas
21  */

22 public class DropIndex extends SchemaCommand {
23
24     private String JavaDoc indexName;
25     private boolean ifExists;
26
27     public DropIndex(Session session, Schema schema) {
28         super(session, schema);
29     }
30
31     public void setIfExists(boolean b) {
32         ifExists = b;
33     }
34
35     public void setIndexName(String JavaDoc indexName) {
36         this.indexName = indexName;
37     }
38
39     public int update() throws SQLException JavaDoc {
40         session.commit();
41         Database db = session.getDatabase();
42         Index index = getSchema().findIndex(indexName);
43         if(index == null) {
44             if(!ifExists) {
45                 throw Message.getSQLException(Message.INDEX_NOT_FOUND_1, indexName);
46             }
47         } else {
48             Table table = index.getTable();
49             ObjectArray constraints = table.getConstraints();
50             for(int i=0; constraints != null && i<constraints.size(); i++) {
51                 Constraint cons = (Constraint) constraints.get(i);
52                 if(cons.usesIndex(index)) {
53                     throw Message.getSQLException(Message.INDEX_BELONGS_TO_CONSTRAINT_1, indexName);
54                 }
55             }
56             session.getUser().checkRight(index.getTable(), Right.ALL);
57             index.getTable().setModified();
58             db.removeSchemaObject(session, index);
59         }
60         return 0;
61     }
62
63 }
64
Popular Tags