17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
182 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
17 |
ilm |
5 |
*
|
|
|
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
|
|
|
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.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.sql.model;
|
|
|
15 |
|
83 |
ilm |
16 |
import org.openconcerto.sql.Configuration;
|
182 |
ilm |
17 |
import org.openconcerto.sql.element.SQLElement;
|
73 |
ilm |
18 |
import org.openconcerto.sql.model.graph.SQLKey;
|
|
|
19 |
import org.openconcerto.sql.model.graph.TablesMap;
|
83 |
ilm |
20 |
import org.openconcerto.sql.preferences.SQLPreferences;
|
73 |
ilm |
21 |
import org.openconcerto.sql.utils.AlterTable;
|
|
|
22 |
import org.openconcerto.sql.utils.ChangeTable;
|
|
|
23 |
import org.openconcerto.sql.utils.SQLCreateTable;
|
|
|
24 |
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
|
185 |
ilm |
25 |
import org.openconcerto.utils.GestionDevise;
|
83 |
ilm |
26 |
import org.openconcerto.utils.StringUtils;
|
73 |
ilm |
27 |
import org.openconcerto.utils.cc.ITransformer;
|
17 |
ilm |
28 |
|
|
|
29 |
import java.sql.SQLException;
|
177 |
ilm |
30 |
import java.text.DateFormat;
|
|
|
31 |
import java.text.SimpleDateFormat;
|
17 |
ilm |
32 |
import java.util.ArrayList;
|
73 |
ilm |
33 |
import java.util.Arrays;
|
17 |
ilm |
34 |
import java.util.HashMap;
|
177 |
ilm |
35 |
import java.util.HashSet;
|
73 |
ilm |
36 |
import java.util.List;
|
17 |
ilm |
37 |
import java.util.Map;
|
73 |
ilm |
38 |
import java.util.Set;
|
17 |
ilm |
39 |
|
|
|
40 |
public class SQLInjector {
|
|
|
41 |
|
73 |
ilm |
42 |
private final SQLTable tableSrc, tableDest;
|
|
|
43 |
private final ArrayList<SQLField> from = new ArrayList<SQLField>();
|
|
|
44 |
private final ArrayList<SQLField> to = new ArrayList<SQLField>();
|
|
|
45 |
private final Map<SQLField, Object> values = new HashMap<SQLField, Object>();
|
|
|
46 |
private final static Map<DBRoot, Map<SQLTable, Map<SQLTable, SQLInjector>>> allRegisteredInjectors = new HashMap<DBRoot, Map<SQLTable, Map<SQLTable, SQLInjector>>>();
|
17 |
ilm |
47 |
|
73 |
ilm |
48 |
private boolean storeTransfer;
|
|
|
49 |
// maps of injectors that store transfer
|
|
|
50 |
private static Map<DBRoot, Map<SQLTable, Map<SQLTable, SQLInjector>>> injectors = new HashMap<DBRoot, Map<SQLTable, Map<SQLTable, SQLInjector>>>();
|
|
|
51 |
|
|
|
52 |
public SQLInjector(final DBRoot r, final String src, final String dest, boolean storeTransfer) {
|
|
|
53 |
this(r.findTable(src), r.findTable(dest), storeTransfer);
|
17 |
ilm |
54 |
}
|
|
|
55 |
|
73 |
ilm |
56 |
public SQLInjector(SQLTable src, SQLTable dest, boolean storeTransfer) {
|
17 |
ilm |
57 |
this.tableDest = dest;
|
|
|
58 |
this.tableSrc = src;
|
73 |
ilm |
59 |
this.storeTransfer = storeTransfer;
|
|
|
60 |
final DBRoot dbRoot = src.getDBRoot();
|
|
|
61 |
Map<SQLTable, Map<SQLTable, SQLInjector>> inj = allRegisteredInjectors.get(dbRoot);
|
|
|
62 |
if (inj == null) {
|
|
|
63 |
inj = new HashMap<SQLTable, Map<SQLTable, SQLInjector>>();
|
|
|
64 |
allRegisteredInjectors.put(dbRoot, inj);
|
|
|
65 |
}
|
|
|
66 |
Map<SQLTable, SQLInjector> srcs = inj.get(src);
|
17 |
ilm |
67 |
if (srcs == null) {
|
|
|
68 |
srcs = new HashMap<SQLTable, SQLInjector>();
|
73 |
ilm |
69 |
inj.put(src, srcs);
|
17 |
ilm |
70 |
}
|
|
|
71 |
srcs.put(dest, this);
|
73 |
ilm |
72 |
|
|
|
73 |
if (storeTransfer) {
|
|
|
74 |
// Register only SQLInjector that store transfer
|
|
|
75 |
inj = injectors.get(dbRoot);
|
|
|
76 |
if (inj == null) {
|
|
|
77 |
inj = new HashMap<SQLTable, Map<SQLTable, SQLInjector>>();
|
|
|
78 |
injectors.put(dbRoot, inj);
|
|
|
79 |
}
|
|
|
80 |
srcs = inj.get(src);
|
|
|
81 |
if (srcs == null) {
|
|
|
82 |
srcs = new HashMap<SQLTable, SQLInjector>();
|
|
|
83 |
inj.put(src, srcs);
|
|
|
84 |
}
|
|
|
85 |
srcs.put(dest, this);
|
|
|
86 |
}
|
17 |
ilm |
87 |
}
|
|
|
88 |
|
73 |
ilm |
89 |
public synchronized SQLRowValues createRowValuesFrom(int idSrc) {
|
|
|
90 |
final List<SQLRowAccessor> srcRows = new ArrayList<SQLRowAccessor>(1);
|
93 |
ilm |
91 |
srcRows.add(getSource().getRow(idSrc));
|
73 |
ilm |
92 |
return createRowValuesFrom(srcRows);
|
17 |
ilm |
93 |
}
|
|
|
94 |
|
73 |
ilm |
95 |
public synchronized SQLRowValues createRowValuesFrom(final SQLRow srcRow) {
|
|
|
96 |
final SQLRowValues rowVals = new SQLRowValues(getDestination());
|
17 |
ilm |
97 |
if (!srcRow.getTable().equals(getSource()))
|
|
|
98 |
throw new IllegalArgumentException("Row not from source table : " + srcRow);
|
73 |
ilm |
99 |
merge(srcRow, rowVals);
|
|
|
100 |
return rowVals;
|
|
|
101 |
}
|
17 |
ilm |
102 |
|
73 |
ilm |
103 |
public synchronized SQLRowValues createRowValuesFrom(final List<? extends SQLRowAccessor> srcRows) {
|
|
|
104 |
final SQLRowValues rowVals = new SQLRowValues(getDestination());
|
|
|
105 |
for (SQLRowAccessor srcRow : srcRows) {
|
|
|
106 |
if (!srcRow.getTable().equals(getSource()))
|
|
|
107 |
throw new IllegalArgumentException("Row not from source table : " + srcRow);
|
|
|
108 |
merge(srcRow, rowVals);
|
|
|
109 |
}
|
|
|
110 |
return rowVals;
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
public void commitTransfert(final List<? extends SQLRowAccessor> srcRows, int destId) throws SQLException {
|
|
|
114 |
|
|
|
115 |
if (storeTransfer) {
|
|
|
116 |
System.err.println("SQLInjector.commitTransfert() : transfert from " + this.getSource().getName() + " to " + this.getDestination().getName());
|
|
|
117 |
// Transfert
|
|
|
118 |
final SQLTable tableTransfert = getSource().getDBRoot().getTable(getTableTranferName());
|
|
|
119 |
if (tableTransfert == null) {
|
|
|
120 |
throw new IllegalStateException("No table transfer for " + getSource().getName());
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
for (SQLRowAccessor srcRow : srcRows) {
|
|
|
124 |
|
|
|
125 |
final SQLRowValues rowTransfer = new SQLRowValues(tableTransfert);
|
|
|
126 |
|
|
|
127 |
final Set<SQLField> foreignKeysSrc = tableTransfert.getForeignKeys(getSource());
|
|
|
128 |
final Set<SQLField> foreignKeysDest = tableTransfert.getForeignKeys(getDestination());
|
|
|
129 |
if (foreignKeysSrc.isEmpty()) {
|
|
|
130 |
throw new IllegalStateException("No foreign (src) to " + getSource().getName() + " in " + tableTransfert.getName());
|
|
|
131 |
}
|
|
|
132 |
if (foreignKeysDest.isEmpty()) {
|
|
|
133 |
throw new IllegalStateException("No foreign (dest) to " + getDestination().getName() + " in " + tableTransfert.getName());
|
|
|
134 |
}
|
|
|
135 |
rowTransfer.put(foreignKeysSrc.iterator().next().getName(), srcRow.getIDNumber());
|
|
|
136 |
rowTransfer.put(foreignKeysDest.iterator().next().getName(), destId);
|
|
|
137 |
// TODO: commit in one shot
|
|
|
138 |
rowTransfer.commit();
|
|
|
139 |
|
|
|
140 |
}
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
private String getTableTranferName() {
|
|
|
146 |
return "TR_" + getSource().getName();
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
protected void merge(SQLRowAccessor srcRow, SQLRowValues rowVals) {
|
17 |
ilm |
150 |
for (SQLField field : this.values.keySet()) {
|
|
|
151 |
rowVals.put(field.getName(), this.values.get(field));
|
|
|
152 |
}
|
73 |
ilm |
153 |
final SQLSystem dbSystem = srcRow.getTable().getDBSystemRoot().getServer().getSQLSystem();
|
|
|
154 |
final int size = getFrom().size();
|
|
|
155 |
for (int i = 0; i < size; i++) {
|
17 |
ilm |
156 |
|
|
|
157 |
final SQLField sqlFieldFrom = getFrom().get(i);
|
|
|
158 |
final SQLField sqlFieldTo = getTo().get(i);
|
|
|
159 |
final Object o = srcRow.getObject(sqlFieldFrom.getName());
|
|
|
160 |
|
|
|
161 |
// Probleme avec H2 Primary Key en Long et foreignKey en Int
|
|
|
162 |
if (dbSystem == SQLSystem.H2 && sqlFieldFrom.getType().getJavaType() == Long.class && sqlFieldTo.getType().getJavaType() == Integer.class) {
|
73 |
ilm |
163 |
merge(sqlFieldTo, ((Long) o).intValue(), rowVals);
|
17 |
ilm |
164 |
} else {
|
73 |
ilm |
165 |
merge(sqlFieldTo, o, rowVals);
|
17 |
ilm |
166 |
}
|
|
|
167 |
}
|
73 |
ilm |
168 |
}
|
17 |
ilm |
169 |
|
73 |
ilm |
170 |
protected void merge(SQLField field, Object value, SQLRowValues rowVals) {
|
|
|
171 |
rowVals.put(field.getName(), value);
|
17 |
ilm |
172 |
}
|
|
|
173 |
|
180 |
ilm |
174 |
private String cleanRef(String value) {
|
|
|
175 |
List<String> l = StringUtils.fastSplit(value, ',');
|
|
|
176 |
Set<String> s = new HashSet<>(l);
|
|
|
177 |
String nom = "";
|
|
|
178 |
if (s.size() > 1) {
|
|
|
179 |
Set<String> refAdded = new HashSet<>();
|
|
|
180 |
for (String string : s) {
|
|
|
181 |
if (string.trim().length() > 0 && !refAdded.contains(string.trim())) {
|
|
|
182 |
nom += string + ",";
|
|
|
183 |
refAdded.add(string.trim());
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
} else if (s.size() == 1) {
|
|
|
187 |
nom = s.iterator().next();
|
|
|
188 |
}
|
|
|
189 |
return nom;
|
|
|
190 |
}
|
|
|
191 |
|
174 |
ilm |
192 |
protected void transfertReference(SQLRowAccessor srcRow, SQLRowValues rowVals, final SQLTable tableElementDestination, String refField, String from, String to) {
|
83 |
ilm |
193 |
|
|
|
194 |
String label = rowVals.getString(to);
|
174 |
ilm |
195 |
SQLPreferences prefs = SQLPreferences.getMemCached(srcRow.getTable().getDBRoot());
|
|
|
196 |
|
185 |
ilm |
197 |
final String ref = srcRow.getString(from);
|
|
|
198 |
if (ref != null && ref.trim().length() > 0) {
|
|
|
199 |
if (prefs.getBoolean("TransfertRef", true) || !to.equals("NOM")) {
|
|
|
200 |
if (label != null && label.trim().length() > 0) {
|
|
|
201 |
rowVals.put(to, cleanRef(label + ", " + ref));
|
|
|
202 |
} else {
|
|
|
203 |
rowVals.put(to, ref);
|
|
|
204 |
}
|
|
|
205 |
} else if (prefs.getBoolean("TransfertMultiRef", false)) {
|
|
|
206 |
SQLRowValues rowValsHeader = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(tableElementDestination));
|
|
|
207 |
// TODO taxe may be undefined set it to default
|
|
|
208 |
rowValsHeader.put("NOM", ref);
|
|
|
209 |
rowValsHeader.put(refField, rowVals);
|
|
|
210 |
|
|
|
211 |
// On reprend les références dans la facture
|
|
|
212 |
if (rowValsHeader.getTable().getName().equalsIgnoreCase("SAISIE_VENTE_FACTURE_ELEMENT")) {
|
|
|
213 |
SQLRowValues rowValsHeaderRef = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(tableElementDestination));
|
|
|
214 |
rowValsHeaderRef.put("ID_STYLE", 3);
|
|
|
215 |
if (srcRow.getTable().getName().equalsIgnoreCase("DEVIS")) {
|
|
|
216 |
rowValsHeaderRef.put("NOM", srcRow.getString("OBJET"));
|
|
|
217 |
} else {
|
|
|
218 |
rowValsHeaderRef.put("NOM", srcRow.getString("NOM"));
|
|
|
219 |
}
|
|
|
220 |
rowValsHeaderRef.put(refField, rowVals);
|
|
|
221 |
}
|
174 |
ilm |
222 |
}
|
83 |
ilm |
223 |
}
|
|
|
224 |
}
|
|
|
225 |
|
177 |
ilm |
226 |
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
|
|
|
227 |
|
83 |
ilm |
228 |
protected void transfertNumberReference(SQLRowAccessor srcRow, SQLRowValues rowVals, final SQLTable tableElementDestination, String refField) {
|
174 |
ilm |
229 |
SQLPreferences prefs = SQLPreferences.getMemCached(srcRow.getTable().getDBRoot());
|
83 |
ilm |
230 |
|
185 |
ilm |
231 |
if (!prefs.getBoolean("TransfertNoRef", false)) {
|
|
|
232 |
if (prefs.getBoolean("TransfertRef", true)) {
|
|
|
233 |
String label = rowVals.getString("NOM");
|
|
|
234 |
if (label != null && label.trim().length() > 0) {
|
|
|
235 |
final String value = label + ", " + srcRow.getString("NUMERO");
|
|
|
236 |
rowVals.put("NOM", cleanRef(value));
|
|
|
237 |
} else {
|
|
|
238 |
rowVals.put("NOM", srcRow.getString("NUMERO"));
|
|
|
239 |
}
|
83 |
ilm |
240 |
}
|
185 |
ilm |
241 |
if (prefs.getBoolean("TransfertMultiRef", false)) {
|
|
|
242 |
SQLRowValues rowValsHeader = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(tableElementDestination));
|
|
|
243 |
// FIXME Style forcé en titre 1 via l'ID
|
|
|
244 |
rowValsHeader.put("ID_STYLE", 3);
|
|
|
245 |
SQLElement element = Configuration.getInstance().getDirectory().getElement(getSource());
|
|
|
246 |
// Utiliser pour les transfert du module commande interne
|
|
|
247 |
if (element == null) {
|
|
|
248 |
element = Configuration.getInstance().getDirectory().getElement(getSource().getName());
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
String elementName = StringUtils.firstUp(element.getName().getVariant(org.openconcerto.utils.i18n.Grammar.SINGULAR));
|
|
|
252 |
rowValsHeader.put("NOM", elementName + "\n N° " + srcRow.getString("NUMERO") + " du " + dateFormat.format(srcRow.getDate("DATE").getTime()));
|
|
|
253 |
rowValsHeader.put(refField, rowVals);
|
182 |
ilm |
254 |
}
|
83 |
ilm |
255 |
}
|
|
|
256 |
}
|
|
|
257 |
|
73 |
ilm |
258 |
public synchronized SQLRow insertFrom(final SQLRowAccessor srcRow) throws SQLException {
|
|
|
259 |
return createRowValuesFrom(Arrays.asList(srcRow)).insert();
|
17 |
ilm |
260 |
}
|
|
|
261 |
|
|
|
262 |
// TODO gettable()..getName()..equalsIgnoreCase( by .getTable().equals(
|
|
|
263 |
/**
|
|
|
264 |
* mettre une valeur par défaut pour un champ donné
|
|
|
265 |
*
|
|
|
266 |
* @param fieldDest
|
|
|
267 |
* @param defaultValue
|
|
|
268 |
*/
|
73 |
ilm |
269 |
protected synchronized final void mapDefaultValues(SQLField fieldDest, Object defaultValue) {
|
17 |
ilm |
270 |
if (fieldDest.getTable().getName().equalsIgnoreCase(this.tableDest.getName())) {
|
|
|
271 |
this.values.put(fieldDest, defaultValue);
|
|
|
272 |
} else {
|
|
|
273 |
throw new IllegalArgumentException("SQLField " + fieldDest + " is not a field of table " + this.tableDest);
|
|
|
274 |
}
|
|
|
275 |
}
|
|
|
276 |
|
73 |
ilm |
277 |
protected synchronized final void map(SQLField from, SQLField to) throws IllegalArgumentException {
|
17 |
ilm |
278 |
// Verification de la validité des SQLField
|
|
|
279 |
if (!from.getTable().getName().equalsIgnoreCase(this.tableSrc.getName())) {
|
|
|
280 |
throw new IllegalArgumentException("SQLField " + from + " is not a field of table " + this.tableSrc);
|
|
|
281 |
} else {
|
|
|
282 |
if (!to.getTable().getName().equalsIgnoreCase(this.tableDest.getName())) {
|
|
|
283 |
throw new IllegalArgumentException("SQLField " + to + " is not a field of table " + this.tableDest);
|
|
|
284 |
}
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
int index = this.from.indexOf(from);
|
|
|
288 |
if (index > 0) {
|
|
|
289 |
this.to.set(index, to);
|
|
|
290 |
} else {
|
|
|
291 |
this.from.add(from);
|
|
|
292 |
this.to.add(to);
|
|
|
293 |
}
|
|
|
294 |
}
|
|
|
295 |
|
73 |
ilm |
296 |
protected synchronized final void remove(SQLField from, SQLField to) throws IllegalArgumentException {
|
17 |
ilm |
297 |
// Verification de la validité des SQLField
|
|
|
298 |
if (!from.getTable().getName().equalsIgnoreCase(this.tableSrc.getName())) {
|
|
|
299 |
throw new IllegalArgumentException("SQLField " + from + " is not a field of table " + this.tableSrc);
|
|
|
300 |
} else {
|
|
|
301 |
if (!to.getTable().getName().equalsIgnoreCase(this.tableDest.getName())) {
|
|
|
302 |
throw new IllegalArgumentException("SQLField " + to + " is not a field of table " + this.tableDest);
|
|
|
303 |
}
|
|
|
304 |
}
|
|
|
305 |
|
|
|
306 |
int index = this.from.indexOf(from);
|
|
|
307 |
if (this.to.get(index).getName().equalsIgnoreCase(to.getName())) {
|
|
|
308 |
this.to.remove(to);
|
|
|
309 |
this.from.remove(from);
|
|
|
310 |
}
|
|
|
311 |
}
|
|
|
312 |
|
|
|
313 |
/**
|
|
|
314 |
* Créer l'association entre les champs portant le nom dans les deux tables
|
|
|
315 |
*
|
|
|
316 |
*/
|
73 |
ilm |
317 |
public synchronized void createDefaultMap() {
|
17 |
ilm |
318 |
for (SQLField field : this.tableSrc.getContentFields()) {
|
|
|
319 |
|
|
|
320 |
if (this.tableDest.contains(field.getName())) {
|
|
|
321 |
map(field, this.tableDest.getField(field.getName()));
|
|
|
322 |
}
|
|
|
323 |
}
|
|
|
324 |
}
|
|
|
325 |
|
73 |
ilm |
326 |
public synchronized ArrayList<SQLField> getFrom() {
|
17 |
ilm |
327 |
return this.from;
|
|
|
328 |
}
|
|
|
329 |
|
73 |
ilm |
330 |
public synchronized ArrayList<SQLField> getTo() {
|
17 |
ilm |
331 |
return this.to;
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
/**
|
|
|
335 |
* Creer un SQLInjector par défaut si aucun n'est déja défini
|
|
|
336 |
*
|
|
|
337 |
* @param src
|
|
|
338 |
* @param dest
|
|
|
339 |
* @return un SQLInjector par défaut si aucun n'est déja défini
|
|
|
340 |
*/
|
73 |
ilm |
341 |
public static synchronized SQLInjector getInjector(SQLTable src, SQLTable dest) {
|
|
|
342 |
SQLInjector injector = getRegistrereddInjector(src, dest);
|
|
|
343 |
if (injector == null) {
|
|
|
344 |
injector = createDefaultInjector(src, dest);
|
17 |
ilm |
345 |
}
|
73 |
ilm |
346 |
return injector;
|
17 |
ilm |
347 |
}
|
|
|
348 |
|
73 |
ilm |
349 |
public static synchronized SQLInjector getRegistrereddInjector(SQLTable src, SQLTable dest) {
|
|
|
350 |
final Map<SQLTable, Map<SQLTable, SQLInjector>> map = allRegisteredInjectors.get(src.getDBRoot());
|
|
|
351 |
if (map == null) {
|
|
|
352 |
return null;
|
|
|
353 |
}
|
|
|
354 |
Map<SQLTable, SQLInjector> m = map.get(src);
|
|
|
355 |
if (m != null) {
|
|
|
356 |
return m.get(dest);
|
|
|
357 |
}
|
|
|
358 |
return null;
|
|
|
359 |
}
|
|
|
360 |
|
|
|
361 |
private static synchronized SQLInjector createDefaultInjector(SQLTable src, SQLTable dest) {
|
|
|
362 |
System.err.println("No SQLInjector defined for " + src + " , " + dest + ". SQLInjector created automatically.");
|
|
|
363 |
SQLInjector injector = new SQLInjector(src, dest, false);
|
|
|
364 |
injector.createDefaultMap();
|
|
|
365 |
return injector;
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
public synchronized SQLTable getDestination() {
|
17 |
ilm |
369 |
return this.tableDest;
|
|
|
370 |
}
|
|
|
371 |
|
73 |
ilm |
372 |
public synchronized SQLTable getSource() {
|
17 |
ilm |
373 |
return this.tableSrc;
|
|
|
374 |
}
|
73 |
ilm |
375 |
|
|
|
376 |
public synchronized static void createTransferTables(DBRoot root) throws SQLException {
|
|
|
377 |
Map<SQLTable, Map<SQLTable, SQLInjector>> map = injectors.get(root);
|
|
|
378 |
if (root == null) {
|
|
|
379 |
System.err.println("No SQLInjector for root " + root);
|
|
|
380 |
return;
|
|
|
381 |
}
|
|
|
382 |
|
|
|
383 |
final Set<SQLTable> srcTables = map.keySet();
|
|
|
384 |
if (srcTables.isEmpty()) {
|
|
|
385 |
System.err.println("No SQLInjector for root " + root);
|
|
|
386 |
return;
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
final List<SQLCreateTable> createTablesQueries = new ArrayList<SQLCreateTable>();
|
|
|
390 |
// Create table if needed
|
|
|
391 |
for (SQLTable sqlTable : srcTables) {
|
|
|
392 |
final String trTableName = "TR_" + sqlTable.getName();
|
|
|
393 |
if (root.getTable(trTableName) == null) {
|
|
|
394 |
final SQLCreateTable createTable = new SQLCreateTable(root, trTableName);
|
|
|
395 |
createTable.setPlain(false);
|
|
|
396 |
// createTable.addColumn(SQLSyntax.ID_NAME,
|
|
|
397 |
// createTable.getSyntax().getPrimaryIDDefinition());
|
|
|
398 |
createTable.addForeignColumn(SQLKey.PREFIX + sqlTable.getName(), sqlTable);
|
|
|
399 |
createTablesQueries.add(createTable);
|
|
|
400 |
}
|
|
|
401 |
}
|
|
|
402 |
if (createTablesQueries.size() > 0) {
|
|
|
403 |
root.createTables(createTablesQueries);
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
// Create transfer fields if needed
|
|
|
407 |
final List<AlterTable> alterTablesQueries = new ArrayList<AlterTable>();
|
|
|
408 |
final TablesMap toRefresh = new TablesMap();
|
|
|
409 |
for (SQLTable srcTable : srcTables) {
|
|
|
410 |
final String trTableName = "TR_" + srcTable.getName();
|
|
|
411 |
final SQLTable transfertTable = root.getTable(trTableName);
|
|
|
412 |
final AlterTable alter = new AlterTable(transfertTable);
|
|
|
413 |
final Set<SQLTable> destTables = map.get(srcTable).keySet();
|
|
|
414 |
for (SQLTable destTable : destTables) {
|
|
|
415 |
final String fk = SQLKey.PREFIX + destTable.getName();
|
|
|
416 |
if (!transfertTable.contains(fk)) {
|
|
|
417 |
alter.addForeignColumn(fk, destTable);
|
|
|
418 |
}
|
|
|
419 |
}
|
|
|
420 |
if (!alter.isEmpty()) {
|
|
|
421 |
alterTablesQueries.add(alter);
|
|
|
422 |
toRefresh.add(alter.getRootName(), alter.getName());
|
|
|
423 |
}
|
|
|
424 |
}
|
|
|
425 |
for (final String q : ChangeTable.cat(alterTablesQueries)) {
|
|
|
426 |
root.getDBSystemRoot().getDataSource().execute(q);
|
|
|
427 |
}
|
|
|
428 |
root.getSchema().updateVersion();
|
|
|
429 |
root.getDBSystemRoot().refresh(toRefresh, false);
|
|
|
430 |
|
|
|
431 |
}
|
|
|
432 |
|
|
|
433 |
public void setOnlyTransfered(SQLTableModelSourceOnline tableSource) {
|
|
|
434 |
// needed for distinct
|
|
|
435 |
tableSource.getReq().setLockSelect(false);
|
|
|
436 |
|
|
|
437 |
tableSource.getReq().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
|
|
|
438 |
|
|
|
439 |
@Override
|
|
|
440 |
public SQLSelect transformChecked(SQLSelect input) {
|
|
|
441 |
|
|
|
442 |
final SQLTable tableTR = getSource().getTable(getTableTranferName());
|
|
|
443 |
// FIXME: preprocess TR_ .. content before join : group by id_src
|
|
|
444 |
final SQLSelectJoin j = input.addBackwardJoin("INNER", null, tableTR.getForeignKeys(getSource()).iterator().next(), null);
|
|
|
445 |
j.setWhere(new Where(tableTR.getForeignKeys(getDestination()).iterator().next(), "!=", getDestination().getUndefinedID()));
|
|
|
446 |
input.setDistinct(true);
|
|
|
447 |
|
|
|
448 |
System.err.println(input.asString());
|
|
|
449 |
return input;
|
|
|
450 |
}
|
|
|
451 |
});
|
|
|
452 |
}
|
|
|
453 |
|
|
|
454 |
public void setOnlyNotTransfered(SQLTableModelSourceOnline tableSource) {
|
|
|
455 |
tableSource.getReq().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
|
|
|
456 |
|
|
|
457 |
@Override
|
|
|
458 |
public SQLSelect transformChecked(SQLSelect input) {
|
|
|
459 |
final SQLTable tableTR = getSource().getTable(getTableTranferName());
|
|
|
460 |
|
|
|
461 |
final Where w = new Where(tableTR.getForeignKeys(getSource()).iterator().next(), "=", input.getAlias(getSource().getKey()));
|
|
|
462 |
input.addJoin("LEFT", tableTR, w);
|
|
|
463 |
final Where w2 = new Where(tableTR.getForeignKeys(getDestination()).iterator().next(), "IS", (Object) null);
|
|
|
464 |
input.setWhere(w2);
|
|
|
465 |
|
|
|
466 |
System.err.println(input.asString());
|
|
|
467 |
return input;
|
|
|
468 |
}
|
|
|
469 |
});
|
|
|
470 |
}
|
|
|
471 |
|
|
|
472 |
/**
|
|
|
473 |
* register manually a transfer, use with caution
|
|
|
474 |
*
|
|
|
475 |
* @throws SQLException
|
174 |
ilm |
476 |
*/
|
73 |
ilm |
477 |
public void addTransfert(int idFrom, int idTo) throws SQLException {
|
|
|
478 |
final SQLTable tableTransfert = getSource().getTable(getTableTranferName());
|
|
|
479 |
final SQLRowValues rowTransfer = new SQLRowValues(tableTransfert);
|
|
|
480 |
final Set<SQLField> foreignKeysSrc = tableTransfert.getForeignKeys(getSource());
|
|
|
481 |
final Set<SQLField> foreignKeysDest = tableTransfert.getForeignKeys(getDestination());
|
|
|
482 |
rowTransfer.put(foreignKeysSrc.iterator().next().getName(), idFrom);
|
|
|
483 |
rowTransfer.put(foreignKeysDest.iterator().next().getName(), idTo);
|
|
|
484 |
rowTransfer.commit();
|
|
|
485 |
|
|
|
486 |
}
|
|
|
487 |
|
17 |
ilm |
488 |
}
|