OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 20 | Rev 41 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 20 Rev 25
Line 20... Line 20...
20
import org.openconcerto.sql.utils.ChangeTable;
20
import org.openconcerto.sql.utils.ChangeTable;
21
import org.openconcerto.sql.utils.DropTable;
21
import org.openconcerto.sql.utils.DropTable;
22
import org.openconcerto.sql.utils.SQLCreateTable;
22
import org.openconcerto.sql.utils.SQLCreateTable;
23
import org.openconcerto.sql.utils.SQLCreateTableBase;
23
import org.openconcerto.sql.utils.SQLCreateTableBase;
24
import org.openconcerto.utils.CollectionMap;
24
import org.openconcerto.utils.CollectionMap;
-
 
25
import org.openconcerto.utils.cc.IClosure;
25
 
26
 
-
 
27
import java.io.File;
26
import java.sql.SQLException;
28
import java.sql.SQLException;
27
import java.util.ArrayList;
29
import java.util.ArrayList;
28
import java.util.Collections;
30
import java.util.Collections;
29
import java.util.HashSet;
31
import java.util.HashSet;
30
import java.util.LinkedHashMap;
32
import java.util.LinkedHashMap;
Line 37... Line 39...
37
 * 
39
 * 
38
 * @author Sylvain
40
 * @author Sylvain
39
 */
41
 */
40
public final class DBContext {
42
public final class DBContext {
41
 
43
 
-
 
44
    private final File dir;
-
 
45
    private final ModuleVersion localVersion;
42
    private final ModuleVersion lastInstalledVersion;
46
    private final ModuleVersion lastInstalledVersion;
43
    private final DBRoot root;
47
    private final DBRoot root;
44
    private final List<ChangeTable<?>> changeTables;
48
    private final List<ChangeTable<?>> changeTables;
45
    private final List<AlterTableRestricted> alterTables;
49
    private final List<AlterTableRestricted> alterTables;
-
 
50
    // Data Manipulation
-
 
51
    private final List<IClosure<? super DBRoot>> dm;
46
 
52
 
47
    private final Set<String> tables;
53
    private final Set<String> tables;
48
    private final CollectionMap<String, SQLField> fields;
54
    private final CollectionMap<String, SQLField> fields;
49
 
55
 
50
    DBContext(ModuleVersion lastInstalledVersion, DBRoot root, final Set<String> tables, final Set<SQLName> fields) {
56
    DBContext(final File dir, final ModuleVersion localVersion, final DBRoot root, final ModuleVersion dbVersion, final Set<String> tables, final Set<SQLName> fields) {
51
        super();
57
        super();
-
 
58
        this.dir = dir;
-
 
59
        this.localVersion = localVersion;
52
        this.lastInstalledVersion = lastInstalledVersion;
60
        this.lastInstalledVersion = dbVersion;
53
        this.root = root;
61
        this.root = root;
54
        this.tables = Collections.unmodifiableSet(tables);
62
        this.tables = Collections.unmodifiableSet(tables);
55
        this.fields = new CollectionMap<String, SQLField>(new HashSet<SQLField>());
63
        this.fields = new CollectionMap<String, SQLField>(new HashSet<SQLField>());
56
        for (final SQLName f : fields) {
64
        for (final SQLName f : fields) {
57
            final String tableName = f.getItem(0);
65
            final String tableName = f.getItem(0);
58
            this.fields.put(tableName, this.root.getTable(tableName).getField(f.getItem(1)));
66
            this.fields.put(tableName, this.root.getTable(tableName).getField(f.getItem(1)));
59
        }
67
        }
60
        this.changeTables = new ArrayList<ChangeTable<?>>();
68
        this.changeTables = new ArrayList<ChangeTable<?>>();
61
        this.alterTables = new ArrayList<AlterTableRestricted>();
69
        this.alterTables = new ArrayList<AlterTableRestricted>();
-
 
70
        this.dm = new ArrayList<IClosure<? super DBRoot>>();
-
 
71
    }
-
 
72
 
-
 
73
    public final File getLocalDirectory() {
-
 
74
        return this.dir;
-
 
75
    }
-
 
76
 
-
 
77
    public final ModuleVersion getLocalVersion() {
-
 
78
        return this.localVersion;
62
    }
79
    }
63
 
80
 
64
    public final ModuleVersion getLastInstalledVersion() {
81
    public final ModuleVersion getLastInstalledVersion() {
65
        return this.lastInstalledVersion;
82
        return this.lastInstalledVersion;
66
    }
83
    }
Line 93... Line 110...
93
            // for now OK to not use an undefined row since we can't modify List/ComboSQLRequet
110
            // for now OK to not use an undefined row since we can't modify List/ComboSQLRequet
94
            for (final String addedTable : getAddedTables())
111
            for (final String addedTable : getAddedTables())
95
                SQLTable.setUndefID(getRoot().getSchema(), addedTable, null);
112
                SQLTable.setUndefID(getRoot().getSchema(), addedTable, null);
96
            getRoot().refetch();
113
            getRoot().refetch();
97
        }
114
        }
-
 
115
        for (final IClosure<? super DBRoot> closure : this.dm) {
-
 
116
            closure.executeChecked(getRoot());
-
 
117
        }
98
    }
118
    }
99
 
119
 
100
    // DDL
120
    // DDL
101
 
121
 
102
    public final SQLCreateTable getCreateTable(final String name) {
122
    public final SQLCreateTable getCreateTable(final String name) {
Line 118... Line 138...
118
        if (!this.getTablesPreviouslyCreated().contains(name))
138
        if (!this.getTablesPreviouslyCreated().contains(name))
119
            throw new IllegalArgumentException(name + "doesn't belong to this module");
139
            throw new IllegalArgumentException(name + " doesn't belong to this module");
120
        this.changeTables.add(new DropTable(this.root.getTable(name)));
140
        this.changeTables.add(new DropTable(this.root.getTable(name)));
121
    }
141
    }
122
 
142
 
-
 
143
    // DML
-
 
144
 
-
 
145
    /**
-
 
146
     * The closure will be executed after the DDL statements. Note: you generally need to undo in
-
 
147
     * {@link AbstractModule#uninstall()} what <code>closure</code> does (exceptions include
-
 
148
     * inserting rows in a created table, since it will be automatically dropped).
-
 
149
     * 
-
 
150
     * @param closure what to do.
-
 
151
     */
-
 
152
    public final void manipulateData(final IClosure<? super DBRoot> closure) {
-
 
153
        this.dm.add(closure);
-
 
154
    }
-
 
155
 
-
 
156
    /**
-
 
157
     * The closure will be executed after the DDL statements. E.g. if you called
-
 
158
     * {@link #getCreateTable(String)} this allows you to insert rows.
-
 
159
     * 
-
 
160
     * @param name a table name.
-
 
161
     * @param closure what to do.
-
 
162
     * @see #manipulateData(IClosure)
-
 
163
     */
-
 
164
    public final void manipulateTable(final String name, final IClosure<SQLTable> closure) {
-
 
165
        this.manipulateData(new IClosure<DBRoot>() {
-
 
166
            @Override
-
 
167
            public void executeChecked(DBRoot input) {
-
 
168
                closure.executeChecked(input.getTable(name));
-
 
169
            }
-
 
170
        });
-
 
171
    }
-
 
172
 
123
    // getter
173
    // getter
124
 
174
 
125
    final List<String> getAddedTables() {
175
    final List<String> getAddedTables() {
126
        return new ArrayList<String>(getCreateTables().keySet());
176
        return new ArrayList<String>(getCreateTables().keySet());
127
    }
177
    }