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 @ 44740
History | View | Annotate | Download (5.66 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.List; |
27 |
import java.util.Objects; |
28 |
import org.apache.commons.lang3.Range; |
29 |
import org.gvsig.expressionevaluator.Codes; |
30 |
import org.gvsig.expressionevaluator.Expression; |
31 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
32 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
33 |
import org.gvsig.expressionevaluator.Interpreter; |
34 |
import org.gvsig.expressionevaluator.impl.DALFunctions; |
35 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
36 |
import org.gvsig.fmap.dal.DALLocator; |
37 |
import org.gvsig.fmap.dal.DataManager; |
38 |
import org.gvsig.fmap.dal.DataStore; |
39 |
import org.gvsig.fmap.dal.feature.Feature; |
40 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
41 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
42 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
43 |
|
44 |
/**
|
45 |
*
|
46 |
* @author jjdelcerro
|
47 |
*/
|
48 |
public class SelectFunction extends AbstractFunction { |
49 |
|
50 |
public SelectFunction() {
|
51 |
super(DALFunctions.GROUP_DATA_ACCESS,
|
52 |
DALFunctions.FUNCTION_SELECT, |
53 |
Range.between(1,5), |
54 |
"Returns a list of features of the table by applying the filter, order and limit indicated.\n"+
|
55 |
"The syntax is:\n\n"+
|
56 |
"SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;\n\n"+
|
57 |
"Indicate a filter expression with WHERE, an order or LIMIT is optional.\n"+
|
58 |
"The SELECT statement must always end with a semicolon.",
|
59 |
"SELECT * FROM {{table_name}} WHERE filter ;",
|
60 |
new String[]{ |
61 |
"table_name - Name of the table",
|
62 |
"filter - boolean expression to apply as filter",
|
63 |
"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.",
|
64 |
"limit - Maximum number of features to return"
|
65 |
}, |
66 |
"List",
|
67 |
true
|
68 |
); |
69 |
} |
70 |
|
71 |
@Override
|
72 |
public boolean isHidden() { |
73 |
return false; |
74 |
} |
75 |
|
76 |
@Override
|
77 |
public boolean allowConstantFolding() { |
78 |
return false; |
79 |
} |
80 |
|
81 |
@Override
|
82 |
public boolean allowArgNames() { |
83 |
return true; |
84 |
} |
85 |
|
86 |
@Override
|
87 |
public boolean useArgumentsInsteadObjects() { |
88 |
return true; |
89 |
} |
90 |
|
91 |
@Override
|
92 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
93 |
throw new UnsupportedOperationException(); |
94 |
} |
95 |
|
96 |
@Override
|
97 |
public Object call(Interpreter interpreter, Codes args) throws Exception { |
98 |
|
99 |
String storeName = (String) getObject(interpreter, args, "TABLE"); |
100 |
String where = Objects.toString(args.get("WHERE"),null); |
101 |
Number limit = (Number) getObject(interpreter, args, "LIMIT"); |
102 |
FeatureQueryOrder order = null;
|
103 |
for( int n=0 ; args.contains("ORDER", n); n++) { |
104 |
String member = (String) getObject(interpreter, args, "ORDER", n); |
105 |
Boolean mode = (Boolean) getObject(interpreter, args, "ORDER_MODE", n); |
106 |
if( order == null ) { |
107 |
order = new FeatureQueryOrder();
|
108 |
} |
109 |
order.add(member, mode); |
110 |
} |
111 |
|
112 |
try {
|
113 |
DataStore store = this.getStore(storeName);
|
114 |
if (store == null ) { |
115 |
throw new ExpressionRuntimeException("Cant locate the store '" + storeName + "' in function '" + DALFunctions.FUNCTION_SELECT + "'."); |
116 |
} |
117 |
if (!(store instanceof FeatureStore)) { |
118 |
throw new ExpressionRuntimeException("The store'" + storeName + "' is not valid for function '" + DALFunctions.FUNCTION_SELECT + "', a FeatureStore is required."); |
119 |
} |
120 |
FeatureStore featureStore = (FeatureStore) store; |
121 |
List<Feature> features;
|
122 |
if (where == null && order == null && limit==null ) { |
123 |
features = featureStore.getFeatures(); |
124 |
} else {
|
125 |
FeatureQuery query = featureStore.createFeatureQuery(); |
126 |
if (where != null) { |
127 |
Expression filter = ExpressionUtils.createExpression(where);
|
128 |
filter.getSymbolTable().addSymbolTable(interpreter.getSymbolTable()); |
129 |
query.addFilter(filter); |
130 |
} |
131 |
if (order != null) { |
132 |
query.getOrder().copyFrom(order); |
133 |
} |
134 |
if( limit!=null ) { |
135 |
query.setLimit(limit.longValue()); |
136 |
} |
137 |
query.retrievesAllAttributes(); |
138 |
features = featureStore.getFeatures(query); |
139 |
} |
140 |
return features;
|
141 |
} catch (ExpressionRuntimeException ex) {
|
142 |
throw ex;
|
143 |
} catch (Exception ex) { |
144 |
throw new ExpressionRuntimeException("Problems calling '" + DALFunctions.FUNCTION_SELECT + "' function", ex); |
145 |
} |
146 |
} |
147 |
|
148 |
protected DataStore getStore(String storeName) { |
149 |
DataManager dataManager = DALLocator.getDataManager(); |
150 |
DataStore store = dataManager.getStoresRepository().getStore(storeName); |
151 |
return store;
|
152 |
} |
153 |
|
154 |
} |