OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 19 | Rev 25 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
19 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
 package org.openconcerto.erp.modules;
15
 
16
import org.openconcerto.sql.model.SQLField;
17
import org.openconcerto.sql.model.SQLField.Properties;
18
import org.openconcerto.sql.model.SQLName;
19
import org.openconcerto.sql.model.SQLSyntax;
20
import org.openconcerto.sql.model.SQLTable;
21
import org.openconcerto.sql.utils.AlterTable;
20 ilm 22
import org.openconcerto.sql.utils.SQLCreateTableBase;
19 ilm 23
import org.openconcerto.utils.CollectionUtils;
24
 
25
import java.util.HashSet;
26
import java.util.List;
27
import java.util.Set;
28
 
29
/**
30
 * Construct an ALTER TABLE statement with only "add" clauses or clauses that modify fields
31
 * belonging to a module.
32
 *
33
 * @author Sylvain
34
 */
35
public final class AlterTableRestricted {
36
 
37
    private final DBContext ctxt;
38
    private final SQLTable table;
39
    private final Set<SQLField> previouslyCreatedFields;
40
    private final AlterTable alter;
41
    private final Set<String> addedColumns, removedColumns;
42
 
43
    AlterTableRestricted(DBContext ctxt, String tableName) {
44
        this.ctxt = ctxt;
45
        this.table = ctxt.getRoot().getTable(tableName);
46
        this.previouslyCreatedFields = ctxt.getFieldsPreviouslyCreated(tableName);
47
        this.alter = new AlterTable(this.table);
48
        this.addedColumns = new HashSet<String>();
49
        this.removedColumns = new HashSet<String>();
50
    }
51
 
52
    final AlterTable getAlter() {
53
        return this.alter;
54
    }
55
 
56
    final Set<String> getAddedColumns() {
57
        return this.addedColumns;
58
    }
59
 
60
    final Set<String> getRemovedColumns() {
61
        return this.removedColumns;
62
    }
63
 
64
    public final SQLSyntax getSyntax() {
65
        return this.alter.getSyntax();
66
    }
67
 
68
    public final String getName() {
69
        return this.alter.getName();
70
    }
71
 
72
    private void addCol(String name) {
73
        if (this.table.contains(name))
74
            throw new IllegalArgumentException(name + " already exists");
75
        if (!this.addedColumns.add(name))
76
            throw new IllegalArgumentException(name + " already added to this");
77
    }
78
 
79
    // to alter or drop
80
    private void checkCol(String name) {
81
        final SQLField f = this.table.getField(name);
82
        if (!this.previouslyCreatedFields.contains(f))
83
            throw new IllegalArgumentException(f + " doesn't belong to this module");
84
    }
85
 
86
    public final AlterTable addColumn(String name, String definition) {
87
        addCol(name);
88
        return this.alter.addColumn(name, definition);
89
    }
90
 
91
    public final AlterTable addVarCharColumn(String name, int count) {
92
        addCol(name);
93
        return this.alter.addVarCharColumn(name, count);
94
    }
95
 
96
    public final AlterTable addDateAndTimeColumn(String name) {
97
        addCol(name);
98
        return this.alter.addDateAndTimeColumn(name);
99
    }
100
 
20 ilm 101
    public final AlterTable addIntegerColumn(String name, int defaultVal) {
102
        addCol(name);
103
        return this.alter.addIntegerColumn(name, defaultVal);
104
    }
105
 
19 ilm 106
    public AlterTable addForeignColumn(String fk, String table) {
107
        return this.addForeignColumn(fk, new SQLName(table));
108
    }
109
 
110
    public AlterTable addForeignColumn(String fk, SQLName tableName) {
20 ilm 111
        SQLCreateTableBase<?> createTable;
112
        if (tableName.getItemCount() == 1 && (createTable = this.ctxt.getCreateTables().get(tableName.getFirst())) != null) {
19 ilm 113
            addCol(fk);
20 ilm 114
            return this.alter.addForeignColumn(fk, createTable);
19 ilm 115
        } else {
116
            return this.addForeignColumn(fk, this.table.getDesc(tableName, SQLTable.class));
117
        }
118
    }
119
 
120
    public AlterTable addForeignColumn(String fk, SQLTable foreignTable) {
121
        addCol(fk);
122
        return this.alter.addForeignColumn(fk, foreignTable);
123
    }
124
 
125
    public AlterTable addUniqueConstraint(String name, List<String> cols) {
126
        if (!this.addedColumns.containsAll(cols))
127
            throw new IllegalArgumentException("Can only add constraint to added columns : " + CollectionUtils.substract(cols, this.addedColumns));
128
        return this.alter.addUniqueConstraint(name, cols);
129
    }
130
 
131
    public final AlterTable alterColumn(String fname, Set<Properties> toAlter, String type, String defaultVal, Boolean nullable) {
132
        checkCol(fname);
133
        return this.alter.alterColumn(fname, toAlter, type, defaultVal, nullable);
134
    }
135
 
136
    public final AlterTable alterColumnNullable(String f, boolean b) {
137
        checkCol(f);
138
        return this.alter.alterColumnNullable(f, b);
139
    }
140
 
141
    public final AlterTable dropColumn(String name) {
142
        checkCol(name);
143
        this.removedColumns.add(name);
144
        return this.alter.dropColumn(name);
145
    }
146
 
147
    public boolean isEmpty() {
148
        return this.alter.isEmpty();
149
    }
150
 
151
    @Override
152
    public int hashCode() {
153
        return this.alter.hashCode();
154
    }
155
 
156
    @Override
157
    public boolean equals(Object obj) {
158
        return this.alter.equals(obj);
159
    }
160
 
161
    @Override
162
    public String toString() {
163
        return super.toString() + " " + this.alter.toString();
164
    }
165
}