use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class SelectExpressionTest method concat.
@Test
void concat() {
Database database = testDatabase(new AnsiDialect());
Alias<WidgetRow> w = database.table(WidgetRow.class).as("w");
String sql = database.from(w).select(w.column(WidgetRow::name).concat(WidgetRow::description).concat(WidgetRow::manufacturerId), "name").where(w.column(WidgetRow::name).concat(WidgetRow::description).isEqualTo(value("Bob").concat(w.column(WidgetRow::description)))).sql();
assertThat(sql, is("select w.NAME || w.DESCRIPTION || w.MANUFACTURER_ID as name " + "from SIESTA.WIDGET w " + "where w.NAME || w.DESCRIPTION = ? || w.DESCRIPTION"));
}
use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class SelectGroupByTest method groupBy2.
@Test
void groupBy2() {
Database database = TestDatabase.testDatabase(new AnsiDialect());
database.from(WidgetRow.class, "w").select(WidgetRow::manufacturerId).comma(WidgetRow::description).comma(max(WidgetRow::name)).groupBy(WidgetRow::manufacturerId).comma(WidgetRow::description).list(transaction);
verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
assertThat(sql.getValue(), is("select w.MANUFACTURER_ID as w_MANUFACTURER_ID, w.DESCRIPTION as w_DESCRIPTION, max(w.NAME) as max_w_NAME " + "from SIESTA.WIDGET w " + "group by w.MANUFACTURER_ID, w.DESCRIPTION"));
assertThat(args.getValue(), arrayWithSize(0));
}
use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class DatabaseIntegrationTest method groupBy.
@Test
void groupBy() {
Database database = testDatabase(dataSource, dialect);
long manufacturer1 = newId();
long manufacturer2 = newId();
WidgetRow aWidget1 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturer1).name("Grouper 1").build();
WidgetRow aWidget2 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturer1).name("Grouper 2").build();
WidgetRow aWidget3 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturer2).name("Grouper 3").build();
database.insert(aWidget1, aWidget2, aWidget3);
List<Tuple3<Long, String, String>> result = database.from(WidgetRow.class).select(WidgetRow::manufacturerId).comma(max(WidgetRow::name)).comma(min(WidgetRow::name)).where(WidgetRow::widgetId).isIn(aWidget1.widgetId(), aWidget2.widgetId(), aWidget3.widgetId()).groupBy(WidgetRow::manufacturerId).orderBy(WidgetRow::manufacturerId, DESC).then(max(WidgetRow::name)).then(min(WidgetRow::name)).list();
assertThat(result.get(0).item1(), is(manufacturer2));
assertThat(result.get(0).item2(), is("Grouper 3"));
assertThat(result.get(0).item3(), is("Grouper 3"));
assertThat(result.get(1).item1(), is(manufacturer1));
assertThat(result.get(1).item2(), is("Grouper 2"));
assertThat(result.get(1).item3(), is("Grouper 1"));
}
use of com.cadenzauk.siesta.model.WidgetRow 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.WidgetRow in project siesta by cadenzauk.
the class DatabaseIntegrationTest method update.
@Test
void update() {
Database database = testDatabase(dataSource, dialect);
WidgetRow aWidget = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(newId()).name("Dodacky").description(Optional.of("Thingamibob")).build();
database.insert(aWidget);
int updated = database.update(WidgetRow.class).set(WidgetRow::name).to("Sprocket").set(WidgetRow::description).toNull().where(WidgetRow::widgetId).isEqualTo(aWidget.widgetId()).execute();
Optional<WidgetRow> sprocket = database.from(WidgetRow.class).where(WidgetRow::widgetId).isEqualTo(aWidget.widgetId()).optional();
assertThat(sprocket.map(WidgetRow::name), is(Optional.of("Sprocket")));
assertThat(sprocket.flatMap(WidgetRow::description), is(Optional.empty()));
assertThat(updated, is(1));
}
Aggregations