Search in sources :

Example 16 with WidgetRow

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

Example 17 with WidgetRow

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

Example 18 with WidgetRow

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

Example 19 with WidgetRow

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 --"));
}
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 20 with WidgetRow

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

Aggregations

WidgetRow (com.cadenzauk.siesta.model.WidgetRow)37 Test (org.junit.jupiter.api.Test)32 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)26 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)23 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)15 MockitoTest (com.cadenzauk.core.MockitoTest)9 ManufacturerRow (com.cadenzauk.siesta.model.ManufacturerRow)8 TestDatabase (com.cadenzauk.siesta.model.TestDatabase)8 Database (com.cadenzauk.siesta.Database)7 Tuple2 (com.cadenzauk.core.tuple.Tuple2)5 MethodSource (org.junit.jupiter.params.provider.MethodSource)5 Tuple3 (com.cadenzauk.core.tuple.Tuple3)2 TypedExpression.column (com.cadenzauk.siesta.grammar.expression.TypedExpression.column)2 WidgetViewRow (com.cadenzauk.siesta.model.WidgetViewRow)2 Optional (java.util.Optional)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2 Matchers.is (org.hamcrest.Matchers.is)2 RandomValues (com.cadenzauk.core.RandomValues)1 Tuple5 (com.cadenzauk.core.tuple.Tuple5)1 DatabaseIntegrationTest (com.cadenzauk.siesta.DatabaseIntegrationTest)1