Search in sources :

Example 86 with Handle

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

the class TestNamedParams method testCascadedLazyArgs.

@Test
public void testCascadedLazyArgs() throws Exception {
    Handle h = dbRule.openHandle();
    Update s = h.createUpdate("insert into something (id, name) values (:id, :name)");
    Map<String, Object> args = new HashMap<>();
    args.put("id", 0);
    s.bindMap(args);
    s.bindBean(new Object() {

        @SuppressWarnings("unused")
        public String getName() {
            return "Keith";
        }
    });
    int insert_count = s.execute();
    assertThat(insert_count).isEqualTo(1);
    Something something = h.createQuery("select id, name from something").mapToBean(Something.class).findOnly();
    assertThat(something).isEqualTo(new Something(0, "Keith"));
}
Also used : HashMap(java.util.HashMap) Update(org.jdbi.v3.core.statement.Update) Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 87 with Handle

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

the class TestVendorArrays method testHsqlDb.

@Test
public void testHsqlDb() {
    Jdbi db = Jdbi.create("jdbc:hsqldb:mem:" + UUID.randomUUID());
    init(db);
    try (Handle handle = db.open()) {
        handle.execute("create table player_stats (" + "name varchar(64) primary key, " + "seasons varchar(36) array, " + "points int array)");
        handle.createUpdate("insert into player_stats (name,seasons,points) values (?,?,?)").bind(0, "Jack Johnson").bind(1, new String[] { "2013-2014", "2014-2015", "2015-2016" }).bind(2, new Integer[] { 42, 51, 50 }).execute();
        String[] seasons = handle.createQuery("select seasons from player_stats where name=:name").bind("name", "Jack Johnson").mapTo(String[].class).findOnly();
        assertThat(seasons).containsExactly("2013-2014", "2014-2015", "2015-2016");
    }
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 88 with Handle

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

the class TestPlugins method testCustomizeHandle.

@Test
public void testCustomizeHandle() throws Exception {
    Handle h = mock(Handle.class);
    dbRule.getJdbi().installPlugin(new JdbiPlugin() {

        @Override
        public Handle customizeHandle(Handle handle) {
            return h;
        }
    });
    assertThat(h).isSameAs(dbRule.getJdbi().open());
}
Also used : JdbiPlugin(org.jdbi.v3.core.spi.JdbiPlugin) Test(org.junit.Test)

Example 89 with Handle

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

the class TestArgumentFactory method testRegisterOnJdbi.

@Test
public void testRegisterOnJdbi() throws Exception {
    final Jdbi db = dbRule.getJdbi();
    db.registerArgument(new NameAF());
    try (Handle h = db.open()) {
        h.createUpdate("insert into something (id, name) values (:id, :name)").bind("id", 7).bind("name", new Name("Brian", "McCallister")).execute();
        String full_name = h.createQuery("select name from something where id = 7").mapTo(String.class).findOnly();
        assertThat(full_name).isEqualTo("Brian McCallister");
    }
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 90 with Handle

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

the class TestArgumentFactory method testRegisterOnHandle.

@Test
public void testRegisterOnHandle() throws Exception {
    try (Handle h = dbRule.openHandle()) {
        h.registerArgument(new NameAF());
        h.createUpdate("insert into something (id, name) values (:id, :name)").bind("id", 7).bind("name", new Name("Brian", "McCallister")).execute();
        String full_name = h.createQuery("select name from something where id = 7").mapTo(String.class).findOnly();
        assertThat(full_name).isEqualTo("Brian McCallister");
    }
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)123 Handle (org.jdbi.v3.core.Handle)119 Jdbi (org.jdbi.v3.core.Jdbi)31 Something (org.jdbi.v3.core.Something)29 Before (org.junit.Before)20 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)17 SQLException (java.sql.SQLException)8 Update (org.jdbi.v3.core.statement.Update)7 GenericType (org.jdbi.v3.core.generic.GenericType)6 Query (org.jdbi.v3.core.statement.Query)6 Rule (org.junit.Rule)5 Map (java.util.Map)4 BrokenDao (org.jdbi.v3.sqlobject.subpackage.BrokenDao)4 SomethingDao (org.jdbi.v3.sqlobject.subpackage.SomethingDao)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Method (java.lang.reflect.Method)3 Connection (java.sql.Connection)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3