use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class MergerOptions method prepareMigrator.
/**
* check database and create the {@link List} of {@link MergerToken}s
*/
protected void prepareMigrator() {
try {
adapter = connectionInfo.makeAdapter(getApplication().getClassLoadingService());
MergerTokenFactory mergerTokenFactory = mergerTokenFactoryProvider.get(adapter);
tokens.setMergerTokenFactory(mergerTokenFactory);
FiltersConfig filters = FiltersConfig.create(defaultCatalog, defaultSchema, TableFilter.everything(), PatternFilter.INCLUDE_NOTHING);
DataMapMerger merger = DataMapMerger.builder(mergerTokenFactory).filters(filters).build();
DbLoaderConfiguration config = new DbLoaderConfiguration();
config.setFiltersConfig(filters);
DataSource dataSource = connectionInfo.makeDataSource(getApplication().getClassLoadingService());
DataMap dbImport;
try (Connection conn = dataSource.getConnection()) {
dbImport = new DbLoader(adapter, conn, config, new LoggingDbLoaderDelegate(LoggerFactory.getLogger(DbLoader.class)), new DefaultObjectNameGenerator(NoStemStemmer.getInstance())).load();
} catch (SQLException e) {
throw new CayenneRuntimeException("Can't doLoad dataMap from db.", e);
}
tokens.setTokens(merger.createMergeTokens(dataMap, dbImport));
} catch (Exception ex) {
reportError("Error loading adapter", ex);
}
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class UnitDbAdapter method ddlString.
/**
* Returns a file under test resources DDL directory for the specified
* database.
*/
private String ddlString(String database, String name) {
StringBuffer location = new StringBuffer();
location.append("ddl/").append(database).append("/").append(name);
InputStream resource = Thread.currentThread().getContextClassLoader().getResourceAsStream(location.toString());
if (resource == null) {
throw new CayenneRuntimeException("Can't find DDL file: " + location);
}
BufferedReader in = new BufferedReader(new InputStreamReader(resource));
StringBuffer buf = new StringBuffer();
try {
String line = null;
while ((line = in.readLine()) != null) {
buf.append(line).append('\n');
}
} catch (IOException e) {
throw new CayenneRuntimeException("Error reading DDL file: " + location);
} finally {
try {
in.close();
} catch (IOException e) {
}
}
return buf.toString();
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class UnitDbAdapterProvider method get.
public UnitDbAdapter get() throws ConfigurationException {
String testAdapterType = adapterTypesMap.get(dataSourceInfo.getAdapterClassName());
if (testAdapterType == null) {
throw new IllegalStateException("Unmapped adapter type: " + dataSourceInfo.getAdapterClassName());
}
Class<UnitDbAdapter> type;
try {
type = (Class<UnitDbAdapter>) Util.getJavaClass(testAdapterType);
} catch (ClassNotFoundException e) {
throw new CayenneRuntimeException("Invalid class %s of type AccessStackAdapter", e, testAdapterType);
}
if (!UnitDbAdapter.class.isAssignableFrom(type)) {
throw new CayenneRuntimeException("Class %s is not assignable to AccessStackAdapter", testAdapterType);
}
try {
Constructor<UnitDbAdapter> c = type.getConstructor(DbAdapter.class);
UnitDbAdapter unitAdapter = c.newInstance(adapter);
injector.injectMembers(unitAdapter);
return unitAdapter;
} catch (Exception e) {
throw new ConfigurationException("Error instantiating " + testAdapterType, e);
}
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class SchemaUpdateStrategyBase method tablesMap.
protected Map<String, Boolean> tablesMap() {
DataMap map = node.getEntityResolver().getDataMap("sus-map");
Map<String, String> tables = new HashMap<>();
// add upper/lower case permutations
for (String name : map.getDbEntityMap().keySet()) {
tables.put(name.toLowerCase(), name);
tables.put(name.toUpperCase(), name);
}
Map<String, Boolean> presentInDB = new HashMap<>();
for (String name : map.getDbEntityMap().keySet()) {
presentInDB.put(name, false);
}
String tableLabel = node.getAdapter().tableTypeForTable();
try (Connection con = node.getDataSource().getConnection()) {
try (ResultSet rs = con.getMetaData().getTables(null, null, "%", new String[] { tableLabel })) {
while (rs.next()) {
String dbName = rs.getString("TABLE_NAME");
String name = tables.get(dbName);
if (name != null) {
presentInDB.put(name, true);
}
}
}
} catch (SQLException e) {
throw new CayenneRuntimeException(e);
}
return presentInDB;
}
use of org.apache.cayenne.CayenneRuntimeException in project cayenne by apache.
the class ThrowOnPartialOrCreateSchemaStrategyIT method testMixedStrategyWithOneTable.
@Test
public void testMixedStrategyWithOneTable() throws Exception {
createOneTable("SUS1");
setStrategy(ThrowOnPartialOrCreateSchemaStrategy.class);
String template = "SELECT #result('ARTIST_ID' 'int') FROM ARTIST ORDER BY ARTIST_ID";
SQLTemplate query = new SQLTemplate(Object.class, template);
try {
node.performQueries(Collections.singletonList(query), mock(OperationObserver.class));
assertEquals(1, existingTables().size());
fail("Must have thrown on partial schema");
} catch (CayenneRuntimeException e) {
// expected
}
}
Aggregations