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;
|
- |
|
17 |
import org.openconcerto.utils.Tuple2;
|
16 |
import org.openconcerto.utils.Tuple2;
|
18 |
|
17 |
|
19 |
import java.sql.ResultSet;
|
18 |
import java.sql.ResultSet;
|
20 |
import java.sql.SQLException;
|
19 |
import java.sql.SQLException;
|
21 |
import java.util.ArrayList;
|
20 |
import java.util.ArrayList;
|
22 |
import java.util.Collection;
|
21 |
import java.util.Collection;
|
23 |
import java.util.Collections;
|
- |
|
24 |
import java.util.HashSet;
|
- |
|
25 |
import java.util.List;
|
22 |
import java.util.List;
|
26 |
import java.util.Set;
|
- |
|
27 |
import java.util.stream.Collectors;
|
23 |
import java.util.stream.Collectors;
|
28 |
|
24 |
|
29 |
import org.apache.commons.dbutils.ResultSetHandler;
|
25 |
import org.apache.commons.dbutils.ResultSetHandler;
|
30 |
|
26 |
|
31 |
public final class SQLRowListRSH implements ResultSetHandler {
|
27 |
public final class SQLRowListRSH implements ResultSetHandler {
|
32 |
|
28 |
|
33 |
// hashCode()/equals() needed for data source cache
|
29 |
// hashCode()/equals() needed for data source cache
|
34 |
public static final class RSH implements ResultSetHandler {
|
30 |
public static final class RSH implements ResultSetHandler {
|
35 |
private final Tuple2<SQLTable, List<String>> names;
|
31 |
private final Tuple2<SQLTable, List<String>> names;
|
- |
|
32 |
private final boolean immutableRows;
|
36 |
|
33 |
|
37 |
// allow to create rows from arbitrary columns (and not just directly from actual fields of
|
34 |
// allow to create rows from arbitrary columns (and not just directly from actual fields of
|
38 |
// the same table)
|
35 |
// the same table)
|
39 |
// ATTN doesn't check that the types of columns are coherent with the types of the fields
|
36 |
// ATTN doesn't check that the types of columns are coherent with the types of the fields
|
40 |
public RSH(final SQLTable t, final List<String> names) {
|
37 |
public RSH(final SQLTable t, final List<String> names) {
|
41 |
this(Tuple2.create(t, names));
|
38 |
this(Tuple2.create(t, names), true);
|
42 |
// null are OK (they're ignored)
|
39 |
// null are OK (they're ignored)
|
43 |
final List<String> unknown = names.stream().filter(n -> n != null && !t.getFieldsName().contains(n)).collect(Collectors.toList());
|
40 |
final List<String> unknown = names.stream().filter(n -> n != null && !t.getFieldsName().contains(n)).collect(Collectors.toList());
|
44 |
if (!unknown.isEmpty())
|
41 |
if (!unknown.isEmpty())
|
45 |
throw new IllegalArgumentException("Not all names are fields of " + t + " : " + unknown);
|
42 |
throw new IllegalArgumentException("Not all names are fields of " + t + " : " + unknown);
|
46 |
}
|
43 |
}
|
47 |
|
44 |
|
48 |
private RSH(final Tuple2<SQLTable, List<String>> names) {
|
45 |
private RSH(final Tuple2<SQLTable, List<String>> names, final boolean immutableRows) {
|
49 |
this.names = names;
|
46 |
this.names = names;
|
- |
|
47 |
this.immutableRows = immutableRows;
|
50 |
}
|
48 |
}
|
51 |
|
49 |
|
52 |
@Override
|
50 |
@Override
|
53 |
public List<SQLRow> handle(final ResultSet rs) throws SQLException {
|
51 |
public List<SQLRow> handle(final ResultSet rs) throws SQLException {
|
54 |
// since the result will be cached, disallow its modification (e.g.avoid
|
52 |
// since the result will be cached, disallow its modification (e.g.avoid
|
55 |
// ConcurrentModificationException)
|
53 |
// ConcurrentModificationException)
|
56 |
return Collections.unmodifiableList(SQLRow.createListFromRS(this.names.get0(), rs, this.names.get1()));
|
54 |
return SQLRow.createListFromRS(this.names.get0(), rs, this.names.get1(), this.immutableRows);
|
57 |
}
|
55 |
}
|
58 |
|
56 |
|
59 |
@Override
|
57 |
@Override
|
60 |
public int hashCode() {
|
58 |
public int hashCode() {
|
61 |
return this.names.hashCode();
|
59 |
return this.names.hashCode();
|
Line 128... |
Line 126... |
128 |
* @return a handler creating a list of {@link SQLRow}.
|
126 |
* @return a handler creating a list of {@link SQLRow}.
|
129 |
* @deprecated use {@link SQLSelectHandlerBuilder}
|
127 |
* @deprecated use {@link SQLSelectHandlerBuilder}
|
130 |
*/
|
128 |
*/
|
131 |
@Deprecated
|
129 |
@Deprecated
|
132 |
static public ResultSetHandler createFromSelect(final SQLSelect sel) {
|
130 |
static public ResultSetHandler createFromSelect(final SQLSelect sel) {
|
133 |
return create(getIndexes(sel, null, true));
|
131 |
return create(getIndexes(sel, null, true), false);
|
134 |
}
|
132 |
}
|
135 |
|
133 |
|
136 |
/**
|
134 |
/**
|
137 |
* Create a handler that don't need metadata. Useful since some JDBC drivers perform queries for
|
135 |
* Create a handler that don't need metadata. Useful since some JDBC drivers perform queries for
|
138 |
* each metadata.
|
136 |
* each metadata.
|
Line 142... |
Line 140... |
142 |
* @return a handler creating a list of {@link SQLRow}.
|
140 |
* @return a handler creating a list of {@link SQLRow}.
|
143 |
* @deprecated use {@link SQLSelectHandlerBuilder}
|
141 |
* @deprecated use {@link SQLSelectHandlerBuilder}
|
144 |
*/
|
142 |
*/
|
145 |
@Deprecated
|
143 |
@Deprecated
|
146 |
static public ResultSetHandler createFromSelect(final SQLSelect sel, final TableRef t) {
|
144 |
static public ResultSetHandler createFromSelect(final SQLSelect sel, final TableRef t) {
|
147 |
return create(getIndexes(sel, t, false));
|
145 |
return create(getIndexes(sel, t, false), false);
|
148 |
}
|
146 |
}
|
149 |
|
147 |
|
150 |
static ResultSetHandler create(final Tuple2<SQLTable, List<String>> names) {
|
148 |
static ResultSetHandler create(final Tuple2<SQLTable, List<String>> names, final boolean immutableRows) {
|
- |
|
149 |
return new RSH(names, immutableRows);
|
- |
|
150 |
}
|
- |
|
151 |
|
- |
|
152 |
static public List<SQLRow> fetch(final SQLTable t) throws IllegalArgumentException {
|
151 |
return new RSH(names);
|
153 |
return fetch(t, null);
|
152 |
}
|
154 |
}
|
153 |
|
155 |
|
154 |
static public List<SQLRow> fetch(final SQLTable t, final Collection<? extends Number> ids) throws IllegalArgumentException {
|
156 |
static public List<SQLRow> fetch(final SQLTable t, final Collection<? extends Number> ids) throws IllegalArgumentException {
|
155 |
return fetch(t, ids, null);
|
157 |
return fetch(t, ids, null);
|
156 |
}
|
158 |
}
|
Line 159... |
Line 161... |
159 |
final SQLSelect sel = new SQLSelect();
|
161 |
final SQLSelect sel = new SQLSelect();
|
160 |
if (fields == null)
|
162 |
if (fields == null)
|
161 |
sel.addSelectStar(t);
|
163 |
sel.addSelectStar(t);
|
162 |
else
|
164 |
else
|
163 |
sel.addAllSelect(t, fields);
|
165 |
sel.addAllSelect(t, fields);
|
- |
|
166 |
// deterministic
|
- |
|
167 |
sel.addOrder(t, false);
|
- |
|
168 |
if (ids != null)
|
164 |
sel.setWhere(new Where(t.getKey(), ids));
|
169 |
sel.setWhere(new Where(t.getKey(), ids));
|
165 |
return execute(sel);
|
170 |
return execute(sel);
|
166 |
}
|
171 |
}
|
167 |
|
172 |
|
168 |
/**
|
173 |
/**
|
Line 192... |
Line 197... |
192 |
*/
|
197 |
*/
|
193 |
static public List<SQLRow> execute(final SQLSelect sel, final TableRef t) throws NullPointerException {
|
198 |
static public List<SQLRow> execute(final SQLSelect sel, final TableRef t) throws NullPointerException {
|
194 |
return new SQLSelectHandlerBuilder(sel).setTableRef(t).execute();
|
199 |
return new SQLSelectHandlerBuilder(sel).setTableRef(t).execute();
|
195 |
}
|
200 |
}
|
196 |
|
201 |
|
197 |
static IResultSetHandler createFromSelect(final SQLSelect sel, final Tuple2<SQLTable, List<String>> indexes, final boolean readCache, final boolean writeCache) {
|
- |
|
198 |
final Set<SQLTable> tables = new HashSet<SQLTable>();
|
- |
|
199 |
// not just tables of the fields of the SELECT clause since inner joins can change the rows
|
- |
|
200 |
// returned
|
- |
|
201 |
for (final TableRef ref : sel.getTableRefs().values()) {
|
- |
|
202 |
tables.add(ref.getTable());
|
- |
|
203 |
}
|
- |
|
204 |
|
- |
|
205 |
// the SELECT requesting a lock means the caller expects the DB to be accessed
|
- |
|
206 |
final boolean acquireLock = sel.getLockStrength() != LockStrength.NONE;
|
- |
|
207 |
return new IResultSetHandler(create(indexes), readCache && !acquireLock, writeCache && !acquireLock) {
|
- |
|
208 |
@Override
|
- |
|
209 |
public Set<? extends SQLData> getCacheModifiers() {
|
- |
|
210 |
return tables;
|
- |
|
211 |
}
|
- |
|
212 |
};
|
- |
|
213 |
}
|
- |
|
214 |
|
- |
|
215 |
private final SQLTable t;
|
202 |
private final SQLTable t;
|
216 |
private final boolean tableOnly;
|
203 |
private final boolean tableOnly;
|
217 |
|
204 |
|
218 |
public SQLRowListRSH(final SQLTable t) {
|
205 |
public SQLRowListRSH(final SQLTable t) {
|
219 |
this(t, false);
|
206 |
this(t, false);
|