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

JaQu improvements

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