Search in sources :

Example 1 with EJBQLPath

use of org.apache.cayenne.ejbql.parser.EJBQLPath in project cayenne by apache.

the class EJBQLPathAnaliserTranslator method visitSize.

@Override
public boolean visitSize(EJBQLExpression expression) {
    if (expression.getChildrenCount() != 1) {
        throw new EJBQLException("SIZE must have exactly one child, got: " + expression.getChildrenCount());
    }
    if (!(expression.getChild(0) instanceof EJBQLPath)) {
        throw new EJBQLException("First child of SIZE must be a collection path, got: " + expression.getChild(1));
    }
    QuotingStrategy quoter = context.getQuotingStrategy();
    EJBQLPath path = (EJBQLPath) expression.getChild(0);
    String id = path.getAbsolutePath();
    String correlatedEntityId = path.getId();
    ClassDescriptor correlatedEntityDescriptor = context.getEntityDescriptor(correlatedEntityId);
    String correlatedTableName = quoter.quotedFullyQualifiedName(correlatedEntityDescriptor.getEntity().getDbEntity());
    String correlatedTableAlias = context.getTableAlias(correlatedEntityId, correlatedTableName);
    String subqueryId = context.createIdAlias(id);
    ClassDescriptor targetDescriptor = context.getEntityDescriptor(subqueryId);
    if (expression.isNegated()) {
        context.append(" NOT");
    }
    context.append(" EXISTS (SELECT 1 FROM ");
    String subqueryTableName = quoter.quotedFullyQualifiedName(targetDescriptor.getEntity().getDbEntity());
    String subqueryRootAlias = context.getTableAlias(subqueryId, subqueryTableName);
    ObjRelationship relationship = correlatedEntityDescriptor.getEntity().getRelationship(path.getRelativePath());
    if (relationship.getDbRelationshipPath().contains(".")) {
        // if the DbRelationshipPath contains '.', the relationship is
        // flattened
        subqueryRootAlias = processFlattenedRelationShip(subqueryRootAlias, relationship);
    } else {
        // not using "AS" to separate table name and alias name - OpenBase
        // doesn't
        // support "AS", and the rest of the databases do not care
        context.append(subqueryTableName).append(' ').append(subqueryRootAlias);
    }
    context.append(" WHERE");
    DbRelationship correlatedJoinRelationship = context.getIncomingRelationships(new EJBQLTableId(id)).get(0);
    Iterator<DbJoin> it = correlatedJoinRelationship.getJoins().iterator();
    while (it.hasNext()) {
        DbJoin join = it.next();
        context.append(' ').append(subqueryRootAlias).append('.').append(join.getTargetName()).append(" = ");
        context.append(correlatedTableAlias).append('.').append(quoter.quotedSourceName(join));
        if (it.hasNext()) {
            context.append(" AND");
        }
    }
    context.append(")");
    return false;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) DbRelationship(org.apache.cayenne.map.DbRelationship) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbJoin(org.apache.cayenne.map.DbJoin) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy) EJBQLPath(org.apache.cayenne.ejbql.parser.EJBQLPath)

Example 2 with EJBQLPath

use of org.apache.cayenne.ejbql.parser.EJBQLPath in project cayenne by apache.

the class EJBQLPathAnaliserTranslator method visitMemberOf.

@Override
public boolean visitMemberOf(EJBQLExpression expression) {
    if (expression.getChildrenCount() != 2) {
        throw new EJBQLException("MEMBER OF must have exactly two children, got: " + expression.getChildrenCount());
    }
    if (!(expression.getChild(1) instanceof EJBQLPath)) {
        throw new EJBQLException("Second child of the MEMBER OF must be a collection path, got: " + expression.getChild(1));
    }
    QuotingStrategy quoter = context.getQuotingStrategy();
    EJBQLPath path = (EJBQLPath) expression.getChild(1);
    // make sure the ID for the path does not overlap with other condition
    // joins...
    String id = path.getAbsolutePath();
    String correlatedEntityId = path.getId();
    ClassDescriptor correlatedEntityDescriptor = context.getEntityDescriptor(correlatedEntityId);
    String correlatedTableName = quoter.quotedFullyQualifiedName(correlatedEntityDescriptor.getEntity().getDbEntity());
    String correlatedTableAlias = context.getTableAlias(correlatedEntityId, correlatedTableName);
    String subqueryId = context.createIdAlias(id);
    ClassDescriptor targetDescriptor = context.getEntityDescriptor(subqueryId);
    if (expression.isNegated()) {
        context.append(" NOT");
    }
    context.append(" EXISTS (SELECT 1 FROM ");
    String subqueryTableName = quoter.quotedFullyQualifiedName(targetDescriptor.getEntity().getDbEntity());
    String subqueryRootAlias = context.getTableAlias(subqueryId, subqueryTableName);
    ObjRelationship relationship = correlatedEntityDescriptor.getEntity().getRelationship(path.getRelativePath());
    if (relationship.getDbRelationshipPath().contains(".")) {
        // if the DbRelationshipPath contains '.', the relationship is
        // flattened
        subqueryRootAlias = processFlattenedRelationShip(subqueryRootAlias, relationship);
    } else {
        // not using "AS" to separate table name and alias name - OpenBase
        // doesn't
        // support "AS", and the rest of the databases do not care
        context.append(subqueryTableName).append(' ').append(subqueryRootAlias);
    }
    context.append(" WHERE");
    DbRelationship correlatedJoinRelationship = context.getIncomingRelationships(new EJBQLTableId(id)).get(0);
    for (DbJoin join : correlatedJoinRelationship.getJoins()) {
        context.append(' ').append(subqueryRootAlias).append('.').append(join.getTargetName()).append(" = ");
        context.append(correlatedTableAlias).append('.').append(quoter.quotedSourceName(join));
        context.append(" AND");
    }
    // translate subquery_root_id = LHS_of_memberof
    EJBQLEquals equals = new EJBQLEquals(-1);
    EJBQLIdentificationVariable identifier = new EJBQLIdentificationVariable(-1);
    identifier.setText(subqueryId);
    equals.jjtAddChild(identifier, 0);
    equals.jjtAddChild((Node) expression.getChild(0), 1);
    equals.visit(this);
    context.append(")");
    return false;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) EJBQLIdentificationVariable(org.apache.cayenne.ejbql.parser.EJBQLIdentificationVariable) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) DbRelationship(org.apache.cayenne.map.DbRelationship) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbJoin(org.apache.cayenne.map.DbJoin) QuotingStrategy(org.apache.cayenne.dba.QuotingStrategy) EJBQLPath(org.apache.cayenne.ejbql.parser.EJBQLPath) EJBQLEquals(org.apache.cayenne.ejbql.parser.EJBQLEquals)

Aggregations

QuotingStrategy (org.apache.cayenne.dba.QuotingStrategy)2 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)2 EJBQLPath (org.apache.cayenne.ejbql.parser.EJBQLPath)2 DbJoin (org.apache.cayenne.map.DbJoin)2 DbRelationship (org.apache.cayenne.map.DbRelationship)2 ObjRelationship (org.apache.cayenne.map.ObjRelationship)2 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)2 EJBQLEquals (org.apache.cayenne.ejbql.parser.EJBQLEquals)1 EJBQLIdentificationVariable (org.apache.cayenne.ejbql.parser.EJBQLIdentificationVariable)1