use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class DatabaseIntegrationTest method selectRow.
@Test
void selectRow() {
Database database = testDatabase(dataSource, dialect);
long manufacturerId = newId();
ManufacturerRow aManufacturer = ManufacturerRow.newBuilder().manufacturerId(manufacturerId).name(Optional.of("Acclaimed Widgets")).build();
WidgetRow aWidget1 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturerId).name("Acclaimed 1").build();
WidgetRow aWidget2 = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturerId).name("Acclaimed 2").build();
database.insert(aManufacturer);
database.insert(aWidget1, aWidget2);
List<WidgetRow> acclaimedWidgets1 = database.from(ManufacturerRow.class, "m").join(WidgetRow.class, "w").on(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId).select(WidgetRow.class).where(ManufacturerRow::name).isEqualTo("Acclaimed Widgets").orderBy(WidgetRow::name, DESC).list();
List<Tuple2<String, WidgetRow>> acclaimedWidgets2 = database.from(ManufacturerRow.class, "m").join(WidgetRow.class, "w").on(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId).select(WidgetRow::name, "name").comma(WidgetRow.class).where(ManufacturerRow::name).isEqualTo("Acclaimed Widgets").orderBy(WidgetRow::name, Order.ASC).list();
assertThat(acclaimedWidgets1, hasSize(2));
assertThat(acclaimedWidgets1.get(0).name(), is("Acclaimed 2"));
assertThat(acclaimedWidgets1.get(1).name(), is("Acclaimed 1"));
assertThat(acclaimedWidgets2, hasSize(2));
assertThat(acclaimedWidgets2.get(0).item1(), is("Acclaimed 1"));
assertThat(acclaimedWidgets2.get(1).item1(), is("Acclaimed 2"));
assertThat(acclaimedWidgets2.get(0).item2().name(), is("Acclaimed 1"));
assertThat(acclaimedWidgets2.get(1).item2().name(), is("Acclaimed 2"));
}
use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class DatabaseIntegrationTest method selectFromDatabaseTwoTables.
@Test
void selectFromDatabaseTwoTables() {
Database database = testDatabase(dataSource, dialect);
long manufacturerId = newId();
ManufacturerRow aManufacturer = ManufacturerRow.newBuilder().manufacturerId(manufacturerId).name(Optional.of("Makers")).build();
WidgetRow aWidget = WidgetRow.newBuilder().widgetId(newId()).manufacturerId(manufacturerId).name("Dodacky").description(Optional.of("Thingamibob")).build();
database.insert(aManufacturer);
database.insert(aWidget);
Alias<WidgetRow> w = database.table(WidgetRow.class).as("w");
Alias<ManufacturerRow> m = database.table(ManufacturerRow.class).as("m");
Optional<WidgetRow> theSame = database.from(w).join(m).on(m, ManufacturerRow::manufacturerId).isEqualTo(w, WidgetRow::manufacturerId).where(w, WidgetRow::name).isEqualTo("Dodacky").and(w, WidgetRow::widgetId).isEqualTo(aWidget.widgetId()).optional().map(Tuple2::item1);
assertThat(theSame, is(Optional.of(aWidget)));
}
use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class UpdateTest method update.
@Test
void update() {
Database database = Database.newBuilder().defaultSchema("SIESTA").table(WidgetRow.class, t -> t.builder(WidgetRow.Builder::build)).build();
database.update(WidgetRow.class, "w").set(WidgetRow::name).to("Fred").set(WidgetRow::description).to(Optional.of("Bob")).where(WidgetRow::widgetId).isEqualTo(1L).and(column(WidgetRow::description).isBetween("A").and("w", WidgetRow::name).or(column(WidgetRow::description).isNull())).execute(transaction);
verify(transaction).update(sql.capture(), args.capture());
assertThat(sql.getValue(), is("update SIESTA.WIDGET w " + "set NAME = ?, " + "DESCRIPTION = ? " + "where w.WIDGET_ID = ? " + "and (" + "w.DESCRIPTION between ? and w.NAME " + "or w.DESCRIPTION is null" + ")"));
assertThat(args.getValue(), is(toArray("Fred", "Bob", 1L, "A")));
}
use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class DatabaseIntegrationTestH2 method selectSubquery.
@Test
void selectSubquery() {
Database database = testDatabase(dataSource);
long manufacturerId = newId();
ManufacturerRow manufacturer = ManufacturerRow.newBuilder().manufacturerId(manufacturerId).name(Optional.of("Doc Brown")).build();
WidgetRow[] widgets = IntStream.range(1, 4).mapToObj(i -> WidgetRow.newBuilder().widgetId(newId()).name("Flux capacitor " + i).manufacturerId(manufacturerId).build()).toArray(WidgetRow[]::new);
database.insert(manufacturer);
database.insert(widgets);
Tuple2<Long, Integer> result1 = database.from(ManufacturerRow.class).select(ManufacturerRow::manufacturerId).comma(database.from(WidgetRow.class).select(count()).where(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId)).where(ManufacturerRow::name).isEqualTo("Doc Brown").single();
Tuple2<Long, Integer> result2 = database.from(ManufacturerRow.class).select(ManufacturerRow::manufacturerId).comma(database.from(WidgetRow.class).select(count()).where(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId), "widget_count").where(ManufacturerRow::name).isEqualTo("Doc Brown").single();
Tuple2<Long, Integer> result3 = database.from(ManufacturerRow.class).select(ManufacturerRow::manufacturerId).comma(database.from(WidgetRow.class).select(count(), "wcount").where(WidgetRow::manufacturerId).isEqualTo(ManufacturerRow::manufacturerId), "wcount").where(ManufacturerRow::name).isEqualTo("Doc Brown").single();
assertThat(result1.item1(), is(manufacturerId));
assertThat(result1.item2(), is(3));
assertThat(result2.item1(), is(manufacturerId));
assertThat(result2.item2(), is(3));
assertThat(result3.item1(), is(manufacturerId));
assertThat(result3.item2(), is(3));
}
use of com.cadenzauk.siesta.model.WidgetRow in project siesta by cadenzauk.
the class CaseExpressionTest method initialWhen.
@ParameterizedTest
@MethodSource("parametersForInitialWhen")
void initialWhen(String expected, Function<Alias<WidgetRow>, CaseExpression<String>> f, Object[] expectedArgs) {
Database database = testDatabase(new AnsiDialect());
Alias<WidgetRow> w = database.table(WidgetRow.class).as("w");
database.from(w).select(f.apply(w).orElse(literal("DEFAULT")), "name").list(transaction);
verify(transaction).query(sql.capture(), args.capture(), any());
assertThat(sql.getValue(), is("select case " + expected + " else 'DEFAULT' end as name " + "from SIESTA.WIDGET w"));
assertThat(args.getValue(), is(expectedArgs));
}
Aggregations