File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\command\ddl\CreateView.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.command.dml.Query;
12 import org.h2.constant.ErrorCode;
13 import org.h2.engine.Constants;
14 import org.h2.engine.Database;
15 import org.h2.engine.Session;
16 import org.h2.expression.Parameter;
17 import org.h2.message.DbException;
18 import org.h2.schema.Schema;
19 import org.h2.table.Table;
20 import org.h2.table.TableView;
21
22 /**
23 * This class represents the statement
24 * CREATE VIEW
25 */
26 public class CreateView extends SchemaCommand {
27
28 private Query select;
29 private String viewName;
30 private boolean ifNotExists;
31 private String selectSQL;
32 private String[] columnNames;
33 private String comment;
34 private boolean orReplace;
35 private boolean force;
36
37 public CreateView(Session session, Schema schema) {
38 super(session, schema);
39 }
40
41 public void setViewName(String name) {
42 viewName = name;
43 }
44
45 public void setSelect(Query select) {
46 this.select = select;
47 }
48
49 public void setIfNotExists(boolean ifNotExists) {
50 this.ifNotExists = ifNotExists;
51 }
52
53 public void setSelectSQL(String selectSQL) {
54 this.selectSQL = selectSQL;
55 }
56
57 public void setColumnNames(String[] cols) {
58 this.columnNames = cols;
59 }
60
61 public void setComment(String comment) {
62 this.comment = comment;
63 }
64
65 public void setOrReplace(boolean orReplace) {
66 this.orReplace = orReplace;
67 }
68
69 public void setForce(boolean force) {
70 this.force = force;
71 }
72
73 public int update() {
74 session.commit(true);
75 Database db = session.getDatabase();
76 TableView view = null;
77 Table old = getSchema().findTableOrView(session, viewName);
78 if (old != null) {
79 if (ifNotExists) {
80 return 0;
81 }
82 if (!orReplace || !Table.VIEW.equals(old.getTableType())) {
83 throw DbException.get(ErrorCode.VIEW_ALREADY_EXISTS_1, viewName);
84 }
85 view = (TableView) old;
86 }
87 int id = getObjectId();
88 String querySQL;
89 if (select == null) {
90 querySQL = selectSQL;
91 } else {
92 ArrayList<Parameter> params = select.getParameters();
93 if (params != null && params.size() > 0) {
94 throw DbException.get(ErrorCode.FEATURE_NOT_SUPPORTED_1, "parameters in views");
95 }
96 querySQL = select.getPlanSQL();
97 }
98 Session sysSession = db.getSystemSession();
99 try {
100 if (view == null) {
101 Schema schema = session.getDatabase().getSchema(session.getCurrentSchemaName());
102 sysSession.setCurrentSchema(schema);
103 view = new TableView(getSchema(), id, viewName, querySQL, null, columnNames, sysSession, false);
104 } else {
105 view.replace(querySQL, columnNames, sysSession, false, force);
106 }
107 } finally {
108 sysSession.setCurrentSchema(db.getSchema(Constants.SCHEMA_MAIN));
109 }
110 if (comment != null) {
111 view.setComment(comment);
112 }
113 if (old == null) {
114 db.addSchemaObject(session, view);
115 } else {
116 db.update(session, view);
117 }
118 return 0;
119 }
120
121 public int getType() {
122 return CommandInterface.CREATE_VIEW;
123 }
124
125 }
126