use of org.apache.cayenne.dbsync.DbSyncModule in project cayenne by apache.
the class DbImporterTask method execute.
@Override
public void execute() {
Logger logger = new AntLogger(this);
final Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger), new DbImportModule());
if (reverseEngineering.getCatalogs().size() == 0 && reverseEngineering.isEmptyContainer()) {
config.setUseDataMapReverseEngineering(true);
}
DataSourceFactory dataSourceFactory = injector.getInstance(DataSourceFactory.class);
DbAdapterFactory dbAdapterFactory = injector.getInstance(DbAdapterFactory.class);
DataNodeDescriptor dataNodeDescriptor = config.createDataNodeDescriptor();
try {
DataSource dataSource = dataSourceFactory.getDataSource(dataNodeDescriptor);
DbAdapter dbAdapter = dbAdapterFactory.createAdapter(dataNodeDescriptor, dataSource);
config.setFiltersConfig(new FiltersConfigBuilder(reverseEngineering).dataSource(dataSource).dbAdapter(dbAdapter).build());
} catch (Exception e) {
throw new BuildException("Error getting dataSource", e);
}
validateAttributes();
config.setLogger(logger);
config.setSkipRelationshipsLoading(reverseEngineering.getSkipRelationshipsLoading());
config.setSkipPrimaryKeyLoading(reverseEngineering.getSkipPrimaryKeyLoading());
config.setTableTypes(reverseEngineering.getTableTypes());
DbImportConfigurationValidator validator = new DbImportConfigurationValidator(reverseEngineering, config, injector);
try {
validator.validate();
} catch (Exception ex) {
throw new BuildException(ex.getMessage(), ex);
}
try {
injector.getInstance(DbImportAction.class).execute(config);
} catch (Exception ex) {
Throwable th = Util.unwindException(ex);
String message = "Error importing database schema";
if (th.getLocalizedMessage() != null) {
message += ": " + th.getLocalizedMessage();
}
log(message, Project.MSG_ERR);
throw new BuildException(message, th);
} finally {
injector.shutdown();
}
}
use of org.apache.cayenne.dbsync.DbSyncModule 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.dbsync.DbSyncModule in project cayenne by apache.
the class DefaultDbImportActionTest method testSaveLoadedNoProject.
@Test
public void testSaveLoadedNoProject() throws Exception {
Logger log = mock(Logger.class);
Injector i = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(log), new DbImportModule());
DbImportConfiguration params = mock(DbImportConfiguration.class);
when(params.getCayenneProject()).thenReturn(null);
URL outUrl = new URL(getPackageURL(), "dbimport/testSaveLoaded1.map.xml");
DefaultDbImportAction action = (DefaultDbImportAction) i.getInstance(DbImportAction.class);
File out = new File(outUrl.toURI());
out.delete();
assertFalse(out.exists());
DataMap map = new DataMap("testSaveLoaded1");
map.setConfigurationSource(new URLResource(outUrl));
action.saveLoaded(map, params);
assertTrue(out.isFile());
String contents = Util.stringFromFile(out);
assertTrue("Has no project version saved", contents.contains("project-version=\""));
}
use of org.apache.cayenne.dbsync.DbSyncModule 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.dbsync.DbSyncModule in project cayenne by apache.
the class DbSyncServerRuntimeProvider method getExtraModules.
@Override
protected Collection<? extends Module> getExtraModules() {
Collection<Module> modules = new ArrayList<>(super.getExtraModules());
modules.add(new DbSyncModule());
return modules;
}
Aggregations