use of io.requery.util.FilteringIterator 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;
}
use of io.requery.util.FilteringIterator in project requery by requery.
the class ModifiableResult method iterator.
@Override
public CloseableIterator<E> iterator() {
// iterator that combines query results with not added elements and filtering out
// removed elements
final Iterator<E> queried = result == null ? Collections.<E>emptyIterator() : result.iterator();
final Iterator<E> added = changes.addedElements().iterator();
final Iterator<E> composite = new CompositeIterator<>(queried, added);
final Iterator<E> filtered = new FilteringIterator<>(composite, new Predicate<E>() {
@Override
public boolean test(E e) {
return !changes.removedElements().contains(e);
}
});
return new CloseableIterator<E>() {
@Override
public void close() {
if (queried instanceof CloseableIterator) {
CloseableIterator closeable = (CloseableIterator) queried;
closeable.close();
}
}
@Override
public boolean hasNext() {
return filtered.hasNext();
}
@Override
public E next() {
return filtered.next();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
Aggregations