Search in sources :

Example 1 with Expression

use of org.apache.cayenne.exp.Expression in project cayenne by apache.

the class OptimisticLockException method getFreshSnapshot.

/**
 * Retrieves fresh snapshot for the failed row. Null row indicates that it was
 * deleted.
 *
 * @since 3.0
 */
public Map<?, ?> getFreshSnapshot(ObjectContext context) {
    Expression qualifier = null;
    for (DbAttribute attribute : rootEntity.getPrimaryKeys()) {
        Expression attributeQualifier = ExpressionFactory.matchDbExp(attribute.getName(), qualifierSnapshot.get(attribute.getName()));
        qualifier = (qualifier != null) ? qualifier.andExp(attributeQualifier) : attributeQualifier;
    }
    SelectQuery<DataRow> query = new SelectQuery<DataRow>(rootEntity, qualifier);
    query.setFetchingDataRows(true);
    return (Map<?, ?>) Cayenne.objectForQuery(context, query);
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) Expression(org.apache.cayenne.exp.Expression) DbAttribute(org.apache.cayenne.map.DbAttribute) DataRow(org.apache.cayenne.DataRow) Map(java.util.Map)

Example 2 with Expression

use of org.apache.cayenne.exp.Expression 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 3 with Expression

use of org.apache.cayenne.exp.Expression 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)

Example 4 with Expression

use of org.apache.cayenne.exp.Expression in project cayenne by apache.

the class EJBQLJoinAppender method appendTable.

public String appendTable(EJBQLTableId id) {
    DbEntity dbEntity = id.getDbEntity(context);
    String tableName = context.getQuotingStrategy().quotedFullyQualifiedName(dbEntity);
    String alias;
    if (context.isUsingAliases()) {
        // TODO: andrus 1/5/2007 - if the same table is joined more than once, this
        // will create an incorrect alias.
        alias = context.getTableAlias(id.getEntityId(), tableName);
        // 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(' ').append(tableName).append(' ').append(alias);
        generateJoinsForFlattenedAttributes(id);
    } else {
        context.append(' ').append(tableName);
        alias = tableName;
    }
    // append inheritance qualifier...
    if (id.isPrimaryTable()) {
        Expression qualifier = context.getEntityDescriptor(id.getEntityId()).getEntityQualifier();
        if (qualifier != null) {
            EJBQLExpression ejbqlQualifier = ejbqlQualifierForEntityAndSubclasses(qualifier, id.getEntityId());
            context.pushMarker(context.makeWhereMarker(), true);
            context.append(" WHERE");
            context.popMarker();
            context.pushMarker(context.makeEntityQualifierMarker(), false);
            ejbqlQualifier.visit(context.getTranslatorFactory().getConditionTranslator(context));
            context.popMarker();
        }
    }
    return alias;
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) EJBQLExpression(org.apache.cayenne.ejbql.EJBQLExpression) Expression(org.apache.cayenne.exp.Expression) EJBQLExpression(org.apache.cayenne.ejbql.EJBQLExpression)

Example 5 with Expression

use of org.apache.cayenne.exp.Expression in project cayenne by apache.

the class DataMapUtils method getParameterNames.

private Map<String, String> getParameterNames(Expression expression, Object root) {
    if (expression != null) {
        Map<String, String> types = new HashMap<>();
        String typeName = "";
        List<String> names = new LinkedList<>();
        for (int i = 0; i < expression.getOperandCount(); i++) {
            Object operand = expression.getOperand(i);
            if (operand instanceof Expression) {
                types.putAll(getParameterNames((Expression) operand, root));
            }
            if (operand instanceof ASTObjPath) {
                PathComponent<ObjAttribute, ObjRelationship> component = ((Entity) root).lastPathComponent((ASTObjPath) operand, null);
                ObjAttribute attribute = component.getAttribute();
                if (attribute != null) {
                    typeName = attribute.getType();
                } else {
                    ObjRelationship relationship = component.getRelationship();
                    if (relationship != null) {
                        typeName = relationship.getTargetEntity().getClassName();
                    } else {
                        typeName = "Object";
                    }
                }
            }
            if (operand instanceof ASTList) {
                Object[] values = (Object[]) ((ASTList) operand).getOperand(0);
                for (Object value : values) {
                    if (value instanceof ExpressionParameter) {
                        names.add(((ExpressionParameter) value).getName());
                    }
                }
            }
            if (operand instanceof ExpressionParameter) {
                names.add(((ExpressionParameter) operand).getName());
            }
        }
        for (String name : names) {
            types.put(Util.underscoredToJava(name, false), typeName);
        }
        return types;
    }
    return Collections.emptyMap();
}
Also used : ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) ObjEntity(org.apache.cayenne.map.ObjEntity) Entity(org.apache.cayenne.map.Entity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Expression(org.apache.cayenne.exp.Expression) ExpressionParameter(org.apache.cayenne.exp.ExpressionParameter) ASTList(org.apache.cayenne.exp.parser.ASTList)

Aggregations

Expression (org.apache.cayenne.exp.Expression)298 Test (org.junit.Test)265 Artist (org.apache.cayenne.testdo.testmap.Artist)69 SelectQuery (org.apache.cayenne.query.SelectQuery)47 Painting (org.apache.cayenne.testdo.testmap.Painting)29 BigDecimal (java.math.BigDecimal)23 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)17 DateTestEntity (org.apache.cayenne.testdo.date_time.DateTestEntity)16 ObjEntity (org.apache.cayenne.map.ObjEntity)13 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)12 DbEntity (org.apache.cayenne.map.DbEntity)10 List (java.util.List)9 HashMap (java.util.HashMap)8 DataRow (org.apache.cayenne.DataRow)8 DbAttribute (org.apache.cayenne.map.DbAttribute)8 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)7 DbRelationship (org.apache.cayenne.map.DbRelationship)6 ObjAttribute (org.apache.cayenne.map.ObjAttribute)6 ObjRelationship (org.apache.cayenne.map.ObjRelationship)6 Gallery (org.apache.cayenne.testdo.testmap.Gallery)6