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();
}
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();
}
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;
}
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;
}
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());
}
Aggregations