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.changer.convert;
|
|
|
15 |
|
|
|
16 |
import static java.util.Collections.singletonList;
|
182 |
ilm |
17 |
|
17 |
ilm |
18 |
import org.openconcerto.sql.changer.Changer;
|
|
|
19 |
import org.openconcerto.sql.model.ConnectionHandlerNoSetup;
|
|
|
20 |
import org.openconcerto.sql.model.DBSystemRoot;
|
|
|
21 |
import org.openconcerto.sql.model.SQLDataSource;
|
|
|
22 |
import org.openconcerto.sql.model.SQLSystem;
|
|
|
23 |
import org.openconcerto.sql.model.SQLTable;
|
|
|
24 |
import org.openconcerto.sql.model.graph.Link;
|
|
|
25 |
import org.openconcerto.sql.utils.AlterTable;
|
|
|
26 |
import org.openconcerto.sql.utils.SQLUtils;
|
|
|
27 |
|
|
|
28 |
import java.sql.SQLException;
|
|
|
29 |
import java.util.EnumSet;
|
|
|
30 |
import java.util.Set;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Rename the primary key of a table to "ID".
|
|
|
34 |
*
|
|
|
35 |
* @author Sylvain
|
|
|
36 |
*/
|
|
|
37 |
public class RenamePK extends Changer<SQLTable> {
|
|
|
38 |
|
|
|
39 |
public RenamePK(DBSystemRoot b) {
|
|
|
40 |
super(b);
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
@Override
|
|
|
44 |
protected EnumSet<SQLSystem> getCompatibleSystems() {
|
|
|
45 |
return EnumSet.of(SQLSystem.MYSQL, SQLSystem.POSTGRESQL);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
protected void changeImpl(SQLTable t) throws SQLException {
|
|
|
49 |
renamePrimary(t, "ID");
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
private void renamePrimary(final SQLTable t, final String newName) throws SQLException {
|
|
|
53 |
final SQLSystem system = t.getBase().getServer().getSQLSystem();
|
|
|
54 |
|
|
|
55 |
if (t.getKey() != null) {
|
|
|
56 |
final String keyName = t.getKey().getName();
|
|
|
57 |
if (!keyName.equals(newName) && keyName.startsWith("ID")) {
|
|
|
58 |
getStream().println(t);
|
|
|
59 |
if (system == SQLSystem.MYSQL) {
|
|
|
60 |
final Set<Link> referentLinks = this.getSystemRoot().getGraph().getReferentLinks(t);
|
|
|
61 |
SQLUtils.executeAtomic(t.getDBSystemRoot().getDataSource(), new ConnectionHandlerNoSetup<Object, SQLException>() {
|
|
|
62 |
@Override
|
|
|
63 |
public Object handle(SQLDataSource ds) throws SQLException {
|
|
|
64 |
for (final Link refLink : referentLinks) {
|
|
|
65 |
final String dropIndex = new AlterTable(refLink.getSource()).dropForeignConstraint(refLink.getName()).asString();
|
|
|
66 |
getStream().println(dropIndex);
|
|
|
67 |
getDS().execute(dropIndex);
|
|
|
68 |
}
|
|
|
69 |
|
182 |
ilm |
70 |
final String alter = t.getBase().quote("ALTER TABLE %n CHANGE COLUMN %n %i " + getSyntax().getPrimaryIDDefinitionShort(), t, t.getKey(), newName);
|
17 |
ilm |
71 |
getStream().println(alter);
|
|
|
72 |
getDS().execute(alter);
|
|
|
73 |
t.fetchFields();
|
|
|
74 |
|
|
|
75 |
for (final Link l : referentLinks) {
|
|
|
76 |
final String addFK = new AlterTable(l.getSource()).addForeignConstraint(l.getCols(), l.getContextualName(), false, singletonList(newName)).asString();
|
|
|
77 |
getStream().println(addFK);
|
|
|
78 |
getDS().execute(addFK);
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
return null;
|
|
|
82 |
}
|
|
|
83 |
});
|
|
|
84 |
} else if (system == SQLSystem.POSTGRESQL) {
|
|
|
85 |
final String alter = t.getBase().quote("ALTER TABLE %f RENAME COLUMN %n TO %i", t, t.getKey(), newName);
|
|
|
86 |
getStream().println(alter);
|
|
|
87 |
this.getDS().execute(alter);
|
|
|
88 |
} else
|
|
|
89 |
throw new UnsupportedOperationException("for " + system);
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
}
|