Search in sources :

Example 36 with Jdbi

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

the class BindListTest 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 37 with Jdbi

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

the class JdbiTest method whenMultipleResults_thenFindOnlyThrows.

@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();
        }
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Query(org.jdbi.v3.core.statement.Query) SQLException(java.sql.SQLException) Test(org.junit.Test)

Example 38 with Jdbi

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

the class JdbiTest method whenTableCreated_thenInsertIsPossible.

@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);
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Test(org.junit.Test)

Example 39 with Jdbi

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

the class JdbiTest method whenMultipleParameters_thenReplacement.

@Test
public void whenMultipleParameters_thenReplacement() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_11 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        Update update = handle.createUpdate("INSERT INTO PROJECT_11 (NAME, URL) VALUES (:name, :url)");
        Map<String, String> params = new HashMap<>();
        params.put("name", "REST with Spring");
        params.put("url", "github.com/eugenp/REST-With-Spring");
        update.bindMap(params);
        int rows = update.execute();
        assertEquals(1, rows);
        List<Map<String, Object>> list = handle.select("SELECT * FROM PROJECT_11").mapToMap().list();
        assertEquals(1, list.size());
        class Params {

            private String name;

            private String url;

            public Params(String name, String url) {
                this.name = name;
                this.url = url;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getUrl() {
                return url;
            }

            public void setUrl(String url) {
                this.url = url;
            }
        }
        update.bindBean(new Params("tutorials", "github.com/eugenp/tutorials"));
        rows = update.execute();
        assertEquals(1, rows);
        list = handle.select("SELECT * FROM PROJECT_11").mapToMap().list();
        assertEquals(2, list.size());
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Update(org.jdbi.v3.core.statement.Update) Test(org.junit.Test)

Example 40 with Jdbi

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

the class JdbiTest method whenSelectMapToString_thenResultIsString.

@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));
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Query(org.jdbi.v3.core.statement.Query) Test(org.junit.Test)

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