Search in sources :

Example 66 with Handle

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

the class TestNamedParams method testFunctionsNestedBinding.

@Test
public void testFunctionsNestedBinding() throws Exception {
    Handle h = dbRule.openHandle();
    assertThat(h.createUpdate("insert into something (id, name) values (:my.nested.id, :my.nested.name)").bindMethods("my", new Object() {

        @SuppressWarnings("unused")
        public NoArgFunctions nested() {
            return new NoArgFunctions(0, "Keith");
        }
    }).execute()).isEqualTo(1);
    assertThat(h.select("select * from something where id = ?", 0).mapToBean(Something.class).findOnly()).isEqualTo(new Something(0, "Keith"));
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 67 with Handle

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

the class SqlObjectTest method setUp.

@Before
public void setUp() {
    final Handle handle = dbRule.getSharedHandle();
    dao = handle.attach(SomethingDao.class);
}
Also used : Handle(org.jdbi.v3.core.Handle) Before(org.junit.Before)

Example 68 with Handle

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

the class JdbiTest method whenSelectMapToString_thenStream.

@Test
public void whenSelectMapToString_thenStream() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_5 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        handle.execute("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
        handle.execute("INSERT INTO PROJECT_5 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");
        Query query = handle.createQuery("select name, url from PROJECT_5 order by id");
        query.mapTo(String.class).useStream((Stream<String> stream) -> assertEquals("tutorials", stream.findFirst().get()));
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Query(org.jdbi.v3.core.statement.Query) Stream(java.util.stream.Stream) Test(org.junit.Test)

Example 69 with Handle

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

the class JdbiTest method whenTransactionRollback_thenNoDataInserted.

@Test
public void whenTransactionRollback_thenNoDataInserted() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.useTransaction(h -> {
            handle.execute("create table PROJECT_12 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
            handle.execute("INSERT INTO PROJECT_12 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')");
            handle.execute("INSERT INTO PROJECT_12 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')");
            handle.rollback();
            List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_12").mapToMap().list();
            assertEquals(0, list.size());
        });
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Test(org.junit.Test)

Example 70 with Handle

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

the class JdbiTest method whenParameters_thenReplacement.

@Test
public void whenParameters_thenReplacement() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_10 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        Update update1 = handle.createUpdate("INSERT INTO PROJECT_10 (NAME, URL) VALUES (?, ?)");
        update1.bind(0, "tutorials");
        update1.bind(1, "github.com/eugenp/tutorials");
        int rows = update1.execute();
        assertEquals(1, rows);
        Update update2 = handle.createUpdate("INSERT INTO PROJECT_10 (NAME, URL) VALUES (:name, :url)");
        update2.bind("name", "REST with Spring");
        update2.bind("url", "github.com/eugenp/REST-With-Spring");
        rows = update2.execute();
        assertEquals(1, rows);
        List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_10 WHERE NAME = 'tutorials'").mapToMap().list();
        assertEquals(1, list.size());
        list = handle.select("SELECT * FROM PROJECT_10 WHERE NAME = 'REST with Spring'").mapToMap().list();
        assertEquals(1, list.size());
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Update(org.jdbi.v3.core.statement.Update) 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