svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / expressionevaluator / impl / function / dataaccess / SelectFunction.java @ 45366
History | View | Annotate | Download (11.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2020 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.expressionevaluator.impl.function.dataaccess; |
25 |
|
26 |
import java.util.ArrayList; |
27 |
import java.util.HashSet; |
28 |
import java.util.List; |
29 |
import java.util.Objects; |
30 |
import java.util.Set; |
31 |
import org.apache.commons.lang3.Range; |
32 |
import org.apache.commons.lang3.StringUtils; |
33 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
34 |
import org.apache.commons.lang3.tuple.Pair; |
35 |
import org.gvsig.expressionevaluator.Code; |
36 |
import org.gvsig.expressionevaluator.CodeBuilder; |
37 |
import org.gvsig.expressionevaluator.Codes; |
38 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_GETATTR; |
39 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_TUPLE; |
40 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
41 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
42 |
import org.gvsig.expressionevaluator.Interpreter; |
43 |
import org.gvsig.expressionevaluator.Optimizer; |
44 |
import org.gvsig.expressionevaluator.SymbolTable; |
45 |
import org.gvsig.expressionevaluator.impl.DALFunctions; |
46 |
import org.gvsig.fmap.dal.DALLocator; |
47 |
import org.gvsig.fmap.dal.DataManager; |
48 |
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT; |
49 |
import org.gvsig.fmap.dal.DataStore; |
50 |
import org.gvsig.expressionevaluator.ExpressionEvaluator; |
51 |
import static org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable.SYMBOL_CURRENT_TABLE; |
52 |
import org.gvsig.fmap.dal.expressionevaluator.TableAttributeHandler; |
53 |
import org.gvsig.fmap.dal.feature.Feature; |
54 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
55 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
56 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
57 |
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureExpressionEvaluator; |
58 |
import org.gvsig.expressionevaluator.Code.Callable; |
59 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
60 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureQueryOrder; |
61 |
|
62 |
/**
|
63 |
*
|
64 |
* @author jjdelcerro
|
65 |
*/
|
66 |
@SuppressWarnings("UseSpecificCatch") |
67 |
public class SelectFunction |
68 |
extends AbstractFunction
|
69 |
implements Optimizer.FunctionOptimizer
|
70 |
{ |
71 |
|
72 |
public SelectFunction() {
|
73 |
super(DALFunctions.GROUP_DATA_ACCESS,
|
74 |
FUNCTION_SELECT, |
75 |
Range.is(6),
|
76 |
"Returns a list of features of the table by applying the filter, order and limit indicated.\n"+
|
77 |
"The syntax is:\n\n"+
|
78 |
"SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;\n\n"+
|
79 |
"Indicate a filter expression with WHERE, an order or LIMIT is optional.\n"+
|
80 |
"You can use an asterisk or enter the column names you want to retrieve separated by commas.\n"+
|
81 |
"The SELECT statement must always end with a semicolon.",
|
82 |
"SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;",
|
83 |
new String[]{ |
84 |
"column_names/asterisk - Names of the columns table to retrieve.",
|
85 |
"table_name - Name of the table",
|
86 |
"filter - boolean expression to apply as filter",
|
87 |
"order_column - the order used to retrieve the features. It is a list of column names separated by a comma. The column name can optionally be followed by ASC or DESC to indicate whether the order should be ascending or descending.",
|
88 |
"limit - Maximum number of features to return"
|
89 |
}, |
90 |
"List",
|
91 |
true
|
92 |
); |
93 |
} |
94 |
|
95 |
@Override
|
96 |
public boolean isHidden() { |
97 |
return false; |
98 |
} |
99 |
|
100 |
@Override
|
101 |
public boolean allowConstantFolding() { |
102 |
return false; |
103 |
} |
104 |
|
105 |
@Override
|
106 |
public boolean useArgumentsInsteadObjects() { |
107 |
return true; |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
112 |
throw new UnsupportedOperationException(); |
113 |
} |
114 |
|
115 |
private static final int COLUMNS = 0; |
116 |
private static final int TABLE = 1; |
117 |
private static final int WHERE = 2; |
118 |
private static final int ORDER = 3; |
119 |
private static final int ORDER_MODE = 4; |
120 |
private static final int LIMIT = 5; |
121 |
|
122 |
|
123 |
private Callable getTupleOrNull(Codes args, int argn) { |
124 |
Code code = args.get(argn); |
125 |
if( code.code()==Code.CONSTANT ) {
|
126 |
if( ((Code.Constant)code).value()!=null ) { |
127 |
throw new ExpressionRuntimeException("Tupple or null expected in argument "+argn+ " of function '" + FUNCTION_SELECT + "'."); |
128 |
} |
129 |
return null; |
130 |
} |
131 |
if( code.code()!=Code.CALLABLE ) {
|
132 |
throw new ExpressionRuntimeException("Tupple or null expected in argument "+argn+ " of function '" + FUNCTION_SELECT + "'."); |
133 |
} |
134 |
Callable caller = (Callable) code; |
135 |
if( !StringUtils.equalsIgnoreCase(FUNCTION_TUPLE, caller.name()) ) {
|
136 |
throw new ExpressionRuntimeException("Tupple or null expected in argument "+argn+ " of function '" + FUNCTION_SELECT + "'."); |
137 |
} |
138 |
return caller;
|
139 |
} |
140 |
|
141 |
@Override
|
142 |
public Object call(Interpreter interpreter, Codes args) throws Exception { |
143 |
|
144 |
Code.Identifier storeName = (Code.Identifier) args.get(TABLE); |
145 |
Code columns = getTupleOrNull(args, COLUMNS); |
146 |
Code where = args.get(WHERE); |
147 |
if( where.code()==Code.CONSTANT ) {
|
148 |
if( ((Code.Constant)where).value()==null ) { |
149 |
where = null;
|
150 |
} |
151 |
} |
152 |
Number limit = (Number) getObject(interpreter, args, LIMIT); |
153 |
Callable order = getTupleOrNull(args, ORDER);
|
154 |
Callable order_mode = getTupleOrNull(args, ORDER_MODE);
|
155 |
|
156 |
FeatureQueryOrder queryOrder = null;
|
157 |
if( order!=null || order_mode!=null ) { |
158 |
for( int n=0 ; n<order.parameters().size(); n++) { |
159 |
String member = (String) interpreter.run(order.parameters().get(n)); |
160 |
Boolean mode = (Boolean) interpreter.run(order_mode.parameters().get(n)); |
161 |
if( queryOrder == null ) { |
162 |
queryOrder = new DefaultFeatureQueryOrder();
|
163 |
} |
164 |
queryOrder.add(member, mode); |
165 |
} |
166 |
} |
167 |
// FIXME: add columns to query.addAttributeName()
|
168 |
try {
|
169 |
DataStore store = this.getStore(storeName.name());
|
170 |
if (store == null ) { |
171 |
throw new ExpressionRuntimeException("Cant locate the store '" + storeName + "' in function '" + FUNCTION_SELECT + "'."); |
172 |
} |
173 |
if (!(store instanceof FeatureStore)) { |
174 |
throw new ExpressionRuntimeException("The store'" + storeName + "' is not valid for function '" + FUNCTION_SELECT + "', a FeatureStore is required."); |
175 |
} |
176 |
FeatureStore featureStore = (FeatureStore) store; |
177 |
List<Feature> features;
|
178 |
if (where == null && queryOrder == null && limit==null ) { |
179 |
features = featureStore.getFeatures(); |
180 |
} else {
|
181 |
FeatureQuery query = featureStore.createFeatureQuery(); |
182 |
if (where != null) { |
183 |
Code where2 = removeOuterTablesReferences(interpreter, where); |
184 |
ExpressionEvaluator filter = new DefaultFeatureExpressionEvaluator(where2.toString());
|
185 |
filter.toSymbolTable().addSymbolTable(interpreter.getSymbolTable()); |
186 |
query.addFilter(filter); |
187 |
} |
188 |
if (queryOrder != null) { |
189 |
query.getOrder().copyFrom(queryOrder); |
190 |
} |
191 |
if( limit!=null ) { |
192 |
query.setLimit(limit.longValue()); |
193 |
} |
194 |
query.retrievesAllAttributes(); |
195 |
features = featureStore.getFeatures(query); |
196 |
} |
197 |
return features;
|
198 |
} catch (ExpressionRuntimeException ex) {
|
199 |
throw ex;
|
200 |
} catch (Exception ex) { |
201 |
throw new ExpressionRuntimeException("Problems calling '" + FUNCTION_SELECT + "' function", ex); |
202 |
} |
203 |
} |
204 |
|
205 |
protected DataStore getStore(String storeName) { |
206 |
DataManager dataManager = DALLocator.getDataManager(); |
207 |
DataStore store = dataManager.getStoresRepository().getStore(storeName); |
208 |
return store;
|
209 |
} |
210 |
|
211 |
public static Code removeOuterTablesReferences(Interpreter interpreter, Code where) { |
212 |
try {
|
213 |
SymbolTable symbolTable = interpreter.getSymbolTable(); |
214 |
TableAttributeHandler table = (TableAttributeHandler) symbolTable.value(SYMBOL_CURRENT_TABLE); |
215 |
List<Pair<Code,Code>>replaces = new ArrayList<>(); |
216 |
CodeBuilder codeBuilder = ExpressionUtils.createCodeBuilder(); |
217 |
final Set<Code> replacesToASkip = new HashSet<>(); |
218 |
Code where2 = where.clone(); |
219 |
where2.accept((Object o) -> {
|
220 |
if( o == null ) { |
221 |
return;
|
222 |
} |
223 |
Code code = (Code) o; |
224 |
switch(code.code()) {
|
225 |
case Code.CALLABLE:
|
226 |
Code.Callable caller = (Code.Callable) code; |
227 |
if( StringUtils.equalsIgnoreCase(caller.name(),FUNCTION_GETATTR) ) {
|
228 |
Codes args = caller.parameters(); |
229 |
Code arg0 = args.get(0);
|
230 |
Code arg1 = args.get(1);
|
231 |
replacesToASkip.add(arg1); |
232 |
if( arg0 instanceof Code.Identifier && arg1 instanceof Code.Constant ) { |
233 |
Object tt = symbolTable.value(((Code.Identifier)arg0).name());
|
234 |
if( tt instanceof TableAttributeHandler && |
235 |
StringUtils.equalsIgnoreCase(((TableAttributeHandler)tt).getName(), table.getName()) ) { |
236 |
String columnName = Objects.toString(((Code.Constant)arg1).value(), null); |
237 |
if( columnName!=null ) { |
238 |
Object value = table.get(columnName);
|
239 |
replaces.add( |
240 |
new ImmutablePair<>(
|
241 |
caller, |
242 |
codeBuilder.constant(value) |
243 |
) |
244 |
); |
245 |
} |
246 |
} |
247 |
} |
248 |
} |
249 |
break;
|
250 |
} |
251 |
}); |
252 |
where2.accept((Object o) -> {
|
253 |
if( o == null ) { |
254 |
return;
|
255 |
} |
256 |
Code code = (Code) o; |
257 |
if( replacesToASkip.contains(code) ) {
|
258 |
return;
|
259 |
} |
260 |
switch(code.code()) {
|
261 |
case Code.IDENTIFIER:
|
262 |
Code.Identifier id = (Code.Identifier)code; |
263 |
if( symbolTable.exists(id.name()) ) {
|
264 |
Object value = symbolTable.value(id.name());
|
265 |
replaces.add( |
266 |
new ImmutablePair<>(
|
267 |
id, |
268 |
codeBuilder.constant(value) |
269 |
) |
270 |
); |
271 |
} |
272 |
break;
|
273 |
} |
274 |
}); |
275 |
if( replaces.isEmpty() ) {
|
276 |
return where;
|
277 |
} |
278 |
for (Pair<Code, Code> replace : replaces) {
|
279 |
if( replace!=null ) { |
280 |
where2.replace(replace.getLeft(), replace.getRight()); |
281 |
} |
282 |
} |
283 |
return where2;
|
284 |
} catch (Exception ex) { |
285 |
throw new ExpressionRuntimeException("Can't remove references to outer tables.", ex); |
286 |
} |
287 |
} |
288 |
|
289 |
@Override
|
290 |
public Code optimize(Optimizer optimizer, Callable caller) { |
291 |
return caller; // Don't optimize SELECT |
292 |
} |
293 |
|
294 |
} |