OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 73 Rev 83
Line 39... Line 39...
39
import java.util.Set;
39
import java.util.Set;
40
 
40
 
41
import net.jcip.annotations.GuardedBy;
41
import net.jcip.annotations.GuardedBy;
42
 
42
 
43
import org.apache.commons.dbutils.ResultSetHandler;
43
import org.apache.commons.dbutils.ResultSetHandler;
44
import org.jdom.Element;
44
import org.jdom2.Element;
45
 
45
 
46
public final class SQLSchema extends SQLIdentifier {
46
public final class SQLSchema extends SQLIdentifier {
47
 
47
 
48
    /**
48
    /**
49
     * set this system property to avoid writing to the db (be it CREATE TABLE or
49
     * set this system property to avoid writing to the db (be it CREATE TABLE or
Line 85... Line 85...
85
 
85
 
86
    public static final String getVersion(final Element schemaElem) {
86
    public static final String getVersion(final Element schemaElem) {
87
        return schemaElem.getAttributeValue(VERSION_XMLATTR);
87
        return schemaElem.getAttributeValue(VERSION_XMLATTR);
88
    }
88
    }
89
 
89
 
90
    public static final String getVersion(final SQLBase base, final String schemaName) {
90
    public static final Map<String, String> getVersions(final SQLBase base, final Set<String> schemaNames) {
91
        // since we haven't an instance of SQLSchema, we can't know if the table exists
91
        // since we haven't an instance of SQLSchema, we can't know if the table exists
92
        return base.getFwkMetadata(schemaName, VERSION_MDKEY, true);
92
        return base.getFwkMetadata(schemaNames, VERSION_MDKEY);
93
    }
93
    }
94
 
94
 
95
    static private String getVersionSQL(final SQLSyntax syntax) {
95
    static private String getVersionSQL(final SQLSyntax syntax) {
96
        return syntax.getFormatTimestamp("now()", true);
96
        return syntax.getFormatTimestamp("CURRENT_TIMESTAMP", true);
97
    }
97
    }
98
 
98
 
99
    static SQLCreateMoveableTable getCreateMetadata(final SQLSyntax syntax) throws SQLException {
99
    static SQLCreateMoveableTable getCreateMetadata(final SQLSyntax syntax) throws SQLException {
100
        if (Boolean.getBoolean(NOAUTO_CREATE_METADATA))
100
        if (Boolean.getBoolean(NOAUTO_CREATE_METADATA))
101
            return null;
101
            return null;
Line 133... Line 133...
133
 
133
 
134
    public final SQLBase getBase() {
134
    public final SQLBase getBase() {
135
        return (SQLBase) this.getParent();
135
        return (SQLBase) this.getParent();
136
    }
136
    }
137
 
137
 
-
 
138
    @Override
-
 
139
    protected void onDrop() {
-
 
140
        SQLTable.removeUndefID(this);
-
 
141
        super.onDrop();
-
 
142
    }
-
 
143
 
138
    /**
144
    /**
139
     * The version when this instance was last fully refreshed. In other words, if we refresh tables
145
     * The version when this instance was last fully refreshed. In other words, if we refresh tables
140
     * by names (even if we name them all) this version isn't updated.
146
     * by names (even if we name them all) this version isn't updated.
141
     * 
147
     * 
142
     * @return the version.
148
     * @return the version.
Line 171... Line 177...
171
    }
177
    }
172
 
178
 
173
    // XMLStructureSource always pre-verify so we don't need the system root lock
179
    // XMLStructureSource always pre-verify so we don't need the system root lock
174
    void load(Element schemaElem, Set<String> tableNames) {
180
    void load(Element schemaElem, Set<String> tableNames) {
175
        this.setFullyRefreshedVersion(getVersion(schemaElem));
181
        this.setFullyRefreshedVersion(getVersion(schemaElem));
176
        final List<?> l = schemaElem.getChildren("table");
182
        for (final Element elementTable : schemaElem.getChildren("table")) {
177
        for (int i = 0; i < l.size(); i++) {
-
 
178
            final Element elementTable = (Element) l.get(i);
-
 
179
            this.refreshTable(elementTable, tableNames);
183
            this.refreshTable(elementTable, tableNames);
180
        }
184
        }
181
        final Map<String, String> procMap = new HashMap<String, String>();
185
        final Map<String, String> procMap = new HashMap<String, String>();
182
        for (final Object proc : schemaElem.getChild("procedures").getChildren("proc")) {
186
        for (final Element procElem : schemaElem.getChild("procedures").getChildren("proc")) {
183
            final Element procElem = (Element) proc;
-
 
184
            final Element src = procElem.getChild("src");
187
            final Element src = procElem.getChild("src");
185
            procMap.put(procElem.getAttributeValue("name"), src == null ? null : src.getText());
188
            procMap.put(procElem.getAttributeValue("name"), src == null ? null : src.getText());
186
        }
189
        }
187
        this.putProcedures(procMap);
190
        this.putProcedures(procMap);
188
    }
191
    }
Line 194... Line 197...
194
     * @return the up to date table, <code>null</code> if not found
197
     * @return the up to date table, <code>null</code> if not found
195
     * @throws SQLException if an error occurs.
198
     * @throws SQLException if an error occurs.
196
     */
199
     */
197
    final SQLTable fetchTable(final String tableName) throws SQLException {
200
    final SQLTable fetchTable(final String tableName) throws SQLException {
198
        synchronized (getTreeMutex()) {
201
        synchronized (getTreeMutex()) {
199
            synchronized (this) {
-
 
200
                this.getBase().fetchTables(TablesMap.createFromTables(getName(), Collections.singleton(tableName)));
202
            this.getBase().fetchTables(TablesMap.createFromTables(getName(), Collections.singleton(tableName)));
201
                return this.getTable(tableName);
203
            return this.getTable(tableName);
202
            }
204
        }
203
        }
205
    }
204
    }
-
 
205
 
206
 
206
    void mutateTo(SQLSchema newSchema) {
207
    void mutateTo(SQLSchema newSchema) {
207
        assert Thread.holdsLock(this.getDBSystemRoot().getTreeMutex());
208
        assert Thread.holdsLock(this.getDBSystemRoot().getTreeMutex());
208
        synchronized (this) {
209
        synchronized (this) {
209
            this.version = newSchema.version;
210
            this.version = newSchema.version;
Line 355... Line 356...
355
    String getFwkMetadata(String name) {
356
    String getFwkMetadata(String name) {
356
        if (!this.contains(METADATA_TABLENAME))
357
        if (!this.contains(METADATA_TABLENAME))
357
            return null;
358
            return null;
358
 
359
 
359
        // we just tested for table existence
360
        // we just tested for table existence
360
        return this.getBase().getFwkMetadata(this.getName(), name, false);
361
        return this.getBase().getFwkMetadata(this.getName(), name);
361
    }
362
    }
362
 
363
 
363
    boolean setFwkMetadata(String name, String value) throws SQLException {
364
    boolean setFwkMetadata(String name, String value) throws SQLException {
364
        return this.setFwkMetadata(name, value, true).get0();
365
        return this.setFwkMetadata(name, value, true).get0();
365
    }
366
    }