OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | 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
 *
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 org.openconcerto.sql.changer.Changer;
17
import org.openconcerto.sql.model.DBSystemRoot;
18
import org.openconcerto.sql.model.SQLBase;
19
import org.openconcerto.sql.model.SQLField;
20
import org.openconcerto.sql.model.SQLSystem;
21
import org.openconcerto.sql.model.SQLTable;
22
 
23
import java.sql.ResultSet;
24
import java.sql.SQLException;
25
import java.util.EnumSet;
26
import java.util.HashMap;
27
import java.util.Map;
28
 
29
import org.apache.commons.dbutils.ResultSetHandler;
30
 
31
/**
32
 * Set '' as the default for non nullable String fields with a null default. ATTN mysql 5.0 doesn't
33
 * support TEXT defaults, use textToVarChar first.
34
 *
35
 * @author Sylvain
36
 */
37
public class TextDefault extends Changer<SQLTable> {
38
 
39
    public TextDefault(DBSystemRoot b) {
40
        super(b);
41
    }
42
 
43
    @Override
44
    protected EnumSet<SQLSystem> getCompatibleSystems() {
45
        return EnumSet.of(SQLSystem.MYSQL);
46
    }
47
 
48
    @SuppressWarnings("unchecked")
49
    @Override
50
    protected void changeImpl(SQLTable t) throws SQLException {
182 ilm 51
        final String infoSchema = t.getBase().quote("SELECT TABLE_NAME, COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE from information_schema.COLUMNS where TABLE_SCHEMA=%s and TABLE_NAME=%s",
52
                t.getBase().getName(), t.getName());
17 ilm 53
        final Map<String, Object> defaults = (Map<String, Object>) this.getDS().execute(infoSchema, new ResultSetHandler() {
54
            public Object handle(ResultSet rs) throws SQLException {
55
                final Map<String, Object> res = new HashMap<String, Object>();
56
                while (rs.next()) {
57
                    res.put(rs.getString("COLUMN_NAME"), rs.getObject("COLUMN_DEFAULT"));
58
                }
59
                return res;
60
            }
61
        });
62
        for (final SQLField f : t.getFields()) {
63
            if (f.getType().getJavaType().equals(String.class) && Boolean.FALSE.equals(f.isNullable()) && defaults.get(f.getName()) == null) {
64
                final String req = "ALTER TABLE " + SQLBase.quoteIdentifier(t.getName()) + " MODIFY COLUMN " + SQLBase.quoteIdentifier(f.getName()) + " " + f.getType().getTypeName() + "("
65
                        + f.getType().getSize() + ") NOT NULL DEFAULT ''";
66
                System.err.println(req);
67
                this.getDS().execute(req);
68
            }
69
        }
70
    }
71
 
72
}