Search in sources :

Example 1 with DataNode

use of org.apache.cayenne.access.DataNode in project cayenne by apache.

the class ServerRuntime method getDataSource.

/**
 * Provides access to the JDBC DataSource assigned to a given DataNode. A
 * null argument will work if there's only one DataNode configured.
 * <p>
 * Normally Cayenne applications don't need to access DataSource or any
 * other JDBC code directly, however in some unusual conditions it may be
 * needed, and this method provides a shortcut to raw JDBC.
 */
public DataSource getDataSource(String dataNodeName) {
    DataDomain domain = getDataDomain();
    if (dataNodeName == null) {
        return getDataSource();
    }
    DataNode node = domain.getDataNode(dataNodeName);
    if (node == null) {
        throw new IllegalArgumentException("Unknown DataNode name: " + dataNodeName);
    }
    return node.getDataSource();
}
Also used : DataDomain(org.apache.cayenne.access.DataDomain) DataNode(org.apache.cayenne.access.DataNode)

Example 2 with DataNode

use of org.apache.cayenne.access.DataNode in project cayenne by apache.

the class ServerRuntime method getDataSource.

/**
 * Returns a default DataSource for this runtime. If no default DataSource
 * exists, an exception is thrown.
 *
 * @since 4.0
 */
public DataSource getDataSource() {
    DataDomain domain = getDataDomain();
    DataNode defaultNode = domain.getDefaultNode();
    if (defaultNode == null) {
        int s = domain.getDataNodes().size();
        if (s == 0) {
            throw new IllegalStateException("No DataSources configured");
        } else {
            throw new IllegalArgumentException("No default DataSource configured. You can get explicitly named DataSource by using 'getDataSource(String)'");
        }
    }
    return defaultNode.getDataSource();
}
Also used : DataDomain(org.apache.cayenne.access.DataDomain) DataNode(org.apache.cayenne.access.DataNode)

Example 3 with DataNode

use of org.apache.cayenne.access.DataNode in project cayenne by apache.

the class DefaultDataNodeFactory method createDataNode.

@Override
public DataNode createDataNode(DataNodeDescriptor nodeDescriptor) throws Exception {
    DataNode dataNode = new DataNode(nodeDescriptor.getName());
    dataNode.setJdbcEventLogger(jdbcEventLogger);
    dataNode.setRowReaderFactory(rowReaderFactory);
    dataNode.setBatchTranslatorFactory(batchTranslatorFactory);
    dataNode.setSelectTranslatorFactory(selectTranslatorFactory);
    dataNode.setSqlTemplateProcessor(sqlTemplateProcessor);
    DataSource dataSource = dataSourceFactory.getDataSource(nodeDescriptor);
    dataNode.setDataSourceFactory(nodeDescriptor.getDataSourceFactoryType());
    dataNode.setDataSource(dataSource);
    dataNode.setSchemaUpdateStrategy(schemaUpdateStrategyFactory.create(nodeDescriptor));
    dataNode.setAdapter(adapterFactory.createAdapter(nodeDescriptor, dataSource));
    return dataNode;
}
Also used : DataNode(org.apache.cayenne.access.DataNode) DataSource(javax.sql.DataSource)

Example 4 with DataNode

use of org.apache.cayenne.access.DataNode in project cayenne by apache.

the class ServerCaseDataDomainProvider method createAndInitDataDomain.

@Override
protected DataDomain createAndInitDataDomain() throws Exception {
    DataDomain domain = super.createAndInitDataDomain();
    DataNode node = null;
    for (DataMap dataMap : domain.getDataMaps()) {
        // add nodes and DataSources dynamically...
        DataNodeDescriptor descriptor = new DataNodeDescriptor(dataMap.getName());
        node = dataNodeFactory.createDataNode(descriptor);
        node.addDataMap(dataMap);
        // tweak procedures for testing...
        for (Procedure proc : dataMap.getProcedures()) {
            unitDbAdapter.tweakProcedure(proc);
        }
        // customizations from SimpleAccessStackAdapter that are not yet
        // ported...
        // those can be done better now
        // node
        // .getAdapter()
        // .getExtendedTypes()
        // .registerType(new StringET1ExtendedType());
        // 
        domain.addNode(node);
    }
    if (domain.getDataMaps().size() == 1) {
        domain.setDefaultNode(node);
    }
    return domain;
}
Also used : DataDomain(org.apache.cayenne.access.DataDomain) DataNode(org.apache.cayenne.access.DataNode) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) Procedure(org.apache.cayenne.map.Procedure) DataMap(org.apache.cayenne.map.DataMap)

Example 5 with DataNode

use of org.apache.cayenne.access.DataNode in project cayenne by apache.

the class TokensToModelExecutionTest method testCreateAndDropTable.

@Test
public void testCreateAndDropTable() throws Exception {
    DbEntity entity = dbEntity().build();
    DataMap dataMap = dataMap().build();
    assertTrue(dataMap.getDbEntityMap().isEmpty());
    assertTrue(dataMap.getObjEntityMap().isEmpty());
    MergerContext context = MergerContext.builder(dataMap).dataNode(new DataNode()).build();
    new DefaultMergerTokenFactory().createCreateTableToModel(entity).execute(context);
    assertEquals(1, dataMap.getDbEntityMap().size());
    assertEquals(1, dataMap.getObjEntities().size());
    assertEquals(entity, dataMap.getDbEntity(entity.getName()));
    new DefaultMergerTokenFactory().createDropTableToModel(entity).execute(context);
    assertTrue(dataMap.getDbEntityMap().isEmpty());
    assertTrue(dataMap.getObjEntityMap().isEmpty());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DataNode(org.apache.cayenne.access.DataNode) DefaultMergerTokenFactory(org.apache.cayenne.dbsync.merge.factory.DefaultMergerTokenFactory) MergerContext(org.apache.cayenne.dbsync.merge.context.MergerContext) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Aggregations

DataNode (org.apache.cayenne.access.DataNode)23 Test (org.junit.Test)12 DataMap (org.apache.cayenne.map.DataMap)8 RowReaderFactory (org.apache.cayenne.access.jdbc.reader.RowReaderFactory)7 DataDomain (org.apache.cayenne.access.DataDomain)6 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)6 EntityResolver (org.apache.cayenne.map.EntityResolver)6 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)4 DbEntity (org.apache.cayenne.map.DbEntity)4 InsertBatchQuery (org.apache.cayenne.query.InsertBatchQuery)4 SkipSchemaUpdateStrategy (org.apache.cayenne.access.dbsync.SkipSchemaUpdateStrategy)3 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)3 PreparedStatementResultSetHandler (com.mockrunner.jdbc.PreparedStatementResultSetHandler)2 MockConnection (com.mockrunner.mock.jdbc.MockConnection)2 HashMap (java.util.HashMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 DataRowStoreFactory (org.apache.cayenne.access.DataRowStoreFactory)2 MockOperationObserver (org.apache.cayenne.access.MockOperationObserver)2 DefaultBatchTranslatorFactory (org.apache.cayenne.access.translator.batch.DefaultBatchTranslatorFactory)2 DeleteBatchTranslator (org.apache.cayenne.access.translator.batch.DeleteBatchTranslator)2