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 @ 47784
History | View | Annotate | Download (23.8 KB)
1 | 44738 | jjdelcerro | /**
|
---|---|---|---|
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 | 47117 | jjdelcerro | import java.util.Iterator; |
27 | 44738 | jjdelcerro | import java.util.List; |
28 | 47168 | fdiaz | import java.util.function.Function; |
29 | 47117 | jjdelcerro | import java.util.function.Predicate; |
30 | 44738 | jjdelcerro | import org.apache.commons.lang3.Range; |
31 | 46517 | fdiaz | import org.apache.commons.lang3.StringUtils; |
32 | 44750 | jjdelcerro | import org.gvsig.expressionevaluator.Code; |
33 | 46518 | fdiaz | import org.gvsig.expressionevaluator.Code.Callable; |
34 | 47117 | jjdelcerro | import org.gvsig.expressionevaluator.CodeBuilder; |
35 | 44738 | jjdelcerro | import org.gvsig.expressionevaluator.Codes; |
36 | 46993 | fdiaz | import org.gvsig.expressionevaluator.Expression; |
37 | 46518 | fdiaz | import org.gvsig.expressionevaluator.ExpressionBuilder; |
38 | 47174 | jjdelcerro | import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_LET; |
39 | 46518 | fdiaz | import org.gvsig.expressionevaluator.ExpressionEvaluator; |
40 | 47117 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator; |
41 | import org.gvsig.expressionevaluator.ExpressionEvaluatorManager; |
||
42 | 44738 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
43 | 46993 | fdiaz | import org.gvsig.expressionevaluator.ExpressionUtils; |
44 | 47117 | jjdelcerro | import org.gvsig.expressionevaluator.Formatter; |
45 | 44738 | jjdelcerro | import org.gvsig.expressionevaluator.Interpreter; |
46 | 46961 | jjdelcerro | import org.gvsig.expressionevaluator.MutableSymbolTable; |
47 | 44750 | jjdelcerro | import org.gvsig.expressionevaluator.Optimizer; |
48 | 44738 | jjdelcerro | import org.gvsig.expressionevaluator.impl.DALFunctions; |
49 | 46518 | fdiaz | import org.gvsig.fmap.dal.DALLocator; |
50 | import org.gvsig.fmap.dal.DataManager; |
||
51 | 44748 | jjdelcerro | import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT; |
52 | 46518 | fdiaz | import org.gvsig.fmap.dal.SQLBuilder; |
53 | import static org.gvsig.fmap.dal.SQLBuilder.PROP_FEATURE_TYPE; |
||
54 | import static org.gvsig.fmap.dal.SQLBuilder.PROP_SQLBUILDER; |
||
55 | import static org.gvsig.fmap.dal.SQLBuilder.PROP_TABLE; |
||
56 | 47174 | jjdelcerro | import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
57 | 44738 | jjdelcerro | import org.gvsig.fmap.dal.feature.Feature; |
58 | 47784 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
59 | 44738 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureQuery; |
60 | import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
||
61 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
62 | 46517 | fdiaz | import org.gvsig.fmap.dal.feature.FeatureType; |
63 | 45308 | fdiaz | import org.gvsig.fmap.dal.feature.impl.DefaultFeatureQueryOrder; |
64 | 47117 | jjdelcerro | import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase; |
65 | 46518 | fdiaz | import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureExpressionEvaluator; |
66 | 47174 | jjdelcerro | import org.gvsig.timesupport.DataTypes; |
67 | 47117 | jjdelcerro | import org.gvsig.tools.dispose.DisposeUtils; |
68 | import org.gvsig.tools.evaluator.EvaluatorData; |
||
69 | import org.gvsig.tools.evaluator.EvaluatorException; |
||
70 | import org.gvsig.tools.util.FilteredIterator; |
||
71 | import org.gvsig.tools.util.LimitIterator; |
||
72 | 44738 | jjdelcerro | |
73 | /**
|
||
74 | *
|
||
75 | * @author jjdelcerro
|
||
76 | */
|
||
77 | 45127 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
78 | 46088 | jjdelcerro | public class SelectFunction |
79 | extends AbstractSelectFunction
|
||
80 | implements Optimizer.FunctionOptimizer {
|
||
81 | 47117 | jjdelcerro | |
82 | public static final int MODE_FROM_STORE = 0; |
||
83 | public static final int MODE_FROM_SELECTION = 1; |
||
84 | public static final int MODE_FROM_SELECTION_IF_NOT_EMPTY = 2; |
||
85 | 47168 | fdiaz | |
86 | public static final String PROP_FEATURETYPESUPPLIER = "FEATURETYPESUPPLIER"; |
||
87 | 44738 | jjdelcerro | |
88 | 47117 | jjdelcerro | private static final int COLUMNS = 0; |
89 | private static final int TABLE = 1; |
||
90 | private static final int WHERE = 2; |
||
91 | private static final int ORDER = 3; |
||
92 | private static final int ORDER_MODE = 4; |
||
93 | private static final int LIMIT = 5; |
||
94 | private static final int INTOVAR = 6; |
||
95 | private static final int MODE_FROM = 7; |
||
96 | 47168 | fdiaz | |
97 | 47117 | jjdelcerro | |
98 | 46088 | jjdelcerro | public SelectFunction() {
|
99 | super(DALFunctions.GROUP_DATA_ACCESS,
|
||
100 | FUNCTION_SELECT, |
||
101 | 47117 | jjdelcerro | Range.between(6,8), |
102 | 46088 | jjdelcerro | "Returns a list of features of the table by applying the filter, order and limit indicated.\n"
|
103 | + "The syntax is:\n\n"
|
||
104 | + "SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;\n\n"
|
||
105 | + "Indicate a filter expression with WHERE, an order or LIMIT is optional.\n"
|
||
106 | + "You can use an asterisk or enter the column names you want to retrieve separated by commas.\n"
|
||
107 | + "The SELECT statement must always end with a semicolon.",
|
||
108 | "SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;",
|
||
109 | new String[]{ |
||
110 | "column_names/asterisk - Names of the columns table to retrieve.",
|
||
111 | "table_name - Name of the table",
|
||
112 | "filter - boolean expression to apply as filter",
|
||
113 | "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.",
|
||
114 | "limit - Maximum number of features to return"
|
||
115 | }, |
||
116 | "List",
|
||
117 | true
|
||
118 | ); |
||
119 | } |
||
120 | 44738 | jjdelcerro | |
121 | 46088 | jjdelcerro | @Override
|
122 | public boolean isHidden() { |
||
123 | return false; |
||
124 | } |
||
125 | 44738 | jjdelcerro | |
126 | 46088 | jjdelcerro | @Override
|
127 | public boolean allowConstantFolding() { |
||
128 | return false; |
||
129 | } |
||
130 | 44738 | jjdelcerro | |
131 | 46088 | jjdelcerro | @Override
|
132 | public boolean useArgumentsInsteadObjects() { |
||
133 | return true; |
||
134 | 44750 | jjdelcerro | } |
135 | 44738 | jjdelcerro | |
136 | 46088 | jjdelcerro | @Override
|
137 | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
||
138 | throw new UnsupportedOperationException(); |
||
139 | 45117 | jjdelcerro | } |
140 | 44738 | jjdelcerro | |
141 | 47117 | jjdelcerro | protected Callable getColumnsCode(Codes args) { |
142 | Code code = args.get(COLUMNS); |
||
143 | if( code == null ) { |
||
144 | return null; |
||
145 | } |
||
146 | CodeBuilder builder; |
||
147 | Callable callable;
|
||
148 | switch (code.code()) {
|
||
149 | case Code.CONSTANT:
|
||
150 | if (((Code.Constant) code).value() != null) { |
||
151 | throw new ExpressionRuntimeException("Null expected in argument COLUMNS of function '" + this.name() + "'."); |
||
152 | } |
||
153 | return null; |
||
154 | 44738 | jjdelcerro | |
155 | 47117 | jjdelcerro | case Code.IDENTIFIER:
|
156 | builder = ExpressionUtils.createCodeBuilder(); |
||
157 | return builder.block(code);
|
||
158 | |||
159 | case Code.CALLABLE:
|
||
160 | callable = (Callable) code;
|
||
161 | if( callable.parameters().isEmpty() ) {
|
||
162 | return null; |
||
163 | } |
||
164 | if (StringUtils.equalsIgnoreCase(callable.name(), "BLOCK")) { |
||
165 | return callable;
|
||
166 | } |
||
167 | if (StringUtils.equalsIgnoreCase(callable.name(), "TUPLE")) { |
||
168 | return callable;
|
||
169 | } |
||
170 | if (StringUtils.equalsIgnoreCase(callable.name(), "GETATTR")) { |
||
171 | builder = ExpressionUtils.createCodeBuilder(); |
||
172 | return builder.block(code);
|
||
173 | } |
||
174 | throw new ExpressionRuntimeException("BLOCK, TUPLE expected in argument COLUMNS of function '" + this.name() + "'."); |
||
175 | |||
176 | |||
177 | default:
|
||
178 | throw new ExpressionRuntimeException("BLOCK or NULL expected in argument COLUMNS of function '" + this.name() + "'."); |
||
179 | } |
||
180 | } |
||
181 | |||
182 | 46088 | jjdelcerro | @Override
|
183 | public Object call(Interpreter interpreter, Codes args) throws Exception { |
||
184 | |||
185 | 46954 | fdiaz | String storeName = this.getIdentifier(args, TABLE); |
186 | 47117 | jjdelcerro | Callable columns = getColumnsCode(args);
|
187 | 46088 | jjdelcerro | Code where = this.getWhereCode(args, WHERE);
|
188 | Number limit = (Number) getObject(interpreter, args, LIMIT); |
||
189 | 47117 | jjdelcerro | |
190 | int mode_from = MODE_FROM_STORE;
|
||
191 | if( args.size() > MODE_FROM ) {
|
||
192 | mode_from = this.getInt(interpreter, args, MODE_FROM);
|
||
193 | } |
||
194 | 46088 | jjdelcerro | |
195 | Callable order = getTupleOrNull(args, ORDER);
|
||
196 | Callable order_mode = getTupleOrNull(args, ORDER_MODE);
|
||
197 | FeatureQueryOrder queryOrder = null;
|
||
198 | 46100 | jjdelcerro | FeatureStore featureStore; |
199 | 46088 | jjdelcerro | try {
|
200 | featureStore = this.getFeatureStore(storeName);
|
||
201 | if (featureStore == null) { |
||
202 | throw new ExpressionRuntimeException("Cant locate the feature store '" + storeName + "' in function '" + this.name() + "'."); |
||
203 | } |
||
204 | 47178 | fdiaz | if (order != null && order_mode != null) { |
205 | 47062 | fdiaz | for (int n = 0; n < order.parameters().size(); n++) { |
206 | if (queryOrder == null) { |
||
207 | queryOrder = new DefaultFeatureQueryOrder();
|
||
208 | } |
||
209 | Boolean mode = (Boolean) ((Code.Constant)order_mode.parameters().get(n)).value(); |
||
210 | Code memberCode = order.parameters().get(n); |
||
211 | if(memberCode.code() == Code.IDENTIFIER) {
|
||
212 | queryOrder.add(((Code.Identifier)memberCode).name(), mode); |
||
213 | } else {
|
||
214 | Code memberCode2 = replaceLocalVariables(interpreter, memberCode, featureStore.getDefaultFeatureTypeQuietly()); |
||
215 | Expression exp = ExpressionUtils.createExpression(memberCode2.toString());
|
||
216 | queryOrder.add(exp, mode); |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | String intovar = null; |
||
221 | if( args.size()>INTOVAR ) {
|
||
222 | intovar = this.getIdentifier(args, INTOVAR);
|
||
223 | } |
||
224 | 46088 | jjdelcerro | FeatureQuery query = featureStore.createFeatureQuery(); |
225 | if (where != null) { |
||
226 | 46993 | fdiaz | Code where2 = removeOuterTablesReferences(interpreter, where, featureStore.getDefaultFeatureTypeQuietly()); |
227 | 46088 | jjdelcerro | ExpressionEvaluator filter = new DefaultFeatureExpressionEvaluator(where2.toString());
|
228 | filter.toSymbolTable().addSymbolTable(interpreter.getSymbolTable()); |
||
229 | query.addFilter(filter); |
||
230 | } |
||
231 | if (queryOrder != null) { |
||
232 | query.getOrder().copyFrom(queryOrder); |
||
233 | } |
||
234 | if (limit != null) { |
||
235 | query.setLimit(limit.longValue()); |
||
236 | } |
||
237 | 47174 | jjdelcerro | if( columns!=null ) { |
238 | for (Code column : columns.parameters()) {
|
||
239 | if( column instanceof Code.Identifier ) { |
||
240 | query.addAttributeName( ((Code.Identifier)column).name()); |
||
241 | } else {
|
||
242 | // TODO: Aqui habria que a?adir una columna calculada.
|
||
243 | String name = "nose"; |
||
244 | Code exp; |
||
245 | if( column instanceof Code.Callable && |
||
246 | StringUtils.equals(((Code.Callable)column).name(),FUNCTION_LET) ) { |
||
247 | name = ((Code.Identifier)((Code.Callable)column).parameters().get(0)).name();
|
||
248 | exp = ((Code.Callable)column).parameters().get(1);
|
||
249 | } else {
|
||
250 | exp = column; |
||
251 | } |
||
252 | EditableFeatureAttributeDescriptor fad = query.getExtraColumns().add(name, DataTypes.STRING); |
||
253 | fad.setFeatureAttributeEmulator(ExpressionUtils.createExpression(exp.toString())); |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | 47117 | jjdelcerro | |
258 | switch(mode_from) {
|
||
259 | case MODE_FROM_SELECTION:
|
||
260 | return select_from_selection(interpreter, featureStore, query, intovar);
|
||
261 | case MODE_FROM_SELECTION_IF_NOT_EMPTY:
|
||
262 | return select_from_selection_if_not_empty(interpreter, featureStore, query, intovar);
|
||
263 | case MODE_FROM_STORE:
|
||
264 | default:
|
||
265 | return select_from_store(interpreter, featureStore, query, intovar);
|
||
266 | } |
||
267 | } catch (ExpressionRuntimeException ex) {
|
||
268 | throw ex;
|
||
269 | } catch (Exception ex) { |
||
270 | throw new ExpressionRuntimeException("Problems calling '" + this.name() + "' function", ex); |
||
271 | } |
||
272 | } |
||
273 | 46088 | jjdelcerro | |
274 | 47117 | jjdelcerro | private Object select_from_selection(Interpreter interpreter, FeatureStore store, FeatureQuery query, String intovar) { |
275 | try {
|
||
276 | if( store.isFeatureSelectionEmpty() ) {
|
||
277 | return null; |
||
278 | } |
||
279 | Iterable features = store.getFeatureSelection();
|
||
280 | if ( query.hasFilter() ) {
|
||
281 | final Iterable baseSelection = features; |
||
282 | final ExpressionEvaluator evaluator = new DefaultFeatureExpressionEvaluator( |
||
283 | query.getExpressionFilter().toString() |
||
284 | ); |
||
285 | evaluator.toSymbolTable().addSymbolTable(interpreter.getSymbolTable()); |
||
286 | final Predicate filter = (Predicate) (Object t) -> { |
||
287 | try {
|
||
288 | return (boolean) evaluator.evaluate((EvaluatorData) t); |
||
289 | } catch (EvaluatorException ex1) {
|
||
290 | throw new ExpressionRuntimeException("Can't evaluate expression for row.", ex1); |
||
291 | } |
||
292 | }; |
||
293 | features = (Iterable) () -> new FilteredIterator(baseSelection.iterator(), filter); |
||
294 | } |
||
295 | if( query.hasLimit() ) {
|
||
296 | final Iterable baseSelection = features; |
||
297 | features = (Iterable) () -> new LimitIterator(baseSelection.iterator(), query.getLimit()); |
||
298 | } |
||
299 | 46961 | jjdelcerro | if( intovar!=null ) { |
300 | 47117 | jjdelcerro | Feature f = null;
|
301 | Iterator<Feature> it = features.iterator();
|
||
302 | if( it.hasNext() ) {
|
||
303 | f = it.next(); |
||
304 | } |
||
305 | DisposeUtils.dispose(it); |
||
306 | DisposeUtils.dispose(features); |
||
307 | 46961 | jjdelcerro | ((MutableSymbolTable)(interpreter.getSymbolTable())).setVar(intovar, f); |
308 | return f;
|
||
309 | } |
||
310 | 46088 | jjdelcerro | return features;
|
311 | |||
312 | } catch (ExpressionRuntimeException ex) {
|
||
313 | throw ex;
|
||
314 | } catch (Exception ex) { |
||
315 | 46100 | jjdelcerro | throw new ExpressionRuntimeException("Problems calling '" + this.name() + "' function", ex); |
316 | 45127 | jjdelcerro | } |
317 | 44750 | jjdelcerro | } |
318 | 47117 | jjdelcerro | |
319 | private Object select_from_store(Interpreter interpreter, FeatureStore store, FeatureQuery query, String intovar) throws Exception { |
||
320 | // FIXME: add columns to query.addAttributeName()
|
||
321 | query.retrievesAllAttributes(); |
||
322 | if( intovar!=null ) { |
||
323 | Feature f = store.findFirst(query); |
||
324 | ((MutableSymbolTable)(interpreter.getSymbolTable())).setVar(intovar, f); |
||
325 | return f;
|
||
326 | } |
||
327 | List<Feature> features = store.getFeatures(query);
|
||
328 | return features;
|
||
329 | } |
||
330 | |||
331 | private Object select_from_selection_if_not_empty(Interpreter interpreter, FeatureStore store, FeatureQuery query, String intovar) throws Exception { |
||
332 | if( store.isFeatureSelectionEmpty() ) {
|
||
333 | return select_from_store(interpreter, store, query, intovar);
|
||
334 | } |
||
335 | return select_from_selection(interpreter, store, query, intovar);
|
||
336 | } |
||
337 | |||
338 | 46088 | jjdelcerro | @Override
|
339 | public Code optimize(Optimizer optimizer, Callable caller) { |
||
340 | return caller; // Don't optimize SELECT |
||
341 | } |
||
342 | 47168 | fdiaz | |
343 | 46505 | fdiaz | public ExpressionBuilder.Value toValue(ExpressionBuilder builder, Codes args) {
|
344 | try {
|
||
345 | 46518 | fdiaz | SQLBuilder sqlBuilder = (SQLBuilder) builder.getProperty(PROP_SQLBUILDER); |
346 | 47168 | fdiaz | if (sqlBuilder == null) { |
347 | 46505 | fdiaz | return super.toValue(builder, args); |
348 | } |
||
349 | 47784 | jjdelcerro | // Function<String, FeatureType> featureTypeSupplier = (Function<String, FeatureType>) builder.getProperty(PROP_FEATURETYPESUPPLIER);
|
350 | // if (featureTypeSupplier == null) {
|
||
351 | // featureTypeSupplier = (String tableName) -> {
|
||
352 | // DataManager dataManager = DALLocator.getDataManager();
|
||
353 | // FeatureType featureType = dataManager.getStoresRepository().getFeatureType(tableName);
|
||
354 | // return featureType;
|
||
355 | // };
|
||
356 | // }
|
||
357 | 46505 | fdiaz | SQLBuilder.SelectBuilder select = sqlBuilder.createSelectBuilder(); |
358 | 46518 | fdiaz | String builderTableName = (String) builder.getProperty(SQLBuilder.PROP_TABLENAME); |
359 | 46505 | fdiaz | |
360 | 47117 | jjdelcerro | Code tableCode = args.get(TABLE); |
361 | Callable columns = getColumnsCode(args);
|
||
362 | 46505 | fdiaz | Code where = this.getWhereCode(args, WHERE);
|
363 | Callable order = getTupleOrNull(args, ORDER);
|
||
364 | Callable order_mode = getTupleOrNull(args, ORDER_MODE);
|
||
365 | Code limit = args.get(LIMIT); |
||
366 | 47117 | jjdelcerro | int mode_from = MODE_FROM_STORE;
|
367 | if( args.size() > MODE_FROM ) {
|
||
368 | Code codeModeFrom = args.get(MODE_FROM); |
||
369 | mode_from = (Integer)((Code.Constant)codeModeFrom).value();
|
||
370 | 46505 | fdiaz | } |
371 | 47117 | jjdelcerro | String intovar = null; |
372 | if( args.size()>INTOVAR && args.get(INTOVAR)!=null ) { |
||
373 | intovar = this.getIdentifier(args, INTOVAR);
|
||
374 | } |
||
375 | |||
376 | if (tableCode != null) { |
||
377 | String tableName = null; |
||
378 | String tableNameHE = null; |
||
379 | if( isHostExpression(tableCode) ) {
|
||
380 | tableNameHE = tableCode.toString(); |
||
381 | } else {
|
||
382 | tableName = this.getIdentifier(args, TABLE);
|
||
383 | } |
||
384 | switch (mode_from) {
|
||
385 | case MODE_FROM_SELECTION: {
|
||
386 | SQLBuilder.FromBuilder from = select.from(); |
||
387 | if( tableNameHE == null ) { |
||
388 | select.from().custom("SELECTION OF " + from.table().name(tableName).toString());
|
||
389 | } else {
|
||
390 | select.from().custom("SELECTION OF " + tableNameHE);
|
||
391 | } |
||
392 | } |
||
393 | break;
|
||
394 | case MODE_FROM_SELECTION_IF_NOT_EMPTY: {
|
||
395 | SQLBuilder.FromBuilder from = select.from(); |
||
396 | if( tableNameHE == null ) { |
||
397 | select.from().custom("SELECTION IF NOT EMPTY OF " + from.table().name(tableName).toString());
|
||
398 | } else {
|
||
399 | select.from().custom("SELECTION IF NOT EMPTY OF " + tableNameHE);
|
||
400 | } |
||
401 | } |
||
402 | break;
|
||
403 | case MODE_FROM_STORE:
|
||
404 | if( tableNameHE == null ) { |
||
405 | select.from().table().name(tableName); |
||
406 | } else {
|
||
407 | select.from().custom(tableNameHE); |
||
408 | } |
||
409 | break;
|
||
410 | } |
||
411 | } |
||
412 | 46505 | fdiaz | |
413 | 46518 | fdiaz | SQLBuilder.TableNameBuilder table = select.from().table(); |
414 | String tableName = table.getName();
|
||
415 | 47784 | jjdelcerro | FeatureType featureType; |
416 | if (StringUtils.equalsIgnoreCase(builderTableName, tableName)) {
|
||
417 | featureType = (FeatureType) builder.getProperty(SQLBuilder.PROP_FEATURE_TYPE); |
||
418 | } else {
|
||
419 | DataManager dataManager = DALLocator.getDataManager(); |
||
420 | featureType = dataManager.getStoresRepository().getFeatureType(tableName); |
||
421 | } |
||
422 | table.setFeatureType(featureType); |
||
423 | |||
424 | 47117 | jjdelcerro | if (columns == null || columns.parameters().isEmpty()){ |
425 | if( intovar==null ) { |
||
426 | 46517 | fdiaz | select.column().all(); |
427 | } else {
|
||
428 | 47117 | jjdelcerro | select.column().value(builder.custom("* INTO "+intovar));
|
429 | } |
||
430 | } else {
|
||
431 | for (Code column : columns.parameters()) {
|
||
432 | 47174 | jjdelcerro | if( column == null ) { |
433 | select.column().value(builder.constant(null));
|
||
434 | } else if (column instanceof Code.Identifier) { |
||
435 | 47117 | jjdelcerro | String columnName = ((Code.Identifier) column).name();
|
436 | 47784 | jjdelcerro | // if(featureType == null) {
|
437 | // if(StringUtils.equalsIgnoreCase(builderTableName, tableName)){
|
||
438 | // featureType = (FeatureType) builder.getProperty(SQLBuilder.PROP_FEATURE_TYPE);
|
||
439 | // } else {
|
||
440 | // featureType = featureTypeSupplier.apply(tableName);
|
||
441 | // }
|
||
442 | // }
|
||
443 | // if(featureType == null){
|
||
444 | select.column().name(table,columnName); |
||
445 | // } else if(featureType.get(columnName) != null) {
|
||
446 | // select.column().name(table,columnName);
|
||
447 | // } else {
|
||
448 | // select.column().name(columnName).table(null);
|
||
449 | // }
|
||
450 | 47117 | jjdelcerro | } else {
|
451 | 47784 | jjdelcerro | select.column().value(setTableName(table, column.toValue(builder))); |
452 | 46505 | fdiaz | } |
453 | } |
||
454 | } |
||
455 | |||
456 | if (where != null) { |
||
457 | 47784 | jjdelcerro | ExpressionBuilder.Value value = setTableName(table, where.toValue(builder)); |
458 | 46518 | fdiaz | select.where().value(value); |
459 | sqlBuilder.setProperties(value, null, SQLBuilder.PROP_ADD_TABLE_NAME_TO_COLUMNS, true); |
||
460 | 46505 | fdiaz | } |
461 | |||
462 | if (limit != null) { |
||
463 | 46678 | fdiaz | Object value = ((Code.Constant) limit).value();
|
464 | if(value != null){ |
||
465 | select.limit(((Number) value).longValue());
|
||
466 | } |
||
467 | 46505 | fdiaz | } |
468 | |||
469 | 47178 | fdiaz | if (order != null && order_mode != null) { |
470 | 46505 | fdiaz | for (int n = 0; n < order.parameters().size(); n++) { |
471 | Code member = order.parameters().get(n); |
||
472 | Code.Constant mode = (Code.Constant) order_mode.parameters().get(n); |
||
473 | 47784 | jjdelcerro | select.order_by() |
474 | .value(setTableName(table,member.toValue(builder))) |
||
475 | .ascending((boolean) mode.value());
|
||
476 | 46505 | fdiaz | } |
477 | } |
||
478 | 47784 | jjdelcerro | // if (featureType == null) {
|
479 | // if (StringUtils.equalsIgnoreCase(builderTableName, tableName)) {
|
||
480 | // featureType = (FeatureType) builder.getProperty(SQLBuilder.PROP_FEATURE_TYPE);
|
||
481 | // } else {
|
||
482 | // featureType = featureTypeSupplier.apply(tableName);
|
||
483 | // }
|
||
484 | // }
|
||
485 | 46518 | fdiaz | |
486 | sqlBuilder.setProperties( |
||
487 | select, |
||
488 | null,
|
||
489 | PROP_FEATURE_TYPE, featureType, |
||
490 | PROP_TABLE, table |
||
491 | ); |
||
492 | |||
493 | 46678 | fdiaz | return builder.group(select);
|
494 | 46505 | fdiaz | } catch (Exception ex) { |
495 | return super.toValue(builder, args); |
||
496 | } |
||
497 | } |
||
498 | 47117 | jjdelcerro | |
499 | @Override
|
||
500 | public String toString(Codes args, Formatter<Code> formatter) { |
||
501 | SQLBuilderBase sqlbuilder = new SQLBuilderBase();
|
||
502 | ExpressionEvaluatorManager expressionManager = ExpressionEvaluatorLocator.getExpressionEvaluatorManager(); |
||
503 | ExpressionBuilder expressionBuilder = expressionManager.createExpressionBuilder(); |
||
504 | expressionBuilder.setProperty(PROP_SQLBUILDER, sqlbuilder); |
||
505 | 47168 | fdiaz | expressionBuilder.setProperty(PROP_FEATURETYPESUPPLIER, new Function<String, FeatureType>() { |
506 | @Override
|
||
507 | public FeatureType apply(String t) { |
||
508 | return null; |
||
509 | } |
||
510 | }); |
||
511 | 47117 | jjdelcerro | |
512 | ExpressionBuilder.Value values = this.toValue(expressionBuilder, args);
|
||
513 | 47168 | fdiaz | |
514 | 47117 | jjdelcerro | return values.toString();
|
515 | } |
||
516 | 47784 | jjdelcerro | |
517 | private ExpressionBuilder.Value setTableName(SQLBuilder.TableNameBuilder table, ExpressionBuilder.Value value) {
|
||
518 | value.accept(new ExpressionBuilder.Visitor() {
|
||
519 | @Override
|
||
520 | public void visit(ExpressionBuilder.Visitable value) { |
||
521 | if( value instanceof SQLBuilder.Column) { |
||
522 | SQLBuilder.Column c = (SQLBuilder.Column) value; |
||
523 | SQLBuilder.TableNameBuilder t = c.table(); |
||
524 | if( t==null ) { |
||
525 | c.table(table); |
||
526 | return;
|
||
527 | } |
||
528 | if( t.equals(table) ) {
|
||
529 | t.setFeatureType(table.featureType()); |
||
530 | } |
||
531 | } |
||
532 | } |
||
533 | }, null);
|
||
534 | return value;
|
||
535 | } |
||
536 | 44738 | jjdelcerro | } |