Search in sources :

Example 6 with SalespersonRow

use of com.cadenzauk.siesta.model.SalespersonRow in project siesta by cadenzauk.

the class DatabaseIntegrationTest method updateWithExpression.

@Test
void updateWithExpression() {
    Database database = testDatabase(dataSource);
    SalespersonRow salespersonRow = aRandomSalesperson();
    database.insert(salespersonRow);
    database.update(SalespersonRow.class).set(SalespersonRow::numberOfSales).to(20).plus(literal(2)).times(literal(5).plus(literal(3))).plus(literal(11)).where(SalespersonRow::salespersonId).isEqualTo(salespersonRow.salespersonId()).execute();
    database.update(SalespersonRow.class).set(SalespersonRow::numberOfSales).to(SalespersonRow::numberOfSales).plus(1).where(SalespersonRow::salespersonId).isEqualTo(salespersonRow.salespersonId()).execute();
    int result = database.from(SalespersonRow.class).select(SalespersonRow::numberOfSales).where(SalespersonRow::salespersonId).isEqualTo(salespersonRow.salespersonId()).single();
    assertThat(result, is(48));
}
Also used : TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with SalespersonRow

use of com.cadenzauk.siesta.model.SalespersonRow in project siesta by cadenzauk.

the class DatabaseIntegrationTest method olapWithPartitionAndOrder.

@Test
void olapWithPartitionAndOrder() {
    assumeTrue(dialect.supportsPartitionByInOlap() && dialect.supportsOrderByInOlap(), dialect.getClass().getSimpleName() + " does not support PARTITION BY/ORDER BY in OLAP functions.");
    Database database = testDatabase(dataSource, dialect);
    Tuple2<Long, Long> inserted = insertSalespeople(database, 5);
    List<Tuple3<Long, Integer, Long>> result = database.from(SalespersonRow.class).select(SalespersonRow::salespersonId).comma(Olap.rowNumber().partitionBy(SalespersonRow::salespersonId).orderBy(SalespersonRow::salespersonId, DESC).then(SalespersonRow::surname, ASC)).comma(Olap.sum(SalespersonRow::salespersonId).partitionBy(SalespersonRow::salespersonId).orderBy(SalespersonRow::salespersonId, DESC)).where(SalespersonRow::salespersonId).isBetween(inserted.item1()).and(inserted.item2()).orderBy(SalespersonRow::salespersonId).list();
    assertThat(result, hasSize(5));
    assertThat(result.get(0), is(Tuple.of(inserted.item1(), 1, inserted.item1())));
    assertThat(result.get(4), is(Tuple.of(inserted.item2(), 1, inserted.item2())));
}
Also used : Tuple3(com.cadenzauk.core.tuple.Tuple3) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with SalespersonRow

use of com.cadenzauk.siesta.model.SalespersonRow in project siesta by cadenzauk.

the class DatabaseIntegrationTest method selectIn.

@Test
void selectIn() {
    Database database = testDatabase(dataSource, dialect);
    SalespersonRow fred = SalespersonRow.newBuilder().salespersonId(newId()).firstName("Fred").surname("Smith").build();
    SalespersonRow bruce = SalespersonRow.newBuilder().salespersonId(newId()).firstName("Bruce").surname("Smith").build();
    SalespersonRow trevor = SalespersonRow.newBuilder().salespersonId(newId()).firstName("Trevor").surname("Dagg").build();
    database.insert(fred, bruce, trevor);
    List<SalespersonRow> smiths1 = database.from(SalespersonRow.class).where(SalespersonRow::salespersonId).isIn(database.from(SalespersonRow.class, "s").select(SalespersonRow::salespersonId).where(SalespersonRow::surname).isEqualTo("Smith")).orderBy(SalespersonRow::firstName).list();
    List<SalespersonRow> smiths2 = database.from(SalespersonRow.class).where(SalespersonRow::salespersonId).isNotIn(database.from(SalespersonRow.class, "s").select(SalespersonRow::salespersonId).where(SalespersonRow::surname).isNotEqualTo("Smith")).orderBy(SalespersonRow::firstName, DESC).list();
    assertThat(smiths1, hasSize(2));
    assertThat(smiths1.get(0).firstName(), is("Bruce"));
    assertThat(smiths1.get(1).firstName(), is("Fred"));
    assertThat(smiths2, hasSize(2));
    assertThat(smiths2.get(0).firstName(), is("Fred"));
    assertThat(smiths2.get(1).firstName(), is("Bruce"));
}
Also used : TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with SalespersonRow

use of com.cadenzauk.siesta.model.SalespersonRow in project siesta by cadenzauk.

the class DatabaseIntegrationTest method commonTableExpression.

@Test
void commonTableExpression() {
    assumeFalse(dialect instanceof H2Dialect, "H2 is buggy");
    Database database = testDatabase(dataSource, dialect);
    Tuple2<Long, Long> inserted = insertSalespeople(database, 10);
    CommonTableExpression<SalespersonRow> first5 = database.with("first5").as(database.from(SalespersonRow.class).where(SalespersonRow::salespersonId).isBetween(inserted.item1()).and(inserted.item2()).orderBy(SalespersonRow::salespersonId).fetchFirst(5));
    CommonTableExpression<SalespersonRow> last2 = database.with("last2").as(database.from(first5, "f5").orderBy(SalespersonRow::salespersonId, DESC).fetchFirst(2));
    List<SalespersonRow> lastTwo = database.from(last2, "l2").list();
    assertThat(lastTwo, hasSize(2));
    assertThat(lastTwo.get(0).salespersonId(), is(inserted.item1() + 4));
    assertThat(lastTwo.get(1).salespersonId(), is(inserted.item1() + 3));
}
Also used : H2Dialect(com.cadenzauk.siesta.dialect.H2Dialect) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with SalespersonRow

use of com.cadenzauk.siesta.model.SalespersonRow in project siesta by cadenzauk.

the class DatabaseIntegrationTest method olapWithPartition.

@Test
void olapWithPartition() {
    assumeTrue(dialect.supportsPartitionByInOlap(), dialect.getClass().getSimpleName() + " does not support PARTITION BY in OLAP functions.");
    Database database = testDatabase(dataSource, dialect);
    Tuple2<Long, Long> inserted = insertSalespeople(database, 5);
    TypedExpression<Integer> rowNumber = dialect.requiresOrderByInRowNumber() ? Olap.rowNumber().partitionBy(column(SalespersonRow::salespersonId)).orderBy(SalespersonRow::salespersonId, ASC) : Olap.rowNumber().partitionBy(column(SalespersonRow::salespersonId));
    List<Tuple3<Long, Integer, Long>> result = database.from(SalespersonRow.class).select(SalespersonRow::salespersonId).comma(rowNumber).comma(Olap.sum(SalespersonRow::salespersonId).partitionBy(SalespersonRow::salespersonId).then(SalespersonRow::firstName)).where(SalespersonRow::salespersonId).isBetween(inserted.item1()).and(inserted.item2()).orderBy(SalespersonRow::salespersonId).list();
    assertThat(result, hasSize(5));
    assertThat(result.get(0), is(Tuple.of(inserted.item1(), 1, inserted.item1())));
    assertThat(result.get(4), is(Tuple.of(inserted.item2(), 1, inserted.item2())));
}
Also used : Tuple3(com.cadenzauk.core.tuple.Tuple3) TestDatabase.testDatabase(com.cadenzauk.siesta.model.TestDatabase.testDatabase) SalespersonRow(com.cadenzauk.siesta.model.SalespersonRow) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

SalespersonRow (com.cadenzauk.siesta.model.SalespersonRow)39 Test (org.junit.jupiter.api.Test)31 TestDatabase.testDatabase (com.cadenzauk.siesta.model.TestDatabase.testDatabase)27 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 Database (com.cadenzauk.siesta.Database)12 AnsiDialect (com.cadenzauk.siesta.dialect.AnsiDialect)10 MockitoTest (com.cadenzauk.core.MockitoTest)8 MethodSource (org.junit.jupiter.params.provider.MethodSource)8 TestDatabase (com.cadenzauk.siesta.model.TestDatabase)6 InWhereExpectingAnd (com.cadenzauk.siesta.grammar.select.InWhereExpectingAnd)4 Tuple3 (com.cadenzauk.core.tuple.Tuple3)3 Scope (com.cadenzauk.siesta.Scope)3 DatabaseIntegrationTest (com.cadenzauk.siesta.DatabaseIntegrationTest)2 IncorrectSalesAreaRow (com.cadenzauk.siesta.model.IncorrectSalesAreaRow)2 SalesAreaRow (com.cadenzauk.siesta.model.SalesAreaRow)2 LockingException (com.cadenzauk.core.sql.exception.LockingException)1 Tuple2 (com.cadenzauk.core.tuple.Tuple2)1 H2Dialect (com.cadenzauk.siesta.dialect.H2Dialect)1 Objects (java.util.Objects)1 CompletableFuture (java.util.concurrent.CompletableFuture)1