use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.
the class EJBQLJoinAppender method appendJoin.
protected void appendJoin(String marker, EJBQLTableId lhsId, EJBQLTableId rhsId, String semantics) {
List<DbRelationship> joinRelationships = context.getIncomingRelationships(rhsId);
if (joinRelationships.isEmpty()) {
throw new EJBQLException("No join configured for id " + rhsId);
}
QuotingStrategy quoter = context.getQuotingStrategy();
DbRelationship incomingDB = joinRelationships.get(0);
// TODO: andrus, 1/6/2008 - move reusable join check here...
DbEntity sourceEntity = incomingDB.getSourceEntity();
String tableName = quoter.quotedFullyQualifiedName(sourceEntity);
String sourceAlias = context.getTableAlias(lhsId.getEntityId(), tableName);
if (marker != null) {
context.pushMarker(marker, false);
}
try {
context.append(" ").append(semantics);
if (joinRelationships.size() > 1) {
// if size of relationship list greater than 1,
// it's a flattened relationship
context.append(" ");
for (int i = 1; i < joinRelationships.size(); i++) {
DbRelationship dbRelationship = joinRelationships.get(i);
String subquerySourceTableName = quoter.quotedFullyQualifiedName(dbRelationship.getSourceEntity());
String subquerySourceAlias = context.getTableAlias(subquerySourceTableName, subquerySourceTableName);
String subqueryTargetTableName = quoter.quotedFullyQualifiedName(dbRelationship.getTargetEntity());
String subqueryTargetAlias;
if (i == joinRelationships.size() - 1) {
// it's the last table alias
subqueryTargetAlias = context.getTableAlias(rhsId.getEntityId(), subqueryTargetTableName);
} else {
subqueryTargetAlias = context.getTableAlias(subqueryTargetTableName, subqueryTargetTableName);
}
if (i == 1) {
// first apply the joins defined in query
context.append(subquerySourceTableName).append(' ').append(subquerySourceAlias);
generateJoiningExpression(incomingDB, sourceAlias, subquerySourceAlias);
}
context.append(" JOIN ");
context.append(subqueryTargetTableName).append(' ').append(subqueryTargetAlias);
generateJoiningExpression(dbRelationship, subquerySourceAlias, subqueryTargetAlias);
}
} else {
// non-flattened relationship
String targetAlias = appendTable(rhsId);
// apply the joins defined in query
generateJoiningExpression(incomingDB, sourceAlias, targetAlias);
}
} finally {
if (marker != null) {
context.popMarker();
}
}
}
use of org.apache.cayenne.dba.QuotingStrategy in project cayenne by apache.
the class EJBQLJoinAppender method generateJoiningExpression.
private void generateJoiningExpression(DbRelationship incomingDB, String sourceAlias, String targetAlias) {
context.append(" ON (");
QuotingStrategy quoter = context.getQuotingStrategy();
Iterator<DbJoin> it = incomingDB.getJoins().iterator();
if (it.hasNext()) {
DbJoin dbJoin = it.next();
context.append(sourceAlias).append('.').append(quoter.quotedSourceName(dbJoin)).append(" = ").append(targetAlias).append('.').append(quoter.quotedTargetName(dbJoin));
}
while (it.hasNext()) {
context.append(" AND ");
DbJoin dbJoin = it.next();
context.append(sourceAlias).append('.').append(quoter.quotedSourceName(dbJoin)).append(" = ").append(targetAlias).append('.').append(quoter.quotedTargetName(dbJoin));
}
context.append(")");
}
Aggregations