Search in sources :

Example 31 with AnsiDialect

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

the class CaseExpressionTest method subsequentClauses.

@ParameterizedTest
@MethodSource("parametersForSubsequentClauses")
void subsequentClauses(String expected, BiFunction<CaseExpression<String>, Alias<WidgetRow>, TypedExpression<String>> f, Object[] expectedArgs) {
    Database database = testDatabase(new AnsiDialect());
    Alias<WidgetRow> w = database.table(WidgetRow.class).as("w");
    database.from(w).select(f.apply(Case.when(literal(1)).isEqualTo(literal(2)).then("UNLIKELY"), w), "name").list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), any());
    assertThat(sql.getValue(), is("select case when 1 = 2 then 'UNLIKELY' " + expected + " end as name " + "from SIESTA.WIDGET w"));
    assertThat(args.getValue(), is(expectedArgs));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Database(com.cadenzauk.siesta.Database) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 32 with AnsiDialect

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

the class CastExpressionTest method sql.

@ParameterizedTest
@MethodSource("parametersForSql")
<T> void sql(String type, Function<CastBuilder<String>, TypedExpression<T>> castFunction) {
    when(expression.sql(scope)).thenReturn("input");
    when(scope.database()).thenReturn(database);
    when(scope.dialect()).thenReturn(new AnsiDialect());
    CastBuilder<String> builder = new CastBuilder<>(expression);
    TypedExpression<T> sut = castFunction.apply(builder);
    String result = sut.sql(scope);
    assertThat(result, is("cast(input as " + type + ")"));
    verifyNoMoreInteractions(expression, scope, dialect, database, resultSet);
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 33 with AnsiDialect

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

the class ExpressionBuilderTest method isNotInWithNoValuesShouldThrow.

@Test
void isNotInWithNoValuesShouldThrow() {
    Database database = testDatabase(new AnsiDialect());
    ExpressionBuilder<String, InWhereExpectingAnd<SalespersonRow>> select = database.from(SalespersonRow.class).where(SalespersonRow::firstName);
    // noinspection Convert2MethodRef
    calling(() -> select.isNotIn(new String[0])).shouldThrow(IllegalArgumentException.class);
}
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) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MockitoTest(com.cadenzauk.core.MockitoTest)

Example 34 with AnsiDialect

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

the class ExpressionBuilderTest method isInWithNoValuesShouldThrow.

@Test
void isInWithNoValuesShouldThrow() {
    Database database = testDatabase(new AnsiDialect());
    ExpressionBuilder<String, InWhereExpectingAnd<SalespersonRow>> select = database.from(SalespersonRow.class).where(SalespersonRow::firstName);
    // noinspection Convert2MethodRef
    calling(() -> select.isIn(new String[0])).shouldThrow(IllegalArgumentException.class);
}
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) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MockitoTest(com.cadenzauk.core.MockitoTest)

Example 35 with AnsiDialect

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

the class ExpectingUnionTest method union.

@Test
void union() {
    Database database = testDatabase(new AnsiDialect());
    database.select(TypedExpression.value(1), "one").union(database.select(TypedExpression.literal(2), "two")).unionAll(database.select(TypedExpression.value(3), "three")).orderBy(1).list(transaction);
    verify(transaction).query(sql.capture(), args.capture(), rowMapper.capture());
    assertThat(sql.getValue(), is("select ? as one from DUAL union select 2 as two from DUAL union all select ? as three from DUAL order by 1 asc"));
    assertThat(args.getValue(), is(toArray(1, 3)));
}
Also used : AnsiDialect(com.cadenzauk.siesta.dialect.AnsiDialect) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) Database(com.cadenzauk.siesta.Database) 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