Revision 46088
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.impl/src/test/java/org/gvsig/expressionevaluator/TestGrammarCompiler.java | ||
---|---|---|
157 | 157 |
checkEquals("testSelectCountWhere", "SELECT_COUNT(\"countries\", (\"LASTCENSUS\" > 0))", code.toString()); |
158 | 158 |
} |
159 | 159 |
|
160 |
public void testSelectCountWhere2() { |
|
161 |
StringBuilder source = new StringBuilder(); |
|
162 |
source.append("BEGIN"); |
|
163 |
source.append(" tableName := 'countries';"); |
|
164 |
source.append(" SELECT COUNT(*) FROM :tableName "); |
|
165 |
source.append(" WHERE countries.LASTCENSUS > 0 ; "); |
|
166 |
source.append("END"); |
|
167 |
|
|
168 |
Compiler compiler = createCompiler(); |
|
169 |
|
|
170 |
Code code = compiler.compileExpression(source.toString()); |
|
171 |
link(code); |
|
172 |
checkEquals("testSelectCountWhere", "BLOCK(LET('tableName', 'countries'), SELECT_COUNT($HOSTEXPRESSION(\"tableName\", 'IN'), (\"countries\".\"LASTCENSUS\" > 0)))", code.toString()); |
|
173 |
} |
|
174 |
|
|
160 | 175 |
public void testSelectWhere2() { |
161 | 176 |
StringBuilder source = new StringBuilder(); |
162 | 177 |
source.append("SELECT * FROM countries "); |
... | ... | |
169 | 184 |
checkEquals("testSelectWhere2", "SELECT(TUPLE(), \"countries\", (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL)", code.toString()); |
170 | 185 |
} |
171 | 186 |
|
187 |
public void testSelectWhere3() { |
|
188 |
StringBuilder source = new StringBuilder(); |
|
189 |
source.append("BEGIN"); |
|
190 |
source.append(" tableName := 'countries';"); |
|
191 |
source.append(" SELECT * FROM :tableName "); |
|
192 |
source.append(" WHERE countries.LASTCENSUS > 0 ; "); |
|
193 |
source.append("END"); |
|
194 |
|
|
195 |
Compiler compiler = createCompiler(); |
|
196 |
|
|
197 |
Code code = compiler.compileExpression(source.toString()); |
|
198 |
link(code); |
|
199 |
checkEquals("testSelectWhere3", "BLOCK(LET('tableName', 'countries'), SELECT(TUPLE(), $HOSTEXPRESSION(\"tableName\", 'IN'), (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL))", code.toString()); |
|
200 |
} |
|
201 |
|
|
172 | 202 |
public void testSelectOrder() { |
173 | 203 |
StringBuilder source = new StringBuilder(); |
174 | 204 |
source.append("SELECT * FROM countries "); |
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/SelectCountFunction.java | ||
---|---|---|
29 | 29 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
30 | 30 |
import org.gvsig.expressionevaluator.Interpreter; |
31 | 31 |
import org.gvsig.expressionevaluator.impl.DALFunctions; |
32 |
import static org.gvsig.expressionevaluator.impl.function.dataaccess.SelectFunction.removeOuterTablesReferences; |
|
33 | 32 |
import org.gvsig.fmap.dal.DALLocator; |
34 | 33 |
import org.gvsig.fmap.dal.DataManager; |
35 | 34 |
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT_COUNT; |
... | ... | |
46 | 45 |
* @author jjdelcerro |
47 | 46 |
*/ |
48 | 47 |
@SuppressWarnings("UseSpecificCatch") |
49 |
public class SelectCountFunction extends AbstractFeatureFunction {
|
|
48 |
public class SelectCountFunction extends AbstractSelectFunction {
|
|
50 | 49 |
|
51 |
public SelectCountFunction() { |
|
52 |
super(DALFunctions.GROUP_DATA_ACCESS, |
|
53 |
FUNCTION_SELECT_COUNT, |
|
54 |
Range.between(1,2), |
|
55 |
"Returns the number of features of the table by applying the filter indicated.\n" + |
|
56 |
"The syntax is:\n\n"+ |
|
57 |
"SELECT COUNT(*) FROM table WHERE boolean_expression;\n\n"+ |
|
58 |
"Indicate a filter expression with WHERE is optional.\n"+ |
|
59 |
"The SELECT statement must always end with a semicolon.", |
|
60 |
"SELECT COUNT(*) FROM {{table}} WHERE filter ;", |
|
61 |
new String[]{ |
|
62 |
"table - Name of the table", |
|
63 |
"filter - boolean expression with the filter to apply", |
|
64 |
}, |
|
65 |
"Long", |
|
66 |
false |
|
67 |
); |
|
68 |
} |
|
50 |
public SelectCountFunction() { |
|
51 |
super(DALFunctions.GROUP_DATA_ACCESS, |
|
52 |
FUNCTION_SELECT_COUNT, |
|
53 |
Range.between(1, 2), |
|
54 |
"Returns the number of features of the table by applying the filter indicated.\n" |
|
55 |
+ "The syntax is:\n\n" |
|
56 |
+ "SELECT COUNT(*) FROM table WHERE boolean_expression;\n\n" |
|
57 |
+ "Indicate a filter expression with WHERE is optional.\n" |
|
58 |
+ "The SELECT statement must always end with a semicolon.", |
|
59 |
"SELECT COUNT(*) FROM {{table}} WHERE filter ;", |
|
60 |
new String[]{ |
|
61 |
"table - Name of the table", |
|
62 |
"filter - boolean expression with the filter to apply",}, |
|
63 |
"Long", |
|
64 |
false |
|
65 |
); |
|
66 |
} |
|
69 | 67 |
|
70 |
@Override |
|
71 |
public boolean isHidden() { |
|
72 |
return false; |
|
73 |
} |
|
68 |
@Override
|
|
69 |
public boolean isHidden() {
|
|
70 |
return false;
|
|
71 |
}
|
|
74 | 72 |
|
75 |
@Override |
|
76 |
public boolean allowConstantFolding() { |
|
77 |
return false; |
|
78 |
} |
|
73 |
@Override
|
|
74 |
public boolean allowConstantFolding() {
|
|
75 |
return false;
|
|
76 |
}
|
|
79 | 77 |
|
80 |
@Override |
|
81 |
public boolean useArgumentsInsteadObjects() { |
|
82 |
return true; |
|
83 |
} |
|
78 |
@Override
|
|
79 |
public boolean useArgumentsInsteadObjects() {
|
|
80 |
return true;
|
|
81 |
}
|
|
84 | 82 |
|
85 |
@Override |
|
86 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
|
87 |
throw new UnsupportedOperationException(); |
|
88 |
} |
|
83 |
@Override
|
|
84 |
public Object call(Interpreter interpreter, Object[] args) throws Exception {
|
|
85 |
throw new UnsupportedOperationException();
|
|
86 |
}
|
|
89 | 87 |
|
90 |
@Override
|
|
91 |
public Object call(Interpreter interpreter, Codes args) throws Exception {
|
|
88 |
private static final int TABLE = 0;
|
|
89 |
private static final int WHERE = 1;
|
|
92 | 90 |
|
93 |
String storeName = (String) getObject(interpreter, args, 0); |
|
94 |
Code where = args.get(1); |
|
95 |
if( where.code()==Code.CONSTANT ) { |
|
96 |
if( ((Code.Constant)where).value()==null ) { |
|
97 |
where = null; |
|
91 |
@Override |
|
92 |
public Object call(Interpreter interpreter, Codes args) throws Exception { |
|
93 |
|
|
94 |
String storeName = this.getTableName(args, TABLE); |
|
95 |
Code where = this.getWhereCode(args, WHERE); |
|
96 |
|
|
97 |
FeatureStore featureStore = null; |
|
98 |
FeatureSet set = null; |
|
99 |
try { |
|
100 |
featureStore = this.getFeatureStore(storeName); |
|
101 |
if (featureStore == null) { |
|
102 |
throw new ExpressionRuntimeException("Cant locate the feature store '" + storeName + "' in function '" + this.name() + "'."); |
|
103 |
} |
|
104 |
if (where == null) { |
|
105 |
set = featureStore.getFeatureSet(); |
|
106 |
} else { |
|
107 |
FeatureQuery query = featureStore.createFeatureQuery(); |
|
108 |
Code where2 = removeOuterTablesReferences(interpreter, where); |
|
109 |
ExpressionEvaluator filter = new DefaultFeatureExpressionEvaluator(where2.toString()); |
|
110 |
filter.toSymbolTable().addSymbolTable(interpreter.getSymbolTable()); |
|
111 |
query.addFilter(filter); |
|
112 |
query.retrievesAllAttributes(); |
|
113 |
set = featureStore.getFeatureSet(query); |
|
114 |
} |
|
115 |
return set.getSize(); |
|
116 |
} catch (ExpressionRuntimeException ex) { |
|
117 |
throw ex; |
|
118 |
} catch (Exception ex) { |
|
119 |
throw new ExpressionRuntimeException("Problems calling '" + this.name() + "' function", ex); |
|
120 |
} finally { |
|
121 |
DisposeUtils.disposeQuietly(set); |
|
122 |
DisposeUtils.disposeQuietly(featureStore); |
|
98 | 123 |
} |
99 | 124 |
} |
100 |
|
|
101 |
FeatureStore featureStore = null; |
|
102 |
FeatureSet set = null; |
|
103 |
try { |
|
104 |
DataStore store = this.getStore(storeName); |
|
105 |
if (store == null ) { |
|
106 |
throw new ExpressionRuntimeException("Cant locate the store '" + storeName + "' in function '" + FUNCTION_SELECT_COUNT + "'."); |
|
107 |
} |
|
108 |
if (!(store instanceof FeatureStore)) { |
|
109 |
throw new ExpressionRuntimeException("The store'" + storeName + "' is not valid for function '" + FUNCTION_SELECT_COUNT + "', a FeatureStore is required."); |
|
110 |
} |
|
111 |
featureStore = (FeatureStore) store; |
|
112 |
if (where == null ) { |
|
113 |
set = featureStore.getFeatureSet(); |
|
114 |
} else { |
|
115 |
FeatureQuery query = featureStore.createFeatureQuery(); |
|
116 |
Code where2 = removeOuterTablesReferences(interpreter, where); |
|
117 |
ExpressionEvaluator filter = new DefaultFeatureExpressionEvaluator(where2.toString()); |
|
118 |
filter.toSymbolTable().addSymbolTable(interpreter.getSymbolTable()); |
|
119 |
query.addFilter(filter); |
|
120 |
query.retrievesAllAttributes(); |
|
121 |
set = featureStore.getFeatureSet(query); |
|
122 |
} |
|
123 |
return set.getSize(); |
|
124 |
} catch (ExpressionRuntimeException ex) { |
|
125 |
throw ex; |
|
126 |
} catch (Exception ex) { |
|
127 |
throw new ExpressionRuntimeException("Problems calling '" + FUNCTION_SELECT_COUNT + "' function", ex); |
|
128 |
} finally { |
|
129 |
DisposeUtils.disposeQuietly(set); |
|
130 |
DisposeUtils.disposeQuietly(featureStore); |
|
131 |
} |
|
132 |
} |
|
133 | 125 |
|
134 |
protected DataStore getStore(String storeName) { |
|
135 |
DataManager dataManager = DALLocator.getDataManager(); |
|
136 |
DataStore store = dataManager.getStoresRepository().getStore(storeName); |
|
137 |
return store; |
|
138 |
} |
|
139 |
|
|
140 | 126 |
} |
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/AbstractSelectFunction.java | ||
---|---|---|
1 |
package org.gvsig.expressionevaluator.impl.function.dataaccess; |
|
2 |
|
|
3 |
import java.util.ArrayList; |
|
4 |
import java.util.HashSet; |
|
5 |
import java.util.List; |
|
6 |
import java.util.Objects; |
|
7 |
import java.util.Set; |
|
8 |
import org.apache.commons.lang3.Range; |
|
9 |
import org.apache.commons.lang3.StringUtils; |
|
10 |
import org.apache.commons.lang3.tuple.ImmutablePair; |
|
11 |
import org.apache.commons.lang3.tuple.Pair; |
|
12 |
import org.gvsig.expressionevaluator.Code; |
|
13 |
import org.gvsig.expressionevaluator.Code.Callable; |
|
14 |
import org.gvsig.expressionevaluator.CodeBuilder; |
|
15 |
import org.gvsig.expressionevaluator.Codes; |
|
16 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_GETATTR; |
|
17 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_TUPLE; |
|
18 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
|
19 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
|
20 |
import org.gvsig.expressionevaluator.Interpreter; |
|
21 |
import org.gvsig.expressionevaluator.SymbolTable; |
|
22 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
|
23 |
import org.gvsig.fmap.dal.DALLocator; |
|
24 |
import org.gvsig.fmap.dal.DataManager; |
|
25 |
import org.gvsig.fmap.dal.DataStore; |
|
26 |
import static org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable.SYMBOL_CURRENT_TABLE; |
|
27 |
import org.gvsig.fmap.dal.expressionevaluator.TableAttributeHandler; |
|
28 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
|
29 |
|
|
30 |
/** |
|
31 |
* |
|
32 |
* @author gvSIG Team |
|
33 |
*/ |
|
34 |
@SuppressWarnings("UseSpecificCatch") |
|
35 |
public abstract class AbstractSelectFunction extends AbstractFunction { |
|
36 |
|
|
37 |
protected AbstractSelectFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs, String returnType, boolean sqlCompatible) { |
|
38 |
super(group, name, argc, description, template, descriptionArgs, returnType, sqlCompatible); |
|
39 |
} |
|
40 |
|
|
41 |
protected AbstractSelectFunction(String group, String name, Range argc, String description, String template, String[] descriptionArgs, String returnType) { |
|
42 |
super(group, name, argc, description, template, descriptionArgs, returnType); |
|
43 |
} |
|
44 |
|
|
45 |
protected String getTableName(Codes args, int argn) { |
|
46 |
Code storeName_code = (Code) args.get(argn); |
|
47 |
if (storeName_code instanceof Code.Identifier) { |
|
48 |
return ((Code.Identifier) storeName_code).name(); |
|
49 |
} else if (storeName_code instanceof Code.Constant) { |
|
50 |
Code.Constant storeName_const = (Code.Constant) storeName_code; |
|
51 |
if (storeName_const.value() instanceof CharSequence) { |
|
52 |
return storeName_const.value().toString(); |
|
53 |
} |
|
54 |
} |
|
55 |
throw new ExpressionRuntimeException("Invalid table name (" + Objects.toString(storeName_code) + ") in function '" + this.name() + "'."); |
|
56 |
} |
|
57 |
|
|
58 |
protected Code getWhereCode(Codes args, int argn) { |
|
59 |
Code where = args.get(argn); |
|
60 |
if (where.code() == Code.CONSTANT) { |
|
61 |
if (((Code.Constant) where).value() == null) { |
|
62 |
where = null; |
|
63 |
} |
|
64 |
} |
|
65 |
return where; |
|
66 |
} |
|
67 |
|
|
68 |
protected Callable getTupleOrNull(Codes args, int argn) { |
|
69 |
Code code = args.get(argn); |
|
70 |
if (code.code() == Code.CONSTANT) { |
|
71 |
if (((Code.Constant) code).value() != null) { |
|
72 |
throw new ExpressionRuntimeException("Tupple or null expected in argument " + argn + " of function '" + this.name() + "'."); |
|
73 |
} |
|
74 |
return null; |
|
75 |
} |
|
76 |
if (code.code() != Code.CALLABLE) { |
|
77 |
throw new ExpressionRuntimeException("Tupple or null expected in argument " + argn + " of function '" + this.name() + "'."); |
|
78 |
} |
|
79 |
Callable caller = (Callable) code; |
|
80 |
if (!StringUtils.equalsIgnoreCase(FUNCTION_TUPLE, caller.name())) { |
|
81 |
throw new ExpressionRuntimeException("Tupple or null expected in argument " + argn + " of function '" + this.name() + "'."); |
|
82 |
} |
|
83 |
return caller; |
|
84 |
} |
|
85 |
|
|
86 |
protected Code removeOuterTablesReferences(Interpreter interpreter, Code where) { |
|
87 |
try { |
|
88 |
SymbolTable symbolTable = interpreter.getSymbolTable(); |
|
89 |
TableAttributeHandler table = (TableAttributeHandler) symbolTable.value(SYMBOL_CURRENT_TABLE); |
|
90 |
List<Pair<Code, Code>> replaces = new ArrayList<>(); |
|
91 |
CodeBuilder codeBuilder = ExpressionUtils.createCodeBuilder(); |
|
92 |
final Set<Code> replacesToASkip = new HashSet<>(); |
|
93 |
Code where2 = where.clone(); |
|
94 |
where2.accept((Object o) -> { |
|
95 |
if (o == null) { |
|
96 |
return; |
|
97 |
} |
|
98 |
Code code = (Code) o; |
|
99 |
switch (code.code()) { |
|
100 |
case Code.CALLABLE: |
|
101 |
Code.Callable caller = (Code.Callable) code; |
|
102 |
if (StringUtils.equalsIgnoreCase(caller.name(), FUNCTION_GETATTR)) { |
|
103 |
Codes args = caller.parameters(); |
|
104 |
Code arg0 = args.get(0); |
|
105 |
Code arg1 = args.get(1); |
|
106 |
replacesToASkip.add(arg1); |
|
107 |
if (arg0 instanceof Code.Identifier && arg1 instanceof Code.Constant) { |
|
108 |
Object tt = symbolTable.value(((Code.Identifier) arg0).name()); |
|
109 |
if (tt instanceof TableAttributeHandler |
|
110 |
&& StringUtils.equalsIgnoreCase(((TableAttributeHandler) tt).getName(), table.getName())) { |
|
111 |
String columnName = Objects.toString(((Code.Constant) arg1).value(), null); |
|
112 |
if (columnName != null) { |
|
113 |
Object value = table.get(columnName); |
|
114 |
replaces.add( |
|
115 |
new ImmutablePair<>( |
|
116 |
caller, |
|
117 |
codeBuilder.constant(value) |
|
118 |
) |
|
119 |
); |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
123 |
} |
|
124 |
break; |
|
125 |
} |
|
126 |
}); |
|
127 |
where2.accept((Object o) -> { |
|
128 |
if (o == null) { |
|
129 |
return; |
|
130 |
} |
|
131 |
Code code = (Code) o; |
|
132 |
if (replacesToASkip.contains(code)) { |
|
133 |
return; |
|
134 |
} |
|
135 |
switch (code.code()) { |
|
136 |
case Code.IDENTIFIER: |
|
137 |
Code.Identifier id = (Code.Identifier) code; |
|
138 |
if (symbolTable.exists(id.name())) { |
|
139 |
Object value = symbolTable.value(id.name()); |
|
140 |
replaces.add( |
|
141 |
new ImmutablePair<>( |
|
142 |
id, |
|
143 |
codeBuilder.constant(value) |
|
144 |
) |
|
145 |
); |
|
146 |
} |
|
147 |
break; |
|
148 |
} |
|
149 |
}); |
|
150 |
if (replaces.isEmpty()) { |
|
151 |
return where; |
|
152 |
} |
|
153 |
for (Pair<Code, Code> replace : replaces) { |
|
154 |
if (replace != null) { |
|
155 |
where2.replace(replace.getLeft(), replace.getRight()); |
|
156 |
} |
|
157 |
} |
|
158 |
return where2; |
|
159 |
} catch (Exception ex) { |
|
160 |
throw new ExpressionRuntimeException("Can't remove references to outer tables.", ex); |
|
161 |
} |
|
162 |
} |
|
163 |
|
|
164 |
protected DataStore getStore(String storeName) { |
|
165 |
DataManager dataManager = DALLocator.getDataManager(); |
|
166 |
DataStore store = dataManager.getStoresRepository().getStore(storeName); |
|
167 |
return store; |
|
168 |
} |
|
169 |
|
|
170 |
protected FeatureStore getFeatureStore(String storeName) { |
|
171 |
DataManager dataManager = DALLocator.getDataManager(); |
|
172 |
DataStore store = dataManager.getStoresRepository().getStore(storeName); |
|
173 |
if (store instanceof FeatureStore) { |
|
174 |
return (FeatureStore) store; |
|
175 |
} |
|
176 |
return null; |
|
177 |
} |
|
178 |
|
|
179 |
} |
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 | ||
---|---|---|
43 | 43 |
import org.gvsig.expressionevaluator.Optimizer; |
44 | 44 |
import org.gvsig.expressionevaluator.SymbolTable; |
45 | 45 |
import org.gvsig.expressionevaluator.impl.DALFunctions; |
46 |
import org.gvsig.fmap.dal.DALLocator; |
|
47 |
import org.gvsig.fmap.dal.DataManager; |
|
48 | 46 |
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT; |
49 | 47 |
import org.gvsig.fmap.dal.DataStore; |
50 | 48 |
import org.gvsig.expressionevaluator.ExpressionEvaluator; |
... | ... | |
56 | 54 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
57 | 55 |
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureExpressionEvaluator; |
58 | 56 |
import org.gvsig.expressionevaluator.Code.Callable; |
59 |
import org.gvsig.expressionevaluator.spi.AbstractFunction; |
|
60 | 57 |
import org.gvsig.fmap.dal.feature.impl.DefaultFeatureQueryOrder; |
61 | 58 |
|
62 | 59 |
/** |
... | ... | |
64 | 61 |
* @author jjdelcerro |
65 | 62 |
*/ |
66 | 63 |
@SuppressWarnings("UseSpecificCatch") |
67 |
public class SelectFunction |
|
68 |
extends AbstractFunction |
|
69 |
implements Optimizer.FunctionOptimizer |
|
70 |
{ |
|
64 |
public class SelectFunction |
|
65 |
extends AbstractSelectFunction |
|
66 |
implements Optimizer.FunctionOptimizer { |
|
71 | 67 |
|
72 |
public SelectFunction() { |
|
73 |
super(DALFunctions.GROUP_DATA_ACCESS, |
|
74 |
FUNCTION_SELECT, |
|
75 |
Range.is(6), |
|
76 |
"Returns a list of features of the table by applying the filter, order and limit indicated.\n"+
|
|
77 |
"The syntax is:\n\n"+
|
|
78 |
"SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;\n\n"+
|
|
79 |
"Indicate a filter expression with WHERE, an order or LIMIT is optional.\n"+
|
|
80 |
"You can use an asterisk or enter the column names you want to retrieve separated by commas.\n"+
|
|
81 |
"The SELECT statement must always end with a semicolon.", |
|
82 |
"SELECT * FROM table WHERE boolean_expression ORDER BY order_column LIMIT limit;", |
|
83 |
new String[]{ |
|
84 |
"column_names/asterisk - Names of the columns table to retrieve.", |
|
85 |
"table_name - Name of the table", |
|
86 |
"filter - boolean expression to apply as filter", |
|
87 |
"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.", |
|
88 |
"limit - Maximum number of features to return" |
|
89 |
}, |
|
90 |
"List", |
|
91 |
true |
|
92 |
); |
|
93 |
} |
|
68 |
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 |
}
|
|
94 | 90 |
|
95 |
@Override |
|
96 |
public boolean isHidden() { |
|
97 |
return false; |
|
98 |
} |
|
91 |
@Override
|
|
92 |
public boolean isHidden() {
|
|
93 |
return false;
|
|
94 |
}
|
|
99 | 95 |
|
100 |
@Override |
|
101 |
public boolean allowConstantFolding() { |
|
102 |
return false; |
|
103 |
} |
|
96 |
@Override
|
|
97 |
public boolean allowConstantFolding() {
|
|
98 |
return false;
|
|
99 |
}
|
|
104 | 100 |
|
105 |
@Override |
|
106 |
public boolean useArgumentsInsteadObjects() { |
|
107 |
return true; |
|
108 |
} |
|
109 |
|
|
110 |
@Override |
|
111 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
|
112 |
throw new UnsupportedOperationException(); |
|
113 |
} |
|
114 |
|
|
115 |
private static final int COLUMNS = 0; |
|
116 |
private static final int TABLE = 1; |
|
117 |
private static final int WHERE = 2; |
|
118 |
private static final int ORDER = 3; |
|
119 |
private static final int ORDER_MODE = 4; |
|
120 |
private static final int LIMIT = 5; |
|
121 |
|
|
122 |
|
|
123 |
private Callable getTupleOrNull(Codes args, int argn) { |
|
124 |
Code code = args.get(argn); |
|
125 |
if( code.code()==Code.CONSTANT ) { |
|
126 |
if( ((Code.Constant)code).value()!=null ) { |
|
127 |
throw new ExpressionRuntimeException("Tupple or null expected in argument "+argn+ " of function '" + FUNCTION_SELECT + "'."); |
|
128 |
} |
|
129 |
return null; |
|
101 |
@Override |
|
102 |
public boolean useArgumentsInsteadObjects() { |
|
103 |
return true; |
|
130 | 104 |
} |
131 |
if( code.code()!=Code.CALLABLE ) { |
|
132 |
throw new ExpressionRuntimeException("Tupple or null expected in argument "+argn+ " of function '" + FUNCTION_SELECT + "'."); |
|
133 |
} |
|
134 |
Callable caller = (Callable) code; |
|
135 |
if( !StringUtils.equalsIgnoreCase(FUNCTION_TUPLE, caller.name()) ) { |
|
136 |
throw new ExpressionRuntimeException("Tupple or null expected in argument "+argn+ " of function '" + FUNCTION_SELECT + "'."); |
|
137 |
} |
|
138 |
return caller; |
|
139 |
} |
|
140 |
|
|
141 |
@Override |
|
142 |
public Object call(Interpreter interpreter, Codes args) throws Exception { |
|
143 | 105 |
|
144 |
Code.Identifier storeName = (Code.Identifier) args.get(TABLE); |
|
145 |
Code columns = getTupleOrNull(args, COLUMNS); |
|
146 |
Code where = args.get(WHERE); |
|
147 |
if( where.code()==Code.CONSTANT ) { |
|
148 |
if( ((Code.Constant)where).value()==null ) { |
|
149 |
where = null; |
|
150 |
} |
|
106 |
@Override |
|
107 |
public Object call(Interpreter interpreter, Object[] args) throws Exception { |
|
108 |
throw new UnsupportedOperationException(); |
|
151 | 109 |
} |
152 |
Number limit = (Number) getObject(interpreter, args, LIMIT); |
|
153 |
Callable order = getTupleOrNull(args, ORDER); |
|
154 |
Callable order_mode = getTupleOrNull(args, ORDER_MODE); |
|
155 |
|
|
156 |
FeatureQueryOrder queryOrder = null; |
|
157 |
if( order!=null || order_mode!=null ) { |
|
158 |
for( int n=0 ; n<order.parameters().size(); n++) { |
|
159 |
String member = (String) interpreter.run(order.parameters().get(n)); |
|
160 |
Boolean mode = (Boolean) interpreter.run(order_mode.parameters().get(n)); |
|
161 |
if( queryOrder == null ) { |
|
162 |
queryOrder = new DefaultFeatureQueryOrder(); |
|
163 |
} |
|
164 |
queryOrder.add(member, mode); |
|
165 |
} |
|
166 |
} |
|
167 |
// FIXME: add columns to query.addAttributeName() |
|
168 |
try { |
|
169 |
DataStore store = this.getStore(storeName.name()); |
|
170 |
if (store == null ) { |
|
171 |
throw new ExpressionRuntimeException("Cant locate the store '" + storeName + "' in function '" + FUNCTION_SELECT + "'."); |
|
172 |
} |
|
173 |
if (!(store instanceof FeatureStore)) { |
|
174 |
throw new ExpressionRuntimeException("The store'" + storeName + "' is not valid for function '" + FUNCTION_SELECT + "', a FeatureStore is required."); |
|
175 |
} |
|
176 |
FeatureStore featureStore = (FeatureStore) store; |
|
177 |
List<Feature> features; |
|
178 |
if (where == null && queryOrder == null && limit==null ) { |
|
179 |
features = featureStore.getFeatures(); |
|
180 |
} else { |
|
181 |
FeatureQuery query = featureStore.createFeatureQuery(); |
|
182 |
if (where != null) { |
|
183 |
Code where2 = removeOuterTablesReferences(interpreter, where); |
|
184 |
ExpressionEvaluator filter = new DefaultFeatureExpressionEvaluator(where2.toString()); |
|
185 |
filter.toSymbolTable().addSymbolTable(interpreter.getSymbolTable()); |
|
186 |
query.addFilter(filter); |
|
187 |
} |
|
188 |
if (queryOrder != null) { |
|
189 |
query.getOrder().copyFrom(queryOrder); |
|
190 |
} |
|
191 |
if( limit!=null ) { |
|
192 |
query.setLimit(limit.longValue()); |
|
193 |
} |
|
194 |
query.retrievesAllAttributes(); |
|
195 |
features = featureStore.getFeatures(query); |
|
196 |
} |
|
197 |
return features; |
|
198 |
} catch (ExpressionRuntimeException ex) { |
|
199 |
throw ex; |
|
200 |
} catch (Exception ex) { |
|
201 |
throw new ExpressionRuntimeException("Problems calling '" + FUNCTION_SELECT + "' function", ex); |
|
202 |
} |
|
203 |
} |
|
204 | 110 |
|
205 |
protected DataStore getStore(String storeName) { |
|
206 |
DataManager dataManager = DALLocator.getDataManager(); |
|
207 |
DataStore store = dataManager.getStoresRepository().getStore(storeName); |
|
208 |
return store; |
|
209 |
} |
|
111 |
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; |
|
210 | 117 |
|
211 |
public static Code removeOuterTablesReferences(Interpreter interpreter, Code where) { |
|
212 |
try { |
|
213 |
SymbolTable symbolTable = interpreter.getSymbolTable(); |
|
214 |
TableAttributeHandler table = (TableAttributeHandler) symbolTable.value(SYMBOL_CURRENT_TABLE); |
|
215 |
List<Pair<Code,Code>>replaces = new ArrayList<>(); |
|
216 |
CodeBuilder codeBuilder = ExpressionUtils.createCodeBuilder(); |
|
217 |
final Set<Code> replacesToASkip = new HashSet<>(); |
|
218 |
Code where2 = where.clone(); |
|
219 |
where2.accept((Object o) -> { |
|
220 |
if( o == null ) { |
|
221 |
return; |
|
222 |
} |
|
223 |
Code code = (Code) o; |
|
224 |
switch(code.code()) { |
|
225 |
case Code.CALLABLE: |
|
226 |
Code.Callable caller = (Code.Callable) code; |
|
227 |
if( StringUtils.equalsIgnoreCase(caller.name(),FUNCTION_GETATTR) ) { |
|
228 |
Codes args = caller.parameters(); |
|
229 |
Code arg0 = args.get(0); |
|
230 |
Code arg1 = args.get(1); |
|
231 |
replacesToASkip.add(arg1); |
|
232 |
if( arg0 instanceof Code.Identifier && arg1 instanceof Code.Constant ) { |
|
233 |
Object tt = symbolTable.value(((Code.Identifier)arg0).name()); |
|
234 |
if( tt instanceof TableAttributeHandler && |
|
235 |
StringUtils.equalsIgnoreCase(((TableAttributeHandler)tt).getName(), table.getName()) ) { |
|
236 |
String columnName = Objects.toString(((Code.Constant)arg1).value(), null); |
|
237 |
if( columnName!=null ) { |
|
238 |
Object value = table.get(columnName); |
|
239 |
replaces.add( |
|
240 |
new ImmutablePair<>( |
|
241 |
caller, |
|
242 |
codeBuilder.constant(value) |
|
243 |
) |
|
244 |
); |
|
245 |
} |
|
246 |
} |
|
247 |
} |
|
118 |
@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(); |
|
248 | 135 |
} |
249 |
break; |
|
136 |
queryOrder.add(member, mode); |
|
137 |
} |
|
250 | 138 |
} |
251 |
}); |
|
252 |
where2.accept((Object o) -> { |
|
253 |
if( o == null ) { |
|
254 |
return; |
|
139 |
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); |
|
255 | 169 |
} |
256 |
Code code = (Code) o; |
|
257 |
if( replacesToASkip.contains(code) ) { |
|
258 |
return; |
|
259 |
} |
|
260 |
switch(code.code()) { |
|
261 |
case Code.IDENTIFIER: |
|
262 |
Code.Identifier id = (Code.Identifier)code; |
|
263 |
if( symbolTable.exists(id.name()) ) { |
|
264 |
Object value = symbolTable.value(id.name()); |
|
265 |
replaces.add( |
|
266 |
new ImmutablePair<>( |
|
267 |
id, |
|
268 |
codeBuilder.constant(value) |
|
269 |
) |
|
270 |
); |
|
271 |
} |
|
272 |
break; |
|
273 |
} |
|
274 |
}); |
|
275 |
if( replaces.isEmpty() ) { |
|
276 |
return where; |
|
277 |
} |
|
278 |
for (Pair<Code, Code> replace : replaces) { |
|
279 |
if( replace!=null ) { |
|
280 |
where2.replace(replace.getLeft(), replace.getRight()); |
|
281 |
} |
|
282 |
} |
|
283 |
return where2; |
|
284 |
} catch (Exception ex) { |
|
285 |
throw new ExpressionRuntimeException("Can't remove references to outer tables.", ex); |
|
286 | 170 |
} |
287 |
} |
|
288 | 171 |
|
289 |
@Override |
|
290 |
public Code optimize(Optimizer optimizer, Callable caller) { |
|
291 |
return caller; // Don't optimize SELECT |
|
292 |
} |
|
172 |
@Override
|
|
173 |
public Code optimize(Optimizer optimizer, Callable caller) {
|
|
174 |
return caller; // Don't optimize SELECT
|
|
175 |
}
|
|
293 | 176 |
|
294 | 177 |
} |
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/grammars/DataAccessGrammarFactory.java | ||
---|---|---|
114 | 114 |
args.add(columns); |
115 | 115 |
|
116 | 116 |
Code table = context.getCode("TABLE"); |
117 |
args.add(codeBuilder.identifier((String) ((Code.Constant) table).value()));
|
|
117 |
args.add(table);
|
|
118 | 118 |
|
119 | 119 |
Code where = context.getCode("WHERE"); |
120 | 120 |
if (where == null) { |
... | ... | |
207 | 207 |
Code table = context.getCode("table"); |
208 | 208 |
Code where = context.getCode("where"); |
209 | 209 |
|
210 |
args.add(codeBuilder.identifier((String) ((Code.Constant) table).value()));
|
|
210 |
args.add(table);
|
|
211 | 211 |
|
212 | 212 |
if (where == null) { |
213 | 213 |
args.add(codeBuilder.constant(null)); |
... | ... | |
337 | 337 |
.addRuleOnFalse(stmt.require_identifiers(",").capture_as("COLUMNS")) |
338 | 338 |
); |
339 | 339 |
stmt.addRule(stmt.require_any_token("FROM")); |
340 |
stmt.addRule(stmt.require_identifier().capture_as("TABLE"));
|
|
340 |
stmt.addRule(stmt.require_expression().capture_as("TABLE"));
|
|
341 | 341 |
stmt.addRule(stmt.optional_any_token("WHERE") |
342 | 342 |
.addRuleOnTrue(stmt.require_expression().capture_as("WHERE")) |
343 | 343 |
); |
... | ... | |
368 | 368 |
return stmt; |
369 | 369 |
} |
370 | 370 |
|
371 |
|
|
371 | 372 |
private Statement createInsertIntoTableSelectStatement(Grammar theGrammar) { |
372 | 373 |
Statement stmt; |
373 | 374 |
// |
... | ... | |
448 | 449 |
stmt.addRule(stmt.require_any_token("*")); |
449 | 450 |
stmt.addRule(stmt.require_any_token(")")); |
450 | 451 |
stmt.addRule(stmt.require_any_token("FROM")); |
451 |
stmt.addRule(stmt.require_identifier().capture_as("TABLE"));
|
|
452 |
stmt.addRule(stmt.require_expression().capture_as("TABLE"));
|
|
452 | 453 |
stmt.addRule(stmt.optional_any_token("WHERE") |
453 | 454 |
.addRuleOnTrue(stmt.require_expression().capture_as("WHERE")) |
454 | 455 |
); |
Also available in: Unified diff