use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class SchemaBuilder method filterDataMap.
/**
* Remote binary pk {@link DbEntity} for {@link DbAdapter} not supporting
* that and so on.
*/
protected void filterDataMap(DataMap map) {
boolean supportsBinaryPK = unitDbAdapter.supportsBinaryPK();
if (supportsBinaryPK) {
return;
}
List<DbEntity> entitiesToRemove = new ArrayList<DbEntity>();
for (DbEntity ent : map.getDbEntities()) {
for (DbAttribute attr : ent.getAttributes()) {
// check for BIN PK or FK to BIN Pk
if (attr.getType() == Types.BINARY || attr.getType() == Types.VARBINARY || attr.getType() == Types.LONGVARBINARY) {
if (attr.isPrimaryKey() || attr.isForeignKey()) {
entitiesToRemove.add(ent);
break;
}
}
}
}
for (DbEntity e : entitiesToRemove) {
map.removeDbEntity(e.getName(), true);
}
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class SchemaBuilder method dbEntitiesFilter.
private void dbEntitiesFilter(List<DbEntity> entities) {
// filter various unsupported tests...
// LOBs
boolean excludeLOB = !unitDbAdapter.supportsLobs();
boolean excludeBinPK = !unitDbAdapter.supportsBinaryPK();
if (excludeLOB || excludeBinPK) {
List<DbEntity> filtered = new ArrayList<DbEntity>();
for (DbEntity ent : entities) {
// check for LOB attributes
if (excludeLOB) {
if (Arrays.binarySearch(EXTRA_EXCLUDED_FOR_NO_LOB, ent.getName()) >= 0) {
continue;
}
boolean hasLob = false;
for (final DbAttribute attr : ent.getAttributes()) {
if (attr.getType() == Types.BLOB || attr.getType() == Types.CLOB) {
hasLob = true;
break;
}
}
if (hasLob) {
continue;
}
}
// check for BIN PK
if (excludeBinPK) {
boolean skip = false;
for (final DbAttribute attr : ent.getAttributes()) {
// check for BIN PK or FK to BIN Pk
if (attr.getType() == Types.BINARY || attr.getType() == Types.VARBINARY || attr.getType() == Types.LONGVARBINARY) {
if (attr.isPrimaryKey() || attr.isForeignKey()) {
skip = true;
break;
}
}
}
if (skip) {
continue;
}
}
filtered.add(ent);
}
entities = filtered;
}
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class BatchActionLockingIT method testRunAsIndividualQueriesOptimisticLockingFailure.
@Test
public void testRunAsIndividualQueriesOptimisticLockingFailure() throws Exception {
EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
// test with adapter that supports keys...
JdbcAdapter adapter = buildAdapter(true);
DbEntity dbEntity = resolver.getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
List<DbAttribute> qualifierAttributes = Arrays.asList(dbEntity.getAttribute("LOCKING_TEST_ID"), dbEntity.getAttribute("NAME"));
Collection<String> nullAttributeNames = Collections.singleton("NAME");
Map<String, Object> qualifierSnapshot = new HashMap<>();
qualifierSnapshot.put("LOCKING_TEST_ID", 1);
DeleteBatchQuery batchQuery = new DeleteBatchQuery(dbEntity, qualifierAttributes, nullAttributeNames, 5);
batchQuery.setUsingOptimisticLocking(true);
batchQuery.add(qualifierSnapshot);
DeleteBatchTranslator batchQueryBuilder = new DeleteBatchTranslator(batchQuery, adapter, null);
MockConnection mockConnection = new MockConnection();
PreparedStatementResultSetHandler preparedStatementResultSetHandler = mockConnection.getPreparedStatementResultSetHandler();
preparedStatementResultSetHandler.setExactMatch(false);
preparedStatementResultSetHandler.setCaseSensitive(false);
preparedStatementResultSetHandler.prepareUpdateCount("DELETE", 0);
boolean generatesKeys = false;
DataNode node = new DataNode();
node.setAdapter(adapter);
node.setEntityResolver(resolver);
node.setRowReaderFactory(mock(RowReaderFactory.class));
BatchAction action = new BatchAction(batchQuery, node, false);
try {
action.runAsIndividualQueries(mockConnection, batchQueryBuilder, new MockOperationObserver(), generatesKeys);
fail("No OptimisticLockingFailureException thrown.");
} catch (OptimisticLockException e) {
}
assertEquals(0, mockConnection.getNumberCommits());
assertEquals(0, mockConnection.getNumberRollbacks());
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class BatchActionLockingIT method testRunAsIndividualQueriesSuccess.
@Test
public void testRunAsIndividualQueriesSuccess() throws Exception {
EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
// test with adapter that supports keys...
JdbcAdapter adapter = buildAdapter(true);
DbEntity dbEntity = resolver.getObjEntity(SimpleLockingTestEntity.class).getDbEntity();
List<DbAttribute> qualifierAttributes = Arrays.asList(dbEntity.getAttribute("LOCKING_TEST_ID"), dbEntity.getAttribute("NAME"));
Collection<String> nullAttributeNames = Collections.singleton("NAME");
Map<String, Object> qualifierSnapshot = new HashMap<>();
qualifierSnapshot.put("LOCKING_TEST_ID", new Integer(1));
DeleteBatchQuery batchQuery = new DeleteBatchQuery(dbEntity, qualifierAttributes, nullAttributeNames, 5);
batchQuery.setUsingOptimisticLocking(true);
batchQuery.add(qualifierSnapshot);
DeleteBatchTranslator batchQueryBuilder = new DeleteBatchTranslator(batchQuery, adapter, null);
MockConnection mockConnection = new MockConnection();
PreparedStatementResultSetHandler preparedStatementResultSetHandler = mockConnection.getPreparedStatementResultSetHandler();
preparedStatementResultSetHandler.setExactMatch(false);
preparedStatementResultSetHandler.setCaseSensitive(false);
preparedStatementResultSetHandler.prepareUpdateCount("DELETE", 1);
boolean generatesKeys = false;
DataNode node = new DataNode();
node.setAdapter(adapter);
node.setEntityResolver(resolver);
node.setRowReaderFactory(mock(RowReaderFactory.class));
BatchAction action = new BatchAction(batchQuery, node, false);
action.runAsIndividualQueries(mockConnection, batchQueryBuilder, new MockOperationObserver(), generatesKeys);
assertEquals(0, mockConnection.getNumberCommits());
assertEquals(0, mockConnection.getNumberRollbacks());
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class DefaultBatchTranslatorIT method testAppendDbAttribute1.
@Test
public void testAppendDbAttribute1() throws Exception {
DbAdapter adapter = objectFactory.newInstance(DbAdapter.class, JdbcAdapter.class.getName());
String trimFunction = "testTrim";
DefaultBatchTranslator builder = new DefaultBatchTranslator(mock(BatchQuery.class), adapter, trimFunction) {
@Override
protected String createSql() {
return null;
}
@Override
protected DbAttributeBinding[] createBindings() {
return new DbAttributeBinding[0];
}
@Override
protected DbAttributeBinding[] doUpdateBindings(BatchQueryRow row) {
return new DbAttributeBinding[0];
}
};
StringBuilder buf = new StringBuilder();
DbEntity entity = new DbEntity("Test");
DbAttribute attr = new DbAttribute("testAttr", Types.CHAR, null);
attr.setEntity(entity);
builder.appendDbAttribute(buf, attr);
assertEquals("testTrim(testAttr)", buf.toString());
buf = new StringBuilder();
attr = new DbAttribute("testAttr", Types.VARCHAR, null);
attr.setEntity(entity);
builder.appendDbAttribute(buf, attr);
assertEquals("testAttr", buf.toString());
}
Aggregations