KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > sqls > db2 > DB2SQLGeneratorImpl


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.sqls.db2;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.Collections JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.ws.jaxme.sqls.Column;
26 import org.apache.ws.jaxme.sqls.Schema;
27 import org.apache.ws.jaxme.sqls.Table;
28 import org.apache.ws.jaxme.sqls.impl.SQLGeneratorImpl;
29
30
31 /** <p>Default implementation of an SQL generator for DB2 schemas.</p>
32  *
33  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
34  */

35 public class DB2SQLGeneratorImpl extends SQLGeneratorImpl implements DB2SQLGenerator {
36   private boolean isCreatingTableSpaceReferences = true;
37
38   protected boolean isPrimaryKeyPartOfCreateTable() { return true; }
39   protected boolean isUniqueIndexPartOfCreateTable() { return true; }
40   protected boolean isForeignKeyPartOfCreateTable() { return true; }
41
42   /** <p>Sets whether <code>CREATE TABLE</code> statements will contain
43    * table space references or not. The default is to create table
44    * space references.</p>
45    */

46   public void setCreatingTableSpaceReferences(boolean pCreatingTableSpaceReferences) {
47      isCreatingTableSpaceReferences = pCreatingTableSpaceReferences;
48   }
49
50   /** <p>Returns whether <code>CREATE TABLE</code> statements will contain
51    * table space references or not. The default is to create table
52    * space references.</p>
53    */

54   public boolean isCreatingTableSpaceReferences() {
55      return isCreatingTableSpaceReferences;
56   }
57   
58   protected String JavaDoc getTypeName(Column.Type pType) {
59     if (pType.equals(Column.Type.BINARY) ||
60         pType.equals(Column.Type.VARBINARY)) {
61       return "BLOB";
62     } else {
63       return super.getTypeName(pType);
64     }
65   }
66
67   public Collection JavaDoc getCreate(BufferPool pBufferPool) {
68     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
69     sb.append("CREATE BUFFERPOOL ").append(pBufferPool.getName()).append(" SIZE ");
70     sb.append(pBufferPool.getNumberOfPages());
71     PageSize pageSize = pBufferPool.getPageSize();
72     if (pageSize != null) {
73       sb.append(" PAGESIZE ").append(pageSize.getSize());
74     }
75     Boolean JavaDoc extendedStorage = pBufferPool.getExtendedStorage();
76     if (extendedStorage != null) {
77       if (extendedStorage.booleanValue()) {
78         sb.append(" EXTENDED STORAGE");
79       } else {
80         sb.append(" NOT EXTENDED STORAGE");
81       }
82     }
83     List JavaDoc result = new ArrayList JavaDoc();
84     result.add(newStatement(sb.toString()));
85     return result;
86   }
87
88   public Collection JavaDoc getDrop(BufferPool pBufferPool) {
89      List JavaDoc result = new ArrayList JavaDoc();
90      result.add(newStatement("DROP BUFFERPOOL " + pBufferPool.getName()));
91      return result;
92   }
93
94   public Collection JavaDoc getCreate(TableSpace pTableSpace) {
95     if (pTableSpace.isPredefined()) {
96       return Collections.EMPTY_SET;
97     }
98     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
99     sb.append("CREATE TABLESPACE ");
100     TableSpace.Type type = pTableSpace.getType();
101     if (type != null) {
102       sb.append(type).append(" ");
103     }
104     sb.append(pTableSpace.getName().getName());
105     BufferPool bufferPool = pTableSpace.getBufferPool();
106     PageSize pageSize = (bufferPool == null) ?
107       pTableSpace.getPageSize() : bufferPool.getPageSize();
108     if (pageSize != null) {
109       sb.append(" PAGESIZE ").append(pageSize);
110     }
111     Iterator JavaDoc iter = pTableSpace.getContainers();
112     if (!iter.hasNext()) {
113        throw new IllegalStateException JavaDoc("The TableSpace " + pTableSpace.getName() +
114                                         " doesn't have any containers.");
115     }
116     TableSpace.Container container = (TableSpace.Container) iter.next();
117     String JavaDoc sep = "(";
118     if (container.isSystemManaged()) {
119        sb.append(" MANAGED BY SYSTEM USING ");
120        while (container != null) {
121          if (!container.isSystemManaged()) {
122            throw new IllegalStateException JavaDoc("A TableSpace must not mix system and database managed containers.");
123          }
124          TableSpace.SystemManagedContainer systemContainer = (TableSpace.SystemManagedContainer) container;
125          sb.append(sep);
126          sep = ", ";
127          sb.append("'").append(systemContainer.getFile()).append("'");
128          container = iter.hasNext() ? (TableSpace.Container) iter.next() : null;
129        }
130     } else {
131        sb.append(" MANAGED BY DATABASE USING (");
132        while (container != null) {
133          if (!container.isDatabaseManaged()) {
134            throw new IllegalStateException JavaDoc("A TableSpace must not mix system and database managed containers.");
135          }
136          TableSpace.DatabaseManagedContainer databaseContainer = (TableSpace.DatabaseManagedContainer) container;
137          sb.append(sep);
138          sep = ", ";
139          if (databaseContainer.getFile() != null) {
140             sb.append("FILE '").append(databaseContainer.getFile()).append("'");
141          } else {
142             sb.append("DEVICE '").append(databaseContainer.getDevice()).append("'");
143          }
144          container = iter.hasNext() ? (TableSpace.Container) iter.next() : null;
145        }
146     }
147     sb.append(")");
148     Long JavaDoc extentSize = pTableSpace.getExtentSize();
149     if (extentSize != null) {
150       sb.append(" EXTENTSIZE ").append(extentSize);
151     }
152     Long JavaDoc prefetchSize = pTableSpace.getPrefetchSize();
153     if (prefetchSize != null) {
154       sb.append(" PREFETCHSIZE ").append(prefetchSize);
155     }
156     if (bufferPool != null) {
157       sb.append(" BUFFERPOOL ").append(bufferPool.getName().getName());
158     }
159     Number JavaDoc overhead = pTableSpace.getOverhead();
160     if (overhead != null) {
161       sb.append(" OVERHEAD ").append(overhead);
162     }
163     Number JavaDoc transferRate = pTableSpace.getTransferRate();
164     if (transferRate != null) {
165       sb.append(" TRANSFERRATE ").append(transferRate);
166     }
167     Boolean JavaDoc hasDroppedTableRecovery = pTableSpace.hasDroppedTableRecovery();
168     if (hasDroppedTableRecovery != null) {
169       sb.append(" DROPPED TABLE RECOVERY ");
170       sb.append(hasDroppedTableRecovery.booleanValue() ? "ON" : "OFF");
171     }
172
173     List JavaDoc result = new ArrayList JavaDoc();
174     result.add(newStatement(sb.toString()));
175     return result;
176   }
177
178   public Collection JavaDoc getDrop(TableSpace pTableSpace) {
179     if (pTableSpace.isPredefined()) {
180       return Collections.EMPTY_SET;
181     }
182     List JavaDoc result = new ArrayList JavaDoc();
183     result.add(newStatement("DROP TABLESPACE " + pTableSpace.getName()));
184     return result;
185   }
186
187   protected String JavaDoc getCreateTableHeader(Table pTable) {
188      String JavaDoc statement = super.getCreateTableHeader(pTable);
189      if (!isCreatingTableSpaceReferences() ||
190          !(pTable instanceof DB2Table)) {
191         return statement;
192      }
193      StringBuffer JavaDoc sb = new StringBuffer JavaDoc(statement);
194      DB2Table table = (DB2Table) pTable;
195      TableSpace tableSpace = table.getTableSpace();
196      if (tableSpace != null) {
197         sb.append(" IN ").append(tableSpace.getName());
198      }
199      tableSpace = table.getIndexTableSpace();
200      if (tableSpace != null) {
201         sb.append(" INDEX IN ").append(tableSpace.getName());
202      }
203      tableSpace = table.getLongTableSpace();
204      if (tableSpace != null) {
205         sb.append(" LONG IN ").append(tableSpace.getName());
206      }
207      return sb.toString();
208   }
209
210   protected String JavaDoc getCreate(Column pColumn) {
211     String JavaDoc result = super.getCreate(pColumn);
212     if (pColumn instanceof DB2Column) {
213        DB2Column db2Column = (DB2Column) pColumn;
214        if (db2Column.getGeneratedAs() != null) {
215           result += " GENERATED ALWAYS AS (" + db2Column.getGeneratedAs() + ")";
216        }
217     }
218     return result;
219   }
220
221   public Collection JavaDoc getDrop(Schema pSchema) {
222      List JavaDoc result = new ArrayList JavaDoc();
223      for (Iterator JavaDoc iter = super.getDrop(pSchema).iterator(); iter.hasNext(); ) {
224         String JavaDoc s = (String JavaDoc) iter.next();
225         if (s.startsWith("DROP SCHEMA ")) {
226            s += " RESTRICT";
227         }
228         result.add(s);
229      }
230      return result;
231   }
232 }
233
Popular Tags