Search in sources :

Example 81 with DbRelationship

use of org.apache.cayenne.map.DbRelationship in project cayenne by apache.

the class EJBQLDbPathTranslator method processLastPathComponent.

private void processLastPathComponent() {
    DbAttribute attribute = currentEntity.getAttribute(lastPathComponent);
    if (attribute != null) {
        processTerminatingAttribute(attribute);
        return;
    }
    DbRelationship relationship = currentEntity.getRelationship(lastPathComponent);
    if (relationship != null) {
        processTerminatingRelationship(relationship);
        return;
    }
    throw new IllegalStateException("Invalid path component: " + lastPathComponent);
}
Also used : DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 82 with DbRelationship

use of org.apache.cayenne.map.DbRelationship 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 83 with DbRelationship

use of org.apache.cayenne.map.DbRelationship in project cayenne by apache.

the class EJBQLPathTranslator method chooseDbRelationship.

/**
 * Checks if the object relationship is flattened and then chooses the
 * corresponding db relationship. The last in idPath if isFlattened and the
 * first in list otherwise.
 *
 * @param relationship
 *            the object relationship
 *
 * @return {@link DbRelationship}
 */
protected DbRelationship chooseDbRelationship(ObjRelationship relationship) {
    List<DbRelationship> dbRelationships = relationship.getDbRelationships();
    String dbRelationshipPath = relationship.getDbRelationshipPath();
    if (dbRelationshipPath.contains(".")) {
        String dbRelName = dbRelationshipPath.substring(dbRelationshipPath.lastIndexOf(".") + 1);
        for (DbRelationship dbR : dbRelationships) {
            if (dbR.getName().equals(dbRelName)) {
                return dbR;
            }
        }
    }
    return relationship.getDbRelationships().get(0);
}
Also used : DbRelationship(org.apache.cayenne.map.DbRelationship)

Example 84 with DbRelationship

use of org.apache.cayenne.map.DbRelationship in project cayenne by apache.

the class EJBQLSelectColumnsTranslator method visitDbPath.

@Override
public boolean visitDbPath(EJBQLExpression expression, int finishedChildIndex) {
    EJBQLDbPathTranslator pathTranslator = new EJBQLDbPathTranslator(context) {

        @Override
        protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
            throw new EJBQLException("Can't use multi-column paths in column clause");
        }

        @Override
        protected void processTerminatingRelationship(DbRelationship relationship) {
            Map<String, String> xfields = null;
            if (context.isAppendingResultColumns()) {
                xfields = context.nextEntityResult().getFields();
            }
            final Map<String, String> fields = xfields;
            DbEntity table = (DbEntity) relationship.getTargetEntity();
            Collection<DbAttribute> dbAttr = table.getAttributes();
            Iterator<DbAttribute> it = dbAttr.iterator();
            if (dbAttr.size() > 0) {
                resolveJoin();
            }
            String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
            boolean first = true;
            while (it.hasNext()) {
                context.append(!first ? ", " : " ");
                DbAttribute dbAttribute = it.next();
                appendColumn(TypesMapping.getJavaBySqlType(dbAttribute.getType()), alias, dbAttribute, fields != null ? fields.get(dbAttribute.getName()) : "");
                first = false;
            }
        }

        @Override
        protected void processTerminatingAttribute(DbAttribute attribute) {
            String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(currentEntity));
            appendColumn(TypesMapping.getJavaBySqlType(attribute.getType()), alias, attribute, context.isAppendingResultColumns() ? context.nextColumnAlias() : "");
        }
    };
    expression.visit(pathTranslator);
    return false;
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbAttribute(org.apache.cayenne.map.DbAttribute)

Example 85 with DbRelationship

use of org.apache.cayenne.map.DbRelationship in project cayenne by apache.

the class EJBQLSelectColumnsTranslator method visitPath.

@Override
public boolean visitPath(EJBQLExpression expression, int finishedChildIndex) {
    EJBQLPathTranslator pathTranslator = new EJBQLPathTranslator(context) {

        @Override
        protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
            throw new EJBQLException("Can't use multi-column paths in column clause");
        }

        @Override
        protected void processTerminatingRelationship(ObjRelationship relationship) {
            Map<String, String> xfields = null;
            if (context.isAppendingResultColumns()) {
                xfields = context.nextEntityResult().getFields();
            }
            final Map<String, String> fields = xfields;
            Collection<DbAttribute> dbAttr = ((ObjEntity) relationship.getTargetEntity()).getDbEntity().getAttributes();
            DbRelationship dbRelationship = relationship.getDbRelationships().get(0);
            DbEntity table = (DbEntity) dbRelationship.getTargetEntity();
            Iterator<DbAttribute> it = dbAttr.iterator();
            if (dbAttr.size() > 0) {
                resolveJoin();
            }
            String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
            boolean first = true;
            while (it.hasNext()) {
                context.append(!first ? ", " : " ");
                DbAttribute dbAttribute = it.next();
                appendColumn(TypesMapping.getJavaBySqlType(dbAttribute.getType()), alias, dbAttribute, fields != null ? fields.get(dbAttribute.getName()) : "");
                first = false;
            }
        }

        @Override
        protected void processTerminatingAttribute(ObjAttribute attribute) {
            DbEntity table = currentEntity.getDbEntity();
            String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
            if (attribute.isFlattened()) {
                Iterator<?> dbPathIterator = attribute.getDbPathIterator();
                EJBQLTableId lhsId = new EJBQLTableId(idPath);
                while (dbPathIterator.hasNext()) {
                    Object pathPart = dbPathIterator.next();
                    // later when appending table
                    if (pathPart == null) {
                        throw new CayenneRuntimeException("ObjAttribute has no component: %s", attribute.getName());
                    } else if (pathPart instanceof DbAttribute) {
                        DbAttribute dbAttribute = (DbAttribute) pathPart;
                        appendColumn(attribute.getType(), context.getTableAlias(lhsId.getEntityId(), context.getQuotingStrategy().quotedFullyQualifiedName((DbEntity) dbAttribute.getEntity())), dbAttribute, context.isAppendingResultColumns() ? context.nextColumnAlias() : "");
                    }
                }
            } else {
                DbAttribute dbAttribute = attribute.getDbAttribute();
                appendColumn(attribute.getType(), alias, dbAttribute, context.isAppendingResultColumns() ? context.nextColumnAlias() : "");
            }
        }
    };
    expression.visit(pathTranslator);
    return false;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship)

Aggregations

DbRelationship (org.apache.cayenne.map.DbRelationship)106 DbEntity (org.apache.cayenne.map.DbEntity)59 DbAttribute (org.apache.cayenne.map.DbAttribute)35 DbJoin (org.apache.cayenne.map.DbJoin)35 ObjEntity (org.apache.cayenne.map.ObjEntity)30 ObjRelationship (org.apache.cayenne.map.ObjRelationship)28 ObjAttribute (org.apache.cayenne.map.ObjAttribute)20 Test (org.junit.Test)15 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)13 ArrayList (java.util.ArrayList)11 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)10 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)9 DataMap (org.apache.cayenne.map.DataMap)9 Entity (org.apache.cayenne.map.Entity)8 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)8 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)8 HashMap (java.util.HashMap)6 ObjectId (org.apache.cayenne.ObjectId)6