Search in sources :

Example 1 with ASTPath

use of org.apache.cayenne.exp.parser.ASTPath in project cayenne by apache.

the class PrefetchProcessorJointNode method buildRowMapping.

/**
 * Configures row columns mapping for this node entity.
 */
private void buildRowMapping() {
    final Map<String, ColumnDescriptor> targetSource = new TreeMap<>();
    // build a DB path .. find parent node that terminates the joint group...
    PrefetchTreeNode jointRoot = this;
    while (jointRoot.getParent() != null && !jointRoot.isDisjointPrefetch() && !jointRoot.isDisjointByIdPrefetch()) {
        jointRoot = jointRoot.getParent();
    }
    final String prefix;
    if (jointRoot != this) {
        Expression objectPath = ExpressionFactory.exp(getPath(jointRoot));
        ASTPath translated = (ASTPath) ((PrefetchProcessorNode) jointRoot).getResolver().getEntity().translateToDbPath(objectPath);
        // make sure we do not include "db:" prefix
        prefix = translated.getOperand(0) + ".";
    } else {
        prefix = "";
    }
    if (getParent() != null && !getParent().isPhantom() && getIncoming() != null && !getIncoming().getRelationship().isFlattened()) {
        DbRelationship r = getIncoming().getRelationship().getDbRelationships().get(0);
        for (final DbJoin join : r.getJoins()) {
            appendColumn(targetSource, join.getTargetName(), prefix + join.getTargetName());
        }
    }
    ClassDescriptor descriptor = resolver.getDescriptor();
    descriptor.visitAllProperties(new PropertyVisitor() {

        public boolean visitAttribute(AttributeProperty property) {
            String target = property.getAttribute().getDbAttributePath();
            appendColumn(targetSource, target, prefix + target);
            return true;
        }

        public boolean visitToMany(ToManyProperty property) {
            return visitRelationship(property);
        }

        public boolean visitToOne(ToOneProperty property) {
            return visitRelationship(property);
        }

        private boolean visitRelationship(ArcProperty arc) {
            DbRelationship dbRel = arc.getRelationship().getDbRelationships().get(0);
            for (DbAttribute attribute : dbRel.getSourceAttributes()) {
                String target = attribute.getName();
                appendColumn(targetSource, target, prefix + target);
            }
            return true;
        }
    });
    // append id columns ... (some may have been appended already via relationships)
    for (String pkName : descriptor.getEntity().getPrimaryKeyNames()) {
        appendColumn(targetSource, pkName, prefix + pkName);
    }
    // append inheritance discriminator columns...
    for (ObjAttribute column : descriptor.getDiscriminatorColumns()) {
        String target = column.getDbAttributePath();
        appendColumn(targetSource, target, prefix + target);
    }
    int size = targetSource.size();
    this.rowCapacity = (int) Math.ceil(size / 0.75);
    this.columns = new ColumnDescriptor[size];
    targetSource.values().toArray(columns);
}
Also used : ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjAttribute(org.apache.cayenne.map.ObjAttribute) ColumnDescriptor(org.apache.cayenne.access.jdbc.ColumnDescriptor) DbAttribute(org.apache.cayenne.map.DbAttribute) TreeMap(java.util.TreeMap) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) Expression(org.apache.cayenne.exp.Expression) PrefetchTreeNode(org.apache.cayenne.query.PrefetchTreeNode) DbRelationship(org.apache.cayenne.map.DbRelationship) DbJoin(org.apache.cayenne.map.DbJoin) ASTPath(org.apache.cayenne.exp.parser.ASTPath) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor)

Example 2 with ASTPath

use of org.apache.cayenne.exp.parser.ASTPath in project cayenne by apache.

the class ExpressionFactory method matchAllExp.

/**
 * @since 3.0
 */
public static Expression matchAllExp(String path, Object... values) {
    if (values == null) {
        throw new NullPointerException("Null values collection");
    }
    if (values.length == 0) {
        return new ASTTrue();
    }
    int split = path.indexOf(SPLIT_SEPARATOR);
    List<Expression> matches = new ArrayList<>(values.length);
    if (split >= 0 && split < path.length() - 1) {
        int splitEnd = path.indexOf(Entity.PATH_SEPARATOR, split + 1);
        String beforeSplit = split > 0 ? path.substring(0, split) + "." : "";
        String afterSplit = splitEnd > 0 ? "." + path.substring(splitEnd + 1) : "";
        String aliasBase = "split" + autoAliasId++ + "_";
        String splitChunk = splitEnd > 0 ? path.substring(split + 1, splitEnd) : path.substring(split + 1);
        // fix the path - replace split with dot if it's in the middle, or
        // strip it if
        // it's in the beginning
        path = split == 0 ? path.substring(1) : path.replace(SPLIT_SEPARATOR, '.');
        int i = 0;
        for (Object value : values) {
            String alias = aliasBase + i;
            String aliasedPath = beforeSplit + alias + afterSplit;
            i++;
            ASTPath pathExp = new ASTObjPath(aliasedPath);
            pathExp.setPathAliases(Collections.singletonMap(alias, splitChunk));
            matches.add(new ASTEqual(pathExp, value));
        }
    } else {
        for (Object value : values) {
            matches.add(new ASTEqual(new ASTObjPath(path), value));
        }
    }
    return joinExp(Expression.AND, matches);
}
Also used : ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) ArrayList(java.util.ArrayList) ASTEqual(org.apache.cayenne.exp.parser.ASTEqual) ASTFullObject(org.apache.cayenne.exp.parser.ASTFullObject) ASTPath(org.apache.cayenne.exp.parser.ASTPath) ASTTrue(org.apache.cayenne.exp.parser.ASTTrue)

Aggregations

ASTPath (org.apache.cayenne.exp.parser.ASTPath)2 ArrayList (java.util.ArrayList)1 TreeMap (java.util.TreeMap)1 ColumnDescriptor (org.apache.cayenne.access.jdbc.ColumnDescriptor)1 Expression (org.apache.cayenne.exp.Expression)1 ASTEqual (org.apache.cayenne.exp.parser.ASTEqual)1 ASTFullObject (org.apache.cayenne.exp.parser.ASTFullObject)1 ASTObjPath (org.apache.cayenne.exp.parser.ASTObjPath)1 ASTTrue (org.apache.cayenne.exp.parser.ASTTrue)1 DbAttribute (org.apache.cayenne.map.DbAttribute)1 DbJoin (org.apache.cayenne.map.DbJoin)1 DbRelationship (org.apache.cayenne.map.DbRelationship)1 ObjAttribute (org.apache.cayenne.map.ObjAttribute)1 PrefetchTreeNode (org.apache.cayenne.query.PrefetchTreeNode)1 ArcProperty (org.apache.cayenne.reflect.ArcProperty)1 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)1 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)1 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)1 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)1 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)1