Java Code Examples for org.jdbi.v3.core.Jdbi#useHandle()

The following examples show how to use org.jdbi.v3.core.Jdbi#useHandle() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JdbiIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenIdentityColumn_thenInsertReturnsNewId() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_2 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        Update update = handle.createUpdate(
                "INSERT INTO PROJECT_2 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
        ResultBearing generatedKeys = update.executeAndReturnGeneratedKeys();

        assertEquals(0, generatedKeys.mapToMap().findOnly().get("id"));

        update = handle.createUpdate(
                "INSERT INTO PROJECT_2 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");

        assertEquals(1, generatedKeys.mapToMap().findOnly().get("id"));
    });
}
 
Example 2
Source File: JdbiIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenMultipleResults_thenFindOnlyThrows() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_9 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        handle.execute("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
        handle.execute("INSERT INTO PROJECT_9 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");

        try {
            Query query = handle.createQuery("select * from project_9");
            Map<String, Object> onlyResult = query.mapToMap().findOnly();

            fail("Exception expected");
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
}
 
Example 3
Source File: JdbiIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenTransactionRollbackThenCommit_thenOnlyLastInserted() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.useTransaction((Handle h) -> {
            assertEquals(handle, h);

            handle.execute("create table PROJECT_13 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
            handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')");
            handle.rollback();
            handle.begin();
            handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')");
            handle.rollback();
            handle.begin();
            handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('tutorials', 'https://github.com/eugenp/tutorials')");
            handle.execute("INSERT INTO PROJECT_13 (NAME, URL) VALUES ('REST with Spring', 'https://github.com/eugenp/REST-With-Spring')");
            handle.commit();
        });
        List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_13").mapToMap().list();

        assertEquals(2, list.size());
    });
}
 
Example 4
Source File: TestService.java    From micronaut-sql with Apache License 2.0 5 votes vote down vote up
public TestService(Jdbi db) {
    this.db = db;
    db.useHandle(handle -> {
        handle.execute("CREATE TABLE IF NOT EXISTS foo(id INTEGER);");
        handle.execute("INSERT INTO foo(id) VALUES (0);");
    });

}
 
Example 5
Source File: JdbiIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenTableCreated_thenInsertIsPossible() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        int updateCount = handle.execute("create table PROJECT_1 (id integer identity, name varchar(50), url varchar(100))");

        assertEquals(0, updateCount);

        updateCount = handle.execute("INSERT INTO PROJECT_1 VALUES (1, 'tutorials', 'github.com/eugenp/tutorials')");

        assertEquals(1, updateCount);
    });
}
 
Example 6
Source File: JdbiIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@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"));
    });
}
 
Example 7
Source File: JdbiIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenSelectMapToString_thenResultIsString() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_4 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        handle.execute("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
        handle.execute("INSERT INTO PROJECT_4 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");
        Query query = handle.createQuery("select name, url from PROJECT_4 order by id");
        List<String> list = query.mapTo(String.class).list();

        assertEquals("tutorials", list.get(0));
        assertEquals("REST with Spring", list.get(1));
    });
}
 
Example 8
Source File: JdbiIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@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()));
    });
}
 
Example 9
Source File: JdbiIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@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());
    });
}
 
Example 10
Source File: JdbiIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void whenMultipleResults_thenFindFirstReturnsFirst() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_7 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        handle.execute("INSERT INTO PROJECT_7 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
        handle.execute("INSERT INTO PROJECT_7 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");

        Query query = handle.createQuery("select * from project_7 order by id");
        Optional<Map<String, Object>> first = query.mapToMap().findFirst();

        assertTrue(first.isPresent());
        assertEquals("tutorials", first.get().get("name"));
    });
}
 
Example 11
Source File: JdbiIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@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();
        }
    });
}
 
Example 12
Source File: JdbiIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@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());
    });
}
 
Example 13
Source File: JdbiIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@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());
        });
    });
}