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());
});
}
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)));
}
}
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;
}
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);
}
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");
}
Aggregations