1 21 22 package oracle.toplink.essentials.tools.schemaframework; 24 25 import java.io.Writer ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Vector ; 30 import oracle.toplink.essentials.exceptions.DatabaseException; 31 import oracle.toplink.essentials.exceptions.TopLinkException; 32 import oracle.toplink.essentials.exceptions.ValidationException; 33 import oracle.toplink.essentials.internal.databaseaccess.DatabaseAccessor; 34 import oracle.toplink.essentials.internal.sequencing.Sequencing; 35 import oracle.toplink.essentials.logging.SessionLog; 36 import oracle.toplink.essentials.internal.sessions.DatabaseSessionImpl; 37 import oracle.toplink.essentials.descriptors.ClassDescriptor; 38 import oracle.toplink.essentials.internal.sessions.AbstractSession; 39 import oracle.toplink.essentials.sequencing.DefaultSequence; 40 import oracle.toplink.essentials.sequencing.NativeSequence; 41 import oracle.toplink.essentials.sequencing.Sequence; 42 import oracle.toplink.essentials.sequencing.TableSequence; 43 44 54 public class SchemaManager { 55 protected DatabaseSessionImpl session; 56 protected Writer createSchemaWriter; 57 protected Writer dropSchemaWriter; 58 protected boolean createSQLFiles = true; 59 60 public SchemaManager(DatabaseSessionImpl session) { 61 this.session = session; 62 } 63 64 public SchemaManager(oracle.toplink.essentials.sessions.DatabaseSession session) { 65 this.session = ((DatabaseSessionImpl)session); 66 } 67 68 protected Writer getDropSchemaWriter() { 69 if (null == dropSchemaWriter) { 70 return createSchemaWriter; 71 } else { 72 return dropSchemaWriter; 73 } 74 } 75 76 80 public void appendToDDLWriter(String stringToWrite) { 81 appendToDDLWriter(createSchemaWriter, stringToWrite); 86 } 87 88 public void appendToDDLWriter(Writer schemaWriter, String stringToWrite) { 89 if (schemaWriter == null) { 90 return; } 92 93 try { 94 schemaWriter.write(stringToWrite); 95 schemaWriter.flush(); 96 } catch (java.io.IOException ioException) { 97 throw ValidationException.fileError(ioException); 98 } 99 } 100 101 105 public void buildFieldTypes(TableDefinition tableDef) { 106 tableDef.buildFieldTypes(getSession()); 107 } 108 109 113 public void closeDDLWriter() { 114 closeDDLWriter(createSchemaWriter); 115 if (null != dropSchemaWriter) { 116 closeDDLWriter(dropSchemaWriter); 117 } 118 } 119 120 public void closeDDLWriter(Writer schemaWriter) { 121 if (schemaWriter == null) { 122 return; 123 } 124 125 try { 126 schemaWriter.flush(); 127 schemaWriter.close(); 128 schemaWriter = null; 129 } catch (java.io.IOException ioException) { 130 throw ValidationException.fileError(ioException); 131 } 132 } 133 134 138 public void createConstraints(TableDefinition tableDefinition) throws TopLinkException { 139 if (shouldWriteToDatabase()) { 140 tableDefinition.createConstraintsOnDatabase(getSession()); 141 } else { 142 tableDefinition.setCreateSQLFiles(createSQLFiles); 143 tableDefinition.createConstraints(getSession(), createSchemaWriter); 144 } 145 } 146 147 151 public void createObject(DatabaseObjectDefinition databaseObjectDefinition) throws TopLinkException { 152 if (shouldWriteToDatabase()) { 153 databaseObjectDefinition.createOnDatabase(getSession()); 154 } else { 155 databaseObjectDefinition.createObject(getSession(), createSchemaWriter); 156 if (createSQLFiles){ 157 this.appendToDDLWriter(createSchemaWriter, getSession().getPlatform().getStoredProcedureTerminationToken()); 158 } 159 this.appendToDDLWriter(createSchemaWriter, "\n"); 160 } 161 } 162 163 166 public void createSequences() throws TopLinkException { 167 createOrReplaceSequences(true); 168 } 169 170 173 public void replaceSequences() throws TopLinkException { 174 createOrReplaceSequences(false); 175 } 176 177 180 protected void createOrReplaceSequences(boolean create) throws TopLinkException { 181 Sequencing sequencing = getSession().getSequencing(); 182 183 if ((sequencing == null) || (sequencing.whenShouldAcquireValueForAll() == Sequencing.AFTER_INSERT)) { 184 return; 186 } 187 188 HashMap tableDefinitions = new HashMap (); 191 192 HashSet sequenceDefinitions = new HashSet (); 194 195 HashSet processedSequenceNames = new HashSet (); 197 buildTableAndSequenceDefinitions(sequenceDefinitions, processedSequenceNames, tableDefinitions); 198 processTableDefinitions(tableDefinitions, create); 199 processSequenceDefinitions(sequenceDefinitions, create); 200 } 201 202 205 protected void createOrReplaceSequences(boolean create, boolean drop) throws TopLinkException { 206 Sequencing sequencing = getSession().getSequencing(); 207 208 if ((sequencing == null) || (sequencing.whenShouldAcquireValueForAll() == Sequencing.AFTER_INSERT)) { 209 return; 211 } 212 213 HashMap tableDefinitions = new HashMap (); 216 217 HashSet sequenceDefinitions = new HashSet (); 219 220 HashSet processedSequenceNames = new HashSet (); 222 223 buildTableAndSequenceDefinitions(sequenceDefinitions, processedSequenceNames, tableDefinitions); 224 processTableDefinitions(tableDefinitions, create); 225 processSequenceDefinitions(sequenceDefinitions, drop); 226 } 227 228 private void buildTableAndSequenceDefinitions(final HashSet sequenceDefinitions, 229 final HashSet processedSequenceNames, final HashMap tableDefinitions) { 230 Iterator descriptors = getSession().getDescriptors().values().iterator(); 231 232 while (descriptors.hasNext()) { 233 ClassDescriptor descriptor = (ClassDescriptor)descriptors.next(); 234 235 if (descriptor.usesSequenceNumbers()) { 236 String seqName = descriptor.getSequenceNumberName(); 237 238 if (seqName == null) { 239 seqName = getSession().getDatasourcePlatform().getDefaultSequence().getName(); 240 } 241 242 if (processedSequenceNames.contains(seqName)) { 243 continue; 244 } 245 246 processedSequenceNames.add(seqName); 247 248 Sequence sequence = getSession().getDatasourcePlatform().getSequence(seqName); 249 250 if (sequence.shouldAcquireValueAfterInsert()) { 251 continue; 252 } 253 254 SequenceDefinition sequenceDefinition = buildSequenceDefinition(sequence); 255 256 if (sequenceDefinition == null) { 257 continue; 258 } 259 260 sequenceDefinitions.add(sequenceDefinition); 261 262 TableDefinition tableDefinition = sequenceDefinition.buildTableDefinition(); 263 264 if (tableDefinition != null) { 265 String tableName = tableDefinition.getName(); 266 TableDefinition otherTableDefinition = (TableDefinition)tableDefinitions.get(tableName); 267 268 if (otherTableDefinition != null) { 269 } else { 271 tableDefinitions.put(tableName, tableDefinition); 272 } 273 } 274 } 275 } 276 } 277 278 private void processTableDefinitions(final HashMap tableDefinitions, final boolean create) throws TopLinkException { 279 280 Iterator itTableDefinitions = tableDefinitions.values().iterator(); 282 283 while (itTableDefinitions.hasNext()) { 284 TableDefinition tableDefinition = (TableDefinition)itTableDefinitions.next(); 285 286
|