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 @ 46088
History | View | Annotate | Download (7.27 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 | 44750 | jjdelcerro | import java.util.ArrayList; |
27 | 45127 | jjdelcerro | import java.util.HashSet; |
28 | 44738 | jjdelcerro | import java.util.List; |
29 | import java.util.Objects; |
||
30 | 45127 | jjdelcerro | import java.util.Set; |
31 | 44738 | jjdelcerro | import org.apache.commons.lang3.Range; |
32 | 44750 | jjdelcerro | 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 | 44738 | jjdelcerro | import org.gvsig.expressionevaluator.Codes; |
38 | 44750 | jjdelcerro | import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_GETATTR; |
39 | import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_TUPLE; |
||
40 | 44738 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
41 | import org.gvsig.expressionevaluator.ExpressionUtils; |
||
42 | import org.gvsig.expressionevaluator.Interpreter; |
||
43 | 44750 | jjdelcerro | import org.gvsig.expressionevaluator.Optimizer; |
44 | import org.gvsig.expressionevaluator.SymbolTable; |
||
45 | 44738 | jjdelcerro | import org.gvsig.expressionevaluator.impl.DALFunctions; |
46 | 44748 | jjdelcerro | import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT; |
47 | 44738 | jjdelcerro | import org.gvsig.fmap.dal.DataStore; |
48 | 45366 | omartinez | import org.gvsig.expressionevaluator.ExpressionEvaluator; |
49 | 44750 | jjdelcerro | import static org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable.SYMBOL_CURRENT_TABLE; |
50 | import org.gvsig.fmap.dal.expressionevaluator.TableAttributeHandler; |
||
51 | 44738 | jjdelcerro | import org.gvsig.fmap.dal.feature.Feature; |
52 | import org.gvsig.fmap.dal.feature.FeatureQuery; |
||
53 | import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
||
54 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
55 | 45366 | omartinez | import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureExpressionEvaluator; |
56 | 44752 | jjdelcerro | import org.gvsig.expressionevaluator.Code.Callable; |
57 | 45308 | fdiaz | import org.gvsig.fmap.dal.feature.impl.DefaultFeatureQueryOrder; |
58 | 44738 | jjdelcerro | |
59 | /**
|
||
60 | *
|
||
61 | * @author jjdelcerro
|
||
62 | */
|
||
63 | 45127 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
64 | 46088 | jjdelcerro | public class SelectFunction |
65 | extends AbstractSelectFunction
|
||
66 | implements Optimizer.FunctionOptimizer {
|
||
67 | 44738 | jjdelcerro | |
68 | 46088 | jjdelcerro | public SelectFunction() {
|
69 | super(DALFunctions.GROUP_DATA_ACCESS,
|
||
70 | FUNCTION_SELECT, |
||
71 | Range.is(6),
|
||
72 | "Returns a list of features of the table by applying the filter, order and limit indicated.\n"
|
||
73 | + "The syntax is:\n\n"
|
||
74 | + "SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;\n\n"
|
||
75 | + "Indicate a filter expression with WHERE, an order or LIMIT is optional.\n"
|
||
76 | + "You can use an asterisk or enter the column names you want to retrieve separated by commas.\n"
|
||
77 | + "The SELECT statement must always end with a semicolon.",
|
||
78 | "SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;",
|
||
79 | new String[]{ |
||
80 | "column_names/asterisk - Names of the columns table to retrieve.",
|
||
81 | "table_name - Name of the table",
|
||
82 | "filter - boolean expression to apply as filter",
|
||
83 | "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.",
|
||
84 | "limit - Maximum number of features to return"
|
||
85 | }, |
||
86 | "List",
|
||
87 | true
|
||
88 | ); |
||
89 | } |
||
90 | 44738 | jjdelcerro | |
91 | 46088 | jjdelcerro | @Override
|
92 | public boolean isHidden() { |
||
93 | return false; |
||
94 | } |
||
95 | 44738 | jjdelcerro | |
96 | 46088 | jjdelcerro | @Override
|
97 | public boolean allowConstantFolding() { |
||
98 | return false; |
||
99 | } |
||
100 | 44738 | jjdelcerro | |
101 | 46088 | jjdelcerro | @Override
|
102 | public boolean useArgumentsInsteadObjects() { |
||
103 | return true; |
||
104 | 44750 | jjdelcerro | } |
105 | 44738 | jjdelcerro | |
106 | 46088 | jjdelcerro | @Override
|
107 | public Object call(Interpreter interpreter, Object[] args) throws Exception { |
||
108 | throw new UnsupportedOperationException(); |
||
109 | 45117 | jjdelcerro | } |
110 | 44738 | jjdelcerro | |
111 | 46088 | jjdelcerro | private static final int COLUMNS = 0; |
112 | private static final int TABLE = 1; |
||
113 | private static final int WHERE = 2; |
||
114 | private static final int ORDER = 3; |
||
115 | private static final int ORDER_MODE = 4; |
||
116 | private static final int LIMIT = 5; |
||
117 | 44738 | jjdelcerro | |
118 | 46088 | jjdelcerro | @Override
|
119 | public Object call(Interpreter interpreter, Codes args) throws Exception { |
||
120 | |||
121 | String storeName = this.getTableName(args, TABLE); |
||
122 | Code columns = getTupleOrNull(args, COLUMNS); |
||
123 | Code where = this.getWhereCode(args, WHERE);
|
||
124 | Number limit = (Number) getObject(interpreter, args, LIMIT); |
||
125 | |||
126 | Callable order = getTupleOrNull(args, ORDER);
|
||
127 | Callable order_mode = getTupleOrNull(args, ORDER_MODE);
|
||
128 | FeatureQueryOrder queryOrder = null;
|
||
129 | if (order != null || order_mode != null) { |
||
130 | for (int n = 0; n < order.parameters().size(); n++) { |
||
131 | String member = (String) interpreter.run(order.parameters().get(n)); |
||
132 | Boolean mode = (Boolean) interpreter.run(order_mode.parameters().get(n)); |
||
133 | if (queryOrder == null) { |
||
134 | queryOrder = new DefaultFeatureQueryOrder();
|
||
135 | 44750 | jjdelcerro | } |
136 | 46088 | jjdelcerro | queryOrder.add(member, mode); |
137 | } |
||
138 | 45127 | jjdelcerro | } |
139 | 46088 | jjdelcerro | FeatureStore featureStore = null;
|
140 | try {
|
||
141 | featureStore = this.getFeatureStore(storeName);
|
||
142 | if (featureStore == null) { |
||
143 | throw new ExpressionRuntimeException("Cant locate the feature store '" + storeName + "' in function '" + this.name() + "'."); |
||
144 | } |
||
145 | List<Feature> features;
|
||
146 | FeatureQuery query = featureStore.createFeatureQuery(); |
||
147 | if (where != null) { |
||
148 | Code where2 = removeOuterTablesReferences(interpreter, where); |
||
149 | ExpressionEvaluator filter = new DefaultFeatureExpressionEvaluator(where2.toString());
|
||
150 | filter.toSymbolTable().addSymbolTable(interpreter.getSymbolTable()); |
||
151 | query.addFilter(filter); |
||
152 | } |
||
153 | if (queryOrder != null) { |
||
154 | query.getOrder().copyFrom(queryOrder); |
||
155 | } |
||
156 | if (limit != null) { |
||
157 | query.setLimit(limit.longValue()); |
||
158 | } |
||
159 | |||
160 | // FIXME: add columns to query.addAttributeName()
|
||
161 | query.retrievesAllAttributes(); |
||
162 | features = featureStore.getFeatures(query); |
||
163 | return features;
|
||
164 | |||
165 | } catch (ExpressionRuntimeException ex) {
|
||
166 | throw ex;
|
||
167 | } catch (Exception ex) { |
||
168 | throw new ExpressionRuntimeException("Problems calling '" + FUNCTION_SELECT + "' function", ex); |
||
169 | 45127 | jjdelcerro | } |
170 | 44750 | jjdelcerro | } |
171 | |||
172 | 46088 | jjdelcerro | @Override
|
173 | public Code optimize(Optimizer optimizer, Callable caller) { |
||
174 | return caller; // Don't optimize SELECT |
||
175 | } |
||
176 | 44750 | jjdelcerro | |
177 | 44738 | jjdelcerro | } |