Search in sources :

Example 11 with DbEntity

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

the class EJBQLDbPathTranslator method processTerminatingAttribute.

protected void processTerminatingAttribute(DbAttribute attribute) {
    DbEntity table = (DbEntity) attribute.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));
    } else {
        context.append(' ').append(context.getQuotingStrategy().quotedName(attribute));
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity)

Example 12 with DbEntity

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

the class EJBQLDbPathTranslator method processTerminatingRelationship.

protected void processTerminatingRelationship(DbRelationship relationship) {
    if (relationship.isToMany()) {
        // use an outer join for to-many matches
        resolveJoin(false);
        DbEntity table = (DbEntity) relationship.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
        DbEntity table = (DbEntity) relationship.getSourceEntity();
        String alias = this.lastAlias != null ? lastAlias : context.getTableAlias(idPath, context.getQuotingStrategy().quotedFullyQualifiedName(table));
        List<DbJoin> joins = relationship.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));
        }
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) HashMap(java.util.HashMap) DbAttribute(org.apache.cayenne.map.DbAttribute) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbJoin(org.apache.cayenne.map.DbJoin)

Example 13 with DbEntity

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

the class EJBQLDbPathTranslator method resolveJoin.

protected void resolveJoin() {
    EJBQLJoinAppender joinAppender = context.getTranslatorFactory().getJoinAppender(context);
    String newPath = idPath + '.' + lastPathComponent;
    String oldPath = joinAppender.registerReusableJoin(idPath, lastPathComponent, newPath);
    this.fullPath = fullPath + '.' + lastPathComponent;
    if (oldPath != null) {
        this.idPath = oldPath;
        DbRelationship lastRelationship = currentEntity.getRelationship(lastPathComponent);
        if (lastRelationship != null) {
            DbEntity targetEntity = lastRelationship.getTargetEntity();
            this.lastAlias = context.getTableAlias(fullPath, context.getQuotingStrategy().quotedFullyQualifiedName(targetEntity));
        } else {
            String tableName = context.getQuotingStrategy().quotedFullyQualifiedName(currentEntity);
            this.lastAlias = context.getTableAlias(oldPath, tableName);
        }
    } else {
        DbRelationship lastRelationship = currentEntity.getRelationship(lastPathComponent);
        DbEntity targetEntity = null;
        if (lastRelationship != null) {
            targetEntity = lastRelationship.getTargetEntity();
        } else {
            targetEntity = currentEntity;
        }
        // register join
        joinAppender.appendInnerJoin(joinMarker, new EJBQLTableId(idPath), new EJBQLTableId(fullPath));
        // TODO: outer joins handling
        this.lastAlias = context.getTableAlias(fullPath, context.getQuotingStrategy().quotedFullyQualifiedName(targetEntity));
        this.idPath = newPath;
    }
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship)

Example 14 with DbEntity

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

the class EJBQLGroupByTranslator method visitPath.

@Override
public boolean visitPath(EJBQLExpression expression, int finishedChildIndex) {
    if (itemCount++ > 0) {
        context.append(',');
    }
    EJBQLExpressionVisitor childVisitor = new EJBQLPathTranslator(context) {

        @Override
        protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
            throw new EJBQLException("Can't GROUP BY on multi-column paths or objects");
        }

        @Override
        public boolean visitIdentificationVariable(EJBQLExpression expression) {
            String idVariableAbsolutePath = fullPath + "." + expression.getText();
            ClassDescriptor descriptor = context.getEntityDescriptor(idVariableAbsolutePath);
            if (descriptor != null) {
                this.lastAlias = context.getTableAlias(idVariableAbsolutePath, context.getQuotingStrategy().quotedFullyQualifiedName(descriptor.getEntity().getDbEntity()));
            }
            resolveLastPathComponent(expression.getText());
            this.fullPath = fullPath + '.' + lastPathComponent;
            return true;
        }

        @Override
        protected void processTerminatingRelationship(ObjRelationship relationship) {
            Collection<DbAttribute> dbAttr = ((ObjEntity) relationship.getTargetEntity()).getDbEntity().getAttributes();
            DbRelationship dbRelationship = relationship.getDbRelationships().get(0);
            DbEntity table = (DbEntity) dbRelationship.getTargetEntity();
            Iterator<DbAttribute> it = dbAttr.iterator();
            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();
                context.append(alias).append('.').append(context.getQuotingStrategy().quotedName(dbAttribute));
                first = false;
            }
        }
    };
    expression.visit(childVisitor);
    return false;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) DbAttribute(org.apache.cayenne.map.DbAttribute) EJBQLExpressionVisitor(org.apache.cayenne.ejbql.EJBQLExpressionVisitor) DbEntity(org.apache.cayenne.map.DbEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) EJBQLExpression(org.apache.cayenne.ejbql.EJBQLExpression)

Example 15 with DbEntity

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

the class EJBQLIdentifierColumnsTranslator method addPrefetchedColumnsIfAny.

private void addPrefetchedColumnsIfAny(final String visitedIdentifier) {
    PrefetchTreeNode prefetchTree = context.getCompiledExpression().getPrefetchTree();
    if (prefetchTree != null) {
        for (PrefetchTreeNode prefetch : prefetchTree.adjacentJointNodes()) {
            ClassDescriptor descriptor = context.getEntityDescriptor(prefetch.getEjbqlPathEntityId());
            if (visitedIdentifier.equals(prefetch.getEjbqlPathEntityId())) {
                DbEntity table = descriptor.getRootDbEntities().iterator().next();
                ObjEntity objectEntity = descriptor.getEntity();
                prefetch.setEntityName(objectEntity.getName());
                Expression prefetchExp = ExpressionFactory.exp(prefetch.getPath());
                Expression dbPrefetch = objectEntity.translateToDbPath(prefetchExp);
                DbRelationship r = null;
                for (PathComponent<DbAttribute, DbRelationship> component : table.resolvePath(dbPrefetch, context.getMetadata().getPathSplitAliases())) {
                    r = component.getRelationship();
                }
                if (r == null) {
                    throw new CayenneRuntimeException("Invalid joint prefetch '%s' for entity: %s", prefetch, objectEntity.getName());
                }
                for (DbAttribute attribute : r.getTargetEntity().getAttributes()) {
                    appendColumn(prefetch.getEjbqlPathEntityId() + "." + prefetch.getPath(), attribute, "", prefetch.getPath() + "." + attribute.getName(), null);
                }
            }
        }
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) DbEntity(org.apache.cayenne.map.DbEntity) EJBQLExpression(org.apache.cayenne.ejbql.EJBQLExpression) Expression(org.apache.cayenne.exp.Expression) PrefetchTreeNode(org.apache.cayenne.query.PrefetchTreeNode) DbRelationship(org.apache.cayenne.map.DbRelationship) DbAttribute(org.apache.cayenne.map.DbAttribute) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Aggregations

DbEntity (org.apache.cayenne.map.DbEntity)273 DbAttribute (org.apache.cayenne.map.DbAttribute)106 Test (org.junit.Test)106 ObjEntity (org.apache.cayenne.map.ObjEntity)64 DbRelationship (org.apache.cayenne.map.DbRelationship)55 DataMap (org.apache.cayenne.map.DataMap)47 ObjAttribute (org.apache.cayenne.map.ObjAttribute)26 ArrayList (java.util.ArrayList)25 DbJoin (org.apache.cayenne.map.DbJoin)24 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)20 ObjRelationship (org.apache.cayenne.map.ObjRelationship)19 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)16 JdbcAdapter (org.apache.cayenne.dba.JdbcAdapter)16 Entity (org.apache.cayenne.map.Entity)16 List (java.util.List)15 DbAdapter (org.apache.cayenne.dba.DbAdapter)15 EntityEvent (org.apache.cayenne.map.event.EntityEvent)14 HashMap (java.util.HashMap)12 SelectQuery (org.apache.cayenne.query.SelectQuery)12 DataChannelDescriptor (org.apache.cayenne.configuration.DataChannelDescriptor)11