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 @ 44752
History | View | Annotate | Download (9.88 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.List; |
28 |
import java.util.Objects; |
29 |
import org.apache.commons.lang3.Range; |
30 |
import org.apache.commons.lang3.StringUtils; |
31 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
32 |
import org.apache.commons.lang3.tuple.Pair; |
33 |
import org.gvsig.expressionevaluator.Code; |
34 |
import org.gvsig.expressionevaluator.CodeBuilder; |
35 |
import org.gvsig.expressionevaluator.Codes; |
36 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_GETATTR; |
37 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_TUPLE; |
38 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
39 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
40 |
import org.gvsig.expressionevaluator.Interpreter; |
41 |
import org.gvsig.expressionevaluator.Optimizer; |
42 |
import org.gvsig.expressionevaluator.SymbolTable; |
43 |
import org.gvsig.expressionevaluator.impl.DALFunctions; |
44 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
45 |
import org.gvsig.fmap.dal.DALLocator; |
46 |
import org.gvsig.fmap.dal.DataManager; |
47 |
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT; |
48 |
import org.gvsig.fmap.dal.DataStore; |
49 |
import org.gvsig.fmap.dal.expressionevaluator.ExpressionEvaluator; |
50 |
import static org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable.SYMBOL_CURRENT_TABLE; |
51 |
import org.gvsig.fmap.dal.expressionevaluator.TableAttributeHandler; |
52 |
import org.gvsig.fmap.dal.feature.Feature; |
53 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
54 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
55 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
56 |
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultExpressionEvaluator; |
57 |
import org.gvsig.tools.exception.BaseException; |
58 |
import org.gvsig.expressionevaluator.Code.Callable; |
59 |
|
60 |
/**
|
61 |
*
|
62 |
* @author jjdelcerro
|
63 |
*/
|
64 |
public class SelectFunction |
65 |
extends AbstractFunction
|
66 |
implements Optimizer.FunctionOptimizer
|
67 |
{ |
68 |
|
69 |
public SelectFunction() {
|
70 |
super(DALFunctions.GROUP_DATA_ACCESS,
|
71 |
FUNCTION_SELECT, |
72 |
Range.is(6),
|
73 |
"Returns a list of features of the table by applying the filter, order and limit indicated.\n"+
|
74 |
"The syntax is:\n\n"+
|
75 |
"SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;\n\n"+
|
76 |
"Indicate a filter expression with WHERE, an order or LIMIT is optional.\n"+
|
77 |
"The SELECT statement must always end with a semicolon.",
|
78 |
"SELECT * FROM {{table_name}} WHERE filter ;",
|
79 |
new String[]{ |
80 |
"table_name - Name of the table",
|
81 |
"filter - boolean expression to apply as filter",
|
82 |
"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.",
|
83 |
"limit - Maximum number of features to return"
|
84 |
}, |
85 |
"List",
|
86 |
true
|
87 |
); |
88 |
} |
89 |
|
90 |
@Override
|
91 |
public boolean isHidden() { |
92 |
return false; |
93 |
} |
94 |
|
95 |
@Override
|
96 |
public boolean allowConstantFolding() { |
97 |
return false; |
98 |
} |
99 |
|
100 |
@Override
|
101 |
public boolean useArgumentsInsteadObjects() { |
102 |
return true; |
103 |
} |
104 |
|
105 |
@Override
|
106 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
107 |
throw new UnsupportedOperationException(); |
108 |
} |
109 |
|
110 |
private static final int COLUMNS = 0; |
111 |
private static final int TABLE = 1; |
112 |
private static final int WHERE = 2; |
113 |
private static final int ORDER = 3; |
114 |
private static final int ORDER_MODE = 4; |
115 |
private static final int LIMIT = 5; |
116 |
|
117 |
|
118 |
private Callable getTupleOrNull(Codes args, int argn) { |
119 |
Code code = args.get(argn); |
120 |
if( code.code()==Code.CONSTANT ) {
|
121 |
if( ((Code.Constant)code).value()!=null ) { |
122 |
throw new ExpressionRuntimeException("Tupple or null expected in argument "+argn+ " of function '" + FUNCTION_SELECT + "'."); |
123 |
} |
124 |
return null; |
125 |
} |
126 |
if( code.code()!=Code.CALLABLE ) {
|
127 |
throw new ExpressionRuntimeException("Tupple or null expected in argument "+argn+ " of function '" + FUNCTION_SELECT + "'."); |
128 |
} |
129 |
Callable caller = (Callable) code; |
130 |
if( !StringUtils.equalsIgnoreCase(FUNCTION_TUPLE, caller.name()) ) {
|
131 |
throw new ExpressionRuntimeException("Tupple or null expected in argument "+argn+ " of function '" + FUNCTION_SELECT + "'."); |
132 |
} |
133 |
return caller;
|
134 |
} |
135 |
|
136 |
@Override
|
137 |
public Object call(Interpreter interpreter, Codes args) throws Exception { |
138 |
|
139 |
Code.Identifier storeName = (Code.Identifier) args.get(TABLE); |
140 |
Code columns = getTupleOrNull(args, COLUMNS); |
141 |
Code where = args.get(WHERE); |
142 |
Number limit = (Number) getObject(interpreter, args, LIMIT); |
143 |
Callable order = getTupleOrNull(args, ORDER);
|
144 |
Callable order_mode = getTupleOrNull(args, ORDER_MODE);
|
145 |
|
146 |
FeatureQueryOrder queryOrder = null;
|
147 |
if( order!=null || order_mode!=null ) { |
148 |
for( int n=0 ; n<order.parameters().size(); n++) { |
149 |
String member = (String) interpreter.run(order.parameters().get(n)); |
150 |
Boolean mode = (Boolean) interpreter.run(order_mode.parameters().get(n)); |
151 |
if( queryOrder == null ) { |
152 |
queryOrder = new FeatureQueryOrder();
|
153 |
} |
154 |
queryOrder.add(member, mode); |
155 |
} |
156 |
} |
157 |
// FIXME: add columns to query.addAttributeName()
|
158 |
try {
|
159 |
DataStore store = this.getStore(storeName.name());
|
160 |
if (store == null ) { |
161 |
throw new ExpressionRuntimeException("Cant locate the store '" + storeName + "' in function '" + FUNCTION_SELECT + "'."); |
162 |
} |
163 |
if (!(store instanceof FeatureStore)) { |
164 |
throw new ExpressionRuntimeException("The store'" + storeName + "' is not valid for function '" + FUNCTION_SELECT + "', a FeatureStore is required."); |
165 |
} |
166 |
FeatureStore featureStore = (FeatureStore) store; |
167 |
List<Feature> features;
|
168 |
if (where == null && queryOrder == null && limit==null ) { |
169 |
features = featureStore.getFeatures(); |
170 |
} else {
|
171 |
FeatureQuery query = featureStore.createFeatureQuery(); |
172 |
if (where != null) { |
173 |
removeOuterTablesReferences(interpreter, where); |
174 |
ExpressionEvaluator filter = new DefaultExpressionEvaluator(where.toString());
|
175 |
filter.getSymbolTable().addSymbolTable(interpreter.getSymbolTable()); |
176 |
query.addFilter(filter); |
177 |
} |
178 |
if (queryOrder != null) { |
179 |
query.getOrder().copyFrom(queryOrder); |
180 |
} |
181 |
if( limit!=null ) { |
182 |
query.setLimit(limit.longValue()); |
183 |
} |
184 |
query.retrievesAllAttributes(); |
185 |
features = featureStore.getFeatures(query); |
186 |
} |
187 |
return features;
|
188 |
} catch (ExpressionRuntimeException ex) {
|
189 |
throw ex;
|
190 |
} catch (Exception ex) { |
191 |
throw new ExpressionRuntimeException("Problems calling '" + FUNCTION_SELECT + "' function", ex); |
192 |
} |
193 |
} |
194 |
|
195 |
protected DataStore getStore(String storeName) { |
196 |
DataManager dataManager = DALLocator.getDataManager(); |
197 |
DataStore store = dataManager.getStoresRepository().getStore(storeName); |
198 |
return store;
|
199 |
} |
200 |
|
201 |
private void removeOuterTablesReferences(Interpreter interpreter, Code where) { |
202 |
try {
|
203 |
SymbolTable symbolTable = interpreter.getSymbolTable(); |
204 |
TableAttributeHandler table = (TableAttributeHandler) symbolTable.value(SYMBOL_CURRENT_TABLE); |
205 |
List<Pair<Code,Code>>replaces = new ArrayList<>(); |
206 |
CodeBuilder codeBuilder = ExpressionUtils.createCodeBuilder(); |
207 |
where.accept((Object o) -> {
|
208 |
Code code = (Code) o; |
209 |
if( code!=null && code.code() == Code.CALLABLE ) { |
210 |
Code.Callable caller = (Code.Callable) code; |
211 |
if( StringUtils.equalsIgnoreCase(caller.name(),FUNCTION_GETATTR) ) {
|
212 |
Codes args = caller.parameters(); |
213 |
Code arg0 = args.get(0);
|
214 |
Code arg1 = args.get(1);
|
215 |
if( arg0 instanceof Code.Identifier && arg1 instanceof Code.Identifier ) { |
216 |
Object tt = symbolTable.value(((Code.Identifier)arg0).name());
|
217 |
if( tt instanceof TableAttributeHandler && |
218 |
StringUtils.equalsIgnoreCase(((TableAttributeHandler)tt).getName(), table.getName()) ) { |
219 |
String columnName = Objects.toString(((Code.Identifier)arg1).name(), null); |
220 |
if( columnName!=null ) { |
221 |
Object value = table.get(columnName);
|
222 |
replaces.add( |
223 |
new ImmutablePair<>(
|
224 |
caller, |
225 |
codeBuilder.constant(value) |
226 |
) |
227 |
); |
228 |
} |
229 |
} |
230 |
} |
231 |
} |
232 |
} |
233 |
}); |
234 |
for (Pair<Code, Code> replace : replaces) {
|
235 |
if( replace!=null ) { |
236 |
where.replace(replace.getLeft(), replace.getRight()); |
237 |
} |
238 |
} |
239 |
} catch (BaseException ex) {
|
240 |
throw new ExpressionRuntimeException("Can't remove references to outer tables.", ex); |
241 |
} |
242 |
|
243 |
} |
244 |
|
245 |
@Override
|
246 |
public Code optimize(Optimizer optimizer, Callable caller) { |
247 |
return caller; // Don't optimize SELECT |
248 |
} |
249 |
|
250 |
} |