1 17 package org.apache.ws.jaxme.sqls.db2; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Collections ; 22 import java.util.Iterator ; 23 import java.util.List ; 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 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 46 public void setCreatingTableSpaceReferences(boolean pCreatingTableSpaceReferences) { 47 isCreatingTableSpaceReferences = pCreatingTableSpaceReferences; 48 } 49 50 54 public boolean isCreatingTableSpaceReferences() { 55 return isCreatingTableSpaceReferences; 56 } 57 58 protected String 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 getCreate(BufferPool pBufferPool) { 68 StringBuffer sb = new StringBuffer (); 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 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 result = new ArrayList (); 84 result.add(newStatement(sb.toString())); 85 return result; 86 } 87 88 public Collection getDrop(BufferPool pBufferPool) { 89 List result = new ArrayList (); 90 result.add(newStatement("DROP BUFFERPOOL " + pBufferPool.getName())); 91 return result; 92 } 93 94 public Collection getCreate(TableSpace pTableSpace) { 95 if (pTableSpace.isPredefined()) { 96 return Collections.EMPTY_SET; 97 } 98 StringBuffer sb = new StringBuffer (); 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 iter = pTableSpace.getContainers(); 112 if (!iter.hasNext()) { 113 throw new IllegalStateException ("The TableSpace " + pTableSpace.getName() + 114 " doesn't have any containers."); 115 } 116 TableSpace.Container container = (TableSpace.Container) iter.next(); 117 String sep = "("; 118 if (container.isSystemManaged()) { 119 sb.append(" MANAGED BY SYSTEM USING "); 120 while (container != null) { 121 if (!container.isSystemManaged()) { 122 throw new IllegalStateException ("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 ("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 extentSize = pTableSpace.getExtentSize(); 149 if (extentSize != null) { 150 sb.append(" EXTENTSIZE ").append(extentSize); 151 } 152 Long 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 overhead = pTableSpace.getOverhead(); 160 if (overhead != null) { 161 sb.append(" OVERHEAD ").append(overhead); 162 } 163 Number transferRate = pTableSpace.getTransferRate(); 164 if (transferRate != null) { 165 sb.append(" TRANSFERRATE ").append(transferRate); 166 } 167 Boolean hasDroppedTableRecovery = pTableSpace.hasDroppedTableRecovery(); 168 if (hasDroppedTableRecovery != null) { 169 sb.append(" DROPPED TABLE RECOVERY "); 170 sb.append(hasDroppedTableRecovery.booleanValue() ? "ON" : "OFF"); 171 } 172 173 List result = new ArrayList (); 174 result.add(newStatement(sb.toString())); 175 return result; 176 } 177 178 public Collection getDrop(TableSpace pTableSpace) { 179 if (pTableSpace.isPredefined()) { 180 return Collections.EMPTY_SET; 181 } 182 List result = new ArrayList (); 183 result.add(newStatement("DROP TABLESPACE " + pTableSpace.getName())); 184 return result; 185 } 186 187 protected String getCreateTableHeader(Table pTable) { 188 String statement = super.getCreateTableHeader(pTable); 189 if (!isCreatingTableSpaceReferences() || 190 !(pTable instanceof DB2Table)) { 191 return statement; 192 } 193 StringBuffer sb = new StringBuffer (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 getCreate(Column pColumn) { 211 String 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 getDrop(Schema pSchema) { 222 List result = new ArrayList (); 223 for (Iterator iter = super.getDrop(pSchema).iterator(); iter.hasNext(); ) { 224 String s = (String ) 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 |