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 |
|
|
|
16 |
import org.openconcerto.utils.Tuple2;
|
|
|
17 |
|
|
|
18 |
import java.sql.ResultSet;
|
|
|
19 |
import java.sql.SQLException;
|
|
|
20 |
import java.util.ArrayList;
|
|
|
21 |
import java.util.List;
|
|
|
22 |
|
|
|
23 |
import org.apache.commons.dbutils.ResultSetHandler;
|
|
|
24 |
|
|
|
25 |
public final class SQLRowListRSH implements ResultSetHandler {
|
|
|
26 |
|
|
|
27 |
private static Tuple2<SQLTable, List<String>> getIndexes(SQLSelect sel, final SQLTable passedTable, final boolean findTable) {
|
|
|
28 |
final List<SQLField> selectFields = sel.getSelectFields();
|
|
|
29 |
final int size = selectFields.size();
|
|
|
30 |
if (size == 0)
|
|
|
31 |
throw new IllegalArgumentException("empty select : " + sel);
|
|
|
32 |
final SQLTable t;
|
|
|
33 |
if (findTable) {
|
|
|
34 |
if (passedTable != null)
|
|
|
35 |
throw new IllegalArgumentException("non null table " + passedTable);
|
|
|
36 |
t = selectFields.get(0).getTable();
|
|
|
37 |
} else {
|
|
|
38 |
if (passedTable == null)
|
|
|
39 |
throw new IllegalArgumentException("null table");
|
|
|
40 |
t = passedTable;
|
|
|
41 |
}
|
|
|
42 |
// cannot pass an alias to this method since getSelectFields() returns SQLField and not
|
|
|
43 |
// FieldRef
|
|
|
44 |
final List<AliasedTable> aliases = sel.getAliases(t);
|
|
|
45 |
if (aliases.size() != 1)
|
|
|
46 |
throw new IllegalArgumentException(t + " isn't exactly once : " + aliases);
|
|
|
47 |
final List<String> l = new ArrayList<String>(size);
|
|
|
48 |
for (int i = 0; i < size; i++) {
|
|
|
49 |
final SQLField field = selectFields.get(i);
|
|
|
50 |
if (field.getTable().equals(t))
|
|
|
51 |
l.add(field.getName());
|
|
|
52 |
else if (findTable)
|
|
|
53 |
throw new IllegalArgumentException(field + " is not in " + t);
|
|
|
54 |
else
|
|
|
55 |
l.add(null);
|
|
|
56 |
}
|
|
|
57 |
return Tuple2.create(t, l);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Create a handler that don't need metadata.
|
|
|
62 |
*
|
|
|
63 |
* @param sel the select that will produce the result set, must only have one table.
|
|
|
64 |
* @return a handler creating a list of {@link SQLRow}.
|
|
|
65 |
*/
|
|
|
66 |
static public ResultSetHandler createFromSelect(final SQLSelect sel) {
|
|
|
67 |
return create(getIndexes(sel, null, true));
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Create a handler that don't need metadata. Useful since some JDBC drivers perform queries for
|
|
|
72 |
* each metadata.
|
|
|
73 |
*
|
|
|
74 |
* @param sel the select that will produce the result set.
|
|
|
75 |
* @param t the table for which to create rows, must appear only once in <code>sel</code>.
|
|
|
76 |
* @return a handler creating a list of {@link SQLRow}.
|
|
|
77 |
*/
|
|
|
78 |
static public ResultSetHandler createFromSelect(final SQLSelect sel, final SQLTable t) {
|
|
|
79 |
return create(getIndexes(sel, t, false));
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
static private ResultSetHandler create(final Tuple2<SQLTable, List<String>> names) {
|
|
|
83 |
return new ResultSetHandler() {
|
|
|
84 |
@Override
|
|
|
85 |
public Object handle(ResultSet rs) throws SQLException {
|
|
|
86 |
return SQLRow.createListFromRS(names.get0(), rs, names.get1());
|
|
|
87 |
}
|
|
|
88 |
};
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
@SuppressWarnings("unchecked")
|
|
|
92 |
static public List<SQLRow> execute(final SQLSelect sel) {
|
|
|
93 |
final Tuple2<SQLTable, List<String>> indexes = getIndexes(sel, null, true);
|
|
|
94 |
return (List<SQLRow>) indexes.get0().getDBSystemRoot().getDataSource().execute(sel.asString(), create(indexes));
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
private final SQLTable t;
|
|
|
98 |
private final boolean tableOnly;
|
|
|
99 |
|
|
|
100 |
public SQLRowListRSH(SQLTable t) {
|
|
|
101 |
this(t, false);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
public SQLRowListRSH(SQLTable t, final boolean tableOnly) {
|
|
|
105 |
super();
|
|
|
106 |
this.t = t;
|
|
|
107 |
this.tableOnly = tableOnly;
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
public Object handle(ResultSet rs) throws SQLException {
|
|
|
111 |
return SQLRow.createListFromRS(this.t, rs, this.tableOnly);
|
|
|
112 |
}
|
|
|
113 |
}
|