@Deprecated public Cases<Timestamp, DateTimeExpression<Timestamp>> thenDateTime(Timestamp ts) { return then(ts); }
@Deprecated public Cases<java.util.Date, DateTimeExpression<java.util.Date>> thenDateTime(java.util.Date date) { return then(date); }
@Deprecated public Cases<java.sql.Date, DateExpression<java.sql.Date>> thenDate(java.sql.Date date) { return then(date); }
public <A> Cases<A, SimpleExpression<A>> then(Expression<A> expr) { if (expr instanceof Predicate) { return (Cases) then((Predicate) expr); } else if (expr instanceof StringExpression) { return (Cases) then((StringExpression) expr); } else if (expr instanceof NumberExpression) { return then((NumberExpression) expr); } else if (expr instanceof DateExpression) { return then((DateExpression) expr); } else if (expr instanceof DateTimeExpression) { return then((DateTimeExpression) expr); } else if (expr instanceof TimeExpression) { return then((TimeExpression) expr); } else if (expr instanceof ComparableExpression) { return then((ComparableExpression) expr); } else { return thenSimple(expr); } }
@Test public void Case2() { assertEquals(ImmutableList.of(4, 4, 4, 4, 4, 4), query().from(cat) .list(Expressions.cases().when(cat.toes.eq(2)).then(cat.id.multiply(2)) .when(cat.toes.eq(3)).then(cat.id.multiply(3)) .otherwise(4))); }
@Test public void Case3() { assertEquals(ImmutableList.of(4, 4, 4, 4, 4, 4), query().from(cat).list(Expressions.cases() .when(cat.toes.in(2, 3)).then(cat.id.multiply(cat.toes)) .otherwise(4))); }
@Test public void Case2_Hibernate() { QCat cat = QCat.cat; JPQLSerializer serializer = new JPQLSerializer(HQLTemplates.DEFAULT); Expression<?> expr = Expressions.cases().when(cat.toes.eq(2)).then(cat.id.multiply(2)) .when(cat.toes.eq(3)).then(cat.id.multiply(3)) .otherwise(4); serializer.handle(expr); assertEquals("case when (cat.toes = ?1) then (cat.id * ?1) when (cat.toes = ?2) then (cat.id * ?2) else 4 end", serializer.toString()); }
@Test public void Case2() { QCat cat = QCat.cat; JPQLSerializer serializer = new JPQLSerializer(JPQLTemplates.DEFAULT); Expression<?> expr = Expressions.cases().when(cat.toes.eq(2)).then(cat.id.multiply(2)) .when(cat.toes.eq(3)).then(cat.id.multiply(3)) .otherwise(4); serializer.handle(expr); assertEquals("case when (cat.toes = ?1) then (cat.id * ?1) when (cat.toes = ?2) then (cat.id * ?2) else ?3 end", serializer.toString()); }
@Test public void Case() { QCat cat = QCat.cat; JPQLSerializer serializer = new JPQLSerializer(JPQLTemplates.DEFAULT); Expression<?> expr = Expressions.cases().when(cat.toes.eq(2)).then(2) .when(cat.toes.eq(3)).then(3) .otherwise(4); serializer.handle(expr); assertEquals("case when (cat.toes = ?1) then ?1 when (cat.toes = ?2) then ?2 else ?3 end", serializer.toString()); }
@Test public void CaseBuilder() { QCat cat2 = new QCat("cat2"); NumberExpression<Integer> casex = new CaseBuilder() .when(cat.weight.isNull().and(cat.weight.isNull())).then(0) .when(cat.weight.isNull()).then(cat2.weight) .when(cat2.weight.isNull()).then(cat.weight) .otherwise(cat.weight.add(cat2.weight)); query().from(cat, cat2).orderBy(casex.asc()).list(cat.id, cat2.id); query().from(cat, cat2).orderBy(casex.desc()).list(cat.id, cat2.id); }
@Test public void Case_Hibernate() { QCat cat = QCat.cat; JPQLSerializer serializer = new JPQLSerializer(HQLTemplates.DEFAULT); Expression<?> expr = Expressions.cases().when(cat.toes.eq(2)).then(2) .when(cat.toes.eq(3)).then(3) .otherwise(4); serializer.handle(expr); assertEquals("case when (cat.toes = ?1) then ?1 when (cat.toes = ?2) then ?2 else 4 end", serializer.toString()); }