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