Search in sources :

Example 1 with Update

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

the class TimedAnnotationNameStrategyTest method testAnnotationOnClass.

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

Example 2 with Update

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

the class Handle method execute.

/**
 * Execute a SQL statement, and return the number of rows affected by the statement.
 *
 * @param sql the SQL statement to execute, using positional parameters (if any)
 * @param args positional arguments
 *
 * @return the number of rows affected
 */
public int execute(String sql, Object... args) {
    Update stmt = createUpdate(sql);
    int position = 0;
    for (Object arg : args) {
        stmt.bind(position++, arg);
    }
    return stmt.execute();
}
Also used : Update(org.jdbi.v3.core.statement.Update)

Example 3 with Update

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

the class TestStatements method testSimpleUpdate.

@Test
public void testSimpleUpdate() throws Exception {
    h.execute("insert into something (id, name) values (1, 'eric')");
    h.execute("update something set name = 'cire' where id = 1");
    Something eric = h.createQuery("select * from something where id = 1").mapToBean(Something.class).list().get(0);
    assertThat(eric.getName()).isEqualTo("cire");
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 4 with Update

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

the class TestStatements method testUpdate.

@Test
public void testUpdate() throws Exception {
    h.execute("insert into something (id, name) values (1, 'eric')");
    h.createUpdate("update something set name = 'ERIC' where id = 1").execute();
    Something eric = h.createQuery("select * from something where id = 1").mapToBean(Something.class).list().get(0);
    assertThat(eric.getName()).isEqualTo("ERIC");
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 5 with Update

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

the class TestUpdateGeneratedKeys method testUpdate.

@Test
public void testUpdate() 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 update = h.createUpdate("update something_else set name = :name where id = :id");
    update.bind("id", id1);
    update.bind("name", "Tom");
    Optional<Long> id2 = update.executeAndReturnGeneratedKeys().mapTo(long.class).findFirst();
    assertThat(id2.isPresent()).isFalse();
}
Also used : Handle(org.jdbi.v3.core.Handle) 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