KickJava   Java API By Example, From Geeks To Geeks.

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


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.Prepared;
10 import org.h2.command.dml.Insert;
11 import org.h2.command.dml.Query;
12 import org.h2.engine.Database;
13 import org.h2.engine.Session;
14 import org.h2.expression.Expression;
15 import org.h2.index.Index;
16 import org.h2.index.IndexType;
17 import org.h2.message.Message;
18 import org.h2.schema.Schema;
19 import org.h2.schema.Sequence;
20 import org.h2.table.Column;
21 import org.h2.table.TableData;
22 import org.h2.util.ObjectArray;
23
24 /**
25  * @author Thomas
26  */

27 public class CreateTable extends SchemaCommand {
28
29     private String JavaDoc tableName;
30     private ObjectArray constraintCommands = new ObjectArray();
31     private ObjectArray columns = new ObjectArray();
32     private String JavaDoc[] pkColumnNames;
33     private boolean ifNotExists;
34     private boolean persistent = true;
35     private boolean hashPrimaryKey;
36     private ObjectArray sequences;
37     private boolean temporary;
38     private boolean globalTemporary;
39     private boolean onCommitDrop;
40     private boolean onCommitTruncate;
41     private Query asQuery;
42     private String JavaDoc comment;
43
44     public CreateTable(Session session, Schema schema) {
45         super(session, schema);
46     }
47
48     public void setQuery(Query query) {
49         this.asQuery = query;
50     }
51
52     public void setTemporary(boolean temporary) {
53         this.temporary = temporary;
54     }
55
56     public void setTableName(String JavaDoc tableName) {
57         this.tableName = tableName;
58     }
59
60     public void addColumn(Column column) {
61         columns.add(column);
62     }
63
64     public void addConstraintCommand(Prepared command) throws SQLException JavaDoc {
65         if(command instanceof CreateIndex) {
66             CreateIndex create = (CreateIndex) command;
67             if(create.getPrimaryKey()) {
68                 setPrimaryKeyColumnNames(create.getColumnNames());
69                 setHashPrimaryKey(create.getHash());
70             } else {
71                 constraintCommands.add(command);
72             }
73         } else {
74             constraintCommands.add(command);
75         }
76     }
77
78     public void setIfNotExists(boolean ifNotExists) {
79         this.ifNotExists = ifNotExists;
80     }
81
82     public int update() throws SQLException JavaDoc {
83         // TODO rights: what rights are required to create a table?
84
session.commit();
85         Database db = session.getDatabase();
86         if(!db.isPersistent()) {
87             persistent = false;
88         }
89         if(getSchema().findTableOrView(session, tableName)!=null) {
90             if (ifNotExists) {
91                 return 0;
92             }
93             throw Message.getSQLException(Message.TABLE_OR_VIEW_ALREADY_EXISTS_1, tableName);
94         }
95         if(asQuery != null) {
96             generateColumnFromQuery();
97         }
98         if (pkColumnNames != null) {
99             int len = pkColumnNames.length;
100             for(int i=0; i<columns.size(); i++) {
101                 Column c = (Column) columns.get(i);
102                 for(int j=0; j<len; j++) {
103                     if(c.getName().equals(pkColumnNames[j])) {
104                         c.setNullable(false);
105                     }
106                 }
107             }
108         }
109         sequences = new ObjectArray();
110         for(int i=0; i<columns.size(); i++) {
111             Column c = (Column) columns.get(i);
112             if(c.getAutoIncrement()) {
113                 int objId = getObjectId(true, true);
114                 c.convertAutoIncrementToSequence(session, getSchema(), objId, temporary);
115             }
116             Sequence seq = c.getSequence();
117             if(seq != null) {
118                 sequences.add(seq);
119             }
120         }
121         int id = getObjectId(true, true);
122         TableData table = new TableData(getSchema(), tableName, id, columns, persistent);
123         table.setComment(comment);
124         table.setTemporary(temporary);
125         table.setGlobalTemporary(globalTemporary);
126         if(temporary && !globalTemporary) {
127             if(onCommitDrop) {
128                 table.setOnCommitDrop(true);
129             }
130             if(onCommitTruncate) {
131                 table.setOnCommitTruncate(true);
132             }
133             session.addLocalTempTable(table);
134         } else {
135             db.addSchemaObject(session, table);
136         }
137         try {
138             for(int i=0; i<columns.size(); i++) {
139                 Column c = (Column) columns.get(i);
140                 c.prepareExpression(session);
141             }
142             if (pkColumnNames != null) {
143                 Column[] pk =table.getColumns(pkColumnNames);
144                 int indexId = getObjectId(true, false);
145                 table.addIndex(session, null, indexId, pk, IndexType.createPrimaryKey(persistent, hashPrimaryKey), Index.EMPTY_HEAD, null);
146             }
147             for(int i=0; i<sequences.size(); i++) {
148                 Sequence sequence = (Sequence) sequences.get(i);
149                 table.addSequence(sequence);
150             }
151             for(int i=0; i<constraintCommands.size(); i++) {
152                 Prepared command = (Prepared) constraintCommands.get(i);
153                 command.update();
154             }
155             if(asQuery != null) {
156                 Insert insert = new Insert(session);
157                 insert.setTable(table);
158                 insert.setQuery(asQuery);
159                 insert.prepare();
160                 insert.update();
161             }
162         } catch(SQLException JavaDoc e) {
163             db.checkPowerOff();
164             db.removeSchemaObject(session, table);
165             throw e;
166         }
167         return 0;
168     }
169
170     private void generateColumnFromQuery() throws SQLException JavaDoc {
171         asQuery.prepare();
172         int columnCount = asQuery.getColumnCount();
173         ObjectArray expressions = asQuery.getExpressions();
174         for(int i=0; i<columnCount; i++) {
175             Expression expr = (Expression) expressions.get(i);
176             int type = expr.getType();
177             String JavaDoc name = expr.getColumnName();
178             long precision = expr.getPrecision();
179             int scale = expr.getScale();
180             Column col = new Column(name, type, precision, scale);
181             addColumn(col);
182         }
183     }
184
185     public void setPrimaryKeyColumnNames(String JavaDoc[] colNames) throws SQLException JavaDoc {
186         if(pkColumnNames != null) {
187             if(colNames.length != pkColumnNames.length) {
188                 throw Message.getSQLException(Message.SECOND_PRIMARY_KEY);
189             }
190             for(int i=0; i<colNames.length; i++) {
191                 if(!colNames[i].equals(pkColumnNames[i])) {
192                     throw Message.getSQLException(Message.SECOND_PRIMARY_KEY);
193                 }
194             }
195         }
196         this.pkColumnNames = colNames;
197     }
198
199     public void setPersistent(boolean persistent) {
200         this.persistent = persistent;
201     }
202
203     public void setHashPrimaryKey(boolean b) {
204         this.hashPrimaryKey = b;
205     }
206
207     public void setGlobalTemporary(boolean globalTemporary) {
208         this.globalTemporary = globalTemporary;
209     }
210
211     public void setOnCommitDrop() {
212         this.onCommitDrop = true;
213     }
214
215     public void setOnCommitTruncate() {
216         this.onCommitTruncate = true;
217     }
218
219     public void setComment(String JavaDoc comment) {
220         this.comment = comment;
221     }
222
223 }
224
Popular Tags