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.ConstraintReferential; |
11 | import org.h2.engine.DbObject; |
12 | import org.h2.engine.Right; |
13 | import org.h2.engine.Session; |
14 | import org.h2.message.DbException; |
15 | import org.h2.schema.Schema; |
16 | import org.h2.table.Table; |
17 | import org.h2.table.TableView; |
18 | |
19 | /** |
20 | * This class represents the statement |
21 | * DROP VIEW |
22 | */ |
23 | public class DropView extends SchemaCommand { |
24 | |
25 | private String viewName; |
26 | private boolean ifExists; |
27 | private int dropAction; |
28 | |
29 | public DropView(Session session, Schema schema) { |
30 | super(session, schema); |
31 | dropAction = session.getDatabase().getSettings().dropRestrict ? |
32 | ConstraintReferential.RESTRICT : |
33 | ConstraintReferential.CASCADE; |
34 | } |
35 | |
36 | public void setIfExists(boolean b) { |
37 | ifExists = b; |
38 | } |
39 | |
40 | public void setDropAction(int dropAction) { |
41 | this.dropAction = dropAction; |
42 | } |
43 | |
44 | public void setViewName(String viewName) { |
45 | this.viewName = viewName; |
46 | } |
47 | |
48 | @Override |
49 | public int update() { |
50 | session.commit(true); |
51 | Table view = getSchema().findTableOrView(session, viewName); |
52 | if (view == null) { |
53 | if (!ifExists) { |
54 | throw DbException.get(ErrorCode.VIEW_NOT_FOUND_1, viewName); |
55 | } |
56 | } else { |
57 | if (!Table.VIEW.equals(view.getTableType())) { |
58 | throw DbException.get(ErrorCode.VIEW_NOT_FOUND_1, viewName); |
59 | } |
60 | session.getUser().checkRight(view, Right.ALL); |
61 | |
62 | if (dropAction == ConstraintReferential.RESTRICT) { |
63 | for (DbObject child : view.getChildren()) { |
64 | if (child instanceof TableView) { |
65 | throw DbException.get(ErrorCode.CANNOT_DROP_2, viewName, child.getName()); |
66 | } |
67 | } |
68 | } |
69 | |
70 | view.lock(session, true, true); |
71 | session.getDatabase().removeSchemaObject(session, view); |
72 | } |
73 | return 0; |
74 | } |
75 | |
76 | @Override |
77 | public int getType() { |
78 | return CommandInterface.DROP_VIEW; |
79 | } |
80 | |
81 | } |