use of org.datanucleus.store.schema.SchemaTool.Mode in project datanucleus-core by datanucleus.
the class PersistenceNucleusContextImpl method initialiseSchema.
/**
* Method to handle generation (create, drop, drop+create) of a schema at initialisation.
* Will generate the schema for all classes that have had their metadata loaded at this point, which
* typically means the persistence-unit.
* @param generateModeStr Generate "mode"
* @param generateScripts Whether to generate
*/
protected void initialiseSchema(String generateModeStr, boolean generateScripts) {
Mode mode = null;
if (generateModeStr.equalsIgnoreCase("create")) {
mode = Mode.CREATE;
} else if (generateModeStr.equalsIgnoreCase("drop")) {
mode = Mode.DELETE;
} else if (generateModeStr.equalsIgnoreCase("drop-and-create")) {
mode = Mode.DELETE_CREATE;
}
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
if (mode == Mode.CREATE) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014000"));
} else if (mode == Mode.DELETE) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014001"));
} else if (mode == Mode.DELETE_CREATE) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014045"));
}
}
// Extract the classes that have metadata loaded (e.g persistence-unit)
Set<String> schemaClassNames = null;
MetaDataManager metaDataMgr = getMetaDataManager();
FileMetaData[] filemds = metaDataMgr.getFileMetaData();
schemaClassNames = new TreeSet<String>();
if (filemds == null) {
throw new NucleusUserException("No classes to process in generateSchema");
}
for (int i = 0; i < filemds.length; i++) {
for (int j = 0; j < filemds[i].getNoOfPackages(); j++) {
for (int k = 0; k < filemds[i].getPackage(j).getNoOfClasses(); k++) {
String className = filemds[i].getPackage(j).getClass(k).getFullClassName();
if (!schemaClassNames.contains(className)) {
schemaClassNames.add(className);
}
}
}
}
StoreManager storeMgr = getStoreManager();
if (storeMgr instanceof SchemaAwareStoreManager) {
SchemaAwareStoreManager schemaStoreMgr = (SchemaAwareStoreManager) storeMgr;
SchemaTool schemaTool = new SchemaTool();
if (mode == Mode.CREATE) {
if (generateScripts) {
// Generate the required script
schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_TARGET));
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
} else {
// Process the required metadata/script
String createOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_CREATE_ORDER);
String createScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_SOURCE);
if (StringUtils.isWhitespace(createScript)) {
createScript = null;
}
if (StringUtils.isWhitespace(createOrder)) {
createOrder = (createScript != null) ? "script" : "metadata";
} else {
if (createOrder.equals("script") || createOrder.equals("metadata-then-script") || createOrder.equals("script-the-metadata")) {
if (createScript == null) {
NucleusLogger.DATASTORE_SCHEMA.warn("create order set to " + createOrder + " but no script defined, so using metadata instead");
createOrder = "metadata";
}
}
}
if (createOrder.equals("script")) {
processDatastoreScript(createScript);
} else if (createOrder.equals("script-then-metadata")) {
processDatastoreScript(createScript);
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
} else if (createOrder.equals("metadata-then-script")) {
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
processDatastoreScript(createScript);
} else {
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
}
}
} else if (mode == Mode.DELETE) {
if (generateScripts) {
// Generate the required script
schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_TARGET));
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
} else {
// Process the required metadata/script
String dropOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_DROP_ORDER);
String dropScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_SOURCE);
if (StringUtils.isWhitespace(dropScript)) {
dropScript = null;
}
if (StringUtils.isWhitespace(dropOrder)) {
dropOrder = (dropScript != null) ? "script" : "metadata";
} else {
if (dropOrder.equals("script") || dropOrder.equals("metadata-then-script") || dropOrder.equals("script-the-metadata")) {
if (dropScript == null) {
NucleusLogger.DATASTORE_SCHEMA.warn("drop order set to " + dropOrder + " but no script defined, so using metadata instead");
dropOrder = "metadata";
}
}
}
if (dropOrder.equals("script")) {
processDatastoreScript(dropScript);
} else if (dropOrder.equals("script-then-metadata")) {
processDatastoreScript(dropScript);
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
} else if (dropOrder.equals("metadata-then-script")) {
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
processDatastoreScript(dropScript);
} else {
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
}
}
} else if (mode == Mode.DELETE_CREATE) {
if (generateScripts) {
// Generate the required scripts
schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_TARGET));
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
schemaTool.setDdlFile(config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_TARGET));
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
} else {
// Process the required metadata/scripts
String dropOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_DROP_ORDER);
String dropScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_DROP_SOURCE);
if (StringUtils.isWhitespace(dropScript)) {
dropScript = null;
}
if (StringUtils.isWhitespace(dropOrder)) {
dropOrder = (dropScript != null) ? "script" : "metadata";
} else {
if (dropOrder.equals("script") || dropOrder.equals("metadata-then-script") || dropOrder.equals("script-the-metadata")) {
if (dropScript == null) {
NucleusLogger.DATASTORE_SCHEMA.warn("drop order set to " + dropOrder + " but no script defined, so using metadata instead");
dropOrder = "metadata";
}
}
}
if (dropOrder.equals("script")) {
processDatastoreScript(dropScript);
} else if (dropOrder.equals("script-then-metadata")) {
processDatastoreScript(dropScript);
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
} else if (dropOrder.equals("metadata-then-script")) {
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
processDatastoreScript(dropScript);
} else {
schemaTool.deleteSchemaForClasses(schemaStoreMgr, schemaClassNames);
}
String createOrder = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_CREATE_ORDER);
String createScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_CREATE_SOURCE);
if (StringUtils.isWhitespace(createScript)) {
createScript = null;
}
if (StringUtils.isWhitespace(createOrder)) {
createOrder = (createScript != null) ? "script" : "metadata";
} else {
if (createOrder.equals("script") || createOrder.equals("metadata-then-script") || createOrder.equals("script-the-metadata")) {
if (createScript == null) {
NucleusLogger.DATASTORE_SCHEMA.warn("create order set to " + createOrder + " but no script defined, so using metadata instead");
createOrder = "metadata";
}
}
}
if (createOrder.equals("script")) {
processDatastoreScript(createScript);
} else if (createOrder.equals("script-then-metadata")) {
processDatastoreScript(createScript);
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
} else if (createOrder.equals("metadata-then-script")) {
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
processDatastoreScript(createScript);
} else {
schemaTool.createSchemaForClasses(schemaStoreMgr, schemaClassNames);
}
}
}
String loadScript = config.getStringProperty(PropertyNames.PROPERTY_SCHEMA_GENERATE_SCRIPTS_LOAD_SOURCE);
if (!StringUtils.isWhitespace(loadScript)) {
String scriptContent = getDatastoreScriptForResourceName(loadScript);
if (storeMgr instanceof SchemaScriptAwareStoreManager && !StringUtils.isWhitespace(scriptContent)) {
((SchemaScriptAwareStoreManager) storeMgr).executeScript(scriptContent);
}
}
} else {
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("008016", StringUtils.toJVMIDString(storeMgr)));
}
}
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug(Localiser.msg("014043"));
}
}
Aggregations