提交 d3609029 authored 作者: Thomas Mueller's avatar Thomas Mueller

JaQu improvements

上级 1838ff22
......@@ -49,13 +49,14 @@ public class SamplesTest extends TestBase {
db.insertAll(Product.getProductList());
db.insertAll(Customer.getCustomerList());
db.insertAll(Order.getOrderList());
// TODO SUM, MIN, MAX, LIKE, LIKE ESCAPE...
// TODO MIN, MAX, LIKE, LIKE ESCAPE...
// TODO +, -, *, /, ||, nested operations
// TODO nested AND/OR
// TODO NOT
// TODO DELETE: FROM ... DELETE?
// TODO UPDATE: FROM ... UPDATE?
// TODO SELECT UNION
testSum();
testLength();
testCount();
testGroup();
......@@ -254,6 +255,16 @@ public class SamplesTest extends TestBase {
assertEquals("[4, 5]", s);
}
private void testSum() throws Exception {
Product p = new Product();
Integer sum = db.from(p).
selectFirst(sum(p.unitsInStock));
assertEquals(323, sum.intValue());
Double sumPrice = db.from(p).
selectFirst(sum(p.unitPrice));
assertEquals(313.35, sumPrice.doubleValue());
}
private void testCount() throws Exception {
long count = db.from(new Product()).selectCount();
assertEquals(10, count);
......
......@@ -39,6 +39,11 @@ public class Function implements Token {
Utils.newObject(Integer.class), new Function("LENGTH", x));
}
public static <T extends Number> T sum(T x) {
return (T) Db.registerToken(
Utils.newObject(x.getClass()), new Function("SUM", x));
}
public static Long count(Object x) {
return Db.registerToken(
Utils.newObject(Long.class), new Function("COUNT", x));
......
......@@ -62,6 +62,11 @@ public class Query<T> {
public List<T> selectDistinct() {
return select(true);
}
public <X, Z> X selectFirst(Z x) {
List<X> list = (List<X>) select(x);
return list.isEmpty() ? null : list.get(0);
}
private List<T> select(boolean distinct) {
List<T> result = Utils.newArrayList();
......
......@@ -42,6 +42,11 @@ public class QueryWhere<T> {
return (List<X>) query.selectDistinct(x);
}
public <X, Z> X selectFirst(Z x) {
List<X> list = (List<X>) query.select(x);
return list.isEmpty() ? null : list.get(0);
}
public List<T> select() {
return query.select();
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论