KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > ddl > CreateView


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.command.ddl;
6
7 import java.sql.SQLException JavaDoc;
8
9 import org.h2.command.dml.Query;
10 import org.h2.engine.Database;
11 import org.h2.engine.Session;
12 import org.h2.message.Message;
13 import org.h2.schema.Schema;
14 import org.h2.table.TableView;
15
16 public class CreateView extends SchemaCommand {
17
18     private Query select;
19     private String JavaDoc viewName;
20     private boolean ifNotExists;
21     private String JavaDoc selectSQL;
22     private String JavaDoc[] columnNames;
23     private String JavaDoc comment;
24
25     public CreateView(Session session, Schema schema) {
26         super(session, schema);
27     }
28
29     public void setViewName(String JavaDoc name) {
30         viewName = name;
31     }
32
33     public void setSelect(Query select) {
34         this.select = select;
35     }
36
37     public int update() throws SQLException JavaDoc {
38         // TODO rights: what rights are required to create a view?
39
session.commit();
40         Database db = session.getDatabase();
41         if(getSchema().findTableOrView(session, viewName)!=null) {
42             if (ifNotExists) {
43                 return 0;
44             }
45             throw Message.getSQLException(Message.VIEW_ALREADY_EXISTS_1,
46                     viewName);
47         }
48         int id = getObjectId(true, true);
49         String JavaDoc querySQL;
50         if(select == null) {
51             querySQL = selectSQL;
52         } else {
53             querySQL = select.getSQL();
54         }
55         TableView view = new TableView(getSchema(), id, viewName, querySQL, null, columnNames, session);
56         view.setComment(comment);
57         db.addSchemaObject(session, view);
58         return 0;
59     }
60
61     public void setIfNotExists(boolean ifNotExists) {
62         this.ifNotExists = ifNotExists;
63     }
64
65     public void setSelectSQL(String JavaDoc selectSQL) {
66         this.selectSQL = selectSQL;
67     }
68
69     public void setColumnNames(String JavaDoc[] cols) {
70         this.columnNames = cols;
71     }
72
73     public void setComment(String JavaDoc comment) {
74         this.comment = comment;
75     }
76
77 }
78
Popular Tags