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));
}
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())));
}
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"));
}
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));
}
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())));
}
Aggregations