-
Notifications
You must be signed in to change notification settings - Fork 1
/
AggregateOp.java
49 lines (37 loc) · 1.17 KB
/
AggregateOp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.novarto.sanedbc.core.ops;
import fj.control.db.DB;
import fj.function.TryEffect1;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import static com.novarto.sanedbc.core.ops.Binders.NO_BINDER;
public class AggregateOp extends DB<Long>
{
private final SelectOp.List<Long> selectOp;
public AggregateOp(String sql, TryEffect1<PreparedStatement, SQLException> binder)
{
this.selectOp = new SelectOp.List<>(sql, binder, x -> x.getLong(1));
}
public AggregateOp(String sql)
{
this(sql, NO_BINDER);
}
@Override public Long run(Connection c) throws SQLException
{
return selectOp.map(xs ->
{
Iterator<Long> resultIterator = xs.iterator();
if (!resultIterator.hasNext())
{
throw new IllegalStateException("result is empty");
}
Long result = resultIterator.next();
if (resultIterator.hasNext())
{
throw new IllegalStateException("result has more than one row");
}
return result;
}).run(c);
}
}