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 java.util.ArrayList; |
9 | |
10 | import org.h2.api.ErrorCode; |
11 | import org.h2.command.CommandInterface; |
12 | import org.h2.command.dml.Query; |
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 | @Override |
74 | public int update() { |
75 | session.commit(true); |
76 | session.getUser().checkAdmin(); |
77 | Database db = session.getDatabase(); |
78 | TableView view = null; |
79 | Table old = getSchema().findTableOrView(session, viewName); |
80 | if (old != null) { |
81 | if (ifNotExists) { |
82 | return 0; |
83 | } |
84 | if (!orReplace || !Table.VIEW.equals(old.getTableType())) { |
85 | throw DbException.get(ErrorCode.VIEW_ALREADY_EXISTS_1, viewName); |
86 | } |
87 | view = (TableView) old; |
88 | } |
89 | int id = getObjectId(); |
90 | String querySQL; |
91 | if (select == null) { |
92 | querySQL = selectSQL; |
93 | } else { |
94 | ArrayList<Parameter> params = select.getParameters(); |
95 | if (params != null && params.size() > 0) { |
96 | throw DbException.getUnsupportedException("parameters in views"); |
97 | } |
98 | querySQL = select.getPlanSQL(); |
99 | } |
100 | // The view creates a Prepared command object, which belongs to a |
101 | // session, so we pass the system session down. |
102 | Session sysSession = db.getSystemSession(); |
103 | try { |
104 | if (view == null) { |
105 | Schema schema = session.getDatabase().getSchema(session.getCurrentSchemaName()); |
106 | sysSession.setCurrentSchema(schema); |
107 | view = new TableView(getSchema(), id, viewName, querySQL, null, |
108 | columnNames, sysSession, false); |
109 | } else { |
110 | view.replace(querySQL, columnNames, sysSession, false, force); |
111 | view.setModified(); |
112 | } |
113 | } finally { |
114 | sysSession.setCurrentSchema(db.getSchema(Constants.SCHEMA_MAIN)); |
115 | } |
116 | if (comment != null) { |
117 | view.setComment(comment); |
118 | } |
119 | if (old == null) { |
120 | db.addSchemaObject(session, view); |
121 | } else { |
122 | db.updateMeta(session, view); |
123 | } |
124 | return 0; |
125 | } |
126 | |
127 | @Override |
128 | public int getType() { |
129 | return CommandInterface.CREATE_VIEW; |
130 | } |
131 | |
132 | } |