Search in sources :

Example 31 with Jdbi

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

the class TestDocumentation method testObtainHandleInCallback.

@Test
public void testObtainHandleInCallback() throws Exception {
    Jdbi db = Jdbi.create("jdbc:h2:mem:" + UUID.randomUUID());
    db.useHandle(handle -> handle.execute("create table silly (id int)"));
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Test(org.junit.Test)

Example 32 with Jdbi

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

the class TestUseConfiguredDefaultParameterCustomizerFactory method setUp.

@Before
public void setUp() throws Exception {
    Jdbi db = dbRule.getJdbi();
    ParameterCustomizerFactory defaultParameterCustomizerFactory = (sqlObjectType, method, param, index, type) -> {
        invocationCounter.incrementAndGet();
        return (stmt, arg) -> stmt.bind("mybind" + index, arg);
    };
    db.configure(SqlObjects.class, c -> c.setDefaultParameterCustomizerFactory(defaultParameterCustomizerFactory));
    handle = db.open();
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) ParameterCustomizerFactory(org.jdbi.v3.sqlobject.statement.ParameterCustomizerFactory) SqlObjectPlugin(org.jdbi.v3.sqlobject.SqlObjectPlugin) Bind(org.jdbi.v3.sqlobject.customizer.Bind) Something(org.jdbi.v3.core.Something) H2DatabaseRule(org.jdbi.v3.core.rule.H2DatabaseRule) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) SqlObjects(org.jdbi.v3.sqlobject.SqlObjects) Rule(org.junit.Rule) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Handle(org.jdbi.v3.core.Handle) SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) SqlQuery(org.jdbi.v3.sqlobject.statement.SqlQuery) Before(org.junit.Before) Jdbi(org.jdbi.v3.core.Jdbi) ParameterCustomizerFactory(org.jdbi.v3.sqlobject.statement.ParameterCustomizerFactory) Before(org.junit.Before)

Example 33 with Jdbi

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

the class TestVavrMapCollectorWithDB method testMapCollectorWithGlobalKeyValue_shouldSucceed.

@Test
public void testMapCollectorWithGlobalKeyValue_shouldSucceed() {
    Jdbi jdbiWithKeyColAndValCol = dbRule.getJdbi().setMapKeyColumn("key_c").setMapValueColumn("val_c");
    Boolean executed = jdbiWithKeyColAndValCol.withHandle(h -> {
        HashMap<String, String> valueMap = h.createQuery("select val_c, key_c from keyval").collectInto(new GenericType<HashMap<String, String>>() {
        });
        assertThat(valueMap).containsOnlyElementsOf(expectedMap);
        return true;
    });
    assertTrue(executed);
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) HashMap(io.vavr.collection.HashMap) Test(org.junit.Test)

Example 34 with Jdbi

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

the class BindBeanListTest method init.

@BeforeClass
public static void init() {
    final Jdbi db = dbRule.getJdbi();
    db.installPlugin(new SqlObjectPlugin());
    db.registerRowMapper(new SomethingMapper());
    handle = db.open();
    handle.execute("insert into something(id, name) values(1, '1')");
    handle.execute("insert into something(id, name) values(2, '2')");
    // "control group" element that should *not* be returned by the queries
    handle.execute("insert into something(id, name) values(3, '3')");
    expectedSomethings = Arrays.asList(new Something(1, "1"), new Something(2, "2"));
}
Also used : SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) Jdbi(org.jdbi.v3.core.Jdbi) Something(org.jdbi.v3.core.Something) BeforeClass(org.junit.BeforeClass)

Example 35 with Jdbi

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

the class BindListNullTest method init.

@BeforeClass
public static void init() {
    final Jdbi db = dbRule.getJdbi();
    db.registerRowMapper(new SomethingMapper());
    db.installPlugin(new SqlObjectPlugin());
    handle = db.open();
    handle.execute("insert into something(id, name) values(1, null)");
    handle.execute("insert into something(id, name) values(2, null)");
    handle.execute("insert into something(id, name) values(3, null)");
    handle.execute("insert into something(id, name) values(4, null)");
    handle.execute("insert into something(id, name) values(5, 'bla')");
    handle.execute("insert into something(id, name) values(6, 'null')");
    handle.execute("insert into something(id, name) values(7, '')");
}
Also used : SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) Jdbi(org.jdbi.v3.core.Jdbi) BeforeClass(org.junit.BeforeClass)

Aggregations

Jdbi (org.jdbi.v3.core.Jdbi)36 Test (org.junit.Test)32 Handle (org.jdbi.v3.core.Handle)11 Something (org.jdbi.v3.core.Something)6 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)5 Query (org.jdbi.v3.core.statement.Query)5 Before (org.junit.Before)5 SQLException (java.sql.SQLException)4 Update (org.jdbi.v3.core.statement.Update)3 SqlObjectPlugin (org.jdbi.v3.sqlobject.SqlObjectPlugin)3 BeforeClass (org.junit.BeforeClass)3 Method (java.lang.reflect.Method)2 Stream (java.util.stream.Stream)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 H2DatabaseRule (org.jdbi.v3.core.rule.H2DatabaseRule)2 DefaultStatementBuilder (org.jdbi.v3.core.statement.DefaultStatementBuilder)2 SqlObjects (org.jdbi.v3.sqlobject.SqlObjects)2 Bind (org.jdbi.v3.sqlobject.customizer.Bind)2 SqlQuery (org.jdbi.v3.sqlobject.statement.SqlQuery)2 Rule (org.junit.Rule)2