use of com.cadenzauk.siesta.model.ManufacturerRow in project siesta by cadenzauk.
the class SelectExpressionTest method orExpression.
@Test
void orExpression() {
Database database = testDatabase(new AnsiDialect());
Alias<ManufacturerRow> m = database.table(ManufacturerRow.class).as("m");
String sql = database.from(m).where(column(ManufacturerRow::checked).isLessThan(LocalDate.now()).or(ManufacturerRow::checked).isGreaterThan(LocalDate.now()).or(ManufacturerRow::name).isEqualTo("Fred")).and(column(ManufacturerRow::name).isEqualTo("Bob").or(ManufacturerRow::checked).isNull()).sql();
assertThat(sql, is("select m.MANUFACTURER_ID as m_MANUFACTURER_ID, m.NAME as m_NAME, m.CHECKED as m_CHECKED " + "from SIESTA.MANUFACTURER m " + "where (m.CHECKED < cast(? as date) or m.CHECKED > cast(? as date) or m.NAME = ?) " + "and (m.NAME = ? or m.CHECKED is null)"));
}
use of com.cadenzauk.siesta.model.ManufacturerRow in project siesta by cadenzauk.
the class DatabaseIntegrationTest method coalesceFunc.
@Test
void coalesceFunc() {
Database database = testDatabase(dataSource, dialect);
long manufacturer1 = newId();
long manufacturer2 = newId();
ManufacturerRow twoParts = ManufacturerRow.newBuilder().manufacturerId(manufacturer1).name(Optional.of("Two Parts")).build();
ManufacturerRow noParts = ManufacturerRow.newBuilder().manufacturerId(manufacturer2).name(Optional.of("No Parts")).build();
WidgetRow aWidget1 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturer1).name("Name 1").build();
WidgetRow aWidget2 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturer1).name("Name 2").description(Optional.of("Description 2")).build();
database.insert(noParts);
database.insert(twoParts);
database.insert(aWidget1);
database.insert(aWidget2);
List<Tuple2<String, String>> result = database.from(ManufacturerRow.class, "m").leftJoin(WidgetRow.class, "w").on(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId).select(ManufacturerRow::name).comma(coalesce(WidgetRow::description).orElse(WidgetRow::name).orElse("-- no parts --")).where(ManufacturerRow::manufacturerId).isIn(manufacturer1, manufacturer2).orderBy(ManufacturerRow::manufacturerId).then(WidgetRow::name).list();
assertThat(result, hasSize(3));
assertThat(result.get(0).item1(), is("Two Parts"));
assertThat(result.get(0).item2(), is("Name 1"));
assertThat(result.get(1).item1(), is("Two Parts"));
assertThat(result.get(1).item2(), is("Description 2"));
assertThat(result.get(2).item1(), is("No Parts"));
assertThat(result.get(2).item2(), is("-- no parts --"));
}
use of com.cadenzauk.siesta.model.ManufacturerRow 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.ManufacturerRow in project siesta by cadenzauk.
the class SelectGroupByTest method joinGroupBy.
@Test
void joinGroupBy() {
Database database = TestDatabase.testDatabase(new AnsiDialect());
database.from(WidgetRow.class, "w").leftJoin(ManufacturerRow.class, "m").on(ManufacturerRow::manufacturerId).isEqualTo(WidgetRow::manufacturerId).select(WidgetRow::manufacturerId, "id").comma(ManufacturerRow::name, "manufacturer").comma(max(WidgetRow::name), "name").where(WidgetRow::description).isLike("ABC%").groupBy(WidgetRow::manufacturerId).comma(ManufacturerRow::name).list(transaction);
verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
assertThat(sql.getValue(), is("select w.MANUFACTURER_ID as id, m.NAME as manufacturer, max(w.NAME) as name " + "from SIESTA.WIDGET w " + "left join SIESTA.MANUFACTURER m on m.MANUFACTURER_ID = w.MANUFACTURER_ID " + "where w.DESCRIPTION like ? " + "group by w.MANUFACTURER_ID, m.NAME"));
assertThat(args.getValue(), arrayWithSize(1));
assertThat(args.getValue()[0], is("ABC%"));
}
use of com.cadenzauk.siesta.model.ManufacturerRow in project siesta by cadenzauk.
the class SelectProjectionTest method projectIntoObject.
@Test
void projectIntoObject() {
Database database = TestDatabase.testDatabase(new AnsiDialect());
database.from(WidgetRow.class, "w").join(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).list(transaction);
verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
assertThat(sql.getValue(), is("select w.WIDGET_ID as v_WIDGET_ID, " + "w.NAME as v_WIDGET_NAME, " + "w.DESCRIPTION as v_DESCRIPTION, " + "w.MANUFACTURER_ID as v_MANUFACTURER_ID, " + "m.NAME as v_MANUFACTURER_NAME " + "from SIESTA.WIDGET w " + "join SIESTA.MANUFACTURER m on m.MANUFACTURER_ID = w.MANUFACTURER_ID"));
assertThat(args.getValue(), arrayWithSize(0));
}
Aggregations