use of com.cadenzauk.siesta.model.ManufacturerRow 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.ManufacturerRow 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.ManufacturerRow in project siesta by cadenzauk.
the class SelectExpressionTest method between.
@Test
void between() {
Database database = testDatabase(new AnsiDialect());
Alias<ManufacturerRow> m = database.table(ManufacturerRow.class).as("m");
String sql = database.from(m).where(ManufacturerRow::name).isBetween("Fred").and("Barney").or(ManufacturerRow::checked).isNull().and(ManufacturerRow::manufacturerId).isGreaterThan(3L).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.NAME between ? and ? " + "or m.CHECKED is null and m.MANUFACTURER_ID > ?"));
}
use of com.cadenzauk.siesta.model.ManufacturerRow in project siesta by cadenzauk.
the class SelectExpressionTest method andAndOrsInWhere.
@Test
void andAndOrsInWhere() {
Database database = testDatabase(new AnsiDialect());
Alias<ManufacturerRow> m = database.table(ManufacturerRow.class).as("m");
String sql = database.from(m).where(ManufacturerRow::name).isEqualTo("Fred").or(ManufacturerRow::checked).isNull().and(ManufacturerRow::manufacturerId).isGreaterThan(3L).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.NAME = ? or m.CHECKED is null and m.MANUFACTURER_ID > ?"));
}
use of com.cadenzauk.siesta.model.ManufacturerRow in project siesta by cadenzauk.
the class SelectExpressionTest method andExpression.
@Test
void andExpression() {
Database database = testDatabase(new AnsiDialect());
Alias<ManufacturerRow> m = database.table(ManufacturerRow.class).as("m");
String sql = database.from(m).where(ManufacturerRow::checked).isLessThan(LocalDate.now()).and(ManufacturerRow::name).isEqualTo("Fred").and(column(ManufacturerRow::name).isEqualTo("Bob").or(ManufacturerRow::checked).isNull()).and(ManufacturerRow::checked).isGreaterThan(LocalDate.now()).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) and m.NAME = ? and (m.NAME = ? or m.CHECKED is null) and m.CHECKED > cast(? as date)"));
}
Aggregations