Search in sources :

Example 11 with ManufacturerRow

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)"));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test)

Example 12 with ManufacturerRow

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 --"));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) Tuple2(com.cadenzauk.core.tuple.Tuple2) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 13 with ManufacturerRow

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")));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) WidgetViewRow(com.cadenzauk.siesta.model.WidgetViewRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 14 with ManufacturerRow

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%"));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) TestDatabase(com.cadenzauk.siesta.model.TestDatabase) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Example 15 with ManufacturerRow

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));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) ManufacturerRow(com.cadenzauk.siesta.model.ManufacturerRow) TestDatabase(com.cadenzauk.siesta.model.TestDatabase) WidgetViewRow(com.cadenzauk.siesta.model.WidgetViewRow) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Aggregations

ManufacturerRow (com.cadenzauk.siesta.model.ManufacturerRow)15 Test (org.junit.jupiter.api.Test)15 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)13 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)8 WidgetRow (com.cadenzauk.siesta.model.WidgetRow)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 Tuple2 (com.cadenzauk.core.tuple.Tuple2)5 MockitoTest (com.cadenzauk.core.MockitoTest)2 TestDatabase (com.cadenzauk.siesta.model.TestDatabase)2 WidgetViewRow (com.cadenzauk.siesta.model.WidgetViewRow)2 RandomValues (com.cadenzauk.core.RandomValues)1 Tuple5 (com.cadenzauk.core.tuple.Tuple5)1 Database (com.cadenzauk.siesta.Database)1 DatabaseIntegrationTest (com.cadenzauk.siesta.DatabaseIntegrationTest)1 InvalidJoinException (com.cadenzauk.siesta.grammar.InvalidJoinException)1 Aggregates.count (com.cadenzauk.siesta.grammar.expression.Aggregates.count)1 TypedExpression.column (com.cadenzauk.siesta.grammar.expression.TypedExpression.column)1 MoneyAmount (com.cadenzauk.siesta.model.MoneyAmount)1 PartRow (com.cadenzauk.siesta.model.PartRow)1 SalespersonRow (com.cadenzauk.siesta.model.SalespersonRow)1