Search in sources :

Example 31 with Handle

use of org.jdbi.v3.core.Handle in project jdbi by jdbi.

the class CustomizingStatementHandler method invoke.

@Override
public Object invoke(Object target, Object[] args, HandleSupplier hs) throws Exception {
    final Handle h = hs.getHandle();
    final String locatedSql = locateSql(h);
    final StatementType stmt = createStatement(h, locatedSql);
    final SqlObjectStatementConfiguration cfg = stmt.getConfig(SqlObjectStatementConfiguration.class);
    cfg.setArgs(args);
    configureReturner(stmt, cfg);
    applyCustomizers(stmt, args);
    return cfg.getReturner().get();
}
Also used : Handle(org.jdbi.v3.core.Handle)

Example 32 with Handle

use of org.jdbi.v3.core.Handle in project jdbi by jdbi.

the class SqlBatchHandler method invoke.

@Override
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);
    ResultIterator<Object> result;
    if (batchArgs.hasNext()) {
        result = new ResultIterator<Object>() {

            ResultIterator<?> batchResult;

            boolean closed = false;

            {
                // Ensure our batchResult is prepared, so we can get its context
                hasNext();
            }

            @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()) {
                    return false;
                }
                // execute a single chunk and buffer
                PreparedBatch batch = handle.prepareBatch(sql);
                for (int i = 0; i < chunkSize && batchArgs.hasNext(); i++) {
                    applyCustomizers(batch, batchArgs.next());
                    batch.add();
                }
                batchResult = executeBatch(handle, batch);
                // recurse to ensure we actually got elements
                return hasNext();
            }

            @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();
            }
        };
    } else {
        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 : Handle(org.jdbi.v3.core.Handle) StatementContext(org.jdbi.v3.core.statement.StatementContext) PreparedBatch(org.jdbi.v3.core.statement.PreparedBatch) NoSuchElementException(java.util.NoSuchElementException)

Example 33 with Handle

use of org.jdbi.v3.core.Handle in project jdbi by jdbi.

the class TestDocumentation method testMappingExampleChainedIterator2.

@Test
public void testMappingExampleChainedIterator2() throws Exception {
    try (Handle h = dbRule.openHandle()) {
        h.execute("insert into something (id, name) values (1, 'Brian')");
        h.execute("insert into something (id, name) values (2, 'Keith')");
        Iterator<String> rs = h.createQuery("select name from something order by id").mapTo(String.class).iterator();
        assertThat(rs.next()).isEqualTo("Brian");
        assertThat(rs.next()).isEqualTo("Keith");
        assertThat(rs.hasNext()).isFalse();
    }
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 34 with Handle

use of org.jdbi.v3.core.Handle in project jdbi by jdbi.

the class TestDocumentation method testFiveMinuteFluentApi.

@Test
public void testFiveMinuteFluentApi() throws Exception {
    try (Handle h = dbRule.openHandle()) {
        h.execute("insert into something (id, name) values (?, ?)", 1, "Brian");
        String name = h.createQuery("select name from something where id = :id").bind("id", 1).mapTo(String.class).findOnly();
        assertThat(name).isEqualTo("Brian");
    }
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 35 with Handle

use of org.jdbi.v3.core.Handle in project jdbi by jdbi.

the class TestDocumentation method testAttachToObject.

@Test
public void testAttachToObject() throws Exception {
    try (Handle h = dbRule.openHandle()) {
        MyDAO dao = h.attach(MyDAO.class);
        dao.insert(1, "test");
    }
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Aggregations

Handle (org.jdbi.v3.core.Handle)134 Test (org.junit.Test)124 Jdbi (org.jdbi.v3.core.Jdbi)36 Something (org.jdbi.v3.core.Something)29 Before (org.junit.Before)21 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)17 Namespace (net.sourceforge.argparse4j.inf.Namespace)11 Test (org.junit.jupiter.api.Test)11 SQLException (java.sql.SQLException)10 Query (org.jdbi.v3.core.statement.Query)8 Map (java.util.Map)7 Update (org.jdbi.v3.core.statement.Update)7 GenericType (org.jdbi.v3.core.generic.GenericType)6 Rule (org.junit.Rule)5 List (java.util.List)4 BrokenDao (org.jdbi.v3.sqlobject.subpackage.BrokenDao)4 SomethingDao (org.jdbi.v3.sqlobject.subpackage.SomethingDao)4 ArrayList (java.util.ArrayList)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 JdbiPlugin (org.jdbi.v3.core.spi.JdbiPlugin)3