1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.command.ddl; |
7 | |
8 | import org.h2.api.ErrorCode; |
9 | import org.h2.command.CommandInterface; |
10 | import org.h2.constraint.Constraint; |
11 | import org.h2.engine.Right; |
12 | import org.h2.engine.Session; |
13 | import org.h2.message.DbException; |
14 | import org.h2.schema.Schema; |
15 | |
16 | /** |
17 | * This class represents the statement |
18 | * ALTER TABLE DROP CONSTRAINT |
19 | */ |
20 | public class AlterTableDropConstraint extends SchemaCommand { |
21 | |
22 | private String constraintName; |
23 | private final boolean ifExists; |
24 | |
25 | public AlterTableDropConstraint(Session session, Schema schema, |
26 | boolean ifExists) { |
27 | super(session, schema); |
28 | this.ifExists = ifExists; |
29 | } |
30 | |
31 | public void setConstraintName(String string) { |
32 | constraintName = string; |
33 | } |
34 | |
35 | @Override |
36 | public int update() { |
37 | session.commit(true); |
38 | Constraint constraint = getSchema().findConstraint(session, constraintName); |
39 | if (constraint == null) { |
40 | if (!ifExists) { |
41 | throw DbException.get(ErrorCode.CONSTRAINT_NOT_FOUND_1, constraintName); |
42 | } |
43 | } else { |
44 | session.getUser().checkRight(constraint.getTable(), Right.ALL); |
45 | session.getUser().checkRight(constraint.getRefTable(), Right.ALL); |
46 | session.getDatabase().removeSchemaObject(session, constraint); |
47 | } |
48 | return 0; |
49 | } |
50 | |
51 | @Override |
52 | public int getType() { |
53 | return CommandInterface.ALTER_TABLE_DROP_CONSTRAINT; |
54 | } |
55 | |
56 | } |