Search in sources :

Example 11 with Bind

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

the class JdbiTest method whenParameters_thenReplacement.

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

Example 12 with Bind

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

the class MessageRowMapperTest method testDefaultMapping.

@Test
public void testDefaultMapping() {
    generator.setFillRate(1.0).setMaxCollectionItems(16).withGenerator(CompactFields.kDescriptor, g -> {
        g.setValueGenerator(CompactFields._Field.NAME, ctx -> ctx.getFairy().textProducer().latinWord());
        g.setValueGenerator(CompactFields._Field.LABEL, ctx -> ctx.getFairy().textProducer().word());
    }).withGenerator(NormalFields.kDescriptor, g -> {
        g.setValueGenerator(NormalFields._Field.NAME, ctx -> ctx.getFairy().textProducer().latinWord());
        g.setValueGenerator(NormalFields._Field.LABEL, ctx -> ctx.getFairy().textProducer().word());
    });
    OptionalFields expected = generator.generate(OptionalFields.kDescriptor).mutate().setId(1234).setTimestampS((int) clock.instant().getEpochSecond()).setTimestampMs(clock.instant().getEpochSecond() * 1000).build();
    OptionalFields empty = OptionalFields.builder().setId(2345).build();
    try (Handle handle = db.getDBI().open()) {
        handle.createUpdate("INSERT INTO mappings_v3.default_mappings (" + "  id, present, tiny, small, medium, large, real," + "  fib, name, data, compact," + "  timestamp_s, timestamp_ms," + "  binary_message, blob_message, other_message," + "  blob_data, base64_data, int_bool" + ") VALUES (" + "  :e.id," + "  :e.present," + "  :e.tiny," + "  :e.small," + "  :e.medium," + "  :e.large," + "  :e.real," + "  :e.fib," + "  :e.name," + "  :e.data," + "  :e.message," + "  :timestamp_s," + "  :e.timestamp_ms," + "  :e.binary_message," + "  :e.blob_message," + "  :e.clob_message," + "  :e.blob_data," + "  :e.base64_data," + "  :e.int_bool" + ")").bind("timestamp_s", toField(expected, TIMESTAMP_S, Types.TIMESTAMP)).bindNamedArgumentFinder(forMessage("e", expected, withType(TIMESTAMP_MS, Types.TIMESTAMP), withType(BINARY_MESSAGE, Types.BINARY), withType(BLOB_MESSAGE, Types.BLOB), withType(CLOB_MESSAGE, Types.CLOB), withType(BLOB_DATA, Types.BLOB), withType(BASE64_DATA, Types.VARCHAR))).execute();
        handle.createUpdate("INSERT INTO mappings_v3.default_mappings (" + "  id, present, tiny, small, medium, large, real," + "  fib, name, data, compact," + "  timestamp_s, timestamp_ms," + "  binary_message, blob_message, other_message," + "  blob_data, base64_data, int_bool" + ") VALUES (" + "  :id," + "  :present," + "  :tiny," + "  :small," + "  :medium," + "  :large," + "  :real," + "  :fib," + "  :name," + "  :data," + "  :message," + "  :timestamp_s," + "  :timestamp_ms," + "  :binary_message," + "  :blob_message," + "  :clob_message," + "  :blob_data," + "  :base64_data," + "  :int_bool" + ")").bind("timestamp_s", toField(empty, TIMESTAMP_S, Types.TIMESTAMP)).bindNamedArgumentFinder(forMessage(empty, withType(TIMESTAMP_MS, Types.TIMESTAMP), withType(BINARY_MESSAGE, Types.BINARY), withType(BLOB_MESSAGE, Types.BLOB), withType(CLOB_MESSAGE, Types.CLOB), withType(BLOB_DATA, Types.BLOB), withType(BASE64_DATA, Types.VARCHAR), withType(INT_BOOL, Types.INTEGER))).execute();
        OptionalFields val = handle.createQuery("SELECT m.* FROM mappings_v3.default_mappings m WHERE id = :id").bind("id", toField(expected, ID)).map(toMessage("default_mappings", OptionalFields.kDescriptor, columnsFromAllFields(), withColumn("compact", MESSAGE), withColumn("other_message", CLOB_MESSAGE))).findFirst().orElseThrow(() -> new AssertionError("No content in default_mappings"));
        OptionalFields val2 = handle.createQuery("SELECT * FROM mappings_v3.default_mappings WHERE id = :id").bind("id", toField(empty, ID)).map(toMessage(OptionalFields.kDescriptor, columnsFromAllFields(), withColumn("compact", MESSAGE), withColumn("other_message", CLOB_MESSAGE))).findFirst().orElseThrow(() -> new AssertionError("No content in default_mappings"));
        assertThat(val, is(equalToMessage(expected)));
        assertThat(val2, is(equalToMessage(empty)));
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ID(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.ID) TIMESTAMP_MS(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.TIMESTAMP_MS) ProvidenceJdbi.withType(net.morimekta.providence.jdbi.v3.ProvidenceJdbi.withType) ProvidenceJdbi.forMessage(net.morimekta.providence.jdbi.v3.ProvidenceJdbi.forMessage) CLOB_MESSAGE(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.CLOB_MESSAGE) BLOB_DATA(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.BLOB_DATA) Assert.assertThat(org.junit.Assert.assertThat) Handle(org.jdbi.v3.core.Handle) ProvidenceJdbi.toField(net.morimekta.providence.jdbi.v3.ProvidenceJdbi.toField) TIMESTAMP_S(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.TIMESTAMP_S) INT_BOOL(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.INT_BOOL) ProvidenceJdbi.withColumn(net.morimekta.providence.jdbi.v3.ProvidenceJdbi.withColumn) OptionalFields(net.morimekta.test.providence.storage.jdbc.OptionalFields) Test(org.junit.Test) BASE64_DATA(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.BASE64_DATA) NormalFields(net.morimekta.test.providence.storage.jdbc.NormalFields) BINARY_MESSAGE(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.BINARY_MESSAGE) BLOB_MESSAGE(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.BLOB_MESSAGE) ProvidenceJdbi.toMessage(net.morimekta.providence.jdbi.v3.ProvidenceJdbi.toMessage) MESSAGE(net.morimekta.test.providence.storage.jdbc.OptionalFields._Field.MESSAGE) Rule(org.junit.Rule) SimpleGeneratorWatcher(net.morimekta.providence.testing.generator.SimpleGeneratorWatcher) CompactFields(net.morimekta.test.providence.storage.jdbc.CompactFields) Clock(java.time.Clock) ProvidenceMatchers.equalToMessage(net.morimekta.providence.testing.ProvidenceMatchers.equalToMessage) ProvidenceJdbi.columnsFromAllFields(net.morimekta.providence.jdbi.v3.ProvidenceJdbi.columnsFromAllFields) Types(java.sql.Types) OptionalFields(net.morimekta.test.providence.storage.jdbc.OptionalFields) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 13 with Bind

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

the class Handle method select.

/**
 * Convenience method which creates a query with the given positional arguments
 * @param sql SQL or named statement
 * @param args arguments to bind positionally
 * @return query object
 */
public Query select(String sql, Object... args) {
    Query query = this.createQuery(sql);
    int position = 0;
    for (Object arg : args) {
        query.bind(position++, arg);
    }
    return query;
}
Also used : Query(org.jdbi.v3.core.statement.Query)

Example 14 with Bind

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

the class TestNamedParams method testDemo.

@Test
public void testDemo() throws Exception {
    Handle h = dbRule.getSharedHandle();
    h.createUpdate("insert into something (id, name) values (:id, :name)").bind("id", 1).bind("name", "Brian").execute();
    h.execute("insert into something (id, name) values (?, ?)", 2, "Eric");
    h.execute("insert into something (id, name) values (?, ?)", 3, "Erin");
    List<Something> r = h.createQuery("select id, name from something " + "where name like :name " + "order by id").bind("name", "Eri%").mapToBean(Something.class).list();
    assertThat(r).extracting(Something::getId).containsExactly(2, 3);
}
Also used : Something(org.jdbi.v3.core.Something) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 15 with Bind

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

the class TestNamedParams method testMapKeyBinding.

@Test
public void testMapKeyBinding() throws Exception {
    Handle h = dbRule.openHandle();
    Update s = h.createUpdate("insert into something (id, name) values (:id, :name)");
    Map<String, Object> args = new HashMap<>();
    args.put("id", 0);
    args.put("name", "Keith");
    s.bindMap(args);
    int insert_count = s.execute();
    Query q = h.createQuery("select * from something where id = :id").bind("id", 0);
    final Something fromDb = q.mapToBean(Something.class).findOnly();
    assertThat(insert_count).isEqualTo(1);
    assertThat(fromDb).extracting(Something::getId, Something::getName).containsExactly(0, "Keith");
}
Also used : Query(org.jdbi.v3.core.statement.Query) HashMap(java.util.HashMap) Update(org.jdbi.v3.core.statement.Update) Something(org.jdbi.v3.core.Something) 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