Search in sources :

Example 41 with QuotingStrategy

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();
        }
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Example 42 with QuotingStrategy

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(")");
}
Also used : DbJoin(org.apache.cayenne.map.DbJoin) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy)

Aggregations

QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)42 DbAdapter (org.apache.cayenne.dba.DbAdapter)11 DbAttribute (org.apache.cayenne.map.DbAttribute)11 DbEntity (org.apache.cayenne.map.DbEntity)5 DbJoin (org.apache.cayenne.map.DbJoin)5 DbRelationship (org.apache.cayenne.map.DbRelationship)5 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)4 SetAllowNullToDb (org.apache.cayenne.dbsync.merge.token.db.SetAllowNullToDb)3 SetNotNullToDb (org.apache.cayenne.dbsync.merge.token.db.SetNotNullToDb)3 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DropColumnToDb (org.apache.cayenne.dbsync.merge.token.db.DropColumnToDb)2 EJBQLPath (org.apache.cayenne.ejbql.parser.EJBQLPath)2 DataMap (org.apache.cayenne.map.DataMap)2 ObjRelationship (org.apache.cayenne.map.ObjRelationship)2 UpdateBatchQuery (org.apache.cayenne.query.UpdateBatchQuery)2 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)2