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 SchemaBuilder method tableCreateQueries.
/**
* Returns iterator of preprocessed table create queries.
*/
private Collection<String> tableCreateQueries(DataNode node, DataMap map) throws Exception {
DbAdapter adapter = node.getAdapter();
DbGenerator gen = new DbGenerator(adapter, map, null, domain, jdbcEventLogger);
List<DbEntity> orderedEnts = dbEntitiesInInsertOrder(node, map);
List<String> queries = new ArrayList<String>();
// table definitions
for (DbEntity ent : orderedEnts) {
queries.add(adapter.createTable(ent));
}
// FK constraints
for (DbEntity ent : orderedEnts) {
if (!unitDbAdapter.supportsFKConstraints(ent)) {
continue;
}
List<String> qs = gen.createConstraintsQueries(ent);
queries.addAll(qs);
}
return queries;
}
use of org.apache.cayenne.access.DbGenerator in project cayenne by apache.
the class DbGenerateTask method createGenerator.
DbGenerator createGenerator(DataMap dataMap) {
DbGenerator generator = new DbGenerator(createDbAdapter(), dataMap, NoopJdbcEventLogger.getInstance());
generator.setShouldCreateFKConstraints(createFK);
generator.setShouldCreatePKSupport(createPK);
generator.setShouldCreateTables(createTables);
generator.setShouldDropPKSupport(dropPK);
generator.setShouldDropTables(dropTables);
return generator;
}
use of org.apache.cayenne.access.DbGenerator in project cayenne by apache.
the class ThrowOnPartialOrCreateSchemaStrategy 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(false);
gen.setShouldCreatePKSupport(false);
gen.setShouldDropPKSupport(false);
try {
gen.runGenerator(dataNode.getDataSource());
} catch (Exception e) {
throw new CayenneRuntimeException(e);
}
}
}
use of org.apache.cayenne.access.DbGenerator in project cayenne by apache.
the class DBGeneratorOptions method createSQL.
/**
* Returns SQL statements generated for selected schema generation options.
*/
protected void createSQL() {
// convert them to string representation for display
StringBuilder buf = new StringBuilder();
for (DbGenerator generator : generators) {
Iterator<String> it = generator.configuredStatements().iterator();
String batchTerminator = generator.getAdapter().getBatchTerminator();
String lineEnd = (batchTerminator != null) ? "\n" + batchTerminator + "\n\n" : "\n\n";
while (it.hasNext()) {
buf.append(it.next()).append(lineEnd);
}
}
textForSQL = buf.toString();
}
Aggregations