use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class EJBQLPathTranslator method processTerminatingAttribute.
protected void processTerminatingAttribute(ObjAttribute attribute) {
DbEntity table = null;
Iterator<?> it = attribute.getDbPathIterator();
while (it.hasNext()) {
Object pathComponent = it.next();
if (pathComponent instanceof DbAttribute) {
table = (DbEntity) ((DbAttribute) pathComponent).getEntity();
}
}
if (isUsingAliases()) {
String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
context.append(' ').append(alias).append('.').append(context.getQuotingStrategy().quotedName(attribute.getDbAttribute()));
} else {
context.append(' ').append(context.getQuotingStrategy().quotedName(attribute.getDbAttribute()));
}
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class EJBQLPathTranslator method processTerminatingRelationship.
protected void processTerminatingRelationship(ObjRelationship relationship) {
if (relationship.isSourceIndependentFromTargetChange()) {
// (andrus) use an outer join for to-many matches.. This is somewhat
// different
// from traditional Cayenne SelectQuery, as EJBQL spec does not
// allow regular
// path matches done against to-many relationships, and instead
// provides
// MEMBER OF and IS EMPTY operators. Outer join is needed for IS
// EMPTY... I
// guess MEMBER OF could've been done with an inner join though..
this.innerJoin = false;
resolveJoin();
DbRelationship dbRelationship = chooseDbRelationship(relationship);
DbEntity table = (DbEntity) dbRelationship.getTargetEntity();
String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
Collection<DbAttribute> pks = table.getPrimaryKeys();
if (pks.size() == 1) {
DbAttribute pk = pks.iterator().next();
context.append(' ');
if (isUsingAliases()) {
context.append(alias).append('.');
}
context.append(context.getQuotingStrategy().quotedName(pk));
} else {
throw new EJBQLException("Multi-column PK to-many matches are not yet supported.");
}
} else {
// match FK against the target object
DbRelationship dbRelationship = chooseDbRelationship(relationship);
DbEntity table = (DbEntity) dbRelationship.getSourceEntity();
String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
List<DbJoin> joins = dbRelationship.getJoins();
if (joins.size() == 1) {
DbJoin join = joins.get(0);
context.append(' ');
if (isUsingAliases()) {
context.append(alias).append('.');
}
context.append(context.getQuotingStrategy().quotedName(join.getSource()));
} else {
Map<String, String> multiColumnMatch = new HashMap<>(joins.size() + 2);
for (DbJoin join : joins) {
String column = isUsingAliases() ? alias + "." + join.getSourceName() : join.getSourceName();
multiColumnMatch.put(join.getTargetName(), column);
}
appendMultiColumnPath(EJBQLMultiColumnOperand.getPathOperand(context, multiColumnMatch));
}
}
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class SetAllowNullToDbIT method test.
@Test
public void test() throws Exception {
DbEntity dbEntity = map.getDbEntity("PAINTING");
assertNotNull(dbEntity);
// create and add new column to model and db
DbAttribute column = new DbAttribute("NEWCOL2", Types.VARCHAR, dbEntity);
try {
column.setMandatory(true);
column.setMaxLength(10);
dbEntity.addAttribute(column);
assertTokensAndExecute(2, 0);
// check that is was merged
assertTokensAndExecute(0, 0);
// set null
column.setMandatory(false);
// merge to db
assertTokensAndExecute(1, 0);
// check that is was merged
assertTokensAndExecute(0, 0);
// clean up
} finally {
dbEntity.removeAttribute(column.getName());
assertTokensAndExecute(1, 0);
assertTokensAndExecute(0, 0);
}
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class SetGeneratedFlagToDbIT method dropGeneratedFlag.
@Test
public void dropGeneratedFlag() throws Exception {
DbEntity dbEntity = createTestTable(true);
assertNotNull(dbEntity);
DbAttribute attribute = dbEntity.getAttribute("ID");
assertNotNull(attribute);
assertTrue(attribute.isGenerated());
attribute.setGenerated(false);
List<MergerToken> tokens = createMergeTokens();
if (!dbAdapter.supportsGeneratedKeys()) {
assertEquals(0, tokens.size());
return;
}
assertEquals(1, tokens.size());
MergerToken token = tokens.get(0);
assertTrue(token instanceof SetGeneratedFlagToDb);
try {
execute(token);
if (!dbAdapter.supportsGeneratedKeysDrop()) {
fail("SetGeneratedFlagToDb should fail on current DB");
}
} catch (UnsupportedOperationException ignored) {
return;
}
assertTokensAndExecute(0, 0);
}
use of org.apache.cayenne.map.DbAttribute in project cayenne by apache.
the class SetNotNullToDbIT method test.
@Test
public void test() throws Exception {
DbEntity dbEntity = map.getDbEntity("PAINTING");
assertNotNull(dbEntity);
// create and add new column to model and db
DbAttribute column = new DbAttribute("NEWCOL2", Types.VARCHAR, dbEntity);
column.setMandatory(false);
column.setMaxLength(10);
dbEntity.addAttribute(column);
assertTokensAndExecute(1, 0);
// check that is was merged
assertTokensAndExecute(0, 0);
// set not null
column.setMandatory(true);
// merge to db
assertTokensAndExecute(1, 0);
// check that is was merged
assertTokensAndExecute(0, 0);
// clean up
dbEntity.removeAttribute(column.getName());
assertTokensAndExecute(1, 0);
assertTokensAndExecute(0, 0);
}
Aggregations