Search in sources :

Example 46 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 47 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)

Example 48 with Jdbi

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

the class JdbiTest method whenTransactionRollbackThenCommit_thenOnlyLastInserted.

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

Example 49 with Jdbi

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

the class JdbiTest method whenMultipleResults_thenFindFirstReturnsFirst.

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

Example 50 with Jdbi

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

the class MySqlConnectionModule method setup.

@Override
protected void setup(Binder binder) {
    configBinder(binder).bindConfig(MySqlConnectionConfig.class);
    String databaseUrl = buildConfigObject(MySqlConnectionConfig.class).getDatabaseUrl();
    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    Jdbi jdbi = createJdbi(() -> DriverManager.getConnection(databaseUrl), buildConfigObject(MySqlFunctionNamespaceManagerConfig.class));
    binder.bind(Jdbi.class).toInstance(jdbi);
    binder.bind(FunctionNamespaceDao.class).toProvider(FunctionNamespaceDaoProvider.class);
    binder.bind(new TypeLiteral<Class<? extends FunctionNamespaceDao>>() {
    }).toInstance(FunctionNamespaceDao.class);
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) TypeLiteral(com.google.inject.TypeLiteral)

Aggregations

Jdbi (org.jdbi.v3.core.Jdbi)47 Test (org.junit.Test)32 Handle (org.jdbi.v3.core.Handle)12 Something (org.jdbi.v3.core.Something)6 Query (org.jdbi.v3.core.statement.Query)6 SqlObjectPlugin (org.jdbi.v3.sqlobject.SqlObjectPlugin)6 Before (org.junit.Before)6 SQLException (java.sql.SQLException)5 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)5 Update (org.jdbi.v3.core.statement.Update)3 BeforeClass (org.junit.BeforeClass)3 TypeLiteral (com.google.inject.TypeLiteral)2 JdbiFactory (io.dropwizard.jdbi3.JdbiFactory)2 Method (java.lang.reflect.Method)2 Stream (java.util.stream.Stream)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 SqlStatements (org.jdbi.v3.core.statement.SqlStatements)2 Test (org.junit.jupiter.api.Test)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 HealthCheckRegistry (com.codahale.metrics.health.HealthCheckRegistry)1