OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 177 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
Line 16... Line 16...
16
import org.openconcerto.sql.FieldExpander;
16
import org.openconcerto.sql.FieldExpander;
17
import org.openconcerto.sql.model.FieldRef;
17
import org.openconcerto.sql.model.FieldRef;
18
import org.openconcerto.sql.model.IFieldPath;
18
import org.openconcerto.sql.model.IFieldPath;
19
import org.openconcerto.sql.model.OrderComparator;
19
import org.openconcerto.sql.model.OrderComparator;
20
import org.openconcerto.sql.model.SQLField;
20
import org.openconcerto.sql.model.SQLField;
-
 
21
import org.openconcerto.sql.model.SQLRow;
21
import org.openconcerto.sql.model.SQLRowAccessor;
22
import org.openconcerto.sql.model.SQLRowAccessor;
22
import org.openconcerto.sql.model.SQLRowValues;
23
import org.openconcerto.sql.model.SQLRowValues;
23
import org.openconcerto.sql.model.SQLRowValues.CreateMode;
24
import org.openconcerto.sql.model.SQLRowValues.CreateMode;
24
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
25
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
25
import org.openconcerto.sql.model.SQLSearchMode;
26
import org.openconcerto.sql.model.SQLSearchMode;
Line 107... Line 108...
107
 
108
 
108
    private final SQLTable primaryTable;
109
    private final SQLTable primaryTable;
109
    @GuardedBy("this")
110
    @GuardedBy("this")
110
    private List<Path> order;
111
    private List<Path> order;
111
    @GuardedBy("this")
112
    @GuardedBy("this")
-
 
113
    private Map<Object, Where> wheres;
-
 
114
    @GuardedBy("this")
112
    private Where where;
115
    private Where where;
113
    @GuardedBy("this")
116
    @GuardedBy("this")
114
    private Map<IFieldPath, SearchField> searchFields;
117
    private Map<IFieldPath, SearchField> searchFields;
115
    @GuardedBy("this")
118
    @GuardedBy("this")
116
    private int searchLimit;
119
    private int searchLimit;
Line 137... Line 140...
137
        super();
140
        super();
138
        if (graph == null)
141
        if (graph == null)
139
            throw new NullPointerException();
142
            throw new NullPointerException();
140
        this.primaryTable = graph.getTable();
143
        this.primaryTable = graph.getTable();
141
        this.setOrder(null);
144
        this.setOrder(null);
142
        this.where = w;
145
        this.setWhere(w);
143
        this.searchFields = Collections.emptyMap();
146
        this.searchFields = Collections.emptyMap();
144
        this.searchLimit = 35;
147
        this.searchLimit = 35;
145
        this.selTransf = null;
148
        this.selTransf = null;
146
        this.lockSelect = getDefaultLockSelect();
149
        this.lockSelect = getDefaultLockSelect();
147
        this.graph = graph.toImmutable();
150
        this.graph = graph.toImmutable();
Line 151... Line 154...
151
    public BaseFillSQLRequest(final BaseFillSQLRequest req) {
154
    public BaseFillSQLRequest(final BaseFillSQLRequest req) {
152
        super();
155
        super();
153
        this.primaryTable = req.getPrimaryTable();
156
        this.primaryTable = req.getPrimaryTable();
154
        synchronized (req) {
157
        synchronized (req) {
155
            this.order = req.order;
158
            this.order = req.order;
-
 
159
            this.wheres = req.wheres;
156
            this.where = req.where;
160
            this.where = req.where;
157
            this.searchFields = req.searchFields;
161
            this.searchFields = req.searchFields;
158
            this.searchLimit = req.searchLimit;
162
            this.searchLimit = req.searchLimit;
159
            this.selTransf = req.selTransf;
163
            this.selTransf = req.selTransf;
160
            this.lockSelect = req.lockSelect;
164
            this.lockSelect = req.lockSelect;
Line 379... Line 383...
379
                return res;
383
                return res;
380
        }
384
        }
381
        return 0;
385
        return 0;
382
    }
386
    }
383
 
387
 
-
 
388
    static private final VirtualFields FIELDS_FOR_ORDER = VirtualFields.PRIMARY_KEY.union(VirtualFields.ORDER);
-
 
389
 
-
 
390
    // allow to save memory by only keeping trimmed SQLRow
-
 
391
    public final OrderValue createOrderValue(final SQLRowValues r) {
-
 
392
        if (!r.isFrozen())
-
 
393
            throw new IllegalArgumentException("Row not frozen : " + r);
-
 
394
        final List<Path> order = getOrder();
-
 
395
        final List<SQLRow> rows = new ArrayList<>(order.size());
-
 
396
        for (final Path p : order) {
-
 
397
            rows.add(r.followPath(p).trimmedRow(FIELDS_FOR_ORDER));
-
 
398
        }
-
 
399
        return new OrderValue(Collections.unmodifiableList(rows));
-
 
400
    }
-
 
401
 
-
 
402
    static public final class OrderValue implements Comparable<OrderValue> {
-
 
403
        private final List<SQLRow> rows;
-
 
404
 
-
 
405
        OrderValue(List<SQLRow> rows) {
-
 
406
            super();
-
 
407
            this.rows = rows;
-
 
408
        }
-
 
409
 
-
 
410
        @Override
-
 
411
        public int compareTo(OrderValue o2) {
-
 
412
            if (this == o2)
-
 
413
                return 0;
-
 
414
            final int size = this.rows.size();
-
 
415
            if (size != o2.rows.size())
-
 
416
                throw new IllegalArgumentException("Not same state");
-
 
417
            // same behaviour as SQLSelect
-
 
418
            final Comparator<SQLRowAccessor> comp = OrderComparator.getFallbackToPKInstance();
-
 
419
            for (int i = 0; i < size; i++) {
-
 
420
                final SQLRow r1 = this.rows.get(i);
-
 
421
                final SQLRow r2 = o2.rows.get(i);
-
 
422
                final int res = comp.compare(r1, r2);
-
 
423
                if (res != 0)
-
 
424
                    return res;
-
 
425
            }
-
 
426
            return 0;
-
 
427
        }
-
 
428
    }
-
 
429
 
384
    protected List<Path> getDefaultOrder() {
430
    protected List<Path> getDefaultOrder() {
385
        return getTableOrder();
431
        return getTableOrder();
386
    }
432
    }
387
 
433
 
388
    protected final List<Path> getTableOrder() {
434
    protected final List<Path> getTableOrder() {
Line 398... Line 444...
398
    public synchronized final void setOrder(List<Path> l) {
444
    public synchronized final void setOrder(List<Path> l) {
399
        checkFrozen();
445
        checkFrozen();
400
        this.order = l == null ? null : Collections.unmodifiableList(new ArrayList<Path>(l));
446
        this.order = l == null ? null : Collections.unmodifiableList(new ArrayList<Path>(l));
401
    }
447
    }
402
 
448
 
-
 
449
    /**
-
 
450
     * Set a where to be AND'd.
-
 
451
     * 
-
 
452
     * @param o a key, <code>null</code> to change the where set by {@link #setWhere(Where)}.
-
 
453
     * @param w the new value, <code>null</code> to remove.
-
 
454
     */
-
 
455
    public final void putWhere(final Object o, final Where w) {
-
 
456
        synchronized (this) {
-
 
457
            checkFrozen();
-
 
458
            final Map<Object, Where> newValue = new HashMap<>(this.wheres);
-
 
459
            if (w == null)
-
 
460
                newValue.remove(o);
-
 
461
            else
-
 
462
                newValue.put(o, w);
-
 
463
            this.wheres = Collections.unmodifiableMap(newValue);
-
 
464
            this.where = Where.and(this.wheres.values());
-
 
465
        }
-
 
466
        fireWhereChange();
-
 
467
    }
-
 
468
 
-
 
469
    /**
-
 
470
     * Set the where, replacing any other set by {@link #putWhere(Object, Where)}.
-
 
471
     * 
-
 
472
     * @param w the new value, <code>null</code> to remove.
-
 
473
     */
403
    public final void setWhere(final Where w) {
474
    public final void setWhere(final Where w) {
404
        synchronized (this) {
475
        synchronized (this) {
405
            checkFrozen();
476
            checkFrozen();
-
 
477
            this.wheres = w == null ? Collections.<Object, Where> emptyMap() : Collections.singletonMap(null, w);
406
            this.where = w;
478
            this.where = w;
407
        }
479
        }
408
        fireWhereChange();
480
        fireWhereChange();
409
    }
481
    }
410
 
482