Search in sources :

Example 41 with CayenneRuntimeException

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);
    }
}
Also used : LoggingDbLoaderDelegate(org.apache.cayenne.dbsync.reverse.dbload.LoggingDbLoaderDelegate) SQLException(java.sql.SQLException) Connection(java.sql.Connection) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) MergerTokenFactory(org.apache.cayenne.dbsync.merge.factory.MergerTokenFactory) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) SQLException(java.sql.SQLException) IOException(java.io.IOException) DbLoaderConfiguration(org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration) DataSource(javax.sql.DataSource) DataMap(org.apache.cayenne.map.DataMap) DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) DataMapMerger(org.apache.cayenne.dbsync.merge.DataMapMerger) FiltersConfig(org.apache.cayenne.dbsync.reverse.filters.FiltersConfig) DbLoader(org.apache.cayenne.dbsync.reverse.dbload.DbLoader)

Example 42 with CayenneRuntimeException

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();
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 43 with CayenneRuntimeException

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);
    }
}
Also used : ConfigurationException(org.apache.cayenne.ConfigurationException) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) UnitDbAdapter(org.apache.cayenne.unit.UnitDbAdapter) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ConfigurationException(org.apache.cayenne.ConfigurationException)

Example 44 with CayenneRuntimeException

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;
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DataMap(org.apache.cayenne.map.DataMap)

Example 45 with CayenneRuntimeException

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
    }
}
Also used : SQLTemplate(org.apache.cayenne.query.SQLTemplate) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) OperationObserver(org.apache.cayenne.access.OperationObserver) Test(org.junit.Test)

Aggregations

CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)168 Test (org.junit.Test)25 DbAttribute (org.apache.cayenne.map.DbAttribute)21 DataMap (org.apache.cayenne.map.DataMap)19 ObjectId (org.apache.cayenne.ObjectId)18 ObjEntity (org.apache.cayenne.map.ObjEntity)18 Persistent (org.apache.cayenne.Persistent)17 Expression (org.apache.cayenne.exp.Expression)17 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)17 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)14 DbEntity (org.apache.cayenne.map.DbEntity)14 IOException (java.io.IOException)13 List (java.util.List)12 ObjRelationship (org.apache.cayenne.map.ObjRelationship)12 DbRelationship (org.apache.cayenne.map.DbRelationship)10 DateTestEntity (org.apache.cayenne.testdo.date_time.DateTestEntity)10 File (java.io.File)9 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)9