io.ebeaninternal.server.query.DtoQueryEngine Maven / Gradle / Ivy
package io.ebeaninternal.server.query;
import io.ebean.QueryIterator;
import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.server.core.DtoQueryRequest;
import io.ebeaninternal.server.persist.Binder;
import jakarta.persistence.PersistenceException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
public final class DtoQueryEngine {
private final Binder binder;
public DtoQueryEngine(Binder binder) {
this.binder = binder;
}
public List findList(DtoQueryRequest request) {
try {
request.executeSql(binder, SpiQuery.Type.LIST);
List rows = new ArrayList<>();
while (request.next()) {
rows.add(request.readNextBean());
}
return rows;
} catch (SQLException e) {
throw new PersistenceException(errMsg(e.getMessage(), request.getSql()), e);
} finally {
request.close();
}
}
public QueryIterator findIterate(DtoQueryRequest request) {
try {
request.executeSql(binder, SpiQuery.Type.ITERATE);
return new DtoQueryIterator<>(request);
} catch (SQLException e) {
throw new PersistenceException(errMsg(e.getMessage(), request.getSql()), e);
}
}
public void findEach(DtoQueryRequest request, Consumer consumer) {
try {
request.executeSql(binder, SpiQuery.Type.ITERATE);
while (request.next()) {
consumer.accept(request.readNextBean());
}
} catch (SQLException e) {
throw new PersistenceException(errMsg(e.getMessage(), request.getSql()), e);
} finally {
request.close();
}
}
public void findEach(DtoQueryRequest request, int batchSize, Consumer> consumer) {
try {
List buffer = new ArrayList<>();
request.executeSql(binder, SpiQuery.Type.ITERATE);
while (request.next()) {
buffer.add(request.readNextBean());
if (buffer.size() >= batchSize) {
consumer.accept(buffer);
buffer.clear();
}
}
if (!buffer.isEmpty()) {
// consume the remainder
consumer.accept(buffer);
}
} catch (Exception e) {
throw new PersistenceException(errMsg(e.getMessage(), request.getSql()), e);
} finally {
request.close();
}
}
public void findEachWhile(DtoQueryRequest request, Predicate consumer) {
try {
request.executeSql(binder, SpiQuery.Type.ITERATE);
while (request.next()) {
if (!consumer.test(request.readNextBean())) {
break;
}
}
} catch (SQLException e) {
throw new PersistenceException(errMsg(e.getMessage(), request.getSql()), e);
} finally {
request.close();
}
}
private String errMsg(String msg, String sql) {
return "Query threw SQLException:" + msg + " Query was:" + sql;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy