use of org.apache.cayenne.access.DbGenerator in project cayenne by apache.
the class DbGeneratorTask method execute.
@Override
public void execute() {
Logger logger = new AntLogger(this);
log(String.format("connection settings - [driver: %s, url: %s, username: %s]", driver, url, userName), Project.MSG_VERBOSE);
log(String.format("generator options - [dropTables: %s, dropPK: %s, createTables: %s, createPK: %s, createFK: %s]", dropTables, dropPK, createTables, createPK, createFK), Project.MSG_VERBOSE);
validateAttributes();
ClassLoader loader = null;
Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger));
try {
loader = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(DbGeneratorTask.class.getClassLoader());
// Load the data map and run the db generator.
DataMap dataMap = loadDataMap(injector);
// load driver taking custom CLASSPATH into account...
DriverDataSource dataSource = new DriverDataSource((Driver) Class.forName(driver).newInstance(), url, userName, password);
DbAdapter adapter = getAdapter(injector, dataSource);
DbGenerator generator = new DbGenerator(adapter, dataMap, Collections.<DbEntity>emptyList(), null, NoopJdbcEventLogger.getInstance());
generator.setShouldCreateFKConstraints(createFK);
generator.setShouldCreatePKSupport(createPK);
generator.setShouldCreateTables(createTables);
generator.setShouldDropPKSupport(dropPK);
generator.setShouldDropTables(dropTables);
generator.runGenerator(dataSource);
} catch (Exception ex) {
Throwable th = Util.unwindException(ex);
String message = "Error generating database";
if (th.getLocalizedMessage() != null) {
message += ": " + th.getLocalizedMessage();
}
log(message, Project.MSG_ERR);
throw new BuildException(message, th);
} finally {
Thread.currentThread().setContextClassLoader(loader);
injector.shutdown();
}
}
use of org.apache.cayenne.access.DbGenerator in project cayenne by apache.
the class DbGenerateTask method generateDb.
@TaskAction
public void generateDb() throws GradleException {
dataSource.validate();
getLogger().info("connection settings - [driver: {}, url: {}, username: {}]", dataSource.getDriver(), dataSource.getUrl(), dataSource.getUsername());
getLogger().info("generator options - " + "[dropTables: {}, dropPK: {}, createTables: {}, createPK: {}, createFK: {}]", dropTables, dropPK, createTables, createPK, createFK);
Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(getLogger()));
try {
DbGenerator generator = createGenerator(loadDataMap(injector));
generator.runGenerator(createDataSource());
} catch (Exception ex) {
Throwable th = Util.unwindException(ex);
String message = "Error generating database";
if (th.getLocalizedMessage() != null) {
message += ": " + th.getLocalizedMessage();
}
getLogger().error(message);
throw new GradleException(message, th);
}
}
use of org.apache.cayenne.access.DbGenerator in project cayenne by apache.
the class DBGeneratorOptions method prepareGenerator.
/**
* Creates new internal DbGenerator instance.
*/
protected void prepareGenerator() {
try {
DbAdapter adapter = connectionInfo.makeAdapter(getApplication().getClassLoadingService());
generators = new ArrayList<>();
for (DataMap dataMap : dataMaps) {
this.generators.add(new DbGenerator(adapter, dataMap, tables.getExcludedTables(), null, NoopJdbcEventLogger.getInstance()));
}
} catch (Exception ex) {
reportError("Error loading adapter", ex);
}
}
use of org.apache.cayenne.access.DbGenerator in project cayenne by apache.
the class DBGeneratorDefaults method configureGenerator.
/**
* Updates DbGenerator settings, consulting its own state.
*/
public void configureGenerator(Collection<DbGenerator> generators) {
setCreateFK(createFK);
setCreatePK(createPK);
setCreateTables(createTables);
setDropPK(dropPK);
setDropTables(dropTables);
for (DbGenerator generator : generators) {
generator.setShouldCreateFKConstraints(createFK);
generator.setShouldCreatePKSupport(createPK);
generator.setShouldCreateTables(createTables);
generator.setShouldDropPKSupport(dropPK);
generator.setShouldDropTables(dropTables);
}
}
use of org.apache.cayenne.access.DbGenerator in project cayenne by apache.
the class CreateIfNoSchemaStrategy method generate.
private void generate(DataNode dataNode) {
Collection<DataMap> map = dataNode.getDataMaps();
Iterator<DataMap> iterator = map.iterator();
while (iterator.hasNext()) {
DbGenerator gen = new DbGenerator(dataNode.getAdapter(), iterator.next(), dataNode.getJdbcEventLogger());
gen.setShouldCreateTables(true);
gen.setShouldDropTables(false);
gen.setShouldCreateFKConstraints(true);
gen.setShouldCreatePKSupport(true);
gen.setShouldDropPKSupport(false);
try {
gen.runGenerator(dataNode.getDataSource());
} catch (Exception e) {
throw new CayenneRuntimeException(e);
}
}
}
Aggregations