Line 1... |
Line 1... |
1 |
/*
|
1 |
/*
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
3 |
*
|
3 |
*
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
5 |
*
|
5 |
*
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
9 |
* language governing permissions and limitations under the License.
|
9 |
* language governing permissions and limitations under the License.
|
Line 23... |
Line 23... |
23 |
import org.openconcerto.sql.model.SQLSyntax;
|
23 |
import org.openconcerto.sql.model.SQLSyntax;
|
24 |
import org.openconcerto.sql.model.SQLSystem;
|
24 |
import org.openconcerto.sql.model.SQLSystem;
|
25 |
import org.openconcerto.sql.model.SQLTable;
|
25 |
import org.openconcerto.sql.model.SQLTable;
|
26 |
import org.openconcerto.sql.model.TableRef;
|
26 |
import org.openconcerto.sql.model.TableRef;
|
27 |
import org.openconcerto.sql.model.Where;
|
27 |
import org.openconcerto.sql.model.Where;
|
28 |
import org.openconcerto.utils.Tuple3.List3;
|
28 |
import org.openconcerto.sql.users.User;
|
29 |
|
29 |
|
30 |
import java.util.ArrayList;
|
30 |
import java.util.ArrayList;
|
31 |
import java.util.HashMap;
|
31 |
import java.util.HashMap;
|
32 |
import java.util.LinkedHashMap;
|
32 |
import java.util.LinkedHashMap;
|
33 |
import java.util.List;
|
33 |
import java.util.List;
|
Line 40... |
Line 40... |
40 |
*
|
40 |
*
|
41 |
* @author Sylvain
|
41 |
* @author Sylvain
|
42 |
*/
|
42 |
*/
|
43 |
public class UpdateBuilder {
|
43 |
public class UpdateBuilder {
|
44 |
|
44 |
|
- |
|
45 |
public final class VirtualJoin {
|
- |
|
46 |
private final String alias, definition, updateTableField, op, joinedTableField;
|
- |
|
47 |
|
- |
|
48 |
protected VirtualJoin(final String alias, final String definition, final String updateTableField, final String op, final String joinedTableField) {
|
- |
|
49 |
super();
|
- |
|
50 |
this.alias = alias;
|
- |
|
51 |
this.definition = definition;
|
- |
|
52 |
this.updateTableField = updateTableField;
|
- |
|
53 |
this.op = op;
|
- |
|
54 |
this.joinedTableField = joinedTableField;
|
- |
|
55 |
}
|
- |
|
56 |
|
- |
|
57 |
public final String getAlias() {
|
- |
|
58 |
return this.alias;
|
- |
|
59 |
}
|
- |
|
60 |
|
- |
|
61 |
public final String getDefinition() {
|
- |
|
62 |
return this.definition;
|
- |
|
63 |
}
|
- |
|
64 |
|
- |
|
65 |
public final String getUpdateTableField() {
|
- |
|
66 |
return this.updateTableField;
|
- |
|
67 |
}
|
- |
|
68 |
|
- |
|
69 |
public final String getJoinedTableField() {
|
- |
|
70 |
return this.joinedTableField;
|
- |
|
71 |
}
|
- |
|
72 |
|
- |
|
73 |
protected final String getWhere() {
|
- |
|
74 |
final SQLName joinedTableFieldName = new SQLName(getAlias(), this.getJoinedTableField());
|
- |
|
75 |
return Where.comparison(getSyntax(), getTable().getField(this.getUpdateTableField()).getSQLNameUntilDBRoot(false).quote(), this.op, joinedTableFieldName.quote());
|
- |
|
76 |
}
|
- |
|
77 |
|
- |
|
78 |
protected final String getSelect(final String value) {
|
- |
|
79 |
return "( select " + value + " from " + this.getDefinition() + " where " + this.getWhere() + " )";
|
- |
|
80 |
}
|
- |
|
81 |
}
|
- |
|
82 |
|
45 |
private final SQLTable t;
|
83 |
private final SQLTable t;
|
46 |
private final Map<String, String> fields;
|
84 |
private final Map<String, String> fields;
|
- |
|
85 |
private boolean addMetaData;
|
- |
|
86 |
private User user;
|
47 |
private final List<String> tables;
|
87 |
private final List<String> tables;
|
48 |
private Where where;
|
88 |
private Where where;
|
49 |
// alias -> definition, t field, field of the joined table
|
89 |
// alias -> VirtualJoin
|
50 |
private final Map<String, List3<String>> virtualJoins;
|
90 |
private final Map<String, VirtualJoin> virtualJoins;
|
51 |
private final Map<String, Boolean> virtualJoinsOptimized;
|
91 |
private final Map<String, Boolean> virtualJoinsOptimized;
|
52 |
|
92 |
|
53 |
public UpdateBuilder(SQLTable t) {
|
93 |
public UpdateBuilder(final SQLTable t) {
|
54 |
super();
|
94 |
super();
|
55 |
this.t = t;
|
95 |
this.t = t;
|
56 |
this.fields = new LinkedHashMap<String, String>();
|
96 |
this.fields = new LinkedHashMap<>();
|
- |
|
97 |
// this class is low-level and callers don't expect it to automatically add fields
|
- |
|
98 |
this.addMetaData = false;
|
- |
|
99 |
this.user = null;
|
57 |
this.tables = new ArrayList<String>();
|
100 |
this.tables = new ArrayList<>();
|
58 |
this.virtualJoins = new HashMap<String, List3<String>>(4);
|
101 |
this.virtualJoins = new HashMap<>(4);
|
59 |
this.virtualJoinsOptimized = new HashMap<String, Boolean>(4);
|
102 |
this.virtualJoinsOptimized = new HashMap<>(4);
|
60 |
}
|
103 |
}
|
61 |
|
104 |
|
62 |
public final SQLTable getTable() {
|
105 |
public final SQLTable getTable() {
|
63 |
return this.t;
|
106 |
return this.t;
|
64 |
}
|
107 |
}
|
65 |
|
108 |
|
- |
|
109 |
public final UpdateBuilder setAddMetaData(final boolean addMetaData) {
|
- |
|
110 |
this.addMetaData = addMetaData;
|
- |
|
111 |
return this;
|
- |
|
112 |
}
|
- |
|
113 |
|
- |
|
114 |
public final UpdateBuilder setUser(final User user) {
|
- |
|
115 |
this.setAddMetaData(true);
|
- |
|
116 |
this.user = user;
|
- |
|
117 |
return this;
|
- |
|
118 |
}
|
- |
|
119 |
|
66 |
public final SQLSyntax getSyntax() {
|
120 |
public final SQLSyntax getSyntax() {
|
67 |
return SQLSyntax.get(this.getTable());
|
121 |
return SQLSyntax.get(this.getTable());
|
68 |
}
|
122 |
}
|
69 |
|
123 |
|
70 |
private final void checkField(final String field) {
|
124 |
private final void checkField(final String field) {
|
Line 128... |
Line 182... |
128 |
* @see #addVirtualJoin(String, String, boolean, String, String, boolean)
|
182 |
* @see #addVirtualJoin(String, String, boolean, String, String, boolean)
|
129 |
*/
|
183 |
*/
|
130 |
public final UpdateBuilder setFromVirtualJoin(final String field, final String joinAlias, final String value) {
|
184 |
public final UpdateBuilder setFromVirtualJoin(final String field, final String joinAlias, final String value) {
|
131 |
final String val;
|
185 |
final String val;
|
132 |
if (this.isJoinVirtual(joinAlias)) {
|
186 |
if (this.isJoinVirtual(joinAlias)) {
|
133 |
final List3<String> virtualJoin = this.virtualJoins.get(joinAlias);
|
187 |
final VirtualJoin virtualJoin = this.virtualJoins.get(joinAlias);
|
134 |
val = "( select " + value + " from " + virtualJoin.get0() + " where " + getWhere(joinAlias, virtualJoin) + " )";
|
188 |
val = virtualJoin.getSelect(value);
|
135 |
} else {
|
189 |
} else {
|
136 |
val = value;
|
190 |
val = value;
|
137 |
}
|
191 |
}
|
138 |
return this.set(field, val);
|
192 |
return this.set(field, val);
|
139 |
}
|
193 |
}
|
140 |
|
194 |
|
141 |
private final String getWhere(final String joinAlias, final List3<String> virtualJoin) {
|
- |
|
142 |
assert this.virtualJoins.get(joinAlias) == virtualJoin;
|
- |
|
143 |
final SQLName joinedTableFieldName = new SQLName(joinAlias, virtualJoin.get2());
|
- |
|
144 |
return getTable().getField(virtualJoin.get1()).getSQLNameUntilDBRoot(false) + " = " + joinedTableFieldName.quote();
|
- |
|
145 |
}
|
- |
|
146 |
|
- |
|
147 |
public final Set<String> getFieldsNames() {
|
195 |
public final Set<String> getFieldsNames() {
|
148 |
return this.fields.keySet();
|
196 |
return this.fields.keySet();
|
149 |
}
|
197 |
}
|
150 |
|
198 |
|
151 |
public final boolean isEmpty() {
|
199 |
public final boolean isEmpty() {
|
152 |
return this.fields.isEmpty();
|
200 |
return this.fields.isEmpty();
|
153 |
}
|
201 |
}
|
154 |
|
202 |
|
155 |
public final void setWhere(Where where) {
|
203 |
public final void setWhere(final Where where) {
|
156 |
this.where = where;
|
204 |
this.where = where;
|
157 |
}
|
205 |
}
|
158 |
|
206 |
|
159 |
public final Where getWhere() {
|
207 |
public final Where getWhere() {
|
160 |
return this.where;
|
208 |
return this.where;
|
Line 177... |
Line 225... |
177 |
/**
|
225 |
/**
|
178 |
* Add table to this UPDATE.
|
226 |
* Add table to this UPDATE.
|
179 |
*
|
227 |
*
|
180 |
* @param definition the table to add, ie either a table name or a sub-select.
|
228 |
* @param definition the table to add, ie either a table name or a sub-select.
|
181 |
* @param rawAlias the SQL alias, can be <code>null</code>, e.g. <code>"t"</code>.
|
229 |
* @param rawAlias the SQL alias, can be <code>null</code>, e.g. <code>"t"</code>.
|
- |
|
230 |
* @see #addVirtualJoin(String, String, boolean, String, String, boolean)
|
182 |
*/
|
231 |
*/
|
183 |
public final void addRawTable(final String definition, final String rawAlias) {
|
232 |
public final void addRawTable(final String definition, final String rawAlias) {
|
184 |
this.tables.add(definition + (rawAlias == null ? "" : " " + rawAlias));
|
233 |
this.tables.add(definition + (rawAlias == null ? "" : " " + rawAlias));
|
185 |
}
|
234 |
}
|
186 |
|
235 |
|
Line 197... |
Line 246... |
197 |
public final void addVirtualJoin(final String definition, final String alias, final String joinedTableField) {
|
246 |
public final void addVirtualJoin(final String definition, final String alias, final String joinedTableField) {
|
198 |
this.addVirtualJoin(definition, alias, false, joinedTableField, getTable().getKey().getName());
|
247 |
this.addVirtualJoin(definition, alias, false, joinedTableField, getTable().getKey().getName());
|
199 |
}
|
248 |
}
|
200 |
|
249 |
|
201 |
public final void addVirtualJoin(final String definition, final String alias, final boolean aliasAlreadyDefined, final String joinedTableField, final String field) {
|
250 |
public final void addVirtualJoin(final String definition, final String alias, final boolean aliasAlreadyDefined, final String joinedTableField, final String field) {
|
202 |
this.addVirtualJoin(definition, alias, aliasAlreadyDefined, joinedTableField, field, true);
|
251 |
this.addVirtualJoin(definition, alias, aliasAlreadyDefined, joinedTableField, "=", field, true);
|
- |
|
252 |
}
|
- |
|
253 |
|
- |
|
254 |
public final VirtualJoin addVirtualJoin(final TableRef t, final String joinedTableField, final String op, final String field) {
|
- |
|
255 |
return this.addVirtualJoin(t.getSQL(), t.getAlias(), true, joinedTableField, op, field, true);
|
- |
|
256 |
}
|
- |
|
257 |
|
- |
|
258 |
public final VirtualJoin addVirtualJoin(final String definition, final String alias, final boolean aliasAlreadyDefined, final String joinedTableField, final String op, final String field) {
|
- |
|
259 |
return this.addVirtualJoin(definition, alias, aliasAlreadyDefined, joinedTableField, op, field, true);
|
203 |
}
|
260 |
}
|
204 |
|
261 |
|
205 |
/**
|
262 |
/**
|
206 |
* Add a virtual join to this UPDATE. Some systems don't support
|
263 |
* Add a virtual join to this UPDATE. Some systems don't support
|
207 |
* {@link #addRawTable(String, String) multiple tables}, this method is virtual in the sense
|
264 |
* {@link #addRawTable(String, String) multiple tables}, this method is virtual in the sense
|
Line 213... |
Line 270... |
213 |
* the <code>definition</code>. Needed for
|
270 |
* the <code>definition</code>. Needed for
|
214 |
* {@link SQLSyntax#getConstantTable(List, String, List) constant tables} since the alias
|
271 |
* {@link SQLSyntax#getConstantTable(List, String, List) constant tables} since the alias
|
215 |
* is already inside the definition, e.g. ( VALUES ... ) as "constTable"(field1, ...) .
|
272 |
* is already inside the definition, e.g. ( VALUES ... ) as "constTable"(field1, ...) .
|
216 |
* @param joinedTableField the field in the joined table that will match <code>field</code> of
|
273 |
* @param joinedTableField the field in the joined table that will match <code>field</code> of
|
217 |
* the update {@link #getTable() table}.
|
274 |
* the update {@link #getTable() table}.
|
- |
|
275 |
* @param op the operator to compare <code>joinedTableField</code> and <code>field</code>.
|
218 |
* @param field the field in the update {@link #getTable() table}.
|
276 |
* @param field the field in the update {@link #getTable() table}.
|
219 |
* @param optimize if <code>true</code> and if the system supports it, the virtual join will use
|
277 |
* @param optimize if <code>true</code> and if the system supports it, the virtual join will use
|
220 |
* the multiple table support.
|
278 |
* the multiple table support.
|
- |
|
279 |
* @return the new join.
|
221 |
*/
|
280 |
*/
|
222 |
public final void addVirtualJoin(final String definition, final String alias, final boolean aliasAlreadyDefined, final String joinedTableField, final String field, final boolean optimize) {
|
281 |
public final VirtualJoin addVirtualJoin(final String definition, final String alias, final boolean aliasAlreadyDefined, final String joinedTableField, final String op, final String field,
|
- |
|
282 |
final boolean optimize) {
|
223 |
if (alias == null)
|
283 |
if (alias == null)
|
224 |
throw new NullPointerException("No alias");
|
284 |
throw new NullPointerException("No alias");
|
225 |
if (this.virtualJoins.containsKey(alias))
|
285 |
if (this.virtualJoins.containsKey(alias))
|
226 |
throw new IllegalStateException("Alias already exists : " + alias);
|
286 |
throw new IllegalStateException("Alias already exists : " + alias);
|
227 |
this.checkField(field);
|
287 |
this.checkField(field);
|
228 |
final String completeDef = aliasAlreadyDefined ? definition : definition + ' ' + SQLBase.quoteIdentifier(alias);
|
288 |
final String completeDef = aliasAlreadyDefined ? definition : definition + ' ' + SQLBase.quoteIdentifier(alias);
|
229 |
this.virtualJoins.put(alias, new List3<String>(completeDef, field, joinedTableField));
|
289 |
final VirtualJoin res = new VirtualJoin(alias, completeDef, field, op, joinedTableField);
|
- |
|
290 |
this.virtualJoins.put(alias, res);
|
230 |
this.virtualJoinsOptimized.put(alias, optimize);
|
291 |
this.virtualJoinsOptimized.put(alias, optimize);
|
- |
|
292 |
return res;
|
231 |
}
|
293 |
}
|
232 |
|
294 |
|
233 |
public final String asString() {
|
295 |
public final String asString() {
|
234 |
// add tables and where for virtual joins
|
296 |
// add tables and where for virtual joins
|
235 |
Where computedWhere = this.where;
|
297 |
Where computedWhere = this.where;
|
236 |
final List<String> computedTables = new ArrayList<String>(this.tables);
|
298 |
final List<String> computedTables = new ArrayList<>(this.tables);
|
237 |
for (final Entry<String, List3<String>> e : this.virtualJoins.entrySet()) {
|
299 |
for (final Entry<String, VirtualJoin> e : this.virtualJoins.entrySet()) {
|
238 |
final String joinAlias = e.getKey();
|
300 |
final String joinAlias = e.getKey();
|
239 |
final List3<String> virtualJoin = e.getValue();
|
301 |
final VirtualJoin virtualJoin = e.getValue();
|
240 |
final Where w;
|
302 |
final Where w;
|
241 |
if (this.isJoinVirtual(joinAlias)) {
|
303 |
if (this.isJoinVirtual(joinAlias)) {
|
- |
|
304 |
// use same WHERE as setFromVirtualJoin()
|
242 |
w = Where.createRaw(SQLBase.quoteIdentifier(virtualJoin.get1()) + " in ( select " + SQLBase.quoteIdentifier(virtualJoin.get2()) + " from " + virtualJoin.get0() + " )");
|
305 |
w = Where.createRaw("EXISTS " + virtualJoin.getSelect("1"));
|
243 |
} else {
|
306 |
} else {
|
244 |
w = Where.createRaw(getWhere(joinAlias, virtualJoin));
|
307 |
w = Where.createRaw(virtualJoin.getWhere());
|
245 |
computedTables.add(virtualJoin.get0());
|
308 |
computedTables.add(virtualJoin.getDefinition());
|
246 |
}
|
309 |
}
|
247 |
computedWhere = w.and(computedWhere);
|
310 |
computedWhere = w.and(computedWhere);
|
248 |
}
|
311 |
}
|
249 |
final String w = computedWhere == null ? "" : "\nWHERE " + computedWhere.getClause();
|
312 |
final String w = computedWhere == null ? "" : "\nWHERE " + computedWhere.getClause();
|
- |
|
313 |
final Map<String, String> execFields;
|
- |
|
314 |
if (this.addMetaData) {
|
- |
|
315 |
execFields = new HashMap<>(this.fields);
|
- |
|
316 |
setFieldValue(execFields, this.getTable().getModifUserField(), this.user == null ? null : this.user.getId());
|
- |
|
317 |
setFieldValue(execFields, this.getTable().getModifDateField(), System.currentTimeMillis());
|
- |
|
318 |
} else {
|
- |
|
319 |
execFields = unmodifiableMap(this.fields);
|
- |
|
320 |
}
|
250 |
return "UPDATE " + this.getSyntax().getUpdate(this.getTable(), unmodifiableList(computedTables), unmodifiableMap(this.fields)) + w;
|
321 |
return "UPDATE " + this.getSyntax().getUpdate(this.getTable(), unmodifiableList(computedTables), execFields) + w;
|
- |
|
322 |
}
|
- |
|
323 |
|
- |
|
324 |
static private void setFieldValue(final Map<String, String> vals, final SQLField f, final Object val) {
|
- |
|
325 |
if (f == null)
|
- |
|
326 |
return;
|
- |
|
327 |
vals.put(f.getName(), val == null ? "DEFAULT" : f.getType().toString(val));
|
251 |
}
|
328 |
}
|
252 |
|
329 |
|
253 |
@Override
|
330 |
@Override
|
254 |
public String toString() {
|
331 |
public String toString() {
|
255 |
return this.getClass().getSimpleName() + ": " + this.asString();
|
332 |
return this.getClass().getSimpleName() + ": " + this.asString();
|