Search in sources :

Example 71 with Handle

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

the class JdbiTest method whenSelectMapToMap_thenResultsAreMapEntries.

@Test
public void whenSelectMapToMap_thenResultsAreMapEntries() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_3 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        handle.execute("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
        handle.execute("INSERT INTO PROJECT_3 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");
        Query query = handle.createQuery("select * from PROJECT_3 order by id");
        List<Map<String, Object>> list = query.mapToMap().list();
        assertEquals("tutorials", list.get(0).get("name"));
        assertEquals("REST with Spring", list.get(1).get("name"));
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Query(org.jdbi.v3.core.statement.Query) Test(org.junit.Test)

Example 72 with Handle

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

the class JdbiTest method whenNoResults_thenFindOnlyThrows.

@Test
public void whenNoResults_thenFindOnlyThrows() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_8 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        try {
            handle.createQuery("select * from project_8").mapToMap().findOnly();
            fail("Exception expected");
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 73 with Handle

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

the class JdbiTest method whenException_thenTransactionIsRolledBack.

@Test
public void whenException_thenTransactionIsRolledBack() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        try {
            handle.useTransaction(h -> {
                h.execute("create table PROJECT_14 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
                h.execute("INSERT INTO PROJECT_14 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')");
                List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_14").mapToMap().list();
                assertTrue(h.isInTransaction());
                assertEquals(1, list.size());
                throw new Exception("rollback");
            });
        } catch (Exception ignored) {
        }
        List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_14").mapToMap().list();
        assertFalse(handle.isInTransaction());
        assertEquals(0, list.size());
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 74 with Handle

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

the class JdbiTest method whenNoResults_thenFindFirstReturnsNone.

@Test
public void whenNoResults_thenFindFirstReturnsNone() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_6 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        assertFalse(handle.createQuery("select * from project_6").mapToMap().findFirst().isPresent());
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Test(org.junit.Test)

Example 75 with Handle

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

the class JdbiTest method whenHandle_thenBoh.

@Test
public void whenHandle_thenBoh() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    final Handle[] handleRef = new Handle[1];
    boolean closed = jdbi.withHandle(handle -> {
        handleRef[0] = handle;
        return handle.isClosed();
    });
    assertFalse(closed);
    assertTrue(handleRef[0].isClosed());
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) 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