Search in sources :

Example 6 with Query

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();
}
Also used : ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) TreeSet(java.util.TreeSet) PMessage(net.morimekta.providence.PMessage) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PField(net.morimekta.providence.descriptor.PField) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Handle(org.jdbi.v3.core.Handle) Map(java.util.Map) Update(org.jdbi.v3.core.statement.Update) Collections(java.util.Collections) Update(org.jdbi.v3.core.statement.Update)

Example 7 with Query

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

Example 8 with 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");
}
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)

Example 9 with Query

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

Example 10 with Query

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

Aggregations

Test (org.junit.Test)9 Query (org.jdbi.v3.core.statement.Query)7 Jdbi (org.jdbi.v3.core.Jdbi)5 Something (org.jdbi.v3.core.Something)4 HashMap (java.util.HashMap)2 Handle (org.jdbi.v3.core.Handle)2 Update (org.jdbi.v3.core.statement.Update)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Type (java.lang.reflect.Type)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1