01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.command.ddl;
07:
08: import java.sql.SQLException;
09:
10: import org.h2.command.dml.Query;
11: import org.h2.constant.ErrorCode;
12: import org.h2.engine.Database;
13: import org.h2.engine.Session;
14: import org.h2.message.Message;
15: import org.h2.schema.Schema;
16: import org.h2.table.TableView;
17:
18: /**
19: * This class represents the statement
20: * CREATE VIEW
21: */
22: public class CreateView extends SchemaCommand {
23:
24: private Query select;
25: private String viewName;
26: private boolean ifNotExists;
27: private String selectSQL;
28: private String[] columnNames;
29: private String comment;
30: private boolean recursive;
31:
32: public CreateView(Session session, Schema schema) {
33: super (session, schema);
34: }
35:
36: public void setViewName(String name) {
37: viewName = name;
38: }
39:
40: public void setRecursive(boolean recursive) {
41: this .recursive = recursive;
42: }
43:
44: public void setSelect(Query select) {
45: this .select = select;
46: }
47:
48: public int update() throws SQLException {
49: // TODO rights: what rights are required to create a view?
50: session.commit(true);
51: Database db = session.getDatabase();
52: if (getSchema().findTableOrView(session, viewName) != null) {
53: if (ifNotExists) {
54: return 0;
55: }
56: throw Message.getSQLException(
57: ErrorCode.VIEW_ALREADY_EXISTS_1, viewName);
58: }
59: int id = getObjectId(true, true);
60: String querySQL;
61: if (select == null) {
62: querySQL = selectSQL;
63: } else {
64: querySQL = select.getSQL();
65: }
66: Session s = db.getSystemSession();
67: TableView view = new TableView(getSchema(), id, viewName,
68: querySQL, null, columnNames, s, recursive);
69: view.setComment(comment);
70: db.addSchemaObject(session, view);
71: return 0;
72: }
73:
74: public void setIfNotExists(boolean ifNotExists) {
75: this .ifNotExists = ifNotExists;
76: }
77:
78: public void setSelectSQL(String selectSQL) {
79: this .selectSQL = selectSQL;
80: }
81:
82: public void setColumnNames(String[] cols) {
83: this .columnNames = cols;
84: }
85:
86: public void setComment(String comment) {
87: this.comment = comment;
88: }
89:
90: }
|