-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStatement.java
More file actions
112 lines (96 loc) · 3.58 KB
/
Copy pathStatement.java
File metadata and controls
112 lines (96 loc) · 3.58 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
101
102
103
104
105
106
107
108
109
110
111
112
package jaskell.sql;
import jaskell.script.Directive;
import jaskell.script.Parameter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
public abstract class Statement implements Directive {
public PreparedStatement prepare(Connection connection) throws SQLException {
return connection.prepareStatement(this.script());
}
public boolean execute(Connection connection) throws SQLException, IllegalStateException {
try(PreparedStatement statement = connection.prepareStatement(this.script())) {
syncParameters(statement);
return statement.execute();
}
}
public boolean execute(PreparedStatement statement) throws SQLException, IllegalStateException {
syncParameters(statement);
return statement.execute();
}
public <T> Statement setParameter(Object key, T value) throws IllegalArgumentException{
boolean flag = false;
for (Parameter<?> parameter:parameters()) {
if (Objects.equals(parameter.key(), key)) {
if(parameter.valueClass().isInstance(value)) {
@SuppressWarnings("unchecked")
Parameter<T> p = (Parameter<T>) parameter;
p.value(value);
} else {
throw new IllegalArgumentException(String.format("parameter %s typed %s but received %s",
parameter.key(), parameter.valueClass().getName(), value.getClass().getName()));
}
flag = true;
}
}
if(!flag){
throw new IllegalArgumentException(String.format("parameter named %s not found", key));
}
return this;
}
public void clear(PreparedStatement statement) throws SQLException {
statement.clearParameters();
}
public void syncParameters(PreparedStatement statement) throws SQLException {
List<Parameter<?>> parameters = parameters();
for (Parameter<?> parameter: parameters){
if(!parameter.confirmed()){
throw new IllegalStateException(String.format("parameter %s has not value", parameter.key()));
}
}
clear(statement);
List<Parameter<?>> params = parameters();
setOrder(params);
for (Parameter<?> parameter: params) {
//TODO: overload by parameter.valueClass
statement.setObject(parameter.order(), parameter.value());
}
}
void setOrder(List<Parameter<?>> parameters){
for (int i = 0; i < parameters.size(); i++) {
parameters.get(i).order(i+1);
}
}
public Returning returning(String names){
Returning re = new Returning(names);
re._prefix = this;
return re;
}
public Returning returning(String ... names){
Returning re = new Returning(names);
re._prefix = this;
return re;
}
public Returning returning(Directive names){
Returning re = new Returning(names);
re._prefix = this;
return re;
}
public Statement cache(){
Statement self = this;
return new Statement() {
private final String _script = self.script();
private final List<Parameter<?>> _parameters = self.parameters();
@Override
public String script() {
return _script;
}
@Override
public List<Parameter<?>> parameters() {
return _parameters;
}
};
}
}