-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathQuery.java
More file actions
100 lines (85 loc) · 2.66 KB
/
Copy pathQuery.java
File metadata and controls
100 lines (85 loc) · 2.66 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package jaskell.sql;
import jaskell.parsec.Option;
import jaskell.script.Directive;
import jaskell.script.Parameter;
import java.sql.*;
import java.util.List;
import java.util.Optional;
public abstract class Query extends Statement implements CouldLimit, CouldOffset, CouldUnion {
public Union union(){
Union re = new Union();
re._prefix = this;
return re;
}
public Union union(Query query){
Union re = new Union();
re._prefix = this;
re._query = query;
return re;
}
public Limit limit(int l){
Limit re = new Limit(l);
re._prefix = this;
return re;
}
public Limit limit(Directive l){
Limit re = new Limit(l);
re._prefix = this;
return re;
}
public Offset offset(int o){
Offset re = new Offset(o);
re._prefix = this;
return re;
}
public Offset offset(Directive o){
Offset re = new Offset(o);
re._prefix = this;
return re;
}
public <T> Query setParameter(Object key, T value) throws IllegalArgumentException {
super.setParameter(key, value);
return this;
}
public ResultSet query(PreparedStatement statement) throws SQLException {
syncParameters(statement);
return statement.executeQuery();
}
public Optional scalar(Connection conn) throws SQLException {
try(PreparedStatement statement = conn.prepareStatement(this.script());
ResultSet resultSet = statement.executeQuery()){
if (resultSet.next()){
if(resultSet.getObject(1)!=null) {
return Optional.of(resultSet.getObject(1));
}
}
return Optional.empty();
}
}
public <T> Optional<T> scalar(Connection conn, Class<T> cls) throws SQLException {
try(PreparedStatement statement = conn.prepareStatement(this.script());
ResultSet resultSet = statement.executeQuery()){
if (resultSet.next()){
if(resultSet.getObject(1)!=null) {
return Optional.of(cls.cast(resultSet.getObject(1)));
}
}
return Optional.empty();
}
}
public Query cache(){
Query self = this;
return new Query() {
private String _script = self.script();
private List<Parameter<?>> _parameters = self.parameters();
@Override
public String script() {
return _script;
}
@Override
public List<Parameter<?>> parameters() {
return _parameters;
}
};
}
}