Search in sources :

Example 21 with Attribute

use of io.requery.meta.Attribute in project requery by requery.

the class QueryBuilder method appendWhereConditions.

public <T> QueryBuilder appendWhereConditions(Set<Attribute<T, ?>> attributes) {
    int index = 0;
    for (Attribute attribute : attributes) {
        if (index > 0) {
            keyword(Keyword.AND);
            space();
        }
        attribute(attribute);
        space();
        append("=?");
        space();
        index++;
    }
    return this;
}
Also used : Attribute(io.requery.meta.Attribute)

Example 22 with Attribute

use of io.requery.meta.Attribute in project requery by requery.

the class EntityDataStore method findByKey.

@Override
public <E extends T, K> E findByKey(Class<E> type, K key) {
    Type<E> entityType = entityModel.typeOf(type);
    if (entityType.isCacheable() && entityCache != null) {
        E entity = entityCache.get(type, key);
        if (entity != null) {
            return entity;
        }
    }
    Set<Attribute<E, ?>> keys = entityType.getKeyAttributes();
    if (keys.isEmpty()) {
        throw new MissingKeyException();
    }
    Selection<? extends Result<E>> selection = select(type);
    if (keys.size() == 1) {
        QueryAttribute<E, Object> attribute = Attributes.query(keys.iterator().next());
        selection.where(attribute.equal(key));
    } else {
        if (key instanceof CompositeKey) {
            CompositeKey compositeKey = (CompositeKey) key;
            for (Attribute<E, ?> attribute : keys) {
                QueryAttribute<E, Object> keyAttribute = Attributes.query(attribute);
                Object value = compositeKey.get(keyAttribute);
                selection.where(keyAttribute.equal(value));
            }
        } else {
            throw new IllegalArgumentException("CompositeKey required");
        }
    }
    return selection.get().firstOrNull();
}
Also used : CompositeKey(io.requery.proxy.CompositeKey) UPDATE(io.requery.query.element.QueryType.UPDATE) DELETE(io.requery.query.element.QueryType.DELETE) QueryAttribute(io.requery.meta.QueryAttribute) Attribute(io.requery.meta.Attribute)

Example 23 with Attribute

use of io.requery.meta.Attribute in project requery by requery.

the class EntityDataStore method refresh.

@SuppressWarnings("unchecked")
@Override
public <E extends T> Iterable<E> refresh(Iterable<E> entities, Attribute<?, ?>... attributes) {
    Iterator<E> iterator = entities.iterator();
    if (iterator.hasNext()) {
        E entity = iterator.next();
        EntityProxy<E> proxy = context.proxyOf(entity, false);
        EntityReader<E, T> reader = context.read(proxy.type().getClassType());
        return reader.batchRefresh(entities, (Attribute<E, ?>[]) attributes);
    }
    return entities;
}
Also used : INSERT(io.requery.query.element.QueryType.INSERT) SELECT(io.requery.query.element.QueryType.SELECT) UPDATE(io.requery.query.element.QueryType.UPDATE) DELETE(io.requery.query.element.QueryType.DELETE) QueryAttribute(io.requery.meta.QueryAttribute) Attribute(io.requery.meta.Attribute)

Example 24 with Attribute

use of io.requery.meta.Attribute in project requery by requery.

the class EntityProxy method getKey.

@SuppressWarnings("unchecked")
public Object getKey(Attribute<E, ?> attribute) {
    if (attribute.isAssociation()) {
        Attribute referenced = attribute.getReferencedAttribute().get();
        Object association = get(attribute, false);
        if (association != null) {
            Type<Object> type = referenced.getDeclaringType();
            EntityProxy proxy = type.getProxyProvider().apply(association);
            return proxy == null ? null : proxy.get(referenced, false);
        } else {
            return null;
        }
    }
    return get(attribute, false);
}
Also used : Attribute(io.requery.meta.Attribute)

Example 25 with Attribute

use of io.requery.meta.Attribute in project requery by requery.

the class BaseResult method toMap.

@Override
@SuppressWarnings("unchecked")
public <K> Map<K, E> toMap(Expression<K> key, Map<K, E> map) {
    try (CloseableIterator<E> iterator = createIterator()) {
        while (iterator.hasNext()) {
            E value = iterator.next();
            Type<E> type = null;
            if (key instanceof Attribute) {
                Attribute attribute = (Attribute) key;
                type = (Type<E>) attribute.getDeclaringType();
            }
            if (type != null) {
                EntityProxy<E> proxy = type.getProxyProvider().apply(value);
                map.put(proxy.get((Attribute<E, K>) key), value);
            } else if (value instanceof Tuple) {
                map.put(((Tuple) value).get(key), value);
            } else {
                throw new UnsupportedOperationException();
            }
        }
    }
    return map;
}
Also used : Attribute(io.requery.meta.Attribute)

Aggregations

Attribute (io.requery.meta.Attribute)30 QueryAttribute (io.requery.meta.QueryAttribute)13 ClassName (com.squareup.javapoet.ClassName)5 FieldSpec (com.squareup.javapoet.FieldSpec)5 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)5 TypeName (com.squareup.javapoet.TypeName)5 TypeSpec (com.squareup.javapoet.TypeSpec)5 EntityProxy (io.requery.proxy.EntityProxy)5 Expression (io.requery.query.Expression)5 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 MethodSpec (com.squareup.javapoet.MethodSpec)4 PropertyState (io.requery.proxy.PropertyState)4 UPDATE (io.requery.query.element.QueryType.UPDATE)4 StringJoiner (java.util.StringJoiner)4 TypeElement (javax.lang.model.element.TypeElement)4 TypeMirror (javax.lang.model.type.TypeMirror)4 CodeBlock (com.squareup.javapoet.CodeBlock)3