use of org.apache.cayenne.dba.DbAdapter in project cayenne by apache.
the class UpdateBatchTranslatorIT method testCreateSqlStringWithNulls.
@Test
public void testCreateSqlStringWithNulls() throws Exception {
DbEntity entity = runtime.getDataDomain().getEntityResolver().getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
List idAttributes = Arrays.asList(entity.getAttribute("LOCKING_TEST_ID"), entity.getAttribute("NAME"));
List updatedAttributes = Collections.singletonList(entity.getAttribute("DESCRIPTION"));
Collection nullAttributes = Collections.singleton("NAME");
UpdateBatchQuery updateQuery = new UpdateBatchQuery(entity, idAttributes, updatedAttributes, nullAttributes, 1);
DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
UpdateBatchTranslator builder = new UpdateBatchTranslator(updateQuery, adapter, null);
String generatedSql = builder.getSql();
assertNotNull(generatedSql);
assertEquals("UPDATE " + entity.getName() + " SET DESCRIPTION = ? WHERE LOCKING_TEST_ID = ? AND NAME IS NULL", generatedSql);
}
use of org.apache.cayenne.dba.DbAdapter in project cayenne by apache.
the class DefaultDbAdapterFactoryTest method testCreatedAdapter_Auto.
@Test
public void testCreatedAdapter_Auto() throws Exception {
final DbAdapter adapter = mock(DbAdapter.class);
when(adapter.createTable(any(DbEntity.class))).thenReturn("XXXXX");
List<DbAdapterDetector> detectors = new ArrayList<DbAdapterDetector>();
detectors.add(new DbAdapterDetector() {
public DbAdapter createAdapter(DatabaseMetaData md) throws SQLException {
return adapter;
}
});
MockConnection connection = new MockConnection();
MockDataSource dataSource = new MockDataSource();
dataSource.setupConnection(connection);
Module testModule = binder -> {
ServerModule.contributeProperties(binder);
binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
binder.bind(RuntimeProperties.class).to(DefaultRuntimeProperties.class);
};
Injector injector = DIBootstrap.createInjector(testModule);
DefaultDbAdapterFactory factory = new DefaultDbAdapterFactory(detectors);
injector.injectMembers(factory);
DbAdapter createdAdapter = factory.createAdapter(new DataNodeDescriptor(), dataSource);
assertTrue(createdAdapter instanceof AutoAdapter);
assertEquals("XXXXX", createdAdapter.createTable(new DbEntity("Test")));
}
use of org.apache.cayenne.dba.DbAdapter in project cayenne by apache.
the class SQLServerSnifferIT method testCreateAdapter.
@Test
public void testCreateAdapter() throws Exception {
SQLServerSniffer sniffer = new SQLServerSniffer(objectFactory);
DbAdapter adapter = null;
try (Connection c = dataSourceFactory.getSharedDataSource().getConnection()) {
adapter = sniffer.createAdapter(c.getMetaData());
}
if (accessStackAdapter instanceof SQLServerUnitDbAdapter) {
assertNotNull(adapter);
} else {
assertNull(adapter);
}
}
use of org.apache.cayenne.dba.DbAdapter 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.dba.DbAdapter in project cayenne by apache.
the class DbGeneratorTaskTest method testSetAdapter.
@Test
public void testSetAdapter() throws Exception {
DataSource ds = mock(DataSource.class);
Injector injector = DIBootstrap.createInjector(new ToolsModule(mock(Logger.class)));
DbGeneratorTask task = new DbGeneratorTask();
DbAdapter autoAdapter = task.getAdapter(injector, ds);
assertTrue(autoAdapter instanceof AutoAdapter);
task.setAdapter(SQLServerAdapter.class.getName());
DbAdapter sqlServerAdapter = task.getAdapter(injector, ds);
assertTrue(sqlServerAdapter instanceof SQLServerAdapter);
}
Aggregations