use of org.jdbi.v3.core.result.ResultIterable in project jdbi by jdbi.
the class TestQueries method testFetchSize.
@Test
public void testFetchSize() {
Handle h = h2Extension.openHandle();
h.createScript(findSqlOnClasspath("default-data")).execute();
ResultIterable<Something> ri = h.createQuery("select id, name from something order by id").setFetchSize(1).mapToBean(Something.class);
ResultIterator<Something> r = ri.iterator();
assertThat(r.hasNext()).isTrue();
r.next();
assertThat(r.hasNext()).isTrue();
r.next();
assertThat(r.hasNext()).isFalse();
}
use of org.jdbi.v3.core.result.ResultIterable in project dropwizard by dropwizard.
the class LiquibaseScopingTest method testCustomChange.
@Test
void testCustomChange() throws Exception {
dbCommand.run(null, new Namespace(Collections.singletonMap("subcommand", "migrate")), conf);
try (Handle handle = Jdbi.create(databaseUrl, "sa", "").open()) {
final ResultIterable<Map<String, Object>> rows = handle.select("select * from persons").mapToMap();
assertThat(rows).hasSize(1);
Map<String, Object> dbPerson = rows.first();
assertThat(dbPerson.getOrDefault("name", null)).isInstanceOfSatisfying(String.class, name -> assertThat(name).isEqualTo("Bill Smith"));
}
}
use of org.jdbi.v3.core.result.ResultIterable in project jdbi by jdbi.
the class ResultReturner method forMethod.
/**
* Inspect a Method for its return type, and choose a ResultReturner subclass
* that handles any container that might wrap the results.
* @param extensionType the type that owns the Method
* @param method the method whose return type chooses the ResultReturner
* @return an instance that takes a ResultIterable and constructs the return value
*/
static ResultReturner forMethod(Class<?> extensionType, Method method) {
Type returnType = GenericTypes.resolveType(method.getGenericReturnType(), extensionType);
QualifiedType<?> qualifiedReturnType = QualifiedType.of(returnType).withAnnotations(new Qualifiers().findFor(method));
Class<?> returnClass = getErasedType(returnType);
if (Void.TYPE.equals(returnClass)) {
return findConsumer(method).orElseThrow(() -> new IllegalStateException(String.format("Method %s#%s is annotated as if it should return a value, but the method is void.", method.getDeclaringClass().getName(), method.getName())));
} else if (ResultIterable.class.equals(returnClass)) {
return new ResultIterableReturner(qualifiedReturnType);
} else if (Stream.class.equals(returnClass)) {
return new StreamReturner(qualifiedReturnType);
} else if (ResultIterator.class.equals(returnClass)) {
return new ResultIteratorReturner(qualifiedReturnType);
} else if (Iterator.class.equals(returnClass)) {
return new IteratorReturner(qualifiedReturnType);
} else if (method.isAnnotationPresent(SingleValue.class)) {
return new SingleValueReturner(qualifiedReturnType);
} else {
return new CollectedResultReturner(qualifiedReturnType);
}
}
use of org.jdbi.v3.core.result.ResultIterable in project jdbi by jdbi.
the class TestQueries method testMappedQueryObjectWithNullForPrimitiveIntField.
@Test
public void testMappedQueryObjectWithNullForPrimitiveIntField() {
Handle h = h2Extension.openHandle();
h.execute("insert into something (id, name, intValue) values (1, 'eric', null)");
ResultIterable<Something> query = h.createQuery("select * from something order by id").mapToBean(Something.class);
List<Something> r = query.list();
Something eric = r.get(0);
assertThat(eric).isEqualTo(new Something(1, "eric"));
assertThat(eric.getIntValue()).isZero();
}
use of org.jdbi.v3.core.result.ResultIterable in project jdbi by jdbi.
the class TestQueries method testMappedQueryObject.
@Test
public void testMappedQueryObject() {
Handle h = h2Extension.openHandle();
h.execute("insert into something (id, name) values (1, 'eric')");
h.execute("insert into something (id, name) values (2, 'brian')");
ResultIterable<Something> query = h.createQuery("select * from something order by id").mapToBean(Something.class);
List<Something> r = query.list();
assertThat(r).startsWith(new Something(1, "eric"));
}
Aggregations