OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 93 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 11... Line 11...
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.sql.model;
14
 package org.openconcerto.sql.model;
15
 
15
 
-
 
16
import org.openconcerto.sql.model.SQLSelect.LockStrength;
16
import org.openconcerto.utils.Tuple2;
17
import org.openconcerto.utils.Tuple2;
17
import org.openconcerto.utils.Value;
18
import org.openconcerto.utils.Value;
18
 
19
 
-
 
20
import java.util.HashSet;
19
import java.util.List;
21
import java.util.List;
-
 
22
import java.util.Set;
20
 
23
 
21
public final class SQLSelectHandlerBuilder {
24
public final class SQLSelectHandlerBuilder {
22
 
25
 
23
    private final SQLSelect sel;
26
    private final SQLSelect sel;
24
    private Value<TableRef> t;
27
    private Value<TableRef> t;
25
    private boolean readCache, writeCache;
28
    private boolean readCache, writeCache;
-
 
29
    private boolean immutableRows;
26
 
30
 
27
    public SQLSelectHandlerBuilder(final SQLSelect sel) {
31
    public SQLSelectHandlerBuilder(final SQLSelect sel) {
28
        if (sel == null)
32
        if (sel == null)
29
            throw new NullPointerException("Null query");
33
            throw new NullPointerException("Null query");
30
        this.sel = sel;
34
        this.sel = sel;
31
        this.t = Value.getNone();
35
        this.t = Value.getNone();
32
        this.setUseCache(true);
36
        this.setUseCache(true);
-
 
37
        this.immutableRows = true;
33
    }
38
    }
34
 
39
 
35
    public SQLSelectHandlerBuilder setUseCache(final boolean b) {
40
    public SQLSelectHandlerBuilder setUseCache(final boolean b) {
36
        this.setReadCache(b);
41
        this.setReadCache(b);
37
        this.setWriteCache(b);
42
        this.setWriteCache(b);
Line 54... Line 59...
54
 
59
 
55
    public final boolean isWriteCache() {
60
    public final boolean isWriteCache() {
56
        return this.writeCache;
61
        return this.writeCache;
57
    }
62
    }
58
 
63
 
-
 
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
 
59
    /**
73
    /**
60
     * Set the table of the rows to be created. Must be used if the query has more than one table.
74
     * Set the table of the rows to be created. Must be used if the query has more than one table.
61
     * 
75
     * 
62
     * @param t a table, not <code>null</code>.
76
     * @param t a table, not <code>null</code>.
63
     * @return this.
77
     * @return this.
Line 75... Line 89...
75
        return this;
89
        return this;
76
    }
90
    }
77
 
91
 
78
    public IResultSetHandler createHandler() {
92
    public IResultSetHandler createHandler() {
79
        final Tuple2<SQLTable, List<String>> indexes = SQLRowListRSH.getIndexes(this.sel, this.t.toNonNull(), !this.t.hasValue());
93
        final Tuple2<SQLTable, List<String>> indexes = SQLRowListRSH.getIndexes(this.sel, this.t.toNonNull(), !this.t.hasValue());
-
 
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;
80
        return SQLRowListRSH.createFromSelect(this.sel, indexes, isReadCache(), isWriteCache());
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
        };
81
    }
109
    }
82
 
110
 
83
    @SuppressWarnings("unchecked")
111
    @SuppressWarnings("unchecked")
84
    public List<SQLRow> execute() {
112
    public List<SQLRow> execute() {
85
        return (List<SQLRow>) this.sel.getSystemRoot().getDataSource().execute(this.sel.asString(), createHandler());
113
        return (List<SQLRow>) this.sel.getSystemRoot().getDataSource().execute(this.sel.asString(), createHandler());