Search in sources :

Example 6 with Update

use of org.jdbi.v3.core.statement.Update in project jdbi by jdbi.

the class TestUpdateGeneratedKeys method testDelete.

@Test
public void testDelete() throws Exception {
    Handle h = dbRule.openHandle();
    Update insert = h.createUpdate("insert into something_else (name) values (:name)");
    insert.bind("name", "Brian");
    Long id1 = insert.executeAndReturnGeneratedKeys().mapTo(long.class).findOnly();
    assertThat(id1).isNotNull();
    Update delete = h.createUpdate("delete from something_else where id = :id");
    delete.bind("id", id1);
    Optional<Long> id2 = delete.executeAndReturnGeneratedKeys().mapTo(long.class).findFirst();
    assertThat(id2.isPresent()).isFalse();
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 7 with Update

use of org.jdbi.v3.core.statement.Update 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 8 with Update

use of org.jdbi.v3.core.statement.Update in project tutorials by eugenp.

the class JdbiTest method whenIdentityColumn_thenInsertReturnsNewId.

@Test
public void whenIdentityColumn_thenInsertReturnsNewId() {
    Jdbi jdbi = Jdbi.create("jdbc:hsqldb:mem:testDB", "sa", "");
    jdbi.useHandle(handle -> {
        handle.execute("create table PROJECT_2 (ID IDENTITY, NAME VARCHAR (50), URL VARCHAR (100))");
        Update update = handle.createUpdate("INSERT INTO PROJECT_2 (NAME, URL) VALUES ('tutorials', 'github.com/eugenp/tutorials')");
        ResultBearing generatedKeys = update.executeAndReturnGeneratedKeys();
        assertEquals(0, generatedKeys.mapToMap().findOnly().get("id"));
        update = handle.createUpdate("INSERT INTO PROJECT_2 (NAME, URL) VALUES ('REST with Spring', 'github.com/eugenp/REST-With-Spring')");
        assertEquals(1, generatedKeys.mapToMap().findOnly().get("id"));
    });
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) ResultBearing(org.jdbi.v3.core.result.ResultBearing) Update(org.jdbi.v3.core.statement.Update) Test(org.junit.Test)

Example 9 with Update

use of org.jdbi.v3.core.statement.Update 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 10 with Update

use of org.jdbi.v3.core.statement.Update in project metrics by dropwizard.

the class TimedAnnotationNameStrategyTest method testAnnotationOnMethod.

@Test
public void testAnnotationOnMethod() throws Exception {
    when(ctx.getExtensionMethod()).thenReturn(new ExtensionMethod(Foo.class, Foo.class.getMethod("update")));
    assertThat(timedAnnotationNameStrategy.getStatementName(ctx)).isEqualTo("com.codahale.metrics.jdbi3.strategies.TimedAnnotationNameStrategyTest$Foo.update");
}
Also used : ExtensionMethod(org.jdbi.v3.core.extension.ExtensionMethod) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)19 Handle (org.jdbi.v3.core.Handle)10 Update (org.jdbi.v3.core.statement.Update)8 Something (org.jdbi.v3.core.Something)6 Jdbi (org.jdbi.v3.core.Jdbi)4 ExtensionMethod (org.jdbi.v3.core.extension.ExtensionMethod)4 HashMap (java.util.HashMap)3 SqlUpdate (org.jdbi.v3.sqlobject.statement.SqlUpdate)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)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 Optional (java.util.Optional)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1