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);
}
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);
}
Aggregations