Line 15... |
Line 15... |
15 |
|
15 |
|
16 |
import org.openconcerto.sql.Configuration;
|
16 |
import org.openconcerto.sql.Configuration;
|
17 |
import org.openconcerto.sql.changer.Changer;
|
17 |
import org.openconcerto.sql.changer.Changer;
|
18 |
import org.openconcerto.sql.element.SQLElement;
|
18 |
import org.openconcerto.sql.element.SQLElement;
|
19 |
import org.openconcerto.sql.model.DBSystemRoot;
|
19 |
import org.openconcerto.sql.model.DBSystemRoot;
|
- |
|
20 |
import org.openconcerto.sql.model.SQLField;
|
- |
|
21 |
import org.openconcerto.sql.model.SQLRow;
|
- |
|
22 |
import org.openconcerto.sql.model.SQLRowListRSH;
|
20 |
import org.openconcerto.sql.model.SQLRowValues;
|
23 |
import org.openconcerto.sql.model.SQLRowValues;
|
21 |
import org.openconcerto.sql.model.SQLSelect;
|
24 |
import org.openconcerto.sql.model.SQLSelect;
|
- |
|
25 |
import org.openconcerto.sql.model.SQLSelect.ArchiveMode;
|
22 |
import org.openconcerto.sql.model.SQLTable;
|
26 |
import org.openconcerto.sql.model.SQLTable;
|
23 |
import org.openconcerto.sql.model.Where;
|
27 |
import org.openconcerto.sql.model.Where;
|
24 |
import org.openconcerto.sql.model.SQLSelect.ArchiveMode;
|
- |
|
25 |
import org.openconcerto.sql.utils.SQLUtils;
|
28 |
import org.openconcerto.sql.utils.SQLUtils;
|
26 |
import org.openconcerto.sql.utils.SQLUtils.SQLFactory;
|
29 |
import org.openconcerto.sql.utils.SQLUtils.SQLFactory;
|
27 |
|
30 |
|
28 |
import java.sql.SQLException;
|
31 |
import java.sql.SQLException;
|
29 |
import java.util.List;
|
32 |
import java.util.List;
|
Line 39... |
Line 42... |
39 |
|
42 |
|
40 |
public FixSharedPrivate(DBSystemRoot b) {
|
43 |
public FixSharedPrivate(DBSystemRoot b) {
|
41 |
super(b);
|
44 |
super(b);
|
42 |
}
|
45 |
}
|
43 |
|
46 |
|
44 |
@SuppressWarnings("unchecked")
|
47 |
@Override
|
45 |
protected void changeImpl(final SQLTable t) throws SQLException {
|
48 |
protected void changeImpl(final SQLTable t) throws SQLException {
|
46 |
getStream().println(t + "... ");
|
49 |
getStream().println(t + "... ");
|
47 |
if (Configuration.getInstance() == null || Configuration.getInstance().getDirectory() == null)
|
50 |
if (Configuration.getInstance() == null || Configuration.getInstance().getDirectory() == null)
|
48 |
throw new IllegalStateException("no directory");
|
51 |
throw new IllegalStateException("no directory");
|
49 |
final SQLElement elem = Configuration.getInstance().getDirectory().getElement(t);
|
52 |
final SQLElement elem = Configuration.getInstance().getDirectory().getElement(t);
|
Line 63... |
Line 66... |
63 |
sel.setArchivedPolicy(ArchiveMode.BOTH);
|
66 |
sel.setArchivedPolicy(ArchiveMode.BOTH);
|
64 |
sel.addSelect(privateTable.getKey());
|
67 |
sel.addSelect(privateTable.getKey());
|
65 |
sel.addBackwardJoin("INNER", "m", t.getField(pff), null);
|
68 |
sel.addBackwardJoin("INNER", "m", t.getField(pff), null);
|
66 |
final String req = sel.asString() + " GROUP BY " + privateTable.getKey().getFieldRef() + " HAVING count(" + privateTable.getKey().getFieldRef() + ")>1";
|
69 |
final String req = sel.asString() + " GROUP BY " + privateTable.getKey().getFieldRef() + " HAVING count(" + privateTable.getKey().getFieldRef() + ")>1";
|
67 |
|
70 |
|
- |
|
71 |
@SuppressWarnings("unchecked")
|
68 |
final List<Number> privateIDs = t.getDBSystemRoot().getDataSource().executeCol(req);
|
72 |
final List<Number> privateIDs = t.getDBSystemRoot().getDataSource().executeCol(req);
|
69 |
if (privateIDs.size() > 0) {
|
73 |
if (privateIDs.size() > 0) {
|
70 |
getStream().println("\t" + pff + " fixing " + privateIDs.size() + " ... ");
|
74 |
getStream().println("\t" + pff + " fixing " + privateIDs.size() + " ... ");
|
- |
|
75 |
final SQLField archF = t.getArchiveField();
|
- |
|
76 |
final SQLField privateArchF = privateTable.getArchiveField();
|
- |
|
77 |
if ((archF == null) != (privateArchF == null))
|
- |
|
78 |
throw new IllegalStateException("Incoherent archive field : " + archF + " / " + privateArchF);
|
71 |
SQLUtils.executeAtomic(t.getDBSystemRoot().getDataSource(), new SQLFactory<Object>() {
|
79 |
SQLUtils.executeAtomic(t.getDBSystemRoot().getDataSource(), new SQLFactory<Object>() {
|
72 |
@Override
|
80 |
@Override
|
73 |
public Object create() throws SQLException {
|
81 |
public Object create() throws SQLException {
|
74 |
// for each private pointed by more than one parent
|
82 |
// for each private pointed by more than one parent
|
75 |
for (final Number privateID : privateIDs) {
|
83 |
for (final Number privateID : privateIDs) {
|
76 |
final SQLSelect fixSel = new SQLSelect(t.getBase());
|
84 |
final SQLSelect fixSel = new SQLSelect(t.getBase());
|
77 |
fixSel.setArchivedPolicy(ArchiveMode.BOTH);
|
85 |
fixSel.setArchivedPolicy(ArchiveMode.BOTH);
|
78 |
fixSel.addSelect(t.getKey());
|
86 |
fixSel.addSelect(t.getKey());
|
- |
|
87 |
if (archF != null)
|
- |
|
88 |
fixSel.addSelect(archF);
|
79 |
fixSel.setWhere(new Where(t.getField(pff), "=", privateID));
|
89 |
fixSel.setWhere(new Where(t.getField(pff), "=", privateID));
|
80 |
final List<Number> tIDs = t.getDBSystemRoot().getDataSource().executeCol(fixSel.asString());
|
90 |
final List<SQLRow> tIDs = SQLRowListRSH.execute(fixSel);
|
- |
|
91 |
for (final SQLRow tID : tIDs) {
|
81 |
// the first one can keep its private
|
92 |
// the first one can keep its private
|
- |
|
93 |
final SQLRowValues reallyPrivate;
|
82 |
for (final Number tID : tIDs.subList(1, tIDs.size())) {
|
94 |
if (tID == tIDs.get(0))
|
- |
|
95 |
reallyPrivate = new SQLRowValues(privateElement.getTable()).setID(privateID);
|
- |
|
96 |
else
|
- |
|
97 |
reallyPrivate = privateElement.createCopy(privateID.intValue());
|
- |
|
98 |
// keep archive coherence
|
- |
|
99 |
if (archF != null)
|
- |
|
100 |
reallyPrivate.put(privateArchF.getName(), tID.getObject(archF.getName()));
|
83 |
new SQLRowValues(t).setID(tID).put(pff, privateElement.createCopy(privateID.intValue())).update();
|
101 |
new SQLRowValues(t).setID(tID.getIDNumber()).put(pff, reallyPrivate).update();
|
84 |
}
|
102 |
}
|
85 |
}
|
103 |
}
|
86 |
return null;
|
104 |
return null;
|
87 |
}
|
105 |
}
|
88 |
});
|
106 |
});
|