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