Search in sources :

Example 1 with AliasedExpression

use of io.requery.query.AliasedExpression in project requery by requery.

the class QueryElement method fromExpressions.

public Set<Expression<?>> fromExpressions() {
    if (from == null) {
        types = new LinkedHashSet<>();
        Set<? extends Expression<?>> expressions;
        switch(queryType) {
            case SELECT:
                expressions = getSelection();
                break;
            case INSERT:
            case UPDATE:
            case UPSERT:
                expressions = updates.keySet();
                break;
            default:
                expressions = Collections.emptySet();
        }
        for (Expression<?> expression : expressions) {
            if (expression instanceof AliasedExpression) {
                expression = ((AliasedExpression) expression).getInnerExpression();
            }
            if (expression instanceof Attribute) {
                Type type = ((Attribute) expression).getDeclaringType();
                types.add(type);
            } else if (expression instanceof Function) {
                Function function = (Function) expression;
                for (Object arg : function.arguments()) {
                    Type type = null;
                    if (arg instanceof Attribute) {
                        type = ((Attribute) arg).getDeclaringType();
                        types.add(type);
                    } else if (arg instanceof Class) {
                        type = model.typeOf((Class) arg);
                    }
                    if (type != null) {
                        types.add(type);
                    }
                }
            }
        }
        if (from == null) {
            from = new LinkedHashSet<>();
        }
        if (!types.isEmpty()) {
            from.addAll(types);
        }
    }
    return from;
}
Also used : Function(io.requery.query.function.Function) Type(io.requery.meta.Type) ExpressionType(io.requery.query.ExpressionType) Attribute(io.requery.meta.Attribute) AliasedExpression(io.requery.query.AliasedExpression)

Example 2 with AliasedExpression

use of io.requery.query.AliasedExpression in project requery by requery.

the class EntityReader method batchRefresh.

@SafeVarargs
final Iterable<E> batchRefresh(Iterable<E> entities, Attribute<E, ?>... attributes) {
    // if the type is immutable return a new collection with the rebuilt objects
    final Collection<E> collection = type.isImmutable() ? new ArrayList<E>() : null;
    if (keyAttribute == null) {
        // non optimal case objects with multiple keys or no keys
        for (E entity : entities) {
            entity = refresh(entity, type.getProxyProvider().apply(entity), attributes);
            if (collection != null) {
                collection.add(entity);
            }
        }
    } else {
        Set<Expression<?>> selection = new LinkedHashSet<>();
        Attribute[] selectAttributes;
        if (attributes == null || attributes.length == 0) {
            selection = defaultSelection;
            selectAttributes = defaultSelectionAttributes;
        } else {
            LinkedHashSet<Attribute> selectedAttributes = new LinkedHashSet<>();
            selection.add(keyAttribute);
            selectedAttributes.add(keyAttribute);
            for (Attribute<E, ?> attribute : attributes) {
                if (attribute.isVersion()) {
                    selection.add(aliasVersion(attribute));
                } else if (!attribute.isAssociation()) {
                    QueryAttribute<E, ?> queryAttribute = Attributes.query(attribute);
                    selection.add(queryAttribute);
                }
                selectedAttributes.add(attribute);
            }
            selectAttributes = selectedAttributes.toArray(new Attribute[selection.size()]);
        }
        Map<Object, EntityProxy<E>> map = new HashMap<>();
        for (E entity : entities) {
            EntityProxy<E> proxy = type.getProxyProvider().apply(entity);
            Object key = proxy.key();
            if (key == null) {
                throw new MissingKeyException();
            }
            map.put(key, proxy);
        }
        Condition<?, ?> condition = Attributes.query(keyAttribute).in(map.keySet());
        if (type.isCacheable()) {
            final Consumer<E> collector = new Consumer<E>() {

                @Override
                public void accept(E e) {
                    if (collection != null) {
                        collection.add(e);
                    }
                }
            };
            // readResult will merge the results into the target object in cache mode
            ResultReader<E> resultReader = newResultReader(selectAttributes);
            SelectOperation<E> select = new SelectOperation<>(context, resultReader);
            QueryElement<? extends Result<E>> query = new QueryElement<>(QueryType.SELECT, context.getModel(), select);
            try (Result<E> result = query.select(selection).where(condition).get()) {
                result.each(collector);
            }
        } else {
            try (Result<Tuple> result = queryable.select(selection).where(condition).get()) {
                for (Tuple tuple : result) {
                    Object key = tuple.get((Expression) keyAttribute);
                    EntityProxy<E> proxy = map.get(key);
                    synchronized (proxy.syncObject()) {
                        for (Expression expression : selection) {
                            Object value = tuple.get(expression);
                            if (expression instanceof AliasedExpression) {
                                AliasedExpression aliased = (AliasedExpression) expression;
                                expression = aliased.getInnerExpression();
                            }
                            Attribute<E, Object> attribute = Attributes.query((Attribute) expression);
                            proxy.set(attribute, value, PropertyState.LOADED);
                        }
                    }
                }
            }
        }
        // associations TODO can be optimized
        if (attributes != null) {
            for (Attribute<E, ?> attribute : attributes) {
                if (attribute.isAssociation()) {
                    for (EntityProxy<E> proxy : map.values()) {
                        refreshAssociation(proxy, attribute);
                    }
                }
            }
        }
    }
    return collection == null ? entities : collection;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) WHERE(io.requery.sql.Keyword.WHERE) QueryAttribute(io.requery.meta.QueryAttribute) Attribute(io.requery.meta.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) QueryElement(io.requery.query.element.QueryElement) AliasedExpression(io.requery.query.AliasedExpression) EntityProxy(io.requery.proxy.EntityProxy) Consumer(io.requery.util.function.Consumer) AliasedExpression(io.requery.query.AliasedExpression) Expression(io.requery.query.Expression) QueryAttribute(io.requery.meta.QueryAttribute) Tuple(io.requery.query.Tuple)

Aggregations

Attribute (io.requery.meta.Attribute)2 AliasedExpression (io.requery.query.AliasedExpression)2 QueryAttribute (io.requery.meta.QueryAttribute)1 Type (io.requery.meta.Type)1 EntityProxy (io.requery.proxy.EntityProxy)1 Expression (io.requery.query.Expression)1 ExpressionType (io.requery.query.ExpressionType)1 Tuple (io.requery.query.Tuple)1 QueryElement (io.requery.query.element.QueryElement)1 Function (io.requery.query.function.Function)1 WHERE (io.requery.sql.Keyword.WHERE)1 Consumer (io.requery.util.function.Consumer)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1