use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class DatabaseIntegrationTest method selectIntoView.
@Test
void selectIntoView() {
Database database = testDatabase(dataSource, dialect);
long manufacturerId = newId();
ManufacturerRow aManufacturer = ManufacturerRow.newBuilder().manufacturerId(manufacturerId).name(Optional.of("Acme")).build();
WidgetRow aWidget = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturerId).name("Gizmo").description(Optional.of("Acme's Patent Gizmo")).build();
database.insert(aManufacturer);
database.insert(aWidget);
Optional<WidgetViewRow> gizmo = database.from(WidgetRow.class, "w").leftJoin(ManufacturerRow.class, "m").on(ManufacturerRow::manufacturerId).isEqualTo(WidgetRow::manufacturerId).selectInto(WidgetViewRow.class, "v").with(WidgetRow::widgetId).as(WidgetViewRow::widgetId).with(WidgetRow::name).as(WidgetViewRow::widgetName).with(WidgetRow::description).as(WidgetViewRow::description).with(WidgetRow::manufacturerId).as(WidgetViewRow::manufacturerId).with(ManufacturerRow::name).as(WidgetViewRow::manufacturerName).where(WidgetRow::widgetId).isEqualTo(aWidget.widgetId()).optional();
assertThat(gizmo.map(WidgetViewRow::widgetName), is(Optional.of("Gizmo")));
assertThat(gizmo.flatMap(WidgetViewRow::manufacturerName), is(Optional.of("Acme")));
assertThat(gizmo.flatMap(WidgetViewRow::description), is(Optional.of("Acme's Patent Gizmo")));
}
use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class DeleteTest method update.
@Test
void update() {
Database database = Database.newBuilder().defaultSchema("SIESTA").build();
database.delete(WidgetRow.class).where(WidgetRow::widgetId).isEqualTo(2L).and(column(WidgetRow::description).isBetween("C").and(WidgetRow::name).or(column(WidgetRow::description).isNull())).execute(transaction);
verify(transaction).update(sql.capture(), args.capture());
assertThat(sql.getValue(), is("delete from SIESTA.WIDGET " + "where SIESTA.WIDGET.WIDGET_ID = ? " + "and (" + "SIESTA.WIDGET.DESCRIPTION between ? and SIESTA.WIDGET.NAME " + "or SIESTA.WIDGET.DESCRIPTION is null" + ")"));
assertThat(args.getValue(), is(toArray(2L, "C")));
}
use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class ExpectingWhereTest method where.
@ParameterizedTest
@MethodSource("parametersForWhere")
void where(BiFunction<Alias<WidgetRow>, ExpectingWhere, ExecutableStatementClause> whereClause, String expectedSql, Object[] expectedArgs) {
MockitoAnnotations.initMocks(this);
Database database = Database.newBuilder().defaultSchema("SIESTA").build();
Alias<WidgetRow> w = database.table(WidgetRow.class).as("w");
whereClause.apply(w, database.delete(w)).execute(transaction);
verify(transaction).update(sql.capture(), args.capture());
assertThat(sql.getValue(), is("delete from SIESTA.WIDGET w where " + expectedSql));
assertThat(args.getValue(), is(expectedArgs));
}
use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class CaseExpressionTest method subsequentClauses.
@ParameterizedTest
@MethodSource("parametersForSubsequentClauses")
void subsequentClauses(String expected, BiFunction<CaseExpression<String>, Alias<WidgetRow>, TypedExpression<String>> f, Object[] expectedArgs) {
Database database = testDatabase(new AnsiDialect());
Alias<WidgetRow> w = database.table(WidgetRow.class).as("w");
database.from(w).select(f.apply(Case.when(literal(1)).isEqualTo(literal(2)).then("UNLIKELY"), w), "name").list(transaction);
verify(transaction).query(sql.capture(), args.capture(), any());
assertThat(sql.getValue(), is("select case when 1 = 2 then 'UNLIKELY' " + expected + " end as name " + "from SIESTA.WIDGET w"));
assertThat(args.getValue(), is(expectedArgs));
}
use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class CountTest method countStar.
@Test
void countStar() {
Database database = TestDatabase.testDatabase(new AnsiDialect());
database.from(WidgetRow.class, "w").select(count(), "n").where(WidgetRow::manufacturerId).isEqualTo(4002L).list(transaction);
verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
assertThat(sql.getValue(), is("select count(*) as n " + "from SIESTA.WIDGET w " + "where w.MANUFACTURER_ID = ?"));
assertThat(args.getValue(), arrayWithSize(1));
assertThat(args.getValue()[0], is(4002L));
}
Aggregations