-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathUpdate.java
More file actions
129 lines (105 loc) · 3.26 KB
/
Copy pathUpdate.java
File metadata and controls
129 lines (105 loc) · 3.26 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package jaskell.sql;
import jaskell.script.Directive;
import jaskell.script.Parameter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Update implements Directive {
private Name _table;
public Update(String name){
this._table = new Name(name);
}
public Update(Name name){
this._table = name;
}
public Set set(String field, Directive value){
Set re = new Set(field, value);
re._prefix = this;
return re;
}
public Set set(Directive field, Directive value){
Set re = new Set(field, value);
re._prefix = this;
return re;
}
@Override
public String script() {
return String.format("UPDATE %s", _table.script());
}
@Override
public List<Parameter<?>> parameters() {
return _table.parameters();
}
public static class Set extends Query {
Directive _prefix;
List<Equation> _sets = new ArrayList<>();
Set(String field, Directive value){
_sets.add(new Equation(new Name(field), value));
}
public Set(Directive field, Directive value){
this._sets.add(new Equation(field, value));
}
public Set set(String field, Directive value){
_sets.add(new Equation(field, value));
return this;
}
public Set set(Directive field, Directive value){
_sets.add(new Equation(field, value));
return this;
}
@Override
public String script() {
return String.format("%s set %s",
_prefix.script(),
_sets.stream().map(Directive::script).collect(Collectors.joining(", ")));
}
@Override
public List<Parameter<?>> parameters() {
List<Parameter<?>> re = _prefix.parameters();
_sets.forEach(item -> re.addAll(item.parameters()));
return re;
}
public Using using(String names){
Using re = new Using(names);
re._prefix = this;
return re;
}
public Using using(String... names){
Using re = new Using(names);
re._prefix = this;
return re;
}
public Using using(Directive ... names){
Using re = new Using(names);
re._prefix = this;
return re;
}
public Where where(Predicate predicate){
Where re = new Where(predicate);
re._prefix = this;
return re;
}
}
public static class Equation implements Directive {
Directive _field;
Directive _value;
Equation(String field, Directive value){
this._field = new Name(field);
this._value = value;
}
Equation(Directive field, Directive value){
this._field = field;
this._value = value;
}
@Override
public String script() {
return String.format("%s=%s", _field.script(), _value.script());
}
@Override
public List<Parameter<?>> parameters() {
List<Parameter<?>> re = _field.parameters();
re.addAll(_value.parameters());
return re;
}
}
}