File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\DropIndex.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.command.ddl;
8
9 import java.util.ArrayList;
10 import org.h2.command.CommandInterface;
11 import org.h2.constant.ErrorCode;
12 import org.h2.constraint.Constraint;
13 import org.h2.engine.Database;
14 import org.h2.engine.Right;
15 import org.h2.engine.Session;
16 import org.h2.index.Index;
17 import org.h2.message.DbException;
18 import org.h2.schema.Schema;
19 import org.h2.table.Table;
20
21 /**
22 * This class represents the statement
23 * DROP INDEX
24 */
25 public class DropIndex extends SchemaCommand {
26
27 private String indexName;
28 private boolean ifExists;
29
30 public DropIndex(Session session, Schema schema) {
31 super(session, schema);
32 }
33
34 public void setIfExists(boolean b) {
35 ifExists = b;
36 }
37
38 public void setIndexName(String indexName) {
39 this.indexName = indexName;
40 }
41
42 public int update() {
43 session.commit(true);
44 Database db = session.getDatabase();
45 Index index = getSchema().findIndex(session, indexName);
46 if (index == null) {
47 if (!ifExists) {
48 throw DbException.get(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 ArrayList<Constraint> constraints = table.getConstraints();
55 for (int i = 0; constraints != null && i < constraints.size(); i++) {
56 Constraint cons = constraints.get(i);
57 if (cons.usesIndex(index)) {
58 // can drop primary key index (for compatibility)
59 if (Constraint.PRIMARY_KEY.equals(cons.getConstraintType())) {
60 pkConstraint = cons;
61 } else {
62 throw DbException.get(ErrorCode.INDEX_BELONGS_TO_CONSTRAINT_1, indexName);
63 }
64 }
65 }
66 index.getTable().setModified();
67 if (pkConstraint != null) {
68 db.removeSchemaObject(session, pkConstraint);
69 } else {
70 db.removeSchemaObject(session, index);
71 }
72 }
73 return 0;
74 }
75
76 public int getType() {
77 return CommandInterface.DROP_INDEX;
78 }
79
80 }
81