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