use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class CountTest method countDistinct1.
@Test
void countDistinct1() {
Database database = TestDatabase.testDatabase(new AnsiDialect());
database.from(WidgetRow.class, "w").select(countDistinct(WidgetRow::name), "n").where(WidgetRow::manufacturerId).isEqualTo(4002L).list(transaction);
verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
assertThat(sql.getValue(), is("select count(distinct w.NAME) as n " + "from SIESTA.WIDGET w " + "where w.MANUFACTURER_ID = ?"));
assertThat(args.getValue(), arrayWithSize(1));
assertThat(args.getValue()[0], is(4002L));
}
use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class DatabaseIntegrationTest method leftJoinOfMissingIsNull.
@Test
void leftJoinOfMissingIsNull() {
Database database = testDatabase(dataSource, dialect);
long manufacturer1 = newId();
long manufacturer2 = newId();
ManufacturerRow twoParts = ManufacturerRow.newBuilder().manufacturerId(manufacturer1).name(Optional.of("Has a Part")).build();
ManufacturerRow noParts = ManufacturerRow.newBuilder().manufacturerId(manufacturer2).name(Optional.of("Has No Parts")).build();
WidgetRow aWidget1 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturer1).name("Name 1").build();
database.insert(noParts, twoParts);
database.insert(aWidget1);
List<Tuple2<ManufacturerRow, WidgetRow>> result = database.from(ManufacturerRow.class, "m").leftJoin(WidgetRow.class, "w").on(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId).where(ManufacturerRow::manufacturerId).isIn(manufacturer1, manufacturer2).orderBy(ManufacturerRow::manufacturerId).list();
assertThat(result, hasSize(2));
assertThat(result.get(0).item1().name(), is(Optional.of("Has a Part")));
assertThat(result.get(0).item2().name(), is("Name 1"));
assertThat(result.get(1).item1().name(), is(Optional.of("Has No Parts")));
assertThat(result.get(1).item2(), nullValue());
}
use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class DatabaseIntegrationTest method countAndCountDistinct.
@Test
void countAndCountDistinct() {
Database database = testDatabase(dataSource, dialect);
long manufacturer1 = newId();
long manufacturer2 = newId();
WidgetRow aWidget1 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturer1).name("Gizmo").build();
WidgetRow aWidget2 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturer1).name("Gizmo").build();
WidgetRow aWidget3 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturer2).name("Gizmo 2").build();
database.insert(aWidget1, aWidget2, aWidget3);
List<Tuple3<Long, Integer, Integer>> result = database.from(WidgetRow.class).select(WidgetRow::manufacturerId).comma(countDistinct(WidgetRow::name)).comma(count()).where(WidgetRow::widgetId).isIn(aWidget1.widgetId(), aWidget2.widgetId(), aWidget3.widgetId()).groupBy(WidgetRow::manufacturerId).orderBy(WidgetRow::manufacturerId, Order.ASC).list();
assertThat(result.get(0).item1(), is(manufacturer1));
assertThat(result.get(0).item2(), is(1));
assertThat(result.get(0).item3(), is(2));
assertThat(result.get(1).item1(), is(manufacturer2));
assertThat(result.get(1).item2(), is(1));
assertThat(result.get(1).item3(), is(1));
}
use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class DatabaseIntegrationTest method selectFromDatabaseOneTable.
@Test
void selectFromDatabaseOneTable() {
Database database = testDatabase(dataSource, dialect);
WidgetRow aWidget = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(newId()).name("Dodacky").description(Optional.of("Thingamibob")).build();
database.insert(aWidget);
Optional<WidgetRow> theSame = database.from(WidgetRow.class).where(WidgetRow::widgetId).isEqualTo(aWidget.widgetId()).optional();
assertThat(theSame, is(Optional.of(aWidget)));
}
use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class DatabaseIntegrationTest method caseExpression.
@Test
void caseExpression() {
Database database = testDatabase(dataSource, dialect);
long manufacturerId = newId();
WidgetRow doohicky = WidgetRow.newBuilder().widgetId(newId()).name("Doohicky").manufacturerId(manufacturerId).build();
WidgetRow doohickie = WidgetRow.newBuilder().widgetId(newId()).name("Doohickie").manufacturerId(manufacturerId).build();
WidgetRow doodad = WidgetRow.newBuilder().widgetId(newId()).name("Doodad").manufacturerId(manufacturerId).build();
WidgetRow doofer = WidgetRow.newBuilder().widgetId(newId()).name("Doofer").manufacturerId(manufacturerId).build();
database.insert(doohicky, doohickie, doodad, doofer);
List<String> result = database.from(WidgetRow.class).select(when(WidgetRow::name).isLike("Doohick%").then(lower(WidgetRow::name)).when(WidgetRow::name).isEqualTo(value("Doodad")).then(upper(WidgetRow::name)).orElse(WidgetRow::name)).where(WidgetRow::manufacturerId).isEqualTo(manufacturerId).orderBy(WidgetRow::widgetId).list();
assertThat(result.get(0), is("doohicky"));
assertThat(result.get(1), is("doohickie"));
assertThat(result.get(2), is("DOODAD"));
assertThat(result.get(3), is("Doofer"));
}
Aggregations