OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Rev 177 | Go to most recent revision | 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
 package org.openconcerto.sql.model;
15
 
142 ilm 16
import static org.openconcerto.xml.JDOM2Utils.OUTPUTTER;
17
 
17 ilm 18
import org.openconcerto.sql.Log;
83 ilm 19
import org.openconcerto.sql.model.SQLSelect.ArchiveMode;
142 ilm 20
import org.openconcerto.sql.model.SQLSelect.LockStrength;
17 ilm 21
import org.openconcerto.sql.model.SQLSyntax.ConstraintType;
22
import org.openconcerto.sql.model.SQLTableEvent.Mode;
23
import org.openconcerto.sql.model.graph.DatabaseGraph;
24
import org.openconcerto.sql.model.graph.Link;
61 ilm 25
import org.openconcerto.sql.model.graph.Link.Rule;
93 ilm 26
import org.openconcerto.sql.model.graph.SQLKey;
27
import org.openconcerto.sql.model.graph.SQLKey.Type;
67 ilm 28
import org.openconcerto.sql.model.graph.TablesMap;
17 ilm 29
import org.openconcerto.sql.request.UpdateBuilder;
30
import org.openconcerto.sql.utils.ChangeTable;
144 ilm 31
import org.openconcerto.sql.utils.PartialUniqueTrigger;
17 ilm 32
import org.openconcerto.sql.utils.SQLCreateMoveableTable;
144 ilm 33
import org.openconcerto.sql.utils.UniqueConstraintCreatorHelper;
17 ilm 34
import org.openconcerto.utils.CollectionUtils;
35
import org.openconcerto.utils.CompareUtils;
36
import org.openconcerto.utils.ExceptionUtils;
73 ilm 37
import org.openconcerto.utils.ListMap;
83 ilm 38
import org.openconcerto.utils.SetMap;
39
import org.openconcerto.utils.StringUtils;
25 ilm 40
import org.openconcerto.utils.Tuple2;
61 ilm 41
import org.openconcerto.utils.Tuple3;
83 ilm 42
import org.openconcerto.utils.Value;
61 ilm 43
import org.openconcerto.utils.cc.CopyOnWriteMap;
132 ilm 44
import org.openconcerto.utils.cc.CustomEquals;
17 ilm 45
import org.openconcerto.utils.change.CollectionChangeEventCreator;
46
 
47
import java.math.BigDecimal;
48
import java.sql.DatabaseMetaData;
49
import java.sql.ResultSet;
50
import java.sql.SQLException;
51
import java.util.ArrayList;
67 ilm 52
import java.util.Arrays;
17 ilm 53
import java.util.Collection;
54
import java.util.Collections;
132 ilm 55
import java.util.EnumSet;
17 ilm 56
import java.util.HashMap;
57
import java.util.HashSet;
58
import java.util.Iterator;
59
import java.util.LinkedHashMap;
60
import java.util.LinkedHashSet;
25 ilm 61
import java.util.LinkedList;
17 ilm 62
import java.util.List;
73 ilm 63
import java.util.ListIterator;
17 ilm 64
import java.util.Map;
67 ilm 65
import java.util.Map.Entry;
17 ilm 66
import java.util.Set;
83 ilm 67
import java.util.regex.Matcher;
68
import java.util.regex.Pattern;
17 ilm 69
 
132 ilm 70
import org.apache.commons.dbutils.ResultSetHandler;
71
import org.jdom2.Element;
72
 
61 ilm 73
import net.jcip.annotations.GuardedBy;
93 ilm 74
import net.jcip.annotations.Immutable;
61 ilm 75
 
17 ilm 76
/**
77
 * Une table SQL. Connait ses champs, notamment sa clef primaire et ses clefs externes. Une table
78
 * peut aussi faire des diagnostic sur son intégrité, ou sur la validité d'une valeur d'un de ses
79
 * champs. Enfin elle permet d'accéder aux lignes qui la composent.
80
 *
81
 * @author ILM Informatique 4 mai 2004
82
 * @see #getField(String)
83
 * @see #getKey()
84
 * @see #getForeignKeys()
85
 * @see #checkIntegrity()
86
 * @see #checkValidity(String, int)
87
 * @see #getRow(int)
88
 */
65 ilm 89
public final class SQLTable extends SQLIdentifier implements SQLData, TableRef {
17 ilm 90
 
132 ilm 91
    private static final String UNDEF_TABLE_TABLENAME_FIELD = "TABLENAME";
92
    private static final String UNDEF_TABLE_ID_FIELD = "UNDEFINED_ID";
93
 
17 ilm 94
    /**
95
     * The {@link DBRoot#setMetadata(String, String) meta data} configuring the policy regarding
96
     * undefined IDs for a particular root. Can be either :
97
     * <dl>
67 ilm 98
     * <dt>inDB</dt>
99
     * <dd>all undefined IDs must be in {@value #undefTable}. Allow different IDs like "min" but
100
     * without the performance penalty</dd>
17 ilm 101
     * <dt>min</dt>
102
     * <dd>for min("ID")</dd>
103
     * <dt>nonexistant</dt>
104
     * <dd>(the default) {@link SQLRow#NONEXISTANT_ID}</dd>
105
     * <dt><i>any other value</i></dt>
106
     * <dd>parsed as a number</dd>
107
     * </dl>
108
     */
109
    public static final String UNDEFINED_ID_POLICY = "undefined ID policy";
41 ilm 110
    public static final String undefTable = SQLSchema.FWK_TABLENAME_PREFIX + "UNDEFINED_IDS";
17 ilm 111
    // {SQLSchema=>{TableName=>UndefID}}
112
    private static final Map<SQLSchema, Map<String, Number>> UNDEFINED_IDs = new HashMap<SQLSchema, Map<String, Number>>();
132 ilm 113
    private static final ResultSetHandler UNDEF_RSH = new ResultSetHandler() {
114
        @Override
115
        public Object handle(ResultSet rs) throws SQLException {
116
            final Map<String, Number> res = new HashMap<String, Number>();
117
            while (rs.next()) {
118
                res.put(rs.getString(UNDEF_TABLE_TABLENAME_FIELD), (Number) rs.getObject(UNDEF_TABLE_ID_FIELD));
119
            }
120
            return res;
121
        }
122
    };
17 ilm 123
 
124
    @SuppressWarnings("unchecked")
174 ilm 125
    public static final Map<String, Number> getUndefIDs(final SQLSchema schema) {
17 ilm 126
        if (!UNDEFINED_IDs.containsKey(schema)) {
127
            final Map<String, Number> r;
128
            if (schema.contains(undefTable)) {
129
                final SQLBase b = schema.getBase();
130
                final SQLTable undefT = schema.getTable(undefTable);
80 ilm 131
                final SQLSelect sel = new SQLSelect().addSelectStar(undefT);
132 ilm 132
                // don't use the cache as the result is stored in UNDEFINED_IDs
133
                r = (Map<String, Number>) b.getDataSource().execute(sel.asString(), new IResultSetHandler(UNDEF_RSH, false));
73 ilm 134
                // be called early, since it's more likely that some transaction will create table,
135
                // set its undefined ID, then use it in requests, than some other transaction
136
                // needing the undefined ID. TODO The real fix is one tree per transaction.
137
                undefT.addTableModifiedListener(new ListenerAndConfig(new SQLTableModifiedListener() {
17 ilm 138
                    @Override
139
                    public void tableModified(SQLTableEvent evt) {
140
                        synchronized (UNDEFINED_IDs) {
141
                            UNDEFINED_IDs.remove(schema);
142
                            undefT.removeTableModifiedListener(this);
143
                        }
144
                    }
73 ilm 145
                }, false));
17 ilm 146
            } else {
147
                r = Collections.emptyMap();
148
            }
149
            UNDEFINED_IDs.put(schema, r);
150
        }
151
        return UNDEFINED_IDs.get(schema);
152
    }
153
 
83 ilm 154
    static final void removeUndefID(SQLSchema s) {
155
        synchronized (UNDEFINED_IDs) {
156
            UNDEFINED_IDs.remove(s);
157
        }
158
    }
159
 
67 ilm 160
    static final Tuple2<Boolean, Number> getUndefID(SQLSchema b, String tableName) {
17 ilm 161
        synchronized (UNDEFINED_IDs) {
61 ilm 162
            final Map<String, Number> map = getUndefIDs(b);
163
            return Tuple2.create(map.containsKey(tableName), map.get(tableName));
17 ilm 164
        }
165
    }
166
 
67 ilm 167
    private static final SQLCreateMoveableTable getCreateUndefTable(SQLSyntax syntax) {
168
        final SQLCreateMoveableTable createTable = new SQLCreateMoveableTable(syntax, undefTable);
132 ilm 169
        createTable.addVarCharColumn(UNDEF_TABLE_TABLENAME_FIELD, 250);
170
        createTable.addColumn(UNDEF_TABLE_ID_FIELD, syntax.getIDType());
171
        createTable.setPrimaryKey(UNDEF_TABLE_TABLENAME_FIELD);
67 ilm 172
        return createTable;
173
    }
174
 
175
    private static final SQLTable getUndefTable(SQLSchema schema, boolean create) throws SQLException {
176
        final SQLTable undefT = schema.getTable(undefTable);
177
        if (undefT != null || !create) {
178
            return undefT;
179
        } else {
180
            schema.getDBSystemRoot().getDataSource().execute(getCreateUndefTable(SQLSyntax.get(schema)).asString(schema.getDBRoot().getName()));
181
            schema.updateVersion();
182
            return schema.fetchTable(undefTable);
183
        }
184
    }
185
 
17 ilm 186
    public static final void setUndefID(SQLSchema schema, String tableName, Integer value) throws SQLException {
67 ilm 187
        setUndefIDs(schema, Collections.singletonMap(tableName, value));
188
    }
189
 
190
    // return modified count
191
    public static final int setUndefIDs(SQLSchema schema, Map<String, ? extends Number> values) throws SQLException {
17 ilm 192
        synchronized (UNDEFINED_IDs) {
67 ilm 193
            final SQLTable undefT = getUndefTable(schema, true);
132 ilm 194
            final SQLType undefType = undefT.getField(UNDEF_TABLE_ID_FIELD).getType();
67 ilm 195
            final List<List<String>> toInsert = new ArrayList<List<String>>();
196
            final List<List<String>> toUpdate = new ArrayList<List<String>>();
197
            final Map<String, Number> currentValues = getUndefIDs(schema);
198
            final SQLBase b = schema.getBase();
199
            final SQLSystem system = b.getServer().getSQLSystem();
200
            for (final Entry<String, ? extends Number> e : values.entrySet()) {
201
                final String tableName = e.getKey();
202
                final Number undefValue = e.getValue();
203
                final List<List<String>> l;
204
                if (!currentValues.containsKey(tableName)) {
205
                    l = toInsert;
206
                } else if (CompareUtils.equals(currentValues.get(tableName), undefValue)) {
207
                    l = null;
208
                } else {
209
                    l = toUpdate;
210
                }
211
                if (l != null) {
212
                    final String undefSQL;
213
                    if (undefValue == null && system == SQLSystem.POSTGRESQL)
214
                        // column "UNDEFINED_ID" is of type integer but expression is of type text
215
                        undefSQL = "cast( NULL as " + undefType.getTypeName() + ")";
216
                    else
217
                        undefSQL = undefType.toString(undefValue);
218
                    l.add(Arrays.asList(b.quoteString(tableName), undefSQL));
219
                }
220
            }
142 ilm 221
            final SQLSyntax syntax = schema.getDBSystemRoot().getSyntax();
67 ilm 222
            if (toInsert.size() > 0) {
17 ilm 223
                // INSERT
132 ilm 224
                SQLRowValues.insertCount(undefT, "(" + SQLSyntax.quoteIdentifiers(Arrays.asList(UNDEF_TABLE_TABLENAME_FIELD, UNDEF_TABLE_ID_FIELD)) + ") " + syntax.getValues(toInsert, 2));
67 ilm 225
            }
226
            if (toUpdate.size() > 0) {
17 ilm 227
                // UPDATE
67 ilm 228
                // h2 doesn't support multi-table UPDATE
229
                if (system == SQLSystem.H2) {
230
                    final StringBuilder updates = new StringBuilder();
231
                    for (final List<String> l : toUpdate) {
132 ilm 232
                        final UpdateBuilder update = new UpdateBuilder(undefT).set(UNDEF_TABLE_ID_FIELD, l.get(1));
233
                        update.setWhere(Where.createRaw(undefT.getField(UNDEF_TABLE_TABLENAME_FIELD).getFieldRef() + " = " + l.get(0)));
67 ilm 234
                        updates.append(update.asString());
235
                        updates.append(";\n");
236
                    }
237
                    schema.getDBSystemRoot().getDataSource().execute(updates.toString());
238
                } else {
239
                    final UpdateBuilder update = new UpdateBuilder(undefT);
240
                    final String constantTableAlias = "newUndef";
241
                    update.addRawTable(syntax.getConstantTable(toUpdate, constantTableAlias, Arrays.asList("t", "v")), null);
132 ilm 242
                    update.setWhere(Where.createRaw(undefT.getField(UNDEF_TABLE_TABLENAME_FIELD).getFieldRef() + " = " + new SQLName(constantTableAlias, "t").quote()));
243
                    update.set(UNDEF_TABLE_ID_FIELD, new SQLName(constantTableAlias, "v").quote());
67 ilm 244
                    schema.getDBSystemRoot().getDataSource().execute(update.asString());
245
                }
17 ilm 246
            }
67 ilm 247
            final int res = toInsert.size() + toUpdate.size();
248
            if (res > 0) {
17 ilm 249
                undefT.fireTableModified(SQLRow.NONEXISTANT_ID);
250
            }
67 ilm 251
            return res;
17 ilm 252
        }
253
    }
254
 
73 ilm 255
    static private boolean AFTER_TX_DEFAULT = true;
256
 
257
    static public void setDefaultAfterTransaction(final boolean val) {
258
        AFTER_TX_DEFAULT = val;
259
    }
260
 
261
    static public final class ListenerAndConfig {
262
 
263
        private final SQLTableModifiedListener l;
132 ilm 264
        private final Boolean afterTx;
73 ilm 265
 
132 ilm 266
        /**
267
         * Create a new instance.
268
         *
269
         * @param l the listener.
270
         * @param afterTx <code>true</code> if <code>l</code> should only be called once a
271
         *        transaction is committed, <code>false</code> to be called in real-time (i.e.
272
         *        called a second time if a transaction is aborted), <code>null</code> to be called
273
         *        both in real-time and after the transaction succeeds.
274
         */
275
        public ListenerAndConfig(SQLTableModifiedListener l, Boolean afterTx) {
73 ilm 276
            super();
277
            if (l == null)
278
                throw new NullPointerException("Null listener");
279
            this.l = l;
280
            this.afterTx = afterTx;
281
        }
282
 
283
        public final SQLTableModifiedListener getListener() {
284
            return this.l;
285
        }
286
 
132 ilm 287
        public final Boolean callOnlyAfterTx() {
73 ilm 288
            return this.afterTx;
289
        }
290
 
291
        @Override
292
        public int hashCode() {
293
            final int prime = 31;
294
            int result = 1;
132 ilm 295
            result = prime * result + (this.afterTx == null ? 0 : this.afterTx.hashCode());
73 ilm 296
            result = prime * result + this.l.hashCode();
297
            return result;
298
        }
299
 
300
        @Override
301
        public boolean equals(Object obj) {
302
            if (this == obj)
303
                return true;
304
            if (obj == null)
305
                return false;
306
            if (getClass() != obj.getClass())
307
                return false;
308
            final ListenerAndConfig other = (ListenerAndConfig) obj;
132 ilm 309
            return CompareUtils.equals(this.afterTx, other.afterTx) && this.l.equals(other.l);
73 ilm 310
        }
311
    }
312
 
67 ilm 313
    @GuardedBy("this")
314
    private String version;
61 ilm 315
    private final CopyOnWriteMap<String, SQLField> fields;
316
    @GuardedBy("this")
142 ilm 317
    private Set<SQLField> primaryKeys;
17 ilm 318
    // the vast majority of our code use getKey(), so cache it for performance
61 ilm 319
    @GuardedBy("this")
17 ilm 320
    private SQLField primaryKey;
321
    // true if there's at most 1 primary key
61 ilm 322
    @GuardedBy("this")
17 ilm 323
    private boolean primaryKeyOK;
61 ilm 324
    @GuardedBy("this")
17 ilm 325
    private Set<SQLField> keys;
61 ilm 326
    @GuardedBy("this")
93 ilm 327
    private Map<String, FieldGroup> fieldsGroups;
328
    @GuardedBy("this")
17 ilm 329
    private final Map<String, Trigger> triggers;
330
    // null means it couldn't be retrieved
61 ilm 331
    @GuardedBy("this")
17 ilm 332
    private Set<Constraint> constraints;
333
    // always immutable so that fire can iterate safely ; to modify it, simply copy it before
334
    // (adding listeners is a lot less common than firing events)
63 ilm 335
    @GuardedBy("listenersMutex")
73 ilm 336
    private List<ListenerAndConfig> tableModifiedListeners;
337
    @GuardedBy("listenersMutex")
338
    private final ListMap<TransactionPoint, FireState> transactions;
339
    private final TransactionListener txListener;
63 ilm 340
    private final Object listenersMutex = new String("tableModifiedListeners mutex");
17 ilm 341
    // the id that foreign keys pointing to this, can use instead of NULL
342
    // a null value meaning not yet known
61 ilm 343
    @GuardedBy("this")
17 ilm 344
    private Integer undefinedID;
345
 
61 ilm 346
    @GuardedBy("this")
17 ilm 347
    private String comment;
61 ilm 348
    @GuardedBy("this")
17 ilm 349
    private String type;
350
 
351
    // empty table
352
    SQLTable(SQLSchema schema, String name) {
353
        super(schema, name);
354
        this.tableModifiedListeners = Collections.emptyList();
73 ilm 355
        this.transactions = new ListMap<TransactionPoint, FireState>();
356
        this.txListener = new TransactionListener() {
357
            @Override
358
            public void transactionEnded(TransactionPoint point) {
132 ilm 359
                fireFromTransaction(point);
73 ilm 360
            }
361
        };
61 ilm 362
        // needed for getOrderedFields()
363
        this.fields = new CopyOnWriteMap<String, SQLField>() {
364
            @Override
365
            public Map<String, SQLField> copy(Map<? extends String, ? extends SQLField> src) {
366
                return new LinkedHashMap<String, SQLField>(src);
367
            }
368
        };
369
        assert isOrdered(this.fields);
142 ilm 370
        this.primaryKeys = Collections.emptySet();
17 ilm 371
        this.primaryKey = null;
372
        this.primaryKeyOK = true;
373
        this.keys = null;
93 ilm 374
        this.fieldsGroups = null;
17 ilm 375
        this.triggers = new HashMap<String, Trigger>();
376
        // by default non-null, ie ok, only set to null on error
377
        this.constraints = new HashSet<Constraint>();
378
        // not known
379
        this.undefinedID = null;
67 ilm 380
        assert !this.undefinedIDKnown();
17 ilm 381
    }
382
 
383
    // *** setter
384
 
61 ilm 385
    synchronized void clearNonPersistent() {
17 ilm 386
        this.triggers.clear();
387
        // non-null, see ctor
388
        this.constraints = new HashSet<Constraint>();
389
    }
390
 
391
    // * from XML
392
 
393
    void loadFields(Element xml) {
67 ilm 394
        synchronized (this) {
395
            this.version = SQLSchema.getVersion(xml);
396
        }
397
 
17 ilm 398
        final LinkedHashMap<String, SQLField> newFields = new LinkedHashMap<String, SQLField>();
83 ilm 399
        for (final Element elementField : xml.getChildren("field")) {
17 ilm 400
            final SQLField f = SQLField.create(this, elementField);
401
            newFields.put(f.getName(), f);
402
        }
403
 
404
        final Element primary = xml.getChild("primary");
405
        final List<String> newPrimaryKeys = new ArrayList<String>();
83 ilm 406
        for (final Element elementField : primary.getChildren("field")) {
17 ilm 407
            final String fieldName = elementField.getAttributeValue("name");
408
            newPrimaryKeys.add(fieldName);
409
        }
410
 
61 ilm 411
        synchronized (getTreeMutex()) {
412
            synchronized (this) {
67 ilm 413
                this.setState(newFields, newPrimaryKeys, null);
17 ilm 414
 
61 ilm 415
                final Element triggersElem = xml.getChild("triggers");
416
                if (triggersElem != null)
83 ilm 417
                    for (final Element triggerElem : triggersElem.getChildren()) {
61 ilm 418
                        this.addTrigger(Trigger.fromXML(this, triggerElem));
419
                    }
17 ilm 420
 
61 ilm 421
                final Element constraintsElem = xml.getChild("constraints");
422
                if (constraintsElem == null)
423
                    this.addConstraint((Constraint) null);
424
                else
83 ilm 425
                    for (final Element elem : constraintsElem.getChildren()) {
61 ilm 426
                        this.addConstraint(Constraint.fromXML(this, elem));
427
                    }
428
 
429
                final Element commentElem = xml.getChild("comment");
430
                if (commentElem != null)
431
                    this.setComment(commentElem.getText());
432
                this.setType(xml.getAttributeValue("type"));
17 ilm 433
            }
61 ilm 434
        }
17 ilm 435
    }
436
 
61 ilm 437
    synchronized private void addTrigger(final Trigger t) {
17 ilm 438
        this.triggers.put(t.getName(), t);
439
    }
440
 
61 ilm 441
    synchronized private void addConstraint(final Constraint c) {
17 ilm 442
        if (c == null) {
443
            this.constraints = null;
444
        } else {
445
            if (this.constraints == null)
446
                this.constraints = new HashSet<Constraint>();
447
            this.constraints.add(c);
448
        }
449
    }
450
 
451
    // * from JDBC
452
 
453
    public void fetchFields() throws SQLException {
67 ilm 454
        this.getBase().fetchTables(TablesMap.createBySchemaFromTable(this));
61 ilm 455
    }
17 ilm 456
 
457
    /**
458
     * Fetch fields from the passed args.
459
     *
460
     * @param metaData the metadata.
461
     * @param rs the resultSet of a getColumns(), the cursor must be on a row.
67 ilm 462
     * @param version the version of the schema.
17 ilm 463
     * @return whether the <code>rs</code> has more row.
464
     * @throws SQLException if an error occurs.
465
     * @throws IllegalStateException if the current row of <code>rs</code> doesn't describe this.
466
     */
67 ilm 467
    boolean fetchFields(DatabaseMetaData metaData, ResultSet rs, String version) throws SQLException {
17 ilm 468
        if (!this.isUs(rs))
469
            throw new IllegalStateException("rs current row does not describe " + this);
470
 
61 ilm 471
        synchronized (getTreeMutex()) {
472
            synchronized (this) {
67 ilm 473
                this.version = version;
474
 
61 ilm 475
                // we need to match the database ordering of fields
476
                final LinkedHashMap<String, SQLField> newFields = new LinkedHashMap<String, SQLField>();
477
                // fields
478
                boolean hasNext = true;
479
                while (hasNext && this.isUs(rs)) {
480
                    final SQLField f = SQLField.create(this, rs);
481
                    newFields.put(f.getName(), f);
482
                    hasNext = rs.next();
483
                }
17 ilm 484
 
61 ilm 485
                final List<String> newPrimaryKeys = new ArrayList<String>();
486
                final ResultSet pkRS = metaData.getPrimaryKeys(this.getBase().getMDName(), this.getSchema().getName(), this.getName());
487
                while (pkRS.next()) {
488
                    newPrimaryKeys.add(pkRS.getString("COLUMN_NAME"));
489
                }
17 ilm 490
 
61 ilm 491
                this.setState(newFields, newPrimaryKeys, null);
17 ilm 492
 
61 ilm 493
                return hasNext;
494
            }
495
        }
17 ilm 496
    }
497
 
498
    void emptyFields() {
499
        this.setState(new LinkedHashMap<String, SQLField>(), Collections.<String> emptyList(), null);
500
    }
501
 
502
    private boolean isUs(final ResultSet rs) throws SQLException {
503
        final String n = rs.getString("TABLE_NAME");
504
        final String s = rs.getString("TABLE_SCHEM");
505
        return n.equals(this.getName()) && CompareUtils.equals(s, this.getSchema().getName());
506
    }
507
 
80 ilm 508
    void addTrigger(Map<String, Object> m) {
17 ilm 509
        this.addTrigger(new Trigger(this, m));
510
    }
511
 
512
    void addConstraint(Map<String, Object> m) {
513
        this.addConstraint(m == null ? null : new Constraint(this, m));
514
    }
515
 
516
    // must be called in setState() after fields have been set (for isRowable())
63 ilm 517
    private int fetchUndefID() {
151 ilm 518
        final int res;
63 ilm 519
        final SQLField pk;
520
        synchronized (this) {
521
            pk = isRowable() ? this.getKey() : null;
522
        }
523
        if (pk != null) {
61 ilm 524
            final Tuple2<Boolean, Number> currentValue = getUndefID(this.getSchema(), this.getName());
525
            if (!currentValue.get0()) {
151 ilm 526
                // no row
527
                res = this.findMinID(pk);
17 ilm 528
            } else {
529
                // a row
61 ilm 530
                final Number id = currentValue.get1();
17 ilm 531
                res = id == null ? SQLRow.NONEXISTANT_ID : id.intValue();
532
            }
533
        } else
534
            res = SQLRow.NONEXISTANT_ID;
535
        return res;
536
    }
537
 
538
    // no undef id found
63 ilm 539
    private int findMinID(SQLField pk) {
17 ilm 540
        final String debugUndef = "fwk_sql.debug.undefined_id";
541
        if (System.getProperty(debugUndef) != null)
542
            Log.get().warning("The system property '" + debugUndef + "' is deprecated, use the '" + UNDEFINED_ID_POLICY + "' metadata");
543
 
544
        final String policy = getSchema().getFwkMetadata(UNDEFINED_ID_POLICY);
545
        if (Boolean.getBoolean(debugUndef) || "min".equals(policy)) {
80 ilm 546
            final SQLSelect sel = new SQLSelect(true).addSelect(pk, "min");
17 ilm 547
            final Number undef = (Number) this.getBase().getDataSource().executeScalar(sel.asString());
548
            if (undef == null) {
549
                // empty table
550
                throw new IllegalStateException(this + " is empty, can not infer UNDEFINED_ID");
551
            } else {
142 ilm 552
                final SQLSyntax syntax = SQLSyntax.get(this);
553
                final String update = syntax.getInsertOne(new SQLName(this.getDBRoot().getName(), undefTable), Arrays.asList(UNDEF_TABLE_TABLENAME_FIELD, UNDEF_TABLE_ID_FIELD),
554
                        syntax.quoteString(this.getName()), String.valueOf(undef));
17 ilm 555
                Log.get().config("the first row (which should be the undefined):\n" + update);
556
                return undef.intValue();
557
            }
67 ilm 558
        } else if ("inDB".equals(policy)) {
174 ilm 559
            return SQLRow.NONEXISTANT_ID;
560
            // FIXME : BUG A INVESTIGUER DANS LA CREATION DE MODULE throw new
561
            // throw new IllegalStateException("Not in " + new SQLName(this.getDBRoot().getName(),
562
            // undefTable) + " : " + this.getName());
17 ilm 563
        } else if (policy != null && !"nonexistant".equals(policy)) {
564
            final int res = Integer.parseInt(policy);
565
            if (res < SQLRow.MIN_VALID_ID)
566
                throw new IllegalStateException("ID is not valid : " + res);
567
            return res;
568
        } else {
569
            // by default assume NULL is used
570
            return SQLRow.NONEXISTANT_ID;
571
        }
572
    }
573
 
574
    // * from Java
575
 
576
    void mutateTo(SQLTable table) {
61 ilm 577
        synchronized (getTreeMutex()) {
578
            synchronized (this) {
579
                this.clearNonPersistent();
67 ilm 580
                this.version = table.version;
61 ilm 581
                this.setState(table.fields, table.getPKsNames(), table.undefinedID);
83 ilm 582
                for (final Trigger t : table.triggers.values()) {
583
                    this.addTrigger(new Trigger(this, t));
584
                }
585
                if (table.constraints == null) {
61 ilm 586
                    this.constraints = null;
83 ilm 587
                } else {
588
                    for (final Constraint c : table.constraints) {
589
                        this.constraints.add(new Constraint(this, c));
590
                    }
61 ilm 591
                }
592
                this.setType(table.getType());
593
                this.setComment(table.getComment());
594
            }
17 ilm 595
        }
596
    }
597
 
598
    // * update attributes
599
 
61 ilm 600
    static private <K, V> boolean isOrdered(Map<K, V> m) {
601
        if (m instanceof CopyOnWriteMap)
602
            return isOrdered(((CopyOnWriteMap<K, V>) m).copy(Collections.<K, V> emptyMap()));
603
        return (m instanceof LinkedHashMap);
604
    }
605
 
83 ilm 606
    private void setState(Map<String, SQLField> fields, final List<String> primaryKeys, final Integer undef) {
61 ilm 607
        assert isOrdered(fields);
17 ilm 608
        // checks new fields' table (don't use ==, see below)
609
        for (final SQLField newField : fields.values()) {
610
            if (!newField.getTable().getSQLName().equals(this.getSQLName()))
611
                throw new IllegalArgumentException(newField + " is in table " + newField.getTable().getSQLName() + " not us: " + this.getSQLName());
612
        }
61 ilm 613
        synchronized (getTreeMutex()) {
614
            synchronized (this) {
615
                final CollectionChangeEventCreator c = this.createChildrenCreator();
17 ilm 616
 
61 ilm 617
                if (!fields.keySet().containsAll(this.getFieldsName())) {
618
                    for (String removed : CollectionUtils.substract(this.getFieldsName(), fields.keySet())) {
65 ilm 619
                        this.fields.remove(removed).dropped();
61 ilm 620
                    }
621
                }
17 ilm 622
 
61 ilm 623
                for (final SQLField newField : fields.values()) {
624
                    if (getChildrenNames().contains(newField.getName())) {
625
                        // re-use old instances by refreshing existing ones
626
                        this.getField(newField.getName()).mutateTo(newField);
627
                    } else {
628
                        final SQLField fieldToAdd;
629
                        // happens when the new structure is loaded in-memory
630
                        // before the current one is mutated to it
631
                        // (we already checked the fullname of the table)
632
                        if (newField.getTable() != this)
633
                            fieldToAdd = new SQLField(this, newField);
634
                        else
635
                            fieldToAdd = newField;
636
                        this.fields.put(newField.getName(), fieldToAdd);
637
                    }
638
                }
639
 
142 ilm 640
                // order matters (e.g. for indexes)
641
                final Set<SQLField> newPK = new LinkedHashSet<SQLField>();
61 ilm 642
                for (final String pk : primaryKeys)
142 ilm 643
                    newPK.add(this.getField(pk));
644
                this.primaryKeys = Collections.unmodifiableSet(newPK);
61 ilm 645
                this.primaryKey = primaryKeys.size() == 1 ? this.getField(primaryKeys.get(0)) : null;
646
                this.primaryKeyOK = primaryKeys.size() <= 1;
647
 
93 ilm 648
                this.keys = null;
649
                this.fieldsGroups = null;
650
 
61 ilm 651
                // don't fetch the ID now as it could be too early (e.g. we just created the table
652
                // but haven't inserted the undefined row)
653
                this.undefinedID = undef;
654
                this.fireChildrenChanged(c);
17 ilm 655
            }
656
        }
657
    }
658
 
659
    // *** getter
660
 
61 ilm 661
    synchronized void setType(String type) {
17 ilm 662
        this.type = type;
663
    }
664
 
61 ilm 665
    public synchronized final String getType() {
17 ilm 666
        return this.type;
667
    }
668
 
61 ilm 669
    synchronized void setComment(String comm) {
17 ilm 670
        this.comment = comm;
671
    }
672
 
61 ilm 673
    public synchronized final String getComment() {
17 ilm 674
        return this.comment;
675
    }
676
 
61 ilm 677
    public synchronized final Trigger getTrigger(String name) {
17 ilm 678
        return this.triggers.get(name);
679
    }
680
 
61 ilm 681
    public synchronized final Map<String, Trigger> getTriggers() {
17 ilm 682
        return Collections.unmodifiableMap(this.triggers);
683
    }
684
 
685
    /**
686
     * The constraints on this table.
687
     *
688
     * @return the constraints or <code>null</code> if they couldn't be retrieved.
689
     */
61 ilm 690
    public synchronized final Set<Constraint> getAllConstraints() {
17 ilm 691
        return this.constraints == null ? null : Collections.unmodifiableSet(this.constraints);
692
    }
693
 
694
    /**
83 ilm 695
     * The CHECK and UNIQUE constraints on this table. This is useful since types
696
     * {@link ConstraintType#FOREIGN_KEY FOREIGN_KEY} and {@link ConstraintType#PRIMARY_KEY
132 ilm 697
     * PRIMARY_KEY} are already available through {@link #getForeignLinks()} and
83 ilm 698
     * {@link #getPrimaryKeys()} ; type {@link ConstraintType#DEFAULT DEFAULT} through
699
     * {@link SQLField#getDefaultValue()}.
41 ilm 700
     *
701
     * @return the constraints or <code>null</code> if they couldn't be retrieved.
702
     */
61 ilm 703
    public synchronized final Set<Constraint> getConstraints() {
41 ilm 704
        if (this.constraints == null)
705
            return null;
706
        final Set<Constraint> res = new HashSet<Constraint>();
707
        for (final Constraint c : this.constraints) {
83 ilm 708
            if (c.getType() == ConstraintType.CHECK || c.getType() == ConstraintType.UNIQUE) {
41 ilm 709
                res.add(c);
710
            }
711
        }
712
        return res;
713
    }
714
 
715
    /**
17 ilm 716
     * Returns a specific constraint.
717
     *
718
     * @param type type of constraint, e.g. {@link ConstraintType#UNIQUE}.
719
     * @param cols the fields names, e.g. ["NAME"].
720
     * @return the matching constraint, <code>null</code> if it cannot be found or if constraints
721
     *         couldn't be retrieved.
722
     */
144 ilm 723
    public final Constraint getConstraint(ConstraintType type, List<String> cols) {
724
        if (type == null)
725
            throw new IllegalArgumentException("Missing type");
726
        final Set<Constraint> res = this.getConstraints(type, cols, false);
727
        if (res == null)
728
            return null;
729
        if (res.size() > 1)
730
            throw new IllegalStateException("More than one constraint matches (use getConstraints()) : " + res);
731
        return CollectionUtils.getSole(res);
732
    }
733
 
734
    /**
735
     * Search for constraints.
736
     *
737
     * @param type type of constraint, can be <code>null</code>.
738
     * @param cols the fields names, not <code>null</code>.
739
     * @param containing <code>true</code> if {@link Constraint#getCols() constraints columns}
740
     *        should {@link Collection#containsAll(Collection) contains all} the passed columns,
741
     *        <code>false</code> if they should be equal.
742
     * @return the matching constraints, <code>null</code> if constraints couldn't be retrieved.
743
     */
744
    public synchronized final Set<Constraint> getConstraints(ConstraintType type, List<String> cols, final boolean containing) {
745
        if (cols == null)
746
            throw new IllegalArgumentException("Missing columns");
17 ilm 747
        if (this.constraints == null)
748
            return null;
144 ilm 749
        final Set<Constraint> res = new HashSet<>();
17 ilm 750
        for (final Constraint c : this.constraints) {
144 ilm 751
            if ((type == null || c.getType() == type) && (containing ? c.getCols().containsAll(cols) : c.getCols().equals(cols))) {
752
                res.add(c);
17 ilm 753
            }
754
        }
144 ilm 755
        return res;
17 ilm 756
    }
757
 
758
    /**
759
     * Whether rows of this table can be represented as SQLRow.
760
     *
761
     * @return <code>true</code> if rows of this table can be represented as SQLRow.
762
     */
61 ilm 763
    public synchronized boolean isRowable() {
17 ilm 764
        return this.getPrimaryKeys().size() == 1 && Number.class.isAssignableFrom(this.getKey().getType().getJavaType());
765
    }
766
 
767
    public SQLSchema getSchema() {
768
        return (SQLSchema) this.getParent();
769
    }
770
 
771
    public SQLBase getBase() {
772
        return this.getSchema().getBase();
773
    }
774
 
73 ilm 775
    synchronized final String getVersion() {
776
        return this.version;
777
    }
778
 
17 ilm 779
    /**
780
     * Return the primary key of this table.
781
     *
782
     * @return the field which is the key of this table, or <code>null</code> if it doesn't exist.
783
     * @throws IllegalStateException if there's more than one primary key.
784
     */
65 ilm 785
    @Override
61 ilm 786
    public synchronized SQLField getKey() {
17 ilm 787
        if (!this.primaryKeyOK)
788
            throw new IllegalStateException(this + " has more than 1 primary key: " + this.getPrimaryKeys());
789
        return this.primaryKey;
790
    }
791
 
792
    /**
793
     * Return the primary keys of this table.
794
     *
795
     * @return the fields (SQLField) which are the keys of this table, can be empty.
796
     */
61 ilm 797
    public synchronized Set<SQLField> getPrimaryKeys() {
142 ilm 798
        return this.primaryKeys;
17 ilm 799
    }
800
 
132 ilm 801
    public final Set<Link> getForeignLinks() {
802
        return this.getDBSystemRoot().getGraph().getForeignLinks(this);
803
    }
804
 
17 ilm 805
    /**
806
     * Return the foreign keys of this table.
807
     *
808
     * @return a Set of SQLField which are foreign keys of this table.
809
     */
810
    public Set<SQLField> getForeignKeys() {
811
        return this.getDBSystemRoot().getGraph().getForeignKeys(this);
812
    }
813
 
814
    public Set<String> getForeignKeysNames() {
132 ilm 815
        return DatabaseGraph.getNames(this.getForeignLinks());
17 ilm 816
    }
817
 
818
    public Set<List<SQLField>> getForeignKeysFields() {
819
        return this.getDBSystemRoot().getGraph().getForeignKeysFields(this);
820
    }
821
 
822
    public Set<SQLField> getForeignKeys(String foreignTable) {
823
        return this.getForeignKeys(this.getTable(foreignTable));
824
    }
825
 
826
    public Set<SQLField> getForeignKeys(SQLTable foreignTable) {
827
        return this.getDBSystemRoot().getGraph().getForeignFields(this, foreignTable);
828
    }
829
 
830
    public SQLTable getForeignTable(String foreignField) {
65 ilm 831
        return this.getField(foreignField).getForeignTable();
17 ilm 832
    }
833
 
19 ilm 834
    public SQLTable findReferentTable(String tableName) {
835
        return this.getDBSystemRoot().getGraph().findReferentTable(this, tableName);
836
    }
837
 
17 ilm 838
    /**
839
     * Renvoie toutes les clefs de cette table. C'est à dire les clefs primaires plus les clefs
840
     * externes.
841
     *
842
     * @return toutes les clefs de cette table, can be empty.
843
     */
61 ilm 844
    public synchronized Set<SQLField> getKeys() {
17 ilm 845
        if (this.keys == null) {
132 ilm 846
            this.keys = this.getFields(VirtualFields.KEYS);
17 ilm 847
        }
848
        return this.keys;
849
    }
850
 
93 ilm 851
    @Immutable
852
    static public final class FieldGroup {
853
        private final String field;
854
        private final SQLKey key;
855
 
856
        private FieldGroup(final SQLKey key, final String field) {
857
            assert (key == null) != (field == null);
858
            this.key = key;
859
            this.field = key == null ? field : CollectionUtils.getSole(key.getFields());
860
        }
861
 
862
        /**
863
         * The key type for this group.
864
         *
865
         * @return the key type, <code>null</code> for a simple field.
866
         */
867
        public final Type getKeyType() {
868
            if (this.key == null)
869
                return null;
870
            return this.key.getType();
871
        }
872
 
873
        /**
874
         * The key for this group.
875
         *
876
         * @return the key, <code>null</code> for a simple field.
877
         */
156 ilm 878
        public final SQLKey getRawKey() {
93 ilm 879
            return this.key;
880
        }
881
 
882
        /**
156 ilm 883
         * The key for this group.
884
         *
885
         * @return the key, never <code>null</code>.
886
         * @throws IllegalStateException for a simple field.
887
         */
888
        public final SQLKey getKey() throws IllegalStateException {
889
            if (this.key == null)
890
                throw new IllegalStateException("Not a key : " + this);
891
            return this.key;
892
        }
893
 
894
        /**
895
         * The foreign link for this group.
896
         *
897
         * @return the foreign link, never <code>null</code>.
898
         * @throws IllegalStateException for a simple field or a primary key.
899
         */
900
        public final Link getForeignLink() throws IllegalStateException {
901
            if (this.key != null) {
902
                final Link foreignLink = this.key.getForeignLink();
903
                if (foreignLink != null)
904
                    return foreignLink;
905
            }
906
            throw new IllegalStateException("Not a foreign key : " + this);
907
        }
908
 
909
        /**
93 ilm 910
         * The one and only field of this group.
911
         *
912
         * @return the only field of this group, only <code>null</code> if this group is a
913
         *         {@link #getKey() key} with more than one field.
914
         */
915
        public String getSingleField() {
916
            return this.field;
917
        }
918
 
919
        public final List<String> getFields() {
920
            return this.key == null ? Arrays.asList(this.field) : this.key.getFields();
921
        }
922
 
923
        @Override
924
        public String toString() {
925
            return this.getClass().getSimpleName() + " " + (this.key != null ? this.key.toString() : this.field);
926
        }
927
    }
928
 
929
    /**
930
     * Return the fields grouped by key.
931
     *
932
     * @return for each field of this table the matching group.
933
     */
934
    public synchronized Map<String, FieldGroup> getFieldGroups() {
935
        if (this.fieldsGroups == null) {
936
            final Map<String, FieldGroup> res = new LinkedHashMap<String, FieldGroup>();
937
            // set order
938
            for (final String field : this.getFieldsName()) {
939
                res.put(field, new FieldGroup(null, field));
940
            }
132 ilm 941
            for (final Link l : this.getForeignLinks()) {
93 ilm 942
                indexKey(res, SQLKey.createForeignKey(l));
943
            }
944
            final SQLKey pk = SQLKey.createPrimaryKey(this);
945
            if (pk != null)
946
                indexKey(res, pk);
947
 
948
            this.fieldsGroups = Collections.unmodifiableMap(res);
949
        }
950
        return this.fieldsGroups;
951
    }
952
 
953
    static private final void indexKey(final Map<String, FieldGroup> m, final SQLKey k) {
954
        final FieldGroup group = new FieldGroup(k, null);
955
        for (final String field : k.getFields()) {
956
            final FieldGroup previous = m.put(field, group);
957
            assert previous.getKeyType() == null;
958
        }
959
    }
960
 
17 ilm 961
    public String toString() {
962
        return "/" + this.getName() + "/";
963
    }
964
 
965
    /**
966
     * Return the field named <i>fieldName </i> in this table.
967
     *
968
     * @param fieldName the name of the field.
969
     * @return the matching field, never <code>null</code>.
970
     * @throws IllegalArgumentException if the field is not in this table.
971
     * @see #getFieldRaw(String)
972
     */
65 ilm 973
    @Override
17 ilm 974
    public SQLField getField(String fieldName) {
975
        SQLField res = this.getFieldRaw(fieldName);
19 ilm 976
        if (res == null) {
61 ilm 977
            throw new IllegalArgumentException("unknown field " + fieldName + " in " + this.getName() + ". The table " + this.getName() + " contains the followins fields: " + this.getFieldsName());
19 ilm 978
        }
17 ilm 979
        return res;
980
    }
981
 
982
    /**
983
     * Return the field named <i>fieldName</i> in this table.
984
     *
985
     * @param fieldName the name of the field.
986
     * @return the matching field or <code>null</code> if none exists.
987
     */
988
    public SQLField getFieldRaw(String fieldName) {
61 ilm 989
        return this.fields.get(fieldName);
17 ilm 990
    }
991
 
992
    /**
993
     * Return all the fields in this table.
994
     *
995
     * @return a Set of the fields.
996
     */
997
    public Set<SQLField> getFields() {
998
        return new HashSet<SQLField>(this.fields.values());
999
    }
1000
 
132 ilm 1001
    /**
1002
     * An immutable set of fields.
1003
     *
1004
     * @author Sylvain
1005
     */
1006
    @Immutable
1007
    static public final class VirtualFields {
1008
 
151 ilm 1009
        // must be set first, as they are used by others (e.g. create())
1010
        static public final VirtualFields NONE = new VirtualFields(EnumSet.noneOf(VirtualFieldPartition.class));
1011
        static public final VirtualFields ALL = new VirtualFields(EnumSet.allOf(VirtualFieldPartition.class));
1012
 
132 ilm 1013
        static public final VirtualFields ORDER = new VirtualFields(VirtualFieldPartition.ORDER);
1014
        static public final VirtualFields ARCHIVE = new VirtualFields(VirtualFieldPartition.ARCHIVE);
1015
        static public final VirtualFields METADATA = new VirtualFields(VirtualFieldPartition.METADATA);
1016
        static public final VirtualFields PRIMARY_KEY = new VirtualFields(VirtualFieldPartition.PRIMARY_KEY);
1017
        static public final VirtualFields FOREIGN_KEYS = new VirtualFields(VirtualFieldPartition.FOREIGN_KEYS);
1018
        /**
1019
         * All specific fields of this table without keys.
1020
         */
1021
        static public final VirtualFields LOCAL_CONTENT = new VirtualFields(VirtualFieldPartition.LOCAL_CONTENT);
1022
 
1023
        /**
1024
         * {@link #LOCAL_CONTENT local content fields} with {@link #FOREIGN_KEYS}.
1025
         */
1026
        static public final VirtualFields CONTENT = LOCAL_CONTENT.union(FOREIGN_KEYS);
1027
        /**
1028
         * {@link #CONTENT content fields} with {@link #METADATA}.
1029
         */
1030
        static public final VirtualFields CONTENT_AND_METADATA = CONTENT.union(METADATA);
1031
        /**
1032
         * {@link #PRIMARY_KEY} with {@link #FOREIGN_KEYS}.
1033
         */
1034
        static public final VirtualFields KEYS = PRIMARY_KEY.union(FOREIGN_KEYS);
1035
 
151 ilm 1036
        static private VirtualFields create(final EnumSet<VirtualFieldPartition> set) {
1037
            if (set.isEmpty())
1038
                return NONE;
1039
            else if (set.equals(ALL.set))
1040
                return ALL;
1041
            else
1042
                return new VirtualFields(set);
1043
        }
1044
 
132 ilm 1045
        private final EnumSet<VirtualFieldPartition> set;
1046
 
1047
        // use constants above
1048
        private VirtualFields(final VirtualFieldPartition single) {
1049
            this(EnumSet.of(single));
1050
        }
1051
 
1052
        // private since parameter is not copied
1053
        private VirtualFields(final EnumSet<VirtualFieldPartition> set) {
1054
            if (set == null)
1055
                throw new NullPointerException("Null set");
1056
            this.set = set;
1057
        }
1058
 
151 ilm 1059
        public final boolean contains(VirtualFields other) {
1060
            return this == other || this == ALL || this != NONE && this.set.containsAll(other.set);
1061
        }
1062
 
132 ilm 1063
        public final VirtualFields union(VirtualFields... other) {
151 ilm 1064
            // optimizations
1065
            if (this == ALL || other.length == 0 || other.length == 1 && this.contains(other[0]))
1066
                return this;
1067
            if (other.length == 1 && other[0].contains(this))
1068
                return other[0];
1069
 
132 ilm 1070
            final EnumSet<VirtualFieldPartition> set = this.set.clone();
1071
            for (final VirtualFields o : other)
1072
                set.addAll(o.set);
151 ilm 1073
            return create(set);
132 ilm 1074
        }
1075
 
1076
        public final VirtualFields intersection(VirtualFields... other) {
151 ilm 1077
            // optimizations
1078
            if (this == NONE || other.length == 0 || other.length == 1 && other[0].contains(this))
1079
                return this;
1080
            if (other.length == 1 && this.contains(other[0]))
1081
                return other[0];
1082
 
132 ilm 1083
            final EnumSet<VirtualFieldPartition> set = this.set.clone();
1084
            for (final VirtualFields o : other)
1085
                set.retainAll(o.set);
151 ilm 1086
            return create(set);
132 ilm 1087
        }
1088
 
1089
        public final VirtualFields difference(VirtualFields... other) {
151 ilm 1090
            // optimizations
1091
            if (this == NONE || other.length == 0 || other.length == 1 && Collections.disjoint(this.set, other[0].set))
1092
                return this;
1093
 
132 ilm 1094
            final EnumSet<VirtualFieldPartition> set = this.set.clone();
1095
            for (final VirtualFields o : other)
1096
                set.removeAll(o.set);
151 ilm 1097
            return create(set);
132 ilm 1098
        }
1099
 
1100
        public final VirtualFields complement() {
1101
            // optimizations
1102
            if (this == ALL)
1103
                return NONE;
1104
            else if (this == NONE)
1105
                return ALL;
1106
            return new VirtualFields(EnumSet.complementOf(this.set));
1107
        }
1108
 
1109
        @Override
1110
        public int hashCode() {
1111
            final int prime = 31;
1112
            return prime + this.set.hashCode();
1113
        }
1114
 
1115
        @Override
1116
        public boolean equals(Object obj) {
1117
            if (this == obj)
1118
                return true;
1119
            if (obj == null)
1120
                return false;
1121
            if (getClass() != obj.getClass())
1122
                return false;
1123
            final VirtualFields other = (VirtualFields) obj;
1124
            return this.set.equals(other.set);
1125
        }
151 ilm 1126
 
1127
        @Override
1128
        public String toString() {
1129
            return this.getClass().getSimpleName() + " " + this.set;
1130
        }
132 ilm 1131
    }
1132
 
1133
    /**
1134
     * A partition of the fields (except that some can be empty). Being a partition allow to use
1135
     * {@link EnumSet#complementOf(EnumSet)}.
1136
     *
1137
     * @author Sylvain
1138
     * @see VirtualFields
1139
     */
1140
    static public enum VirtualFieldPartition {
83 ilm 1141
        ORDER {
1142
            @Override
132 ilm 1143
            Set<SQLField> getFields(SQLTable t) {
83 ilm 1144
                final SQLField orderField = t.getOrderField();
1145
                return orderField == null ? Collections.<SQLField> emptySet() : Collections.singleton(orderField);
1146
            }
1147
        },
1148
        ARCHIVE {
1149
            @Override
1150
            Set<SQLField> getFields(SQLTable t) {
1151
                final SQLField f = t.getArchiveField();
1152
                return f == null ? Collections.<SQLField> emptySet() : Collections.singleton(f);
1153
            }
1154
        },
1155
        METADATA {
1156
            @Override
1157
            Set<SQLField> getFields(SQLTable t) {
1158
                final Set<SQLField> res = new HashSet<SQLField>(4);
1159
                res.add(t.getCreationDateField());
1160
                res.add(t.getCreationUserField());
1161
                res.add(t.getModifDateField());
1162
                res.add(t.getModifUserField());
1163
                res.remove(null);
1164
                return res;
1165
            }
1166
        },
1167
        PRIMARY_KEY {
1168
            @Override
1169
            Set<SQLField> getFields(SQLTable t) {
1170
                return t.getPrimaryKeys();
1171
            }
1172
        },
1173
        FOREIGN_KEYS {
1174
            @Override
132 ilm 1175
            Set<SQLField> getFields(SQLTable t) {
1176
                return DatabaseGraph.getColsUnion(t.getForeignLinks());
83 ilm 1177
            }
93 ilm 1178
        },
1179
        /**
1180
         * {@link #CONTENT content fields} without {@link #FOREIGN_KEYS}
1181
         */
1182
        LOCAL_CONTENT {
1183
            @Override
132 ilm 1184
            Set<SQLField> getFields(SQLTable t) {
1185
                throw new IllegalStateException(this + " is any field not in another set");
93 ilm 1186
            }
83 ilm 1187
        };
1188
 
1189
        abstract Set<SQLField> getFields(final SQLTable t);
1190
    }
1191
 
132 ilm 1192
    public final Set<SQLField> getFields(final VirtualFields vfs) {
1193
        return getFields(vfs.set);
83 ilm 1194
    }
1195
 
132 ilm 1196
    final Set<SQLField> getFields(final Set<VirtualFieldPartition> vf) {
1197
        if (vf.isEmpty())
1198
            return Collections.emptySet();
144 ilm 1199
        else if (vf.equals(VirtualFields.ALL.set))
1200
            return Collections.unmodifiableSet(this.getFields());
83 ilm 1201
 
132 ilm 1202
        final Set<SQLField> res;
1203
        // LOCAL_CONTENT is just ALL minus every other set
1204
        if (!vf.contains(VirtualFieldPartition.LOCAL_CONTENT)) {
1205
            res = new HashSet<SQLField>();
1206
            for (final VirtualFieldPartition v : vf) {
1207
                res.addAll(v.getFields(this));
83 ilm 1208
            }
132 ilm 1209
        } else {
1210
            res = this.getFields();
1211
            // don't use EnumSet.complementOf(EnumSet.copyOf()) as it makes multiple copies
1212
            for (final VirtualFieldPartition v : VirtualFieldPartition.values()) {
1213
                if (!vf.contains(v)) {
1214
                    res.removeAll(v.getFields(this));
1215
                }
1216
            }
83 ilm 1217
        }
142 ilm 1218
        return Collections.unmodifiableSet(res);
83 ilm 1219
    }
1220
 
132 ilm 1221
    public final Set<String> getFieldsNames(final VirtualFields vfs) {
144 ilm 1222
        if (vfs.equals(VirtualFields.ALL))
1223
            return this.getFieldsName();
132 ilm 1224
        final Set<String> res = new HashSet<String>();
1225
        for (final SQLField f : this.getFields(vfs)) {
1226
            res.add(f.getName());
83 ilm 1227
        }
1228
        return res;
1229
    }
1230
 
142 ilm 1231
    public final List<SQLField> getFields(final Collection<String> names) {
1232
        return this.getFields(names, new ArrayList<SQLField>());
1233
    }
1234
 
1235
    public final <T extends Collection<SQLField>> T getFields(final Collection<String> names, final T res) {
1236
        return this.getFields(names, res, true);
1237
    }
1238
 
1239
    public final <T extends Collection<SQLField>> T getFields(final Collection<String> names, final T res, final boolean required) {
1240
        for (final String name : names) {
1241
            final SQLField f = required ? this.getField(name) : this.getFieldRaw(name);
1242
            if (f != null)
1243
                res.add(f);
1244
        }
1245
        return res;
1246
    }
1247
 
17 ilm 1248
    /**
1249
     * Retourne les champs du contenu de cette table. C'est à dire ni la clef primaire, ni les
1250
     * champs d'archive et d'ordre.
1251
     *
1252
     * @return les champs du contenu de cette table.
132 ilm 1253
     * @see VirtualFields#CONTENT
17 ilm 1254
     */
1255
    public Set<SQLField> getContentFields() {
1256
        return this.getContentFields(false);
1257
    }
1258
 
61 ilm 1259
    public synchronized Set<SQLField> getContentFields(final boolean includeMetadata) {
132 ilm 1260
        return this.getFields(includeMetadata ? VirtualFields.CONTENT_AND_METADATA : VirtualFields.CONTENT);
17 ilm 1261
    }
1262
 
1263
    /**
1264
     * Retourne les champs du contenu local de cette table. C'est à dire uniquement les champs du
1265
     * contenu qui ne sont pas des clefs externes.
1266
     *
1267
     * @return les champs du contenu local de cette table.
1268
     * @see #getContentFields()
1269
     */
61 ilm 1270
    public synchronized Set<SQLField> getLocalContentFields() {
132 ilm 1271
        return this.getFields(VirtualFields.LOCAL_CONTENT);
17 ilm 1272
    }
1273
 
1274
    /**
1275
     * Return the names of all the fields.
1276
     *
1277
     * @return the names of all the fields.
1278
     */
1279
    public Set<String> getFieldsName() {
1280
        return this.fields.keySet();
1281
    }
1282
 
1283
    /**
1284
     * Return all the fields in this table. The order is the same across reboot.
1285
     *
1286
     * @return a List of the fields.
1287
     */
1288
    public List<SQLField> getOrderedFields() {
1289
        return new ArrayList<SQLField>(this.fields.values());
1290
    }
1291
 
1292
    @Override
61 ilm 1293
    public Map<String, SQLField> getChildrenMap() {
1294
        return this.fields.getImmutable();
17 ilm 1295
    }
1296
 
1297
    public final SQLTable getTable(String name) {
1298
        return this.getDesc(name, SQLTable.class);
1299
    }
1300
 
1301
    /**
1302
     * Retourne le nombre total de lignes contenues dans cette table.
1303
     *
1304
     * @return le nombre de lignes de cette table.
1305
     */
1306
    public int getRowCount() {
67 ilm 1307
        return this.getRowCount(true);
1308
    }
1309
 
1310
    public int getRowCount(final boolean includeUndefined) {
83 ilm 1311
        return this.getRowCount(includeUndefined, ArchiveMode.BOTH);
1312
    }
1313
 
1314
    public int getRowCount(final boolean includeUndefined, final ArchiveMode archiveMode) {
67 ilm 1315
        final SQLSelect sel = new SQLSelect(true).addSelectFunctionStar("count").addFrom(this);
1316
        sel.setExcludeUndefined(!includeUndefined);
83 ilm 1317
        sel.setArchivedPolicy(archiveMode);
17 ilm 1318
        final Number count = (Number) this.getBase().getDataSource().execute(sel.asString(), new IResultSetHandler(SQLDataSource.SCALAR_HANDLER, false));
1319
        return count.intValue();
1320
    }
1321
 
1322
    /**
1323
     * The maximum value of the order field.
1324
     *
1325
     * @return the maximum value of the order field, or -1 if this table is empty.
1326
     */
1327
    public BigDecimal getMaxOrder() {
1328
        return this.getMaxOrder(true);
1329
    }
1330
 
83 ilm 1331
    public BigDecimal getMaxOrder(Boolean useCache) {
1332
        final SQLField orderField = this.getOrderField();
1333
        if (orderField == null)
17 ilm 1334
            throw new IllegalStateException(this + " is not ordered");
83 ilm 1335
        final SQLSelect sel = new SQLSelect(true).addSelect(orderField, "max");
17 ilm 1336
        try {
1337
            final BigDecimal maxOrder = (BigDecimal) this.getBase().getDataSource().execute(sel.asString(), new IResultSetHandler(SQLDataSource.SCALAR_HANDLER, useCache));
1338
            return maxOrder == null ? BigDecimal.ONE.negate() : maxOrder;
1339
        } catch (ClassCastException e) {
144 ilm 1340
            throw new IllegalStateException(orderField.getSQLName() + " must be " + SQLSyntax.get(this).getOrderDefinition(false), e);
17 ilm 1341
        }
1342
    }
1343
 
1344
    /**
1345
     * Retourne la ligne correspondant à l'ID passé.
1346
     *
1347
     * @param ID l'identifiant de la ligne à retourner.
1348
     * @return une ligne existant dans la base sinon <code>null</code>.
1349
     * @see #getValidRow(int)
1350
     */
1351
    public SQLRow getRow(int ID) {
1352
        SQLRow row = this.getUncheckedRow(ID);
1353
        return row.exists() ? row : null;
1354
    }
1355
 
1356
    /**
1357
     * Retourne une la ligne demandée sans faire aucune vérification.
1358
     *
1359
     * @param ID l'identifiant de la ligne à retourner.
1360
     * @return la ligne demandée, jamais <code>null</code>.
1361
     */
1362
    private SQLRow getUncheckedRow(int ID) {
1363
        return new SQLRow(this, ID);
1364
    }
1365
 
1366
    /**
1367
     * Retourne la ligne valide correspondant à l'ID passé.
1368
     *
1369
     * @param ID l'identifiant de la ligne à retourner.
1370
     * @return une ligne existante et non archivée dans la base sinon <code>null</code>.
1371
     * @see SQLRow#isValid()
1372
     */
1373
    public SQLRow getValidRow(int ID) {
1374
        SQLRow row = this.getRow(ID);
1375
        return row.isValid() ? row : null;
1376
    }
1377
 
1378
    /**
1379
     * Vérifie la validité de cet ID. C'est à dire qu'il existe une ligne non archivée avec cet ID,
1380
     * dans cette table.
1381
     *
1382
     * @param ID l'identifiant.
1383
     * @return <code>null</code> si l'ID est valide, sinon une SQLRow qui est soit inexistante, soit
1384
     *         archivée.
1385
     */
1386
    public SQLRow checkValidity(int ID) {
144 ilm 1387
        // don't bother locking if we're outside a transaction
1388
        final SQLRow row = SQLRow.createFromSelect(this, VirtualFields.PRIMARY_KEY.union(VirtualFields.ARCHIVE), ID, LockStrength.NONE);
17 ilm 1389
        // l'inverse de getValidRow()
1390
        return row.isValid() ? null : row;
1391
    }
1392
 
1393
    /**
1394
     * Vérifie cette table est intègre. C'est à dire que toutes ses clefs externes pointent sur des
1395
     * lignes existantes et non effacées. Cette méthode retourne une liste constituée de triplet :
1396
     * SQLRow (la ligne incohérente), SQLField (le champ incohérent), SQLRow (la ligne invalide de
1397
     * la table étrangère).
1398
     *
61 ilm 1399
     * @return a list of inconsistencies or <code>null</code> if this table is not rowable.
17 ilm 1400
     */
61 ilm 1401
    public List<Tuple3<SQLRow, SQLField, SQLRow>> checkIntegrity() {
1402
        final SQLField pk;
1403
        final Set<SQLField> fks;
1404
        synchronized (this) {
1405
            if (!this.isRowable())
1406
                return null;
1407
            pk = this.getKey();
1408
            fks = this.getForeignKeys();
1409
        }
17 ilm 1410
 
61 ilm 1411
        final List<Tuple3<SQLRow, SQLField, SQLRow>> inconsistencies = new ArrayList<Tuple3<SQLRow, SQLField, SQLRow>>();
17 ilm 1412
        // si on a pas de relation externe, c'est OK
61 ilm 1413
        if (!fks.isEmpty()) {
80 ilm 1414
            final SQLSelect sel = new SQLSelect();
17 ilm 1415
            // on ne vérifie pas les lignes archivées mais l'indéfinie oui.
1416
            sel.setExcludeUndefined(false);
61 ilm 1417
            sel.addSelect(pk);
1418
            sel.addAllSelect(fks);
17 ilm 1419
            this.getBase().getDataSource().execute(sel.asString(), new ResultSetHandler() {
1420
                public Object handle(ResultSet rs) throws SQLException {
1421
                    while (rs.next()) {
61 ilm 1422
                        for (final SQLField fk : fks) {
1423
                            final SQLRow pb = SQLTable.this.checkValidity(fk.getName(), rs.getInt(fk.getFullName()));
17 ilm 1424
                            if (pb != null) {
61 ilm 1425
                                final SQLRow row = SQLTable.this.getRow(rs.getInt(pk.getFullName()));
1426
                                inconsistencies.add(Tuple3.create(row, fk, pb));
17 ilm 1427
                            }
1428
                        }
1429
                    }
1430
                    // on s'en sert pas
1431
                    return null;
1432
                }
1433
            });
1434
        }
1435
 
1436
        return inconsistencies;
1437
    }
1438
 
1439
    /**
1440
     * Vérifie que l'on peut affecter <code>foreignID</code> au champ <code>foreignKey</code> de
1441
     * cette table. C'est à dire vérifie que la table sur laquelle pointe <code>foreignKey</code>
1442
     * contient bien une ligne d'ID <code>foreignID</code> et de plus qu'elle n'a pas été archivée.
1443
     *
1444
     * @param foreignKey le nom du champ.
1445
     * @param foreignID l'ID que l'on souhaite tester.
1446
     * @return une SQLRow décrivant l'incohérence ou <code>null</code> sinon.
1447
     * @throws IllegalArgumentException si le champ passé n'est pas une clef étrangère.
1448
     * @see #checkValidity(int)
1449
     */
1450
    public SQLRow checkValidity(String foreignKey, int foreignID) {
1451
        final SQLField fk = this.getField(foreignKey);
1452
        final SQLTable foreignTable = this.getDBSystemRoot().getGraph().getForeignTable(fk);
1453
        if (foreignTable == null)
1454
            throw new IllegalArgumentException("Impossible de tester '" + foreignKey + "' avec " + foreignID + " dans " + this + ". Ce n'est pas une clef étrangère.");
1455
        return foreignTable.checkValidity(foreignID);
1456
    }
1457
 
1458
    public SQLRow checkValidity(String foreignKey, Number foreignID) {
1459
        // NULL is valid
1460
        if (foreignID == null)
1461
            return null;
1462
        else
1463
            return this.checkValidity(foreignKey, foreignID.intValue());
1464
    }
1465
 
1466
    public boolean isOrdered() {
1467
        return this.getOrderField() != null;
1468
    }
1469
 
1470
    public SQLField getOrderField() {
1471
        return this.getFieldRaw(orderField);
1472
    }
1473
 
19 ilm 1474
    /**
1475
     * The number of fractional digits of the order field.
1476
     *
1477
     * @return the number of fractional digits of the order field.
1478
     */
1479
    public final int getOrderDecimalDigits() {
1480
        return this.getOrderField().getType().getDecimalDigits().intValue();
1481
    }
1482
 
1483
    public final BigDecimal getOrderULP() {
1484
        return BigDecimal.ONE.scaleByPowerOfTen(-this.getOrderDecimalDigits());
1485
    }
1486
 
17 ilm 1487
    public boolean isArchivable() {
1488
        return this.getArchiveField() != null;
1489
    }
1490
 
1491
    public SQLField getArchiveField() {
1492
        return this.getFieldRaw(archiveField);
1493
    }
1494
 
1495
    public SQLField getCreationDateField() {
1496
        return this.getFieldRaw("CREATION_DATE");
1497
    }
1498
 
1499
    public SQLField getCreationUserField() {
1500
        return this.getFieldRaw("ID_USER_COMMON_CREATE");
1501
    }
1502
 
1503
    public SQLField getModifDateField() {
1504
        return this.getFieldRaw("MODIFICATION_DATE");
1505
    }
1506
 
1507
    public SQLField getModifUserField() {
1508
        return this.getFieldRaw("ID_USER_COMMON_MODIFY");
1509
    }
1510
 
1511
    /**
1512
     * The id of this table which means empty. Tables that aren't rowable or which use NULL to
1513
     * signify empty have no UNDEFINED_ID.
1514
     *
1515
     * @return the empty id or {@link SQLRow#NONEXISTANT_ID} if this table has no UNDEFINED_ID.
1516
     */
1517
    public final int getUndefinedID() {
63 ilm 1518
        return this.getUndefinedID(false).intValue();
1519
    }
1520
 
67 ilm 1521
    // if false getUndefinedID() might contact the DB
1522
    synchronized final boolean undefinedIDKnown() {
1523
        return this.undefinedID != null;
1524
    }
1525
 
1526
    /*
1527
     * No longer save the undefined IDs. We mustn't search undefined IDs when loading structure
1528
     * since the undefined rows might not yet be inserted. When getUndefinedID() was called, we used
1529
     * to save the ID alongside the table structure with the new structure version. Which is wrong
1530
     * since we haven't refreshed the table structure. One solution would be to create an undefined
1531
     * ID version : when loading, as with the structure, we now have to check the saved version
1532
     * against the one in the metadata table, but since FWK_UNDEFINED_ID is small and already
1533
     * cached, we might as well simplify and forego the version altogether.
1534
     */
1535
 
63 ilm 1536
    private final Integer getUndefinedID(final boolean internal) {
61 ilm 1537
        Integer res = null;
1538
        synchronized (this) {
1539
            if (this.undefinedID != null)
1540
                res = this.undefinedID;
1541
        }
1542
        if (res == null) {
63 ilm 1543
            if (!internal && this.getSchema().isFetchAllUndefinedIDs()) {
61 ilm 1544
                // init all undefined, MAYBE one request with UNION ALL
1545
                for (final SQLTable sibling : this.getSchema().getTables()) {
67 ilm 1546
                    Integer siblingRes = sibling.getUndefinedID(true);
63 ilm 1547
                    assert siblingRes != null;
1548
                    if (sibling == this)
1549
                        res = siblingRes;
17 ilm 1550
                }
61 ilm 1551
            } else {
1552
                res = this.fetchUndefID();
1553
                synchronized (this) {
1554
                    this.undefinedID = res;
1555
                }
17 ilm 1556
            }
1557
        }
67 ilm 1558
        assert this.undefinedIDKnown();
63 ilm 1559
        return res;
17 ilm 1560
    }
1561
 
21 ilm 1562
    public final Number getUndefinedIDNumber() {
1563
        final int res = this.getUndefinedID();
1564
        if (res == SQLRow.NONEXISTANT_ID)
1565
            return null;
1566
        else
1567
            return res;
1568
    }
1569
 
17 ilm 1570
    // static
1571
 
1572
    static private final String orderField = "ORDRE";
1573
    static private final String archiveField = "ARCHIVE";
1574
 
1575
    // /////// ******** OLD CODE ********
1576
 
1577
    /*
1578
     * Gestion des événements
1579
     */
1580
 
1581
    public void addTableModifiedListener(SQLTableModifiedListener l) {
73 ilm 1582
        this.addTableModifiedListener(new ListenerAndConfig(l, AFTER_TX_DEFAULT));
1583
    }
1584
 
1585
    public void addTableModifiedListener(ListenerAndConfig l) {
25 ilm 1586
        this.addTableModifiedListener(l, false);
1587
    }
1588
 
73 ilm 1589
    public void addPremierTableModifiedListener(ListenerAndConfig l) {
25 ilm 1590
        this.addTableModifiedListener(l, true);
1591
    }
1592
 
73 ilm 1593
    private void addTableModifiedListener(ListenerAndConfig l, final boolean before) {
63 ilm 1594
        synchronized (this.listenersMutex) {
73 ilm 1595
            final List<ListenerAndConfig> newListeners = new ArrayList<ListenerAndConfig>(this.tableModifiedListeners.size() + 1);
25 ilm 1596
            if (before)
1597
                newListeners.add(l);
1598
            newListeners.addAll(this.tableModifiedListeners);
1599
            if (!before)
1600
                newListeners.add(l);
17 ilm 1601
            this.tableModifiedListeners = Collections.unmodifiableList(newListeners);
1602
        }
1603
    }
1604
 
1605
    public void removeTableModifiedListener(SQLTableModifiedListener l) {
73 ilm 1606
        this.removeTableModifiedListener(new ListenerAndConfig(l, AFTER_TX_DEFAULT));
1607
    }
1608
 
1609
    public void removeTableModifiedListener(ListenerAndConfig l) {
63 ilm 1610
        synchronized (this.listenersMutex) {
73 ilm 1611
            final List<ListenerAndConfig> newListeners = new ArrayList<ListenerAndConfig>(this.tableModifiedListeners);
17 ilm 1612
            if (newListeners.remove(l))
1613
                this.tableModifiedListeners = Collections.unmodifiableList(newListeners);
1614
        }
1615
    }
1616
 
25 ilm 1617
    private static final class BridgeListener implements SQLTableModifiedListener {
1618
 
1619
        private final SQLTableListener l;
1620
 
1621
        private BridgeListener(SQLTableListener l) {
1622
            super();
1623
            this.l = l;
1624
        }
1625
 
1626
        @Override
1627
        public void tableModified(SQLTableEvent evt) {
1628
            final Mode mode = evt.getMode();
1629
            if (mode == Mode.ROW_ADDED)
1630
                this.l.rowAdded(evt.getTable(), evt.getId());
1631
            else if (mode == Mode.ROW_UPDATED)
1632
                this.l.rowModified(evt.getTable(), evt.getId());
1633
            else if (mode == Mode.ROW_DELETED)
1634
                this.l.rowDeleted(evt.getTable(), evt.getId());
1635
        }
1636
 
1637
        @Override
1638
        public int hashCode() {
1639
            return this.l.hashCode();
1640
        }
1641
 
1642
        @Override
1643
        public boolean equals(Object obj) {
1644
            return obj instanceof BridgeListener && this.l.equals(((BridgeListener) obj).l);
1645
        }
1646
    }
1647
 
17 ilm 1648
    /**
1649
     * Ajoute un listener sur cette table.
1650
     *
1651
     * @param l the listener.
1652
     * @deprecated use {@link #addTableModifiedListener(SQLTableModifiedListener)}
1653
     */
1654
    public void addTableListener(SQLTableListener l) {
25 ilm 1655
        this.addTableModifiedListener(new BridgeListener(l));
17 ilm 1656
    }
1657
 
1658
    public void removeTableListener(SQLTableListener l) {
25 ilm 1659
        this.removeTableModifiedListener(new BridgeListener(l));
17 ilm 1660
    }
1661
 
1662
    /**
1663
     * Previent tous les listeners de la table qu'il y a eu une modification ou ajout si modif de
1664
     * d'une ligne particuliere.
1665
     *
1666
     * @param id -1 signifie tout est modifié.
1667
     */
1668
    public void fireTableModified(final int id) {
1669
        this.fire(Mode.ROW_UPDATED, id);
1670
    }
1671
 
1672
    public void fireRowAdded(final int id) {
1673
        this.fire(Mode.ROW_ADDED, id);
1674
    }
1675
 
1676
    public void fireRowDeleted(final int id) {
1677
        this.fire(Mode.ROW_DELETED, id);
1678
    }
1679
 
1680
    public void fireTableModified(final int id, Collection<String> fields) {
1681
        this.fire(new SQLTableEvent(this, id, Mode.ROW_UPDATED, fields));
1682
    }
1683
 
1684
    private void fire(final Mode mode, final int id) {
1685
        this.fire(new SQLTableEvent(this, id, mode, null));
1686
    }
1687
 
1688
    public final void fire(SQLTableEvent evt) {
25 ilm 1689
        this.fireTableModified(evt);
1690
    }
1691
 
73 ilm 1692
    // the listeners and the event that was notified to them
1693
    static private class FireState extends Tuple2<List<ListenerAndConfig>, SQLTableEvent> {
1694
        public FireState(final List<ListenerAndConfig> listeners, final SQLTableEvent evt) {
1695
            super(listeners, evt);
1696
        }
1697
 
1698
        private DispatchingState createDispatchingState(final Boolean callbackAfterTxListeners, final boolean oppositeEvt) {
1699
            final List<SQLTableModifiedListener> listeners = new LinkedList<SQLTableModifiedListener>();
1700
            for (final ListenerAndConfig l : get0()) {
132 ilm 1701
                if (callbackAfterTxListeners == null || l.callOnlyAfterTx() == null || callbackAfterTxListeners == l.callOnlyAfterTx())
73 ilm 1702
                    listeners.add(l.getListener());
1703
            }
1704
            return new DispatchingState(listeners, oppositeEvt ? get1().opposite() : get1());
1705
        }
1706
    }
1707
 
1708
    static private class DispatchingState extends Tuple2<Iterator<SQLTableModifiedListener>, SQLTableEvent> {
1709
        public DispatchingState(final List<SQLTableModifiedListener> listeners, final SQLTableEvent evt) {
1710
            super(listeners.iterator(), evt);
1711
        }
1712
    }
1713
 
1714
    static private final ThreadLocal<LinkedList<DispatchingState>> events = new ThreadLocal<LinkedList<DispatchingState>>() {
25 ilm 1715
        @Override
73 ilm 1716
        protected LinkedList<DispatchingState> initialValue() {
1717
            return new LinkedList<DispatchingState>();
25 ilm 1718
        }
1719
    };
1720
 
1721
    // allow to maintain the dispatching of events in order when a listener itself fires an event
73 ilm 1722
    static private void fireTableModified(DispatchingState newTuple) {
1723
        final LinkedList<DispatchingState> linkedList = events.get();
25 ilm 1724
        // add new event
1725
        linkedList.addLast(newTuple);
1726
        // process all pending events
73 ilm 1727
        DispatchingState currentTuple;
25 ilm 1728
        while ((currentTuple = linkedList.peekFirst()) != null) {
1729
            final Iterator<SQLTableModifiedListener> iter = currentTuple.get0();
1730
            final SQLTableEvent currentEvt = currentTuple.get1();
1731
            while (iter.hasNext()) {
1732
                final SQLTableModifiedListener l = iter.next();
1733
                l.tableModified(currentEvt);
17 ilm 1734
            }
25 ilm 1735
            // not removeFirst() since the item might have been already removed
1736
            linkedList.pollFirst();
17 ilm 1737
        }
1738
    }
1739
 
1740
    private void fireTableModified(final SQLTableEvent evt) {
132 ilm 1741
        if (evt.getTable() != this)
1742
            throw new IllegalArgumentException("Wrong table : " + this + " ; " + evt);
73 ilm 1743
        final FireState fireState;
132 ilm 1744
        final TransactionPoint point = evt.getTransactionPoint();
73 ilm 1745
        final Boolean callbackAfterTxListeners;
63 ilm 1746
        synchronized (this.listenersMutex) {
73 ilm 1747
            // no need to copy since this.tableModifiedListeners is immutable
1748
            fireState = new FireState(this.tableModifiedListeners, evt);
1749
            if (point == null) {
1750
                // call back every listener
1751
                callbackAfterTxListeners = null;
132 ilm 1752
            } else if (point.isActive()) {
1753
                addFireStates(point, Collections.singleton(fireState));
1754
                callbackAfterTxListeners = false;
1755
                // to free DB resources, it is allowed to fire events after the transaction ended
1756
            } else if (!point.wasCommitted()) {
1757
                throw new IllegalStateException("Fire after an aborted transaction point");
1758
            } else if (point.getSavePoint() != null) {
1759
                addFireStates(point, Collections.singleton(fireState));
1760
                callbackAfterTxListeners = false;
73 ilm 1761
            } else {
132 ilm 1762
                callbackAfterTxListeners = null;
73 ilm 1763
            }
17 ilm 1764
        }
73 ilm 1765
        fireTableModified(fireState.createDispatchingState(callbackAfterTxListeners, false));
17 ilm 1766
    }
1767
 
132 ilm 1768
    private void addFireStates(TransactionPoint point, final Collection<FireState> fireStates) {
1769
        assert Thread.holdsLock(this.listenersMutex) : "Unsafe to access this.transactions";
1770
        // if multiple save points are released before firing, we must go back to the still active
1771
        // point
1772
        while (!point.isActive())
1773
            point = point.getPrevious();
1774
        if (!this.transactions.containsKey(point))
1775
            point.addListener(this.txListener);
1776
        this.transactions.addAll(point, fireStates);
1777
    }
1778
 
73 ilm 1779
    // a transaction was committed or aborted, we must either notify listeners that wanted the
1780
    // transaction to commit, or re-notify the listeners that didn't want to wait
132 ilm 1781
    protected void fireFromTransaction(final TransactionPoint point) {
1782
        final boolean committed = point.wasCommitted();
1783
        // if it's a released savePoint, add all our states to the previous point (and thus don't
1784
        // fire now)
1785
        final boolean releasedSavePoint = committed && point.getSavePoint() != null;
73 ilm 1786
        final List<FireState> states;
1787
        synchronized (this.listenersMutex) {
1788
            states = this.transactions.remove(point);
132 ilm 1789
            if (releasedSavePoint) {
1790
                this.addFireStates(point, states);
1791
            }
73 ilm 1792
        }
132 ilm 1793
        if (!releasedSavePoint) {
1794
            final ListIterator<FireState> iter = CollectionUtils.getListIterator(states, !committed);
1795
            while (iter.hasNext()) {
1796
                final FireState state = iter.next();
1797
                fireTableModified(state.createDispatchingState(committed, !committed));
1798
            }
73 ilm 1799
        }
1800
    }
1801
 
61 ilm 1802
    public synchronized String toXML() {
17 ilm 1803
        final StringBuilder sb = new StringBuilder(16000);
1804
        sb.append("<table name=\"");
142 ilm 1805
        sb.append(OUTPUTTER.escapeAttributeEntities(this.getName()));
17 ilm 1806
        sb.append("\"");
1807
 
1808
        final String schemaName = this.getSchema().getName();
1809
        if (schemaName != null) {
1810
            sb.append(" schema=\"");
142 ilm 1811
            sb.append(OUTPUTTER.escapeAttributeEntities(schemaName));
17 ilm 1812
            sb.append('"');
1813
        }
1814
 
67 ilm 1815
        SQLSchema.appendVersionAttr(this.version, sb);
17 ilm 1816
 
1817
        if (getType() != null) {
1818
            sb.append(" type=\"");
142 ilm 1819
            sb.append(OUTPUTTER.escapeAttributeEntities(getType()));
17 ilm 1820
            sb.append('"');
1821
        }
1822
 
1823
        sb.append(">\n");
1824
 
1825
        if (this.getComment() != null) {
1826
            sb.append("<comment>");
142 ilm 1827
            sb.append(OUTPUTTER.escapeElementEntities(this.getComment()));
17 ilm 1828
            sb.append("</comment>\n");
1829
        }
65 ilm 1830
        for (SQLField field : this.fields.values()) {
17 ilm 1831
            sb.append(field.toXML());
1832
        }
1833
        sb.append("<primary>\n");
1834
        for (SQLField element : this.primaryKeys) {
1835
            sb.append(element.toXML());
1836
        }
1837
        sb.append("</primary>\n");
1838
        // avoid writing unneeded chars
1839
        if (this.triggers.size() > 0) {
1840
            sb.append("<triggers>\n");
1841
            for (Trigger t : this.triggers.values()) {
1842
                sb.append(t.toXML());
1843
            }
1844
            sb.append("</triggers>\n");
1845
        }
1846
        if (this.constraints != null) {
1847
            sb.append("<constraints>\n");
1848
            for (Constraint t : this.constraints) {
1849
                sb.append(t.toXML());
1850
            }
1851
            sb.append("</constraints>\n");
1852
        }
1853
        sb.append("</table>");
1854
        return sb.toString();
1855
    }
1856
 
25 ilm 1857
    @Override
1858
    public SQLTableModifiedListener createTableListener(final SQLDataListener l) {
1859
        return new SQLTableModifiedListener() {
1860
            @Override
1861
            public void tableModified(SQLTableEvent evt) {
132 ilm 1862
                l.dataChanged(evt);
17 ilm 1863
            }
1864
        };
1865
    }
1866
 
65 ilm 1867
    @Override
17 ilm 1868
    public SQLTable getTable() {
1869
        return this;
1870
    }
1871
 
65 ilm 1872
    @Override
1873
    public String getAlias() {
1874
        return getName();
1875
    }
1876
 
1877
    @Override
1878
    public String getSQL() {
93 ilm 1879
        // always use fullname, otherwise must check the datasource's
1880
        // default schema
65 ilm 1881
        return getSQLName().quote();
1882
    }
1883
 
17 ilm 1884
    public boolean equalsDesc(SQLTable o) {
1885
        return this.equalsDesc(o, true) == null;
1886
    }
1887
 
1888
    /**
1889
     * Compare this table and its descendants. This do not compare undefinedID as it isn't part of
1890
     * the structure per se.
1891
     *
1892
     * @param o the table to compare.
1893
     * @param compareName whether to also compare the name, useful for comparing 2 tables in the
1894
     *        same schema.
1895
     * @return <code>null</code> if attributes and children of this and <code>o</code> are equals,
1896
     *         otherwise a String explaining the differences.
1897
     */
1898
    public String equalsDesc(SQLTable o, boolean compareName) {
1899
        return this.equalsDesc(o, null, compareName);
1900
    }
1901
 
61 ilm 1902
    // ATTN otherSystem can be null, meaning compare exactly (even if the system of this table and
1903
    // the system of the other table do not support the same features and thus tables cannot be
1904
    // equal)
1905
    // if otherSystem isn't null, then this method is more lenient and return true if the two tables
1906
    // are the closest possible. NOTE that otherSystem is not required to be the system of the other
1907
    // table, it might be something else if the other table was loaded into a system different than
1908
    // the one which created the dump.
1909
    public synchronized String equalsDesc(SQLTable o, SQLSystem otherSystem, boolean compareName) {
17 ilm 1910
        if (o == null)
1911
            return "other table is null";
1912
        final boolean name = !compareName || this.getName().equals(o.getName());
1913
        if (!name)
1914
            return "name unequal : " + this.getName() + " " + o.getName();
1915
        // TODO triggers, but wait for the dumping of functions
1916
        // which mean wait for psql 8.4 pg_get_functiondef()
1917
        // if (this.getServer().getSQLSystem() == o.getServer().getSQLSystem()) {
1918
        // if (!this.getTriggers().equals(o.getTriggers()))
1919
        // return "triggers unequal : " + this.getTriggers() + " " + o.getTriggers();
1920
        // } else {
1921
        // if (!this.getTriggers().keySet().equals(o.getTriggers().keySet()))
1922
        // return "triggers names unequal : " + this.getTriggers() + " " + o.getTriggers();
1923
        // }
1924
        final boolean checkComment = otherSystem == null || this.getServer().getSQLSystem().isTablesCommentSupported() && otherSystem.isTablesCommentSupported();
1925
        if (checkComment && !CompareUtils.equals(this.getComment(), o.getComment()))
132 ilm 1926
            return "comment unequal : " + SQLBase.quoteStringStd(this.getComment()) + " != " + SQLBase.quoteStringStd(o.getComment());
17 ilm 1927
        return this.equalsChildren(o, otherSystem);
1928
    }
1929
 
61 ilm 1930
    private synchronized String equalsChildren(SQLTable o, SQLSystem otherSystem) {
17 ilm 1931
        if (!this.getChildrenNames().equals(o.getChildrenNames()))
1932
            return "fields differences: " + this.getChildrenNames() + "\n" + o.getChildrenNames();
1933
 
1934
        final String noLink = equalsChildrenNoLink(o, otherSystem);
1935
        if (noLink != null)
1936
            return noLink;
1937
 
1938
        // foreign keys
132 ilm 1939
        final Set<Link> thisLinks = this.getForeignLinks();
1940
        final Set<Link> oLinks = o.getForeignLinks();
17 ilm 1941
        if (thisLinks.size() != oLinks.size())
1942
            return "different number of foreign keys " + thisLinks + " != " + oLinks;
132 ilm 1943
        final SQLSystem thisSystem = this.getServer().getSQLSystem();
17 ilm 1944
        for (final Link l : thisLinks) {
1945
            final Link ol = o.getDBSystemRoot().getGraph().getForeignLink(o, l.getCols());
1946
            if (ol == null)
1947
                return "no foreign key for " + l.getLabel();
1948
            final SQLName thisPath = l.getTarget().getContextualSQLName(this);
1949
            final SQLName oPath = ol.getTarget().getContextualSQLName(o);
1950
            if (thisPath.getItemCount() != oPath.getItemCount())
1951
                return "unequal path size : " + thisPath + " != " + oPath;
1952
            if (!thisPath.getName().equals(oPath.getName()))
1953
                return "unequal referenced table name : " + thisPath.getName() + " != " + oPath.getName();
61 ilm 1954
            if (!getRule(l.getUpdateRule(), thisSystem, otherSystem).equals(getRule(ol.getUpdateRule(), thisSystem, otherSystem)))
57 ilm 1955
                return "unequal update rule for " + l + ": " + l.getUpdateRule() + " != " + ol.getUpdateRule();
61 ilm 1956
            if (!getRule(l.getDeleteRule(), thisSystem, otherSystem).equals(getRule(ol.getDeleteRule(), thisSystem, otherSystem)))
57 ilm 1957
                return "unequal delete rule for " + l + ": " + l.getDeleteRule() + " != " + ol.getDeleteRule();
17 ilm 1958
        }
1959
 
83 ilm 1960
        final Set<Constraint> thisConstraints;
1961
        final Set<Constraint> otherConstraints;
17 ilm 1962
        try {
83 ilm 1963
            final Tuple2<Set<Constraint>, Set<Index>> thisConstraintsAndIndexes = this.getConstraintsAndIndexes();
1964
            final Tuple2<Set<Constraint>, Set<Index>> otherConstraintsAndIndexes = o.getConstraintsAndIndexes();
17 ilm 1965
            // order irrelevant
83 ilm 1966
            final Set<Index> thisIndexesSet = thisConstraintsAndIndexes.get1();
1967
            final Set<Index> oIndexesSet = otherConstraintsAndIndexes.get1();
17 ilm 1968
            if (!thisIndexesSet.equals(oIndexesSet))
1969
                return "indexes differences: " + thisIndexesSet + "\n" + oIndexesSet;
83 ilm 1970
            thisConstraints = thisConstraintsAndIndexes.get0();
1971
            otherConstraints = otherConstraintsAndIndexes.get0();
17 ilm 1972
        } catch (SQLException e) {
1973
            // MAYBE fetch indexes with the rest to avoid exn now
1974
            return "couldn't get indexes: " + ExceptionUtils.getStackTrace(e);
1975
        }
132 ilm 1976
        if (!CustomEquals.equals(thisConstraints, otherConstraints, otherSystem == null || otherSystem.equals(thisSystem) ? null : Constraint.getInterSystemHashStrategy()))
83 ilm 1977
            return "constraints unequal : '" + thisConstraints + "' != '" + otherConstraints + "'";
17 ilm 1978
 
1979
        return null;
1980
    }
1981
 
83 ilm 1982
    private final Tuple2<Set<Constraint>, Set<Index>> getConstraintsAndIndexes() throws SQLException {
1983
        final Set<Constraint> thisConstraints;
1984
        final Set<Index> thisIndexes;
1985
        if (this.getServer().getSQLSystem() != SQLSystem.MSSQL) {
1986
            thisConstraints = this.getConstraints();
1987
            thisIndexes = new HashSet<Index>(this.getIndexes(true));
1988
        } else {
1989
            thisConstraints = new HashSet<Constraint>(this.getConstraints());
1990
            thisIndexes = new HashSet<Index>();
1991
            for (final Index i : this.getIndexes()) {
1992
                final Value<String> where = i.getMSUniqueWhere();
1993
                if (!where.hasValue()) {
1994
                    // regular index
1995
                    thisIndexes.add(i);
1996
                } else if (where.getValue() == null) {
1997
                    final Map<String, Object> map = new HashMap<String, Object>();
1998
                    map.put("CONSTRAINT_NAME", i.getName());
1999
                    map.put("CONSTRAINT_TYPE", "UNIQUE");
2000
                    map.put("COLUMN_NAMES", i.getCols());
2001
                    map.put("DEFINITION", null);
2002
                    thisConstraints.add(new Constraint(this, map));
2003
                } else {
2004
                    // remove extra IS NOT NULL, but does *not* translate [ARCHIVE]=(0) into
2005
                    // "ARCHIVE" = 0
2006
                    thisIndexes.add(this.createUniqueIndex(i.getName(), i.getCols(), where.getValue()));
2007
                }
2008
            }
2009
        }
2010
        return Tuple2.create(thisConstraints, thisIndexes);
2011
    }
2012
 
61 ilm 2013
    private final Rule getRule(Rule r, SQLSystem thisSystem, SQLSystem otherSystem) {
2014
        // compare exactly
2015
        if (otherSystem == null)
2016
            return r;
2017
        // see http://code.google.com/p/h2database/issues/detail?id=352
2018
        if (r == Rule.NO_ACTION && (thisSystem == SQLSystem.H2 || otherSystem == SQLSystem.H2))
2019
            return Rule.RESTRICT;
2020
        else
2021
            return r;
2022
    }
2023
 
17 ilm 2024
    /**
2025
     * Compare the fields of this table, ignoring foreign constraints.
2026
     *
2027
     * @param o the table to compare.
2028
     * @param otherSystem the system <code>o</code> originates from, can be <code>null</code>.
2029
     * @return <code>null</code> if each fields of this exists in <code>o</code> and is equal to it.
2030
     */
61 ilm 2031
    public synchronized final String equalsChildrenNoLink(SQLTable o, SQLSystem otherSystem) {
17 ilm 2032
        for (final SQLField f : this.getFields()) {
2033
            final SQLField oField = o.getField(f.getName());
2034
            final boolean isPrimary = this.getPrimaryKeys().contains(f);
2035
            if (isPrimary != o.getPrimaryKeys().contains(oField))
2036
                return f + " is a primary not in " + o.getPrimaryKeys();
2037
            final String equalsDesc = f.equalsDesc(oField, otherSystem, !isPrimary);
2038
            if (equalsDesc != null)
2039
                return equalsDesc;
2040
        }
2041
        return null;
2042
    }
2043
 
2044
    public final SQLCreateMoveableTable getCreateTable() {
142 ilm 2045
        return this.getCreateTable(SQLSyntax.get(this));
17 ilm 2046
    }
2047
 
142 ilm 2048
    public synchronized final SQLCreateMoveableTable getCreateTable(final SQLSyntax syntax) {
2049
        final SQLSystem system = syntax.getSystem();
67 ilm 2050
        final SQLCreateMoveableTable res = new SQLCreateMoveableTable(syntax, this.getDBRoot().getName(), this.getName());
17 ilm 2051
        for (final SQLField f : this.getOrderedFields()) {
2052
            res.addColumn(f);
2053
        }
2054
        // primary keys
2055
        res.setPrimaryKey(getPKsNames());
2056
        // foreign keys
132 ilm 2057
        for (final Link l : this.getForeignLinks())
17 ilm 2058
            // don't generate explicit CREATE INDEX for fk, we generate all indexes below
2059
            // (this also avoid creating a fk index that wasn't there)
2060
            res.addForeignConstraint(l, false);
2061
        // constraints
2062
        if (this.constraints != null)
41 ilm 2063
            for (final Constraint added : this.getConstraints()) {
17 ilm 2064
                if (added.getType() == ConstraintType.UNIQUE) {
2065
                    res.addUniqueConstraint(added.getName(), added.getCols());
2066
                } else
2067
                    throw new UnsupportedOperationException("unsupported constraint: " + added);
2068
            }
2069
        // indexes
2070
        try {
83 ilm 2071
            // MS unique constraint are not standard so we're forced to create indexes "where col is
2072
            // not null" in addUniqueConstraint(). Thus when converting to another system we must
2073
            // parse indexes to recreate actual constraints.
2074
            final boolean convertMSIndex = this.getServer().getSQLSystem() == SQLSystem.MSSQL && system != SQLSystem.MSSQL;
2075
            for (final Index i : this.getIndexes(true)) {
2076
                Value<String> msWhere = null;
2077
                if (convertMSIndex && (msWhere = i.getMSUniqueWhere()).hasValue()) {
2078
                    if (msWhere.getValue() != null)
2079
                        Log.get().warning("MS filter might not be valid in " + system + " : " + msWhere.getValue());
2080
                    res.addUniqueConstraint(i.getName(), i.getCols(), msWhere.getValue());
142 ilm 2081
                } else {
83 ilm 2082
                    // partial unique index sometimes cannot be handled natively by the DB system
2083
                    if (i.isUnique() && i.getFilter() != null && !system.isIndexFilterConditionSupported())
144 ilm 2084
                        res.addUniqueConstraint(i.getName(), i.createUniqueHelper());
83 ilm 2085
                    else
142 ilm 2086
                        res.addIndex(i);
17 ilm 2087
                }
83 ilm 2088
            }
17 ilm 2089
        } catch (SQLException e) {
2090
            // MAYBE fetch indexes with the rest to avoid exn now
2091
            throw new IllegalStateException("could not get indexes", e);
2092
        }
83 ilm 2093
        // TODO triggers, but they are system dependent and we would have to parse the SQL
2094
        // definitions to replace the different root/table name in DeferredClause.asString()
17 ilm 2095
        if (this.getComment() != null)
2096
            res.addOutsideClause(syntax.getSetTableComment(getComment()));
2097
        return res;
2098
    }
2099
 
2100
    public final List<String> getPKsNames() {
2101
        return this.getPKsNames(new ArrayList<String>());
2102
    }
2103
 
61 ilm 2104
    public synchronized final <C extends Collection<String>> C getPKsNames(C pks) {
17 ilm 2105
        for (final SQLField f : this.getPrimaryKeys()) {
2106
            pks.add(f.getName());
2107
        }
2108
        return pks;
2109
    }
2110
 
2111
    public final String[] getPKsNamesArray() {
2112
        return getPKsNames().toArray(new String[0]);
2113
    }
2114
 
2115
    /**
2116
     * Return the indexes mapped by column names. Ie a key will have as value every index that
65 ilm 2117
     * mentions it, and a multi-column index will be in several entries. Note: this is not robust
2118
     * since {@link Index#getCols()} isn't.
17 ilm 2119
     *
2120
     * @return the indexes mapped by column names.
2121
     * @throws SQLException if an error occurs.
2122
     */
83 ilm 2123
    public final SetMap<String, Index> getIndexesByField() throws SQLException {
17 ilm 2124
        final List<Index> indexes = this.getIndexes();
83 ilm 2125
        final SetMap<String, Index> res = new SetMap<String, Index>(indexes.size()) {
2126
            @Override
2127
            public Set<Index> createCollection(Collection<? extends Index> v) {
2128
                final HashSet<Index> res = new HashSet<Index>(4);
2129
                res.addAll(v);
2130
                return res;
2131
            }
2132
        };
17 ilm 2133
        for (final Index i : indexes)
2134
            for (final String col : i.getCols())
83 ilm 2135
                res.add(col, i);
17 ilm 2136
        return res;
2137
    }
2138
 
2139
    /**
65 ilm 2140
     * Return the indexes on the passed columns names. Note: this is not robust since
2141
     * {@link Index#getCols()} isn't.
2142
     *
2143
     * @param cols fields names.
2144
     * @return the matching indexes.
2145
     * @throws SQLException if an error occurs.
2146
     */
2147
    public final List<Index> getIndexes(final List<String> cols) throws SQLException {
2148
        final List<Index> res = new ArrayList<Index>();
2149
        for (final Index i : this.getIndexes())
2150
            if (i.getCols().equals(cols))
2151
                res.add(i);
2152
        return res;
2153
    }
2154
 
2155
    /**
17 ilm 2156
     * Return the indexes of this table. Except the primary key as every system generates it
2157
     * automatically.
2158
     *
2159
     * @return the list of indexes.
142 ilm 2160
     * @throws SQLException if an error occurs while accessing the DB.
17 ilm 2161
     */
142 ilm 2162
    public final List<Index> getIndexes() throws SQLException {
83 ilm 2163
        return this.getIndexes(false);
2164
    }
2165
 
142 ilm 2166
    public synchronized final List<Index> getIndexes(final boolean normalized) throws SQLException {
17 ilm 2167
        // in pg, a unique constraint creates a unique index that is not removeable
2168
        // (except of course if we drop the constraint)
2169
        // in mysql unique constraints and indexes are one and the same thing
2170
        // so we must return them only in one (either getConstraints() or getIndexes())
2171
        // anyway in all systems, a unique constraint or index achieve the same function
2172
        // and so only generates the constraint and not the index
2173
        final Set<List<String>> uniqConstraints;
2174
        if (this.constraints != null) {
2175
            uniqConstraints = new HashSet<List<String>>();
2176
            for (final Constraint c : this.constraints) {
2177
                if (c.getType() == ConstraintType.UNIQUE)
2178
                    uniqConstraints.add(c.getCols());
2179
            }
2180
        } else
2181
            uniqConstraints = Collections.emptySet();
2182
 
2183
        final List<Index> indexes = new ArrayList<Index>();
2184
        Index currentIndex = null;
142 ilm 2185
        for (final Map<String, Object> norm : this.getDBSystemRoot().getSyntax().getIndexInfo(this)) {
17 ilm 2186
            final Index index = new Index(norm);
2187
            final short seq = ((Number) norm.get("ORDINAL_POSITION")).shortValue();
2188
            if (seq == 1) {
2189
                if (canAdd(currentIndex, uniqConstraints))
2190
                    indexes.add(currentIndex);
2191
                currentIndex = index;
2192
            } else {
2193
                // continuing a multi-field index
2194
                currentIndex.add(index);
2195
            }
2196
        }
2197
        if (canAdd(currentIndex, uniqConstraints))
2198
            indexes.add(currentIndex);
2199
 
83 ilm 2200
        if (normalized) {
2201
            indexes.addAll(this.getPartialUniqueIndexes());
2202
        }
2203
 
17 ilm 2204
        // MAYBE another request to find out index.getMethod() (eg pg.getIndexesReq())
2205
        return indexes;
2206
    }
2207
 
2208
    private boolean canAdd(final Index currentIndex, final Set<List<String>> uniqConstraints) {
2209
        if (currentIndex == null || currentIndex.isPKIndex())
2210
            return false;
2211
 
2212
        return !currentIndex.isUnique() || !uniqConstraints.contains(currentIndex.getCols());
2213
    }
2214
 
83 ilm 2215
    // MAYBE inline
2216
    protected synchronized final List<Index> getPartialUniqueIndexes() throws SQLException {
2217
        final SQLSystem thisSystem = this.getServer().getSQLSystem();
2218
        final List<Index> indexes = new ArrayList<Index>();
2219
        // parse triggers, TODO remove them from triggers to output in getCreateTable()
2220
        if (thisSystem == SQLSystem.H2) {
2221
            for (final Trigger t : this.triggers.values()) {
144 ilm 2222
                Matcher matcher = ChangeTable.H2_UNIQUE_TRIGGER_PATTERN.matcher(t.getSQL());
83 ilm 2223
                if (matcher.find()) {
2224
                    final String indexName = ChangeTable.getIndexName(t.getName(), thisSystem);
2225
                    final String[] javaCols = ChangeTable.H2_LIST_PATTERN.split(matcher.group(1).trim());
2226
                    final List<String> cols = new ArrayList<String>(javaCols.length);
2227
                    for (final String javaCol : javaCols) {
2228
                        cols.add(StringUtils.unDoubleQuote(javaCol));
2229
                    }
2230
                    final String where = StringUtils.unDoubleQuote(matcher.group(2).trim());
2231
                    indexes.add(createUniqueIndex(indexName, cols, where));
144 ilm 2232
                } else {
2233
                    matcher = ChangeTable.H2_UNIQUE_TRIGGER_CLASS_PATTERN.matcher(t.getSQL());
2234
                    if (matcher.find()) {
2235
                        final String className = matcher.group(1);
2236
                        final Class<?> triggerClass;
2237
                        try {
2238
                            triggerClass = Class.forName(className);
2239
                        } catch (ClassNotFoundException e) {
151 ilm 2240
                            // throw new SQLException("Class not found for " + t, e);
2241
                            e.printStackTrace();
2242
                            continue;
144 ilm 2243
                        }
2244
                        PartialUniqueTrigger n;
2245
                        try {
2246
                            n = (PartialUniqueTrigger) triggerClass.newInstance();
2247
                        } catch (Exception e) {
151 ilm 2248
                            // throw new SQLException("Couldn't instantiate class for " + t, e);
2249
                            e.printStackTrace();
2250
                            continue;
144 ilm 2251
                        }
2252
                        final String indexName = ChangeTable.getIndexName(t.getName(), thisSystem);
2253
                        final Index idx = createUniqueIndex(indexName, n.getColumns(), n.getWhere());
2254
                        idx.setH2Class(n.getClass());
2255
                        indexes.add(idx);
2256
                    }
83 ilm 2257
                }
2258
            }
2259
        } else if (thisSystem == SQLSystem.MYSQL) {
2260
            for (final Trigger t : this.triggers.values()) {
2261
                if (t.getAction().contains(ChangeTable.MYSQL_TRIGGER_EXCEPTION)) {
2262
                    final String indexName = ChangeTable.getIndexName(t.getName(), thisSystem);
2263
                    // MySQL needs a pair of triggers
2264
                    final Trigger t2 = indexName == null ? null : this.triggers.get(indexName + ChangeTable.MYSQL_TRIGGER_SUFFIX_2);
2265
                    // and their body must match
2266
                    if (t2 != null && t2.getAction().equals(t.getAction())) {
2267
                        final Matcher matcher = ChangeTable.MYSQL_UNIQUE_TRIGGER_PATTERN.matcher(t.getAction());
2268
                        if (!matcher.find())
2269
                            throw new IllegalStateException("Couldn't parse " + t.getAction());
2270
                        // parse table name
2271
                        final SQLName parsedName = SQLName.parse(matcher.group(1).trim());
2272
                        if (!this.getName().equals(parsedName.getName()))
2273
                            throw new IllegalStateException("Name mismatch : " + this.getSQLName() + " != " + parsedName);
17 ilm 2274
 
83 ilm 2275
                        final String[] wheres = ChangeTable.MYSQL_WHERE_PATTERN.split(matcher.group(2).trim());
2276
                        final String userWhere = wheres[0];
2277
 
2278
                        final List<String> cols = new ArrayList<String>(wheres.length - 1);
2279
                        for (int i = 1; i < wheres.length; i++) {
2280
                            final Matcher eqMatcher = ChangeTable.MYSQL_WHERE_EQ_PATTERN.matcher(wheres[i].trim());
2281
                            if (!eqMatcher.matches())
2282
                                throw new IllegalStateException("Invalid where clause " + wheres[i]);
2283
                            cols.add(SQLName.parse(eqMatcher.group(2).trim()).getName());
2284
                        }
2285
                        if (cols.isEmpty())
2286
                            throw new IllegalStateException("No columns in " + Arrays.asList(wheres));
2287
                        indexes.add(createUniqueIndex(indexName, cols, userWhere));
2288
                    }
2289
                }
2290
            }
2291
        }
2292
        return indexes;
2293
    }
2294
 
2295
    public static class SQLIndex {
2296
 
2297
        private static final Pattern NORMALIZE_SPACES = Pattern.compile("\\s+");
2298
 
17 ilm 2299
        private final String name;
83 ilm 2300
        // SQL, e.g. : lower("name"), "age"
17 ilm 2301
        private final List<String> attrs;
2302
        private final boolean unique;
2303
        private String method;
83 ilm 2304
        private final String filter;
17 ilm 2305
 
83 ilm 2306
        public SQLIndex(final String name, final List<String> attributes, final boolean unique, final String filter) {
2307
            this(name, attributes, false, unique, filter);
17 ilm 2308
        }
2309
 
83 ilm 2310
        public SQLIndex(final String name, final List<String> attributes, final boolean quoteAll, final boolean unique, final String filter) {
17 ilm 2311
            super();
2312
            this.name = name;
83 ilm 2313
            this.attrs = new ArrayList<String>(attributes.size());
2314
            for (final String attr : attributes)
2315
                this.addAttr(quoteAll ? SQLBase.quoteIdentifier(attr) : attr);
2316
            this.unique = unique;
17 ilm 2317
            this.method = null;
83 ilm 2318
            // helps when comparing
2319
            this.filter = filter == null ? null : NORMALIZE_SPACES.matcher(filter.trim()).replaceAll(" ");
17 ilm 2320
        }
2321
 
2322
        public final String getName() {
2323
            return this.name;
2324
        }
2325
 
2326
        public final boolean isUnique() {
2327
            return this.unique;
2328
        }
2329
 
2330
        /**
2331
         * All attributes forming this index.
2332
         *
2333
         * @return the components of this index, eg ["lower(name)", "age"].
2334
         */
2335
        public final List<String> getAttrs() {
83 ilm 2336
            return Collections.unmodifiableList(this.attrs);
17 ilm 2337
        }
2338
 
83 ilm 2339
        protected final void addAttr(final String attr) {
2340
            this.attrs.add(attr);
17 ilm 2341
        }
2342
 
2343
        public final void setMethod(String method) {
2344
            this.method = method;
2345
        }
2346
 
2347
        public final String getMethod() {
2348
            return this.method;
2349
        }
2350
 
2351
        /**
2352
         * Filter for partial index.
2353
         *
2354
         * @return the where clause or <code>null</code>.
2355
         */
2356
        public final String getFilter() {
2357
            return this.filter;
2358
        }
2359
 
2360
        @Override
2361
        public String toString() {
83 ilm 2362
            return getClass().getSimpleName() + " " + this.getName() + " unique: " + this.isUnique() + " cols: " + this.getAttrs() + " filter: " + this.getFilter();
17 ilm 2363
        }
2364
 
2365
        // ATTN don't use name since it is often auto-generated (eg by a UNIQUE field)
2366
        @Override
2367
        public boolean equals(Object obj) {
83 ilm 2368
            if (obj instanceof SQLIndex) {
2369
                final SQLIndex o = (SQLIndex) obj;
2370
                return this.isUnique() == o.isUnique() && this.getAttrs().equals(o.getAttrs()) && CompareUtils.equals(this.getFilter(), o.getFilter())
2371
                        && CompareUtils.equals(this.getMethod(), o.getMethod());
2372
            } else {
17 ilm 2373
                return false;
83 ilm 2374
            }
17 ilm 2375
        }
2376
 
2377
        // ATTN use cols, so use only after cols are done
2378
        @Override
2379
        public int hashCode() {
2380
            return this.getAttrs().hashCode() + ((Boolean) this.isUnique()).hashCode();
2381
        }
2382
    }
83 ilm 2383
 
2384
    private final Index createUniqueIndex(final String name, final List<String> cols, final String where) {
2385
        final Index res = new Index(name, cols.get(0), false, where);
2386
        for (int i = 1; i < cols.size(); i++) {
2387
            res.addFromMD(cols.get(i));
2388
        }
2389
        return res;
2390
    }
2391
 
2392
    private final String removeParens(String filter) {
2393
        if (filter != null) {
2394
            filter = filter.trim();
2395
            final SQLSystem sys = this.getServer().getSQLSystem();
2396
            // postgreSQL always wrap filter with parens, ATTN we shouldn't remove from
2397
            // "(A) and (B)" but still support "(A = (0))"
2398
            if ((sys == SQLSystem.POSTGRESQL || sys == SQLSystem.MSSQL) && filter.startsWith("(") && filter.endsWith(")")) {
2399
                filter = filter.substring(1, filter.length() - 1);
2400
            }
2401
        }
2402
        return filter;
2403
    }
2404
 
144 ilm 2405
    @GuardedBy("uniqueH2Triggers")
2406
    private final Map<Tuple2<List<String>, String>, Class<? extends PartialUniqueTrigger>> uniqueH2Triggers = new HashMap<>();
2407
 
2408
    public final void registerH2UniqueTrigger(Class<? extends PartialUniqueTrigger> uniqTriggerClass) throws InstantiationException, IllegalAccessException {
2409
        final PartialUniqueTrigger newInstance = uniqTriggerClass.newInstance();
2410
        final Tuple2<List<String>, String> key = Tuple2.create(newInstance.getColumns(), newInstance.getWhere().toUpperCase());
2411
        synchronized (this.uniqueH2Triggers) {
2412
            this.uniqueH2Triggers.put(key, uniqTriggerClass);
2413
        }
2414
    }
2415
 
2416
    public static final String workAroundForH2WhereTrigger(final String where) {
2417
        return where.toUpperCase().replaceAll("[()]", "");
2418
    }
2419
 
2420
    public final Class<? extends PartialUniqueTrigger> getH2UniqueTriggerClass(final List<String> cols, final String where) {
2421
        final Tuple2<List<String>, String> key = Tuple2.create(cols, workAroundForH2WhereTrigger(where));
2422
        synchronized (this.uniqueH2Triggers) {
2423
            return this.uniqueH2Triggers.get(key);
2424
        }
2425
    }
2426
 
83 ilm 2427
    public final class Index extends SQLIndex {
2428
 
2429
        private final List<String> cols;
144 ilm 2430
        private Class<? extends PartialUniqueTrigger> h2Class;
83 ilm 2431
 
2432
        Index(final Map<String, Object> row) {
2433
            this((String) row.get("INDEX_NAME"), (String) row.get("COLUMN_NAME"), (Boolean) row.get("NON_UNIQUE"), (String) row.get("FILTER_CONDITION"));
2434
        }
2435
 
2436
        Index(final String name, String col, Boolean nonUnique, String filter) {
2437
            super(name, Collections.<String> emptyList(), !nonUnique, removeParens(filter));
2438
            this.cols = new ArrayList<String>();
2439
            this.addFromMD(col);
2440
        }
2441
 
2442
        public final SQLTable getTable() {
2443
            return SQLTable.this;
2444
        }
2445
 
2446
        /**
2447
         * The table columns in this index. Note that due to DB system limitation this list is
2448
         * incomplete (e.g. missing expressions).
2449
         *
2450
         * @return the unquoted columns, e.g. ["age"].
2451
         */
2452
        public final List<String> getCols() {
2453
            return this.cols;
2454
        }
2455
 
2456
        public final List<SQLField> getFields() {
2457
            final List<SQLField> res = new ArrayList<SQLField>(this.getCols().size());
2458
            for (final String f : this.getCols())
2459
                res.add(getTable().getField(f));
2460
            return res;
2461
        }
2462
 
2463
        /**
2464
         * Adds a column to this multi-field index.
2465
         *
2466
         * @param name the name of the index.
2467
         * @param col the column to add.
2468
         * @param unique whether the index is unique.
2469
         * @throws IllegalStateException if <code>name</code> and <code>unique</code> are not the
2470
         *         same as these.
2471
         */
2472
        private final void add(final Index o) {
2473
            assert o.getAttrs().size() == 1;
2474
            if (!o.getName().equals(this.getName()) || this.isUnique() != o.isUnique())
2475
                throw new IllegalStateException("incoherence");
2476
            this.cols.addAll(o.getCols());
2477
            this.addAttr(o.getAttrs().get(0));
2478
        }
2479
 
2480
        // col is either an expression or a column name
2481
        protected void addFromMD(String col) {
2482
            if (getTable().contains(col)) {
2483
                // e.g. age
2484
                this.cols.add(col);
2485
                this.addAttr(SQLBase.quoteIdentifier(col));
2486
            } else {
2487
                // e.g. lower("name")
2488
                this.addAttr(col);
2489
            }
2490
        }
2491
 
2492
        final boolean isPKIndex() {
2493
            return this.isUnique() && this.getCols().equals(getTable().getPKsNames()) && this.getCols().size() == this.getAttrs().size();
2494
        }
2495
 
2496
        private final Pattern getColPattern(final String col) {
2497
            // e.g. ([NOM] IS NOT NULL AND [PRENOM] IS NOT NULL AND [ARCHIVE]=(0))
2498
            return Pattern.compile("(?i:\\s+AND\\s+)?" + Pattern.quote(new SQLName(col).quoteMS()) + "\\s+(?i)IS\\s+NOT\\s+NULL(\\s+AND\\s+)?");
2499
        }
2500
 
2501
        // in MS SQL we're forced to add IS NOT NULL to get the standard behaviour
2502
        // return none if it's not a unique index, otherwise the value of the where for the partial
2503
        // index (can be null)
2504
        final Value<String> getMSUniqueWhere() {
2505
            assert getServer().getSQLSystem() == SQLSystem.MSSQL;
2506
            if (this.isUnique() && this.getFilter() != null) {
2507
                String filter = this.getFilter().trim();
2508
                // for each column, remove its NOT NULL clause
2509
                for (final String col : getCols()) {
2510
                    final Matcher matcher = this.getColPattern(col).matcher(filter);
2511
                    if (matcher.find()) {
2512
                        filter = matcher.replaceFirst("").trim();
2513
                    } else {
2514
                        return Value.getNone();
2515
                    }
2516
                }
2517
                // what is the left is the actual filter
2518
                filter = filter.trim();
2519
                return Value.getSome(filter.isEmpty() ? null : filter);
2520
            }
2521
            return Value.getNone();
2522
        }
144 ilm 2523
 
2524
        final void setH2Class(final Class<? extends PartialUniqueTrigger> triggerClass) {
2525
            this.h2Class = triggerClass;
2526
        }
2527
 
2528
        final UniqueConstraintCreatorHelper createUniqueHelper() {
2529
            final Class<? extends PartialUniqueTrigger> h2Class = this.h2Class == null ? getH2UniqueTriggerClass(getCols(), getFilter()) : this.h2Class;
2530
            final String comment = h2Class == null ? null : "Unique constraint on " + getCols() + (getFilter() == null ? "" : " where " + getFilter());
2531
            return new UniqueConstraintCreatorHelper(getCols(), getFilter(), comment) {
2532
                @Override
2533
                public Object getObject(SQLSyntax s) {
2534
                    if (s.getSystem() == SQLSystem.H2)
2535
                        return h2Class;
2536
                    else
2537
                        return super.getObject(s);
2538
                }
2539
            };
2540
        }
83 ilm 2541
    }
17 ilm 2542
}