use of io.requery.sql.Keyword.WHERE 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;
}
use of io.requery.sql.Keyword.WHERE in project requery by requery.
the class EntityReader method refresh.
private E refresh(E entity, EntityProxy<E> proxy, final Set<Attribute<E, ?>> attributes) {
Predicate<Attribute<E, ?>> basicFilter = new Predicate<Attribute<E, ?>>() {
@Override
public boolean test(Attribute<E, ?> value) {
return attributes.contains(value) && (!value.isAssociation() || value.isForeignKey());
}
};
FilteringIterator<Attribute<E, ?>> filterator = new FilteringIterator<>(attributes.iterator(), basicFilter);
if (filterator.hasNext()) {
QueryBuilder qb = new QueryBuilder(context.getQueryBuilderOptions()).keyword(SELECT).commaSeparated(filterator, new QueryBuilder.Appender<Attribute<E, ?>>() {
@Override
public void append(QueryBuilder qb, Attribute<E, ?> value) {
String versionColumn = context.getPlatform().versionColumnDefinition().columnName();
if (value.isVersion() && versionColumn != null) {
qb.append(versionColumn).space().append(AS).space().append(value.getName()).space();
} else {
qb.attribute(value);
}
}
}).keyword(FROM).tableName(type.getName()).keyword(WHERE).appendWhereConditions(type.getKeyAttributes());
String sql = qb.toString();
try (Connection connection = context.getConnection();
PreparedStatement statement = connection.prepareStatement(sql)) {
int index = 1;
for (Attribute<E, ?> attribute : type.getKeyAttributes()) {
Object value = proxy.getKey(attribute);
if (value == null) {
throw new MissingKeyException(proxy);
}
mapping.write((Expression) attribute, statement, index++, value);
}
context.getStatementListener().beforeExecuteQuery(statement, sql, null);
ResultSet results = statement.executeQuery();
context.getStatementListener().afterExecuteQuery(statement);
if (results.next()) {
Attribute[] selection = new Attribute[attributes.size()];
attributes.toArray(selection);
// modify the given entity
if (type.isImmutable()) {
entity = fromBuilder(results, selection);
} else {
entity = fromResult(entity, results, selection);
}
}
} catch (SQLException e) {
throw new PersistenceException(e);
}
}
// refresh associations
for (Attribute<E, ?> attribute : attributes) {
// if it's a foreign key its resolved as part of the basic properties
if (attribute.isAssociation()) {
refreshAssociation(proxy, attribute);
}
}
return entity;
}
Aggregations