Search in sources :

Example 51 with AnsiDialect

use of com.cadenzauk.siesta.dialect.AnsiDialect 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 52 with AnsiDialect

use of com.cadenzauk.siesta.dialect.AnsiDialect in project siesta by cadenzauk.

the class SelectGroupByTest method whereGroupBy.

@Test
void whereGroupBy() {
    Database database = TestDatabase.testDatabase(new AnsiDialect());
    database.from(WidgetRow.class, "w").select(WidgetRow::manufacturerId, "id").comma(max(WidgetRow::name), "name").where(WidgetRow::description).isLike("ABC%").groupBy(WidgetRow::manufacturerId).list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
    assertThat(sql.getValue(), is("select w.MANUFACTURER_ID as id, max(w.NAME) as name from SIESTA.WIDGET w " + "where w.DESCRIPTION like ? " + "group by w.MANUFACTURER_ID"));
    assertThat(args.getValue(), arrayWithSize(1));
    assertThat(args.getValue()[0], is("ABC%"));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) TestDatabase(com.cadenzauk.siesta.model.TestDatabase) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Example 53 with AnsiDialect

use of com.cadenzauk.siesta.dialect.AnsiDialect in project siesta by cadenzauk.

the class SelectGroupByTest method whereGroupByOrderBy.

@Test
void whereGroupByOrderBy() {
    Database database = TestDatabase.testDatabase(new AnsiDialect());
    database.from(WidgetRow.class, "w").select(WidgetRow::manufacturerId, "id").comma(max(WidgetRow::name), "name").where(WidgetRow::description).isLike("ABC%").groupBy(WidgetRow::manufacturerId).orderBy(WidgetRow::manufacturerId).then(max(WidgetRow::name)).list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
    assertThat(sql.getValue(), is("select w.MANUFACTURER_ID as id, max(w.NAME) as name from SIESTA.WIDGET w " + "where w.DESCRIPTION like ? " + "group by w.MANUFACTURER_ID " + "order by w.MANUFACTURER_ID asc, max(w.NAME) asc"));
    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) TestDatabase(com.cadenzauk.siesta.model.TestDatabase) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Example 54 with AnsiDialect

use of com.cadenzauk.siesta.dialect.AnsiDialect in project siesta by cadenzauk.

the class SelectHavingTest method having.

@ParameterizedTest
@MethodSource("parametersForHaving")
void having(BiFunction<Alias<WidgetRow>, ExpectingHaving<Tuple3<Long, String, String>>, Select<Tuple3<Long, String, String>>> having, String expectedSql, Object[] expectedArgs) {
    MockitoAnnotations.initMocks(this);
    Database database = TestDatabase.testDatabase(new AnsiDialect());
    Alias<WidgetRow> w = database.table(WidgetRow.class).as("w");
    having.apply(w, database.from(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 " + expectedSql));
    assertThat(args.getValue(), is(expectedArgs));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) TestDatabase(com.cadenzauk.siesta.model.TestDatabase) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 55 with AnsiDialect

use of com.cadenzauk.siesta.dialect.AnsiDialect 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

AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)56 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)36 Test (org.junit.jupiter.api.Test)32 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 WidgetRow (com.cadenzauk.siesta.model.WidgetRow)23 Database (com.cadenzauk.siesta.Database)16 MockitoTest (com.cadenzauk.core.MockitoTest)14 SalespersonRow (com.cadenzauk.siesta.model.SalespersonRow)10 TestDatabase (com.cadenzauk.siesta.model.TestDatabase)10 ManufacturerRow (com.cadenzauk.siesta.model.ManufacturerRow)8 UncheckedAutoCloseable (com.cadenzauk.core.lang.UncheckedAutoCloseable)5 InWhereExpectingAnd (com.cadenzauk.siesta.grammar.select.InWhereExpectingAnd)4 Scope (com.cadenzauk.siesta.Scope)3 Timestamp (java.sql.Timestamp)3 RandomValues.randomLocalDate (com.cadenzauk.core.RandomValues.randomLocalDate)2 RandomValues.randomLocalDateTime (com.cadenzauk.core.RandomValues.randomLocalDateTime)2 InvalidJoinException (com.cadenzauk.siesta.grammar.InvalidJoinException)2 LocalDate (java.time.LocalDate)2 LocalDateTime (java.time.LocalDateTime)2