Search in sources :

Example 1 with ResultIterable

use of org.jdbi.v3.core.result.ResultIterable in project dropwizard by dropwizard.

the class DbMigrateCommandTest method testRun.

@Test
void testRun() throws Exception {
    migrateCommand.run(null, new Namespace(Collections.emptyMap()), 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);
        assertThat(rows.first()).isEqualTo(Maps.of("id", 1, "name", "Bill Smith", "email", "bill@smith.me"));
    }
}
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 2 with ResultIterable

use of org.jdbi.v3.core.result.ResultIterable in project jdbi by jdbi.

the class SqlQueryHandler method configureReturner.

@Override
void configureReturner(Query q, SqlObjectStatementConfiguration cfg) {
    UseRowMapper useRowMapper = getMethod().getAnnotation(UseRowMapper.class);
    UseRowReducer useRowReducer = getMethod().getAnnotation(UseRowReducer.class);
    if (useRowReducer != null && useRowMapper != null) {
        throw new IllegalStateException("Cannot declare @UseRowMapper and @UseRowReducer on the same method.");
    }
    cfg.setReturner(() -> {
        StatementContext ctx = q.getContext();
        QualifiedType<?> elementType = magic.elementType(ctx.getConfig());
        if (useRowReducer != null) {
            return magic.reducedResult(q.reduceRows(rowReducerFor(useRowReducer)), ctx);
        }
        ResultIterable<?> iterable = useRowMapper == null ? q.mapTo(elementType) : q.map(rowMapperFor(useRowMapper));
        return magic.mappedResult(iterable, ctx);
    });
}
Also used : UseRowMapper(org.jdbi.v3.sqlobject.statement.UseRowMapper) UseRowReducer(org.jdbi.v3.sqlobject.statement.UseRowReducer) StatementContext(org.jdbi.v3.core.statement.StatementContext)

Example 3 with ResultIterable

use of org.jdbi.v3.core.result.ResultIterable in project jdbi by jdbi.

the class SqlBatchHandler method invoke.

@Override
@SuppressWarnings("PMD.ExcessiveMethodLength")
public Object invoke(Object target, Object[] args, HandleSupplier h) {
    final Handle handle = h.getHandle();
    final String sql = locateSql(handle);
    final int chunkSize = batchChunkSize.call(args);
    final Iterator<Object[]> batchArgs = zipArgs(getMethod(), args);
    final class BatchChunkIterator implements ResultIterator<Object> {

        private ResultIterator<?> batchResult = null;

        private boolean closed = false;

        BatchChunkIterator() {
            if (batchArgs.hasNext()) {
                // if arguments are present, preload the next result chunk
                batchResult = loadChunk();
            }
        }

        private ResultIterator<?> loadChunk() {
            // execute a single chunk and buffer
            List<Object[]> currArgs = new ArrayList<>();
            for (int i = 0; i < chunkSize && batchArgs.hasNext(); i++) {
                currArgs.add(batchArgs.next());
            }
            Supplier<PreparedBatch> preparedBatchSupplier = () -> createPreparedBatch(handle, sql, currArgs);
            return executeBatch(handle, preparedBatchSupplier);
        }

        private PreparedBatch createPreparedBatch(Handle handle, String sql, List<Object[]> currArgs) {
            PreparedBatch batch = handle.prepareBatch(sql);
            for (Object[] currArg : currArgs) {
                applyCustomizers(batch, currArg);
                batch.add();
            }
            return batch;
        }

        @Override
        public boolean hasNext() {
            if (closed) {
                throw new IllegalStateException("closed");
            }
            // first, any elements already buffered?
            if (batchResult != null) {
                if (batchResult.hasNext()) {
                    return true;
                }
                // no more in this chunk, release resources
                batchResult.close();
            }
            // more chunks?
            if (batchArgs.hasNext()) {
                // preload the next result chunk
                batchResult = loadChunk();
                // recurse to ensure we actually got elements
                return hasNext();
            }
            return false;
        }

        @Override
        public Object next() {
            if (closed) {
                throw new IllegalStateException("closed");
            }
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            return batchResult.next();
        }

        @Override
        public StatementContext getContext() {
            return batchResult.getContext();
        }

        @Override
        public void close() {
            closed = true;
            batchResult.close();
        }
    }
    ResultIterator<Object> result;
    if (batchArgs.hasNext()) {
        result = new BatchChunkIterator();
    } else {
        // only created to get access to the context.
        PreparedBatch dummy = handle.prepareBatch(sql);
        result = new ResultIterator<Object>() {

            @Override
            public void close() {
            // no op
            }

            @Override
            public StatementContext getContext() {
                return dummy.getContext();
            }

            @Override
            public boolean hasNext() {
                return false;
            }

            @Override
            public Object next() {
                throw new NoSuchElementException();
            }
        };
    }
    ResultIterable<Object> iterable = ResultIterable.of(result);
    return magic.mappedResult(iterable, result.getContext());
}
Also used : ResultIterator(org.jdbi.v3.core.result.ResultIterator) ArrayList(java.util.ArrayList) Handle(org.jdbi.v3.core.Handle) StatementContext(org.jdbi.v3.core.statement.StatementContext) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) ArrayList(java.util.ArrayList) List(java.util.List) NoSuchElementException(java.util.NoSuchElementException)

Example 4 with ResultIterable

use of org.jdbi.v3.core.result.ResultIterable in project jdbi by jdbi.

the class TestDocumentation method testFoo.

@Test
public void testFoo() {
    try (Handle h = h2Extension.openHandle()) {
        h.attach(BatchInserter.class).insert(new Something(1, "Brian"), new Something(3, "Patrick"), new Something(2, "Robert"));
        QueryReturningResultIterable qrri = h.attach(QueryReturningResultIterable.class);
        ResultIterable<String> iterable = qrri.findById(1);
        assertThat(iterable.one()).isEqualTo("Brian");
    }
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.jupiter.api.Test)

Example 5 with ResultIterable

use of org.jdbi.v3.core.result.ResultIterable in project jdbi by jdbi.

the class TestQueries method testMappedQueryObjectWithNulls.

@Test
public void testMappedQueryObjectWithNulls() {
    Handle h = h2Extension.openHandle();
    h.execute("insert into something (id, name, integerValue) 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.getIntegerValue()).isNull();
}
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