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.engine.Database; |
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 | import org.h2.schema.TriggerObject; |
16 | import org.h2.table.Table; |
17 | |
18 | /** |
19 | * This class represents the statement |
20 | * DROP TRIGGER |
21 | */ |
22 | public class DropTrigger extends SchemaCommand { |
23 | |
24 | private String triggerName; |
25 | private boolean ifExists; |
26 | |
27 | public DropTrigger(Session session, Schema schema) { |
28 | super(session, schema); |
29 | } |
30 | |
31 | public void setIfExists(boolean b) { |
32 | ifExists = b; |
33 | } |
34 | |
35 | public void setTriggerName(String triggerName) { |
36 | this.triggerName = triggerName; |
37 | } |
38 | |
39 | @Override |
40 | public int update() { |
41 | session.commit(true); |
42 | Database db = session.getDatabase(); |
43 | TriggerObject trigger = getSchema().findTrigger(triggerName); |
44 | if (trigger == null) { |
45 | if (!ifExists) { |
46 | throw DbException.get(ErrorCode.TRIGGER_NOT_FOUND_1, triggerName); |
47 | } |
48 | } else { |
49 | Table table = trigger.getTable(); |
50 | session.getUser().checkRight(table, Right.ALL); |
51 | db.removeSchemaObject(session, trigger); |
52 | } |
53 | return 0; |
54 | } |
55 | |
56 | @Override |
57 | public int getType() { |
58 | return CommandInterface.DROP_TRIGGER; |
59 | } |
60 | |
61 | } |