Search in sources :

Example 16 with Bind

use of org.jdbi.v3.sqlobject.customizer.Bind in project jdbi by jdbi.

the class TestVendorArrays method testHsqlDb.

@Test
public void testHsqlDb() {
    Jdbi db = Jdbi.create("jdbc:hsqldb:mem:" + UUID.randomUUID());
    init(db);
    try (Handle handle = db.open()) {
        handle.execute("create table player_stats (" + "name varchar(64) primary key, " + "seasons varchar(36) array, " + "points int array)");
        handle.createUpdate("insert into player_stats (name,seasons,points) values (?,?,?)").bind(0, "Jack Johnson").bind(1, new String[] { "2013-2014", "2014-2015", "2015-2016" }).bind(2, new Integer[] { 42, 51, 50 }).execute();
        String[] seasons = handle.createQuery("select seasons from player_stats where name=:name").bind("name", "Jack Johnson").mapTo(String[].class).findOnly();
        assertThat(seasons).containsExactly("2013-2014", "2014-2015", "2015-2016");
    }
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 17 with Bind

use of org.jdbi.v3.sqlobject.customizer.Bind in project jdbi by jdbi.

the class TestArgumentFactory method testRegisterOnJdbi.

@Test
public void testRegisterOnJdbi() throws Exception {
    final Jdbi db = dbRule.getJdbi();
    db.registerArgument(new NameAF());
    try (Handle h = db.open()) {
        h.createUpdate("insert into something (id, name) values (:id, :name)").bind("id", 7).bind("name", new Name("Brian", "McCallister")).execute();
        String full_name = h.createQuery("select name from something where id = 7").mapTo(String.class).findOnly();
        assertThat(full_name).isEqualTo("Brian McCallister");
    }
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 18 with Bind

use of org.jdbi.v3.sqlobject.customizer.Bind in project jdbi by jdbi.

the class TestArgumentFactory method testRegisterOnHandle.

@Test
public void testRegisterOnHandle() throws Exception {
    try (Handle h = dbRule.openHandle()) {
        h.registerArgument(new NameAF());
        h.createUpdate("insert into something (id, name) values (:id, :name)").bind("id", 7).bind("name", new Name("Brian", "McCallister")).execute();
        String full_name = h.createQuery("select name from something where id = 7").mapTo(String.class).findOnly();
        assertThat(full_name).isEqualTo("Brian McCallister");
    }
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 19 with Bind

use of org.jdbi.v3.sqlobject.customizer.Bind in project jdbi by jdbi.

the class IntroductionTest method core.

@Test
public void core() {
    // tag::core[]
    // (H2 in-memory database)
    Jdbi jdbi = Jdbi.create("jdbc:h2:mem:test");
    List<User> users = jdbi.withHandle(handle -> {
        handle.execute("CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR)");
        // Inline positional parameters
        handle.execute("INSERT INTO user(id, name) VALUES (?, ?)", 0, "Alice");
        // Positional parameters
        handle.createUpdate("INSERT INTO user(id, name) VALUES (?, ?)").bind(0, // 0-based parameter indexes
        1).bind(1, "Bob").execute();
        // Named parameters
        handle.createUpdate("INSERT INTO user(id, name) VALUES (:id, :name)").bind("id", 2).bind("name", "Clarice").execute();
        // Named parameters from bean properties
        handle.createUpdate("INSERT INTO user(id, name) VALUES (:id, :name)").bindBean(new User(3, "David")).execute();
        // Easy mapping to any type
        return handle.createQuery("SELECT * FROM user ORDER BY name").mapToBean(User.class).list();
    });
    assertThat(users).containsExactly(new User(0, "Alice"), new User(1, "Bob"), new User(2, "Clarice"), new User(3, "David"));
// end::core[]
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Test(org.junit.Test)

Example 20 with Bind

use of org.jdbi.v3.sqlobject.customizer.Bind in project jdbi by jdbi.

the class TestClasspathSqlLocator method testUsefulExceptionForBackTracing.

@Test
public void testUsefulExceptionForBackTracing() throws Exception {
    Handle h = dbRule.openHandle();
    exception.expect(StatementException.class);
    exception.expectMessage("insert into something(id, name) values (:id, :name)");
    exception.expectMessage("insert into something(id, name) values (?, ?)");
    h.createUpdate(findSqlOnClasspath("insert-id-name")).bind("id", 1).execute();
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)34 Handle (org.jdbi.v3.core.Handle)18 Something (org.jdbi.v3.core.Something)15 Jdbi (org.jdbi.v3.core.Jdbi)4 Annotation (java.lang.annotation.Annotation)2 Method (java.lang.reflect.Method)2 Parameter (java.lang.reflect.Parameter)2 Type (java.lang.reflect.Type)2 OptionalFields (net.morimekta.test.providence.storage.jdbc.OptionalFields)2 PreparedBatch (org.jdbi.v3.core.statement.PreparedBatch)2 Query (org.jdbi.v3.core.statement.Query)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Types (java.sql.Types)1 Clock (java.time.Clock)1 HashMap (java.util.HashMap)1 Optional (java.util.Optional)1 ProvidenceJdbi.columnsFromAllFields (net.morimekta.providence.jdbi.v3.ProvidenceJdbi.columnsFromAllFields)1 ProvidenceJdbi.forMessage (net.morimekta.providence.jdbi.v3.ProvidenceJdbi.forMessage)1 ProvidenceJdbi.toField (net.morimekta.providence.jdbi.v3.ProvidenceJdbi.toField)1 ProvidenceJdbi.toMessage (net.morimekta.providence.jdbi.v3.ProvidenceJdbi.toMessage)1