Search in sources :

Example 26 with AnsiDialect

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

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

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

the class SelectGroupByTest method groupBy1.

@Test
void groupBy1() {
    Database database = TestDatabase.testDatabase(new AnsiDialect());
    database.from(WidgetRow.class, "w").select(WidgetRow::manufacturerId).comma(max(WidgetRow::name)).groupBy(WidgetRow::manufacturerId).list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
    assertThat(sql.getValue(), is("select w.MANUFACTURER_ID as w_MANUFACTURER_ID, max(w.NAME) as max_w_NAME " + "from SIESTA.WIDGET w " + "group by w.MANUFACTURER_ID"));
    assertThat(args.getValue(), arrayWithSize(0));
}
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 29 with AnsiDialect

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

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

the class BetweenBuilderTest method and.

@ParameterizedTest
@MethodSource("argsForAnd")
void and(BiFunction<BetweenBuilder<String, InWhereExpectingAnd<String>>, Alias<SalespersonRow>, InWhereExpectingAnd<String>> method, String expectedSql, Object[] expectedArgs) {
    MockitoAnnotations.initMocks(this);
    Database database = testDatabase(new AnsiDialect());
    Alias<SalespersonRow> alias = database.table(SalespersonRow.class).as("s");
    BetweenBuilder<String, InWhereExpectingAnd<String>> between = database.from(alias).select(SalespersonRow::firstName, "name").where(SalespersonRow::firstName).isBetween(SalespersonRow::middleNames);
    method.apply(between, alias).list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
    assertThat(sql.getValue(), is("select s.FIRST_NAME as name from SIESTA.SALESPERSON s where s.FIRST_NAME between s.MIDDLE_NAMES and " + expectedSql));
    assertThat(args.getValue(), is(expectedArgs));
}
Also used : InWhereExpectingAnd(com.cadenzauk.siesta.grammar.select.InWhereExpectingAnd) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Database(com.cadenzauk.siesta.Database) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

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