OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 83 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
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
 /*
15
 * ConvertisseurBaseObs created on 29 avr. 2004
16
 */
17
package org.openconcerto.sql.changer.convert;
18
 
57 ilm 19
import static java.util.Collections.singletonList;
17 ilm 20
import org.openconcerto.sql.changer.Changer;
21
import org.openconcerto.sql.model.DBSystemRoot;
57 ilm 22
import org.openconcerto.sql.model.SQLField;
23
import org.openconcerto.sql.model.SQLName;
17 ilm 24
import org.openconcerto.sql.model.SQLSystem;
25
import org.openconcerto.sql.model.SQLTable;
26
import org.openconcerto.sql.model.SQLTable.Index;
27
import org.openconcerto.sql.model.graph.Link;
57 ilm 28
import org.openconcerto.sql.model.graph.SQLKey;
29
import org.openconcerto.sql.utils.AlterTable;
30
import org.openconcerto.sql.utils.ChangeTable;
31
import org.openconcerto.sql.utils.ChangeTable.ClauseType;
32
import org.openconcerto.sql.utils.ChangeTable.DeferredClause;
17 ilm 33
 
34
import java.sql.SQLException;
35
import java.util.HashSet;
36
import java.util.List;
37
import java.util.Set;
38
 
65 ilm 39
/**
40
 * Add foreign constraints.
41
 *
42
 * @author Sylvain
83 ilm 43
 * @see SQLKey#keyToTable(SQLField)
65 ilm 44
 */
45
public class AddFK extends Changer<SQLTable> {
17 ilm 46
 
47
    public AddFK(DBSystemRoot b) {
48
        super(b);
49
    }
50
 
61 ilm 51
    @Override
65 ilm 52
    protected void changeImpl(SQLTable t) throws SQLException {
53
        this.getStream().println(t + "... ");
17 ilm 54
 
55
        if (this.getSystemRoot().getServer().getSQLSystem() == SQLSystem.MYSQL)
65 ilm 56
            toInno(t);
17 ilm 57
 
65 ilm 58
        {
132 ilm 59
            final Set<Link> foreignLinks = t.getForeignLinks();
57 ilm 60
            final Set<List<String>> realFKs = new HashSet<List<String>>();
61
            for (final Link link : foreignLinks) {
62
                // if name is null, link is virtual
63
                if (link.getName() != null)
64
                    realFKs.add(link.getCols());
65
            }
66
            final Set<List<String>> allFKs = new HashSet<List<String>>(realFKs);
67
            for (final String virtualFK : SQLKey.foreignKeys(t))
68
                allFKs.add(singletonList(virtualFK));
69
 
70
            if (allFKs.size() > 0) {
17 ilm 71
                // indexes already created
72
                final Set<List<String>> indexes = new HashSet<List<String>>();
73
                for (final Index i : t.getIndexes()) {
74
                    indexes.add(i.getCols());
75
                }
76
 
57 ilm 77
                final AlterTable alter = new AlterTable(t);
78
                for (final List<String> cols : allFKs) {
79
                    if (!realFKs.contains(cols)) {
80
                        final SQLField key = t.getField(cols.get(0));
81
                        final SQLTable foreignT = SQLKey.keyToTable(key);
65 ilm 82
                        alter.addForeignConstraint(cols, foreignT.getContextualSQLName(key.getTable()), false, singletonList(foreignT.getKey().getName()));
83
                        getStream().println("ajout de " + key);
17 ilm 84
                    }
85
                    // MySQL automatically creates an index with a foreign key,
86
                    // but ours replace it
57 ilm 87
                    if (!indexes.contains(cols)) {
88
                        alter.addOutsideClause(new DeferredClause() {
89
                            @Override
90
                            public String asString(ChangeTable<?> ct, SQLName tableName) {
91
                                return getSyntax().getCreateIndex("_fki", tableName, cols);
92
                            }
93
 
94
                            @Override
95
                            public ClauseType getType() {
96
                                return ClauseType.ADD_INDEX;
97
                            }
98
                        });
65 ilm 99
                        getStream().println("ajout d'index pour " + cols);
17 ilm 100
                    } else {
65 ilm 101
                        getStream().println("pas besoin d'index pour " + cols);
17 ilm 102
                    }
103
                }
57 ilm 104
                if (!alter.isEmpty()) {
105
                    this.getDS().execute(alter.asString());
106
                    t.getSchema().updateVersion();
107
                }
17 ilm 108
            }
109
        }
110
    }
111
 
65 ilm 112
    private void toInno(SQLTable t) {
113
        if (!this.getDS().execute1("show table status like '" + t.getName() + "'").get("Engine").equals("InnoDB"))
114
            this.getDS().execute("ALTER TABLE " + t.getName() + " ENGINE = InnoDB;");
17 ilm 115
    }
116
 
117
}