Search in sources :

Example 6 with ResultIterable

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();
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.jupiter.api.Test)

Example 7 with ResultIterable

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"));
    }
}
Also used : Map(java.util.Map) Namespace(net.sourceforge.argparse4j.inf.Namespace) Handle(org.jdbi.v3.core.Handle) Test(org.junit.jupiter.api.Test)

Example 8 with ResultIterable

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);
    }
}
Also used : SingleValue(org.jdbi.v3.sqlobject.SingleValue) ResultIterator(org.jdbi.v3.core.result.ResultIterator) ResultIterable(org.jdbi.v3.core.result.ResultIterable) Type(java.lang.reflect.Type) GenericTypes.getErasedType(org.jdbi.v3.core.generic.GenericTypes.getErasedType) QualifiedType(org.jdbi.v3.core.qualifier.QualifiedType) Qualifiers(org.jdbi.v3.core.qualifier.Qualifiers)

Example 9 with ResultIterable

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();
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.jupiter.api.Test)

Example 10 with ResultIterable

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"));
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.jupiter.api.Test)

Aggregations

Handle (org.jdbi.v3.core.Handle)10 Test (org.junit.jupiter.api.Test)9 Something (org.jdbi.v3.core.Something)5 Map (java.util.Map)2 Namespace (net.sourceforge.argparse4j.inf.Namespace)2 ResultIterator (org.jdbi.v3.core.result.ResultIterator)2 StatementContext (org.jdbi.v3.core.statement.StatementContext)2 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 GenericTypes.getErasedType (org.jdbi.v3.core.generic.GenericTypes.getErasedType)1 QualifiedType (org.jdbi.v3.core.qualifier.QualifiedType)1 Qualifiers (org.jdbi.v3.core.qualifier.Qualifiers)1 ResultIterable (org.jdbi.v3.core.result.ResultIterable)1 PreparedBatch (org.jdbi.v3.core.statement.PreparedBatch)1 SingleValue (org.jdbi.v3.sqlobject.SingleValue)1 UseRowMapper (org.jdbi.v3.sqlobject.statement.UseRowMapper)1 UseRowReducer (org.jdbi.v3.sqlobject.statement.UseRowReducer)1