use of org.apache.cayenne.dba.DbAdapter in project cayenne by apache.
the class DefaultDbImportAction method execute.
@Override
public void execute(DbImportConfiguration config) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("DB connection: " + config.getDataSourceInfo());
logger.debug(String.valueOf(config));
}
boolean hasChanges = false;
DataNodeDescriptor dataNodeDescriptor = config.createDataNodeDescriptor();
DataSource dataSource = dataSourceFactory.getDataSource(dataNodeDescriptor);
DbAdapter adapter = adapterFactory.createAdapter(dataNodeDescriptor, dataSource);
DataMap sourceDataMap;
try (Connection connection = dataSource.getConnection()) {
sourceDataMap = load(config, adapter, connection);
}
DataMap targetDataMap = existingTargetMap(config);
if (targetDataMap == null) {
String path = config.getTargetDataMap() == null ? "null" : config.getTargetDataMap().getAbsolutePath() + "'";
logger.info("");
logger.info("Map file does not exist. Loaded db model will be saved into '" + path);
hasChanges = true;
targetDataMap = newTargetDataMap(config);
}
// transform source DataMap before merging
transformSourceBeforeMerge(sourceDataMap, targetDataMap, config);
MergerTokenFactory mergerTokenFactory = mergerTokenFactoryProvider.get(adapter);
DbLoaderConfiguration loaderConfig = config.getDbLoaderConfig();
Collection<MergerToken> tokens = DataMapMerger.builder(mergerTokenFactory).filters(loaderConfig.getFiltersConfig()).skipPKTokens(loaderConfig.isSkipPrimaryKeyLoading()).skipRelationshipsTokens(loaderConfig.isSkipRelationshipsLoading()).build().createMergeTokens(targetDataMap, sourceDataMap);
tokens = log(sort(reverse(mergerTokenFactory, tokens)));
hasChanges |= syncDataMapProperties(targetDataMap, config);
hasChanges |= applyTokens(targetDataMap, tokens, config);
hasChanges |= syncProcedures(targetDataMap, sourceDataMap, loaderConfig.getFiltersConfig());
if (hasChanges) {
saveLoaded(targetDataMap, config);
}
}
use of org.apache.cayenne.dba.DbAdapter in project cayenne by apache.
the class DbGeneratorMojo method execute.
public void execute() throws MojoExecutionException, MojoFailureException {
Logger logger = new MavenLogger(this);
// check missing data source parameters
dataSource.validate();
Injector injector = DIBootstrap.createInjector(new DbSyncModule(), new ToolsModule(logger));
AdhocObjectFactory objectFactory = injector.getInstance(AdhocObjectFactory.class);
logger.info(String.format("connection settings - [driver: %s, url: %s, username: %s]", dataSource.getDriver(), dataSource.getUrl(), dataSource.getUsername()));
logger.info(String.format("generator options - [dropTables: %s, dropPK: %s, createTables: %s, createPK: %s, createFK: %s]", dropTables, dropPK, createTables, createPK, createFK));
try {
final DbAdapter adapterInst = (adapter == null) ? objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName()) : objectFactory.newInstance(DbAdapter.class, adapter);
// Load the data map and run the db generator.
DataMap dataMap = loadDataMap(injector);
DbGenerator generator = new DbGenerator(adapterInst, dataMap, NoopJdbcEventLogger.getInstance());
generator.setShouldCreateFKConstraints(createFK);
generator.setShouldCreatePKSupport(createPK);
generator.setShouldCreateTables(createTables);
generator.setShouldDropPKSupport(dropPK);
generator.setShouldDropTables(dropTables);
// load driver taking custom CLASSPATH into account...
DriverDataSource driverDataSource = new DriverDataSource((Driver) Class.forName(dataSource.getDriver()).newInstance(), dataSource.getUrl(), dataSource.getUsername(), dataSource.getPassword());
generator.runGenerator(driverDataSource);
} catch (Exception ex) {
Throwable th = Util.unwindException(ex);
String message = "Error generating database";
if (th.getLocalizedMessage() != null) {
message += ": " + th.getLocalizedMessage();
}
logger.error(message);
throw new MojoExecutionException(message, th);
}
}
use of org.apache.cayenne.dba.DbAdapter in project cayenne by apache.
the class ImportEOModelAction method loadDataNode.
protected void loadDataNode(Map eomodelIndex) {
// if this is JDBC or JNDI node and connection dictionary is specified, load a
// DataNode, otherwise ignore it (meaning that pre 5.* EOModels will not have a
// node).
String adapter = (String) eomodelIndex.get("adaptorName");
Map connection = (Map) eomodelIndex.get("connectionDictionary");
if (adapter != null && connection != null) {
CreateNodeAction nodeBuilder = (CreateNodeAction) getApplication().getActionManager().getAction(CreateNodeAction.class);
// this should make created node current, resulting in the new map being added
// to the node automatically once it is loaded
DataNodeDescriptor node = nodeBuilder.buildDataNode();
// configure node...
if ("JNDI".equalsIgnoreCase(adapter)) {
node.setDataSourceFactoryType(JNDIDataSourceFactory.class.getName());
node.setParameters((String) connection.get("serverUrl"));
} else {
// guess adapter from plugin or driver
AdapterMapping adapterDefaults = getApplication().getAdapterMapping();
String cayenneAdapter = adapterDefaults.adapterForEOFPluginOrDriver((String) connection.get("plugin"), (String) connection.get("driver"));
if (cayenneAdapter != null) {
try {
Class<DbAdapter> adapterClass = getApplication().getClassLoadingService().loadClass(DbAdapter.class, cayenneAdapter);
node.setAdapterType(adapterClass.toString());
} catch (Throwable ex) {
// ignore...
}
}
node.setDataSourceFactoryType(XMLPoolingDataSourceFactory.class.getName());
DataSourceInfo dsi = node.getDataSourceDescriptor();
dsi.setDataSourceUrl(keyAsString(connection, "URL"));
dsi.setJdbcDriver(keyAsString(connection, "driver"));
dsi.setPassword(keyAsString(connection, "password"));
dsi.setUserName(keyAsString(connection, "username"));
}
DataChannelDescriptor domain = (DataChannelDescriptor) getProjectController().getProject().getRootNode();
domain.getNodeDescriptors().add(node);
// send events after the node creation is complete
getProjectController().fireDataNodeEvent(new DataNodeEvent(this, node, MapEvent.ADD));
getProjectController().fireDataNodeDisplayEvent(new DataNodeDisplayEvent(this, (DataChannelDescriptor) getProjectController().getProject().getRootNode(), node));
}
}
use of org.apache.cayenne.dba.DbAdapter 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.dba.DbAdapter in project cayenne by apache.
the class DefaultBatchTranslatorIT method testAppendDbAttribute1.
@Test
public void testAppendDbAttribute1() throws Exception {
DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
String trimFunction = "testTrim";
DefaultBatchTranslator builder = new DefaultBatchTranslator(mock(BatchQuery.class), adapter, trimFunction) {
@Override
protected String createSql() {
return null;
}
@Override
protected DbAttributeBinding[] createBindings() {
return new DbAttributeBinding[0];
}
@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
return new DbAttributeBinding[0];
}
};
StringBuilder buf = new StringBuilder();
DbEntity entity = new DbEntity("Test");
DbAttribute attr = new DbAttribute("testAttr", Types.CHAR, null);
attr.setEntity(entity);
builder.appendDbAttribute(buf, attr);
assertEquals("testTrim(testAttr)", buf.toString());
buf = new StringBuilder();
attr = new DbAttribute("testAttr", Types.VARCHAR, null);
attr.setEntity(entity);
builder.appendDbAttribute(buf, attr);
assertEquals("testAttr", buf.toString());
}
Aggregations