use of org.jdbi.v3.core.statement.Query in project providence by morimekta.
the class MessageInserter method execute.
public int execute(Handle handle, Collection<M> items) {
if (items.isEmpty()) {
throw new IllegalArgumentException("Nothing to insert");
}
String query = queryPrefix + items.stream().map(item -> valueMarkers).collect(Collectors.joining(", ")) + querySuffix;
Update update = handle.createUpdate(query);
int offset = 0;
for (M item : items) {
for (String column : columnOrder) {
F field = columnToFieldMap.get(column);
int type = columnTypeMap.get(column);
update.bind(offset++, new MessageFieldArgument<>(item, field, type));
}
}
return update.execute();
}
use of org.jdbi.v3.core.statement.Query 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.core.statement.Query 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");
}
use of org.jdbi.v3.core.statement.Query in project jdbi by jdbi.
the class TestQueries method testMappedQueryObjectWithNullForPrimitiveIntField.
@Test
public void testMappedQueryObjectWithNullForPrimitiveIntField() throws Exception {
h.execute("insert into something (id, name, intValue) values (1, 'eric', null)");
ResultIterable<Something> query = h.createQuery("select * from something order by id").mapToBean(Something.class);
List<Something> r = query.list();
Something eric = r.get(0);
assertThat(eric).isEqualTo(new Something(1, "eric"));
assertThat(eric.getIntValue()).isZero();
}
use of org.jdbi.v3.core.statement.Query in project jdbi by jdbi.
the class TestQueries method testMappedQueryObject.
@Test
public void testMappedQueryObject() throws Exception {
h.execute("insert into something (id, name) values (1, 'eric')");
h.execute("insert into something (id, name) values (2, 'brian')");
ResultIterable<Something> query = h.createQuery("select * from something order by id").mapToBean(Something.class);
List<Something> r = query.list();
assertThat(r.get(0)).isEqualTo(new Something(1, "eric"));
}
Aggregations