File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\DropView.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 org.h2.command.CommandInterface;
10 import org.h2.constant.ErrorCode;
11 import org.h2.constraint.ConstraintReferential;
12 import org.h2.engine.DbObject;
13 import org.h2.engine.Right;
14 import org.h2.engine.Session;
15 import org.h2.message.DbException;
16 import org.h2.schema.Schema;
17 import org.h2.table.Table;
18 import org.h2.table.TableView;
19
20 /**
21 * This class represents the statement
22 * DROP VIEW
23 */
24 public class DropView extends SchemaCommand {
25
26 private String viewName;
27 private boolean ifExists;
28 private int dropAction;
29
30 public DropView(Session session, Schema schema) {
31 super(session, schema);
32 dropAction = session.getDatabase().getSettings().dropRestrict ?
33 ConstraintReferential.RESTRICT :
34 ConstraintReferential.CASCADE;
35 }
36
37 public void setIfExists(boolean b) {
38 ifExists = b;
39 }
40
41 public void setDropAction(int dropAction) {
42 this.dropAction = dropAction;
43 }
44
45 public void setViewName(String viewName) {
46 this.viewName = viewName;
47 }
48
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 public int getType() {
77 return CommandInterface.DROP_VIEW;
78 }
79
80 }
81