Search in sources :

Example 46 with Database

use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.

the class SiestaExample method currentDateTest.

@Test
void currentDateTest() {
    Database database = Database.newBuilder().defaultSchema("TEST").defaultSqlExecutor(JdbcSqlExecutor.of(dataSource)).build();
    Tuple2<LocalDate, ZonedDateTime> today = database.select(currentDate()).comma(currentTimestamp()).single();
    System.out.println(today);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Database(com.cadenzauk.siesta.Database) LocalDate(java.time.LocalDate) Test(org.junit.jupiter.api.Test) IntegrationTest(com.cadenzauk.siesta.IntegrationTest)

Example 47 with Database

use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.

the class DeleteTest method update.

@Test
void update() {
    Database database = Database.newBuilder().defaultSchema("SIESTA").build();
    database.delete(WidgetRow.class).where(WidgetRow::widgetId).isEqualTo(2L).and(column(WidgetRow::description).isBetween("C").and(WidgetRow::name).or(column(WidgetRow::description).isNull())).execute(transaction);
    verify(transaction).update(sql.capture(), args.capture());
    assertThat(sql.getValue(), is("delete from SIESTA.WIDGET " + "where SIESTA.WIDGET.WIDGET_ID = ? " + "and (" + "SIESTA.WIDGET.DESCRIPTION between ? and SIESTA.WIDGET.NAME " + "or SIESTA.WIDGET.DESCRIPTION is null" + ")"));
    assertThat(args.getValue(), is(toArray(2L, "C")));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) Database(com.cadenzauk.siesta.Database) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Example 48 with Database

use of com.cadenzauk.siesta.Database in project siesta by cadenzauk.

the class ExpectingWhereTest method where.

@ParameterizedTest
@MethodSource("parametersForWhere")
void where(BiFunction<Alias<WidgetRow>, ExpectingWhere, ExecutableStatementClause> whereClause, String expectedSql, Object[] expectedArgs) {
    MockitoAnnotations.initMocks(this);
    Database database = Database.newBuilder().defaultSchema("SIESTA").build();
    Alias<WidgetRow> w = database.table(WidgetRow.class).as("w");
    whereClause.apply(w, database.delete(w)).execute(transaction);
    verify(transaction).update(sql.capture(), args.capture());
    assertThat(sql.getValue(), is("delete from SIESTA.WIDGET w where " + expectedSql));
    assertThat(args.getValue(), is(expectedArgs));
}
Also used : WidgetRow(com.cadenzauk.siesta.model.WidgetRow) Database(com.cadenzauk.siesta.Database) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 49 with Database

use of com.cadenzauk.siesta.Database 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)

Example 50 with Database

use of com.cadenzauk.siesta.Database 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)

Aggregations

Database (com.cadenzauk.siesta.Database)57 Test (org.junit.jupiter.api.Test)41 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)21 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)18 ZonedDateTime (java.time.ZonedDateTime)17 Optional (java.util.Optional)17 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)16 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)16 MethodSource (org.junit.jupiter.params.provider.MethodSource)16 Alias (com.cadenzauk.siesta.Alias)15 BigDecimal (java.math.BigDecimal)14 Collectors.joining (java.util.stream.Collectors.joining)14 AttributeOverride (javax.persistence.AttributeOverride)14 Embedded (javax.persistence.Embedded)14 EmbeddedId (javax.persistence.EmbeddedId)14 Id (javax.persistence.Id)14 CoreMatchers.is (org.hamcrest.CoreMatchers.is)14 SalespersonRow (com.cadenzauk.siesta.model.SalespersonRow)13 WidgetRow (com.cadenzauk.siesta.model.WidgetRow)7 MockitoTest (com.cadenzauk.core.MockitoTest)5