OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
93 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
93 ilm 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
 
182 ilm 16
import org.openconcerto.sql.model.SQLSelect.LockStrength;
93 ilm 17
import org.openconcerto.utils.Tuple2;
18
import org.openconcerto.utils.Value;
19
 
182 ilm 20
import java.util.HashSet;
93 ilm 21
import java.util.List;
182 ilm 22
import java.util.Set;
93 ilm 23
 
24
public final class SQLSelectHandlerBuilder {
25
 
26
    private final SQLSelect sel;
27
    private Value<TableRef> t;
28
    private boolean readCache, writeCache;
182 ilm 29
    private boolean immutableRows;
93 ilm 30
 
31
    public SQLSelectHandlerBuilder(final SQLSelect sel) {
32
        if (sel == null)
33
            throw new NullPointerException("Null query");
34
        this.sel = sel;
35
        this.t = Value.getNone();
36
        this.setUseCache(true);
182 ilm 37
        this.immutableRows = true;
93 ilm 38
    }
39
 
40
    public SQLSelectHandlerBuilder setUseCache(final boolean b) {
41
        this.setReadCache(b);
42
        this.setWriteCache(b);
43
        return this;
44
    }
45
 
46
    public SQLSelectHandlerBuilder setReadCache(final boolean b) {
47
        this.readCache = b;
48
        return this;
49
    }
50
 
51
    public final boolean isReadCache() {
52
        return this.readCache;
53
    }
54
 
55
    public SQLSelectHandlerBuilder setWriteCache(final boolean b) {
56
        this.writeCache = b;
57
        return this;
58
    }
59
 
60
    public final boolean isWriteCache() {
61
        return this.writeCache;
62
    }
63
 
182 ilm 64
    public final SQLSelectHandlerBuilder setImmutableRows(boolean immutableRows) {
65
        this.immutableRows = immutableRows;
66
        return this;
67
    }
68
 
69
    public final boolean isImmutableRows() {
70
        return this.immutableRows;
71
    }
72
 
93 ilm 73
    /**
74
     * Set the table of the rows to be created. Must be used if the query has more than one table.
75
     *
76
     * @param t a table, not <code>null</code>.
77
     * @return this.
78
     * @throws NullPointerException if t is <code>null</code>.
79
     */
80
    public SQLSelectHandlerBuilder setTableRef(final TableRef t) throws NullPointerException {
81
        if (t == null)
82
            throw new NullPointerException("Null table");
83
        this.t = Value.getSome(t);
84
        return this;
85
    }
86
 
87
    public SQLSelectHandlerBuilder unsetTableRef() {
88
        this.t = Value.getNone();
89
        return this;
90
    }
91
 
92
    public IResultSetHandler createHandler() {
93
        final Tuple2<SQLTable, List<String>> indexes = SQLRowListRSH.getIndexes(this.sel, this.t.toNonNull(), !this.t.hasValue());
182 ilm 94
        final Set<SQLTable> tables = new HashSet<SQLTable>();
95
        // not just tables of the fields of the SELECT clause since inner joins can change the rows
96
        // returned
97
        for (final TableRef ref : this.sel.getTableRefs().values()) {
98
            tables.add(ref.getTable());
99
        }
100
 
101
        // the SELECT requesting a lock means the caller expects the DB to be accessed
102
        final boolean acquireLock = this.sel.getLockStrength() != LockStrength.NONE;
103
        return new IResultSetHandler(SQLRowListRSH.create(indexes, this.isImmutableRows()), isReadCache() && !acquireLock, isWriteCache() && !acquireLock) {
104
            @Override
105
            public Set<? extends SQLData> getCacheModifiers() {
106
                return tables;
107
            }
108
        };
93 ilm 109
    }
110
 
111
    @SuppressWarnings("unchecked")
112
    public List<SQLRow> execute() {
113
        return (List<SQLRow>) this.sel.getSystemRoot().getDataSource().execute(this.sel.asString(), createHandler());
114
    }
115
}