Revision 46678
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 | ||
---|---|---|
237 | 237 |
} |
238 | 238 |
|
239 | 239 |
if (limit != null) { |
240 |
select.limit(((Number) ((Code.Constant) limit).value()).longValue()); |
|
240 |
Object value = ((Code.Constant) limit).value(); |
|
241 |
if(value != null){ |
|
242 |
select.limit(((Number) value).longValue()); |
|
243 |
} |
|
241 | 244 |
} |
242 | 245 |
|
243 | 246 |
if (order != null || order_mode != null) { |
... | ... | |
263 | 266 |
PROP_TABLE, table |
264 | 267 |
); |
265 | 268 |
|
266 |
return select;
|
|
269 |
return builder.group(select);
|
|
267 | 270 |
} catch (Exception ex) { |
268 | 271 |
return super.toValue(builder, args); |
269 | 272 |
} |
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 | ||
---|---|---|
24 | 24 |
package org.gvsig.expressionevaluator.impl.function.dataaccess; |
25 | 25 |
|
26 | 26 |
import org.apache.commons.lang3.Range; |
27 |
import org.apache.commons.lang3.StringUtils; |
|
27 | 28 |
import org.gvsig.expressionevaluator.Code; |
28 | 29 |
import org.gvsig.expressionevaluator.Codes; |
30 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
31 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_COUNT; |
|
29 | 32 |
import org.gvsig.expressionevaluator.ExpressionRuntimeException; |
30 | 33 |
import org.gvsig.expressionevaluator.Interpreter; |
31 | 34 |
import org.gvsig.expressionevaluator.impl.DALFunctions; |
... | ... | |
34 | 37 |
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT_COUNT; |
35 | 38 |
import org.gvsig.fmap.dal.DataStore; |
36 | 39 |
import org.gvsig.expressionevaluator.ExpressionEvaluator; |
40 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
41 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_FEATURE_TYPE; |
|
42 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_SQLBUILDER; |
|
43 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_TABLE; |
|
37 | 44 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
38 | 45 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
39 | 46 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
47 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
40 | 48 |
import org.gvsig.fmap.dal.impl.expressionevaluator.DefaultFeatureExpressionEvaluator; |
41 | 49 |
import org.gvsig.tools.dispose.DisposeUtils; |
42 | 50 |
|
... | ... | |
61 | 69 |
"table - Name of the table", |
62 | 70 |
"filter - boolean expression with the filter to apply",}, |
63 | 71 |
"Long", |
64 |
false
|
|
72 |
true
|
|
65 | 73 |
); |
66 | 74 |
} |
67 | 75 |
|
... | ... | |
123 | 131 |
} |
124 | 132 |
} |
125 | 133 |
|
134 |
@Override |
|
135 |
public ExpressionBuilder.Value toValue(ExpressionBuilder builder, Codes args) { |
|
136 |
try { |
|
137 |
SQLBuilder sqlBuilder = (SQLBuilder) builder.getProperty(PROP_SQLBUILDER); |
|
138 |
if(sqlBuilder == null){ |
|
139 |
return super.toValue(builder, args); |
|
140 |
} |
|
141 |
FeatureType featureType = null; |
|
142 |
SQLBuilder.SelectBuilder select = sqlBuilder.createSelectBuilder(); |
|
143 |
String builderTableName = (String) builder.getProperty(SQLBuilder.PROP_TABLENAME); |
|
144 |
|
|
145 |
String storeName = this.getTableName(args, TABLE); |
|
146 |
Code where = this.getWhereCode(args, WHERE); |
|
147 |
|
|
148 |
if (storeName != null) { |
|
149 |
select.from().table().name(storeName); |
|
150 |
} |
|
151 |
|
|
152 |
SQLBuilder.TableNameBuilder table = select.from().table(); |
|
153 |
String tableName = table.getName(); |
|
154 |
|
|
155 |
select.column().value(builder.function(FUNCTION_COUNT, builder.custom("*"))); |
|
156 |
|
|
157 |
if (where != null) { |
|
158 |
ExpressionBuilder.Value value = where.toValue(builder); |
|
159 |
select.where().value(value); |
|
160 |
sqlBuilder.setProperties(value, null, SQLBuilder.PROP_ADD_TABLE_NAME_TO_COLUMNS, true); |
|
161 |
} |
|
162 |
|
|
163 |
if (featureType == null) { |
|
164 |
if (StringUtils.equalsIgnoreCase(builderTableName, tableName)) { |
|
165 |
featureType = (FeatureType) builder.getProperty(SQLBuilder.PROP_FEATURE_TYPE); |
|
166 |
} else { |
|
167 |
DataManager dataManager = DALLocator.getDataManager(); |
|
168 |
featureType = dataManager.getStoresRepository().getFeatureType(tableName); |
|
169 |
} |
|
170 |
} |
|
171 |
|
|
172 |
sqlBuilder.setProperties( |
|
173 |
select, |
|
174 |
null, |
|
175 |
PROP_FEATURE_TYPE, featureType, |
|
176 |
PROP_TABLE, table |
|
177 |
); |
|
178 |
|
|
179 |
return builder.group(select); |
|
180 |
} catch (Exception ex) { |
|
181 |
return super.toValue(builder, args); |
|
182 |
} |
|
183 |
} |
|
184 |
|
|
126 | 185 |
} |
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 | ||
---|---|---|
494 | 494 |
stmt.addRule(stmt.optional_any_token("WHERE") |
495 | 495 |
.addRuleOnTrue(stmt.require_expression().capture_as("WHERE")) |
496 | 496 |
); |
497 |
stmt.addRule(stmt.require_any_token(";"));
|
|
497 |
stmt.addRule(stmt.optional_any_token(";"));
|
|
498 | 498 |
stmt.code( |
499 | 499 |
FUNCTION_SELECT_COUNT, |
500 | 500 |
new SelectCountArgsBuilder() |
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 | ||
---|---|---|
3 | 3 |
import java.util.Objects; |
4 | 4 |
import junit.framework.TestCase; |
5 | 5 |
import org.gvsig.expressionevaluator.impl.expressionbuilder.formatters.DALFormatter; |
6 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_SQLBUILDER; |
|
7 |
import org.gvsig.fmap.dal.feature.spi.SQLBuilderBase; |
|
6 | 8 |
import org.gvsig.tools.library.impl.DefaultLibrariesInitializer; |
7 | 9 |
|
8 | 10 |
/** |
... | ... | |
84 | 86 |
checkEquals("testSelect", "SELECT(TUPLE(), \"countries\", NULL, TUPLE(), TUPLE(), NULL)", code.toString()); |
85 | 87 |
} |
86 | 88 |
|
89 |
public void testSelectToValue() { |
|
90 |
StringBuilder source = new StringBuilder(); |
|
91 |
source.append("SELECT * FROM countries;"); |
|
92 |
|
|
93 |
Compiler compiler = createCompiler(); |
|
94 |
|
|
95 |
Code code = compiler.compileExpression(source.toString()); |
|
96 |
link(code); |
|
97 |
|
|
98 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
|
99 |
builder.setProperty(PROP_SQLBUILDER, new SQLBuilderBase()); |
|
100 |
checkEquals("testSelectToValue", "( SELECT * FROM \"countries\" )", code.toValue(builder).toString()); |
|
101 |
} |
|
102 |
|
|
103 |
public void testSelectCount() { |
|
104 |
StringBuilder source = new StringBuilder(); |
|
105 |
source.append("SELECT COUNT(*) FROM countries;"); |
|
106 |
|
|
107 |
Compiler compiler = createCompiler(); |
|
108 |
|
|
109 |
Code code = compiler.compileExpression(source.toString()); |
|
110 |
link(code); |
|
111 |
checkEquals("testSelect", "SELECT_COUNT(\"countries\", NULL)", code.toString()); |
|
112 |
} |
|
113 |
|
|
114 |
public void testSelectCountToValue() { |
|
115 |
StringBuilder source = new StringBuilder(); |
|
116 |
source.append("SELECT COUNT(*) FROM countries;"); |
|
117 |
|
|
118 |
Compiler compiler = createCompiler(); |
|
119 |
|
|
120 |
Code code = compiler.compileExpression(source.toString()); |
|
121 |
link(code); |
|
122 |
|
|
123 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
|
124 |
builder.setProperty(PROP_SQLBUILDER, new SQLBuilderBase()); |
|
125 |
checkEquals("testSelectToValue", "( SELECT COUNT(*) FROM \"countries\" )", code.toValue(builder).toString()); |
|
126 |
} |
|
127 |
|
|
128 |
|
|
129 |
|
|
87 | 130 |
public void testSelect1() { |
88 | 131 |
StringBuilder source = new StringBuilder(); |
89 | 132 |
source.append("SELECT * FROM countries"); |
... | ... | |
199 | 242 |
checkEquals("testSelectWhere3", "BLOCK(LET('tableName', 'countries'), SELECT(TUPLE(), $HOSTEXPRESSION(\"tableName\", 'IN'), (\"countries\".\"LASTCENSUS\" > 0), TUPLE(), TUPLE(), NULL))", code.toString()); |
200 | 243 |
} |
201 | 244 |
|
245 |
public void testSelectWhereToValue3() { |
|
246 |
StringBuilder source = new StringBuilder(); |
|
247 |
source.append("BEGIN"); |
|
248 |
source.append(" SELECT * FROM countries "); |
|
249 |
source.append(" WHERE countries.LASTCENSUS > 0 ; "); |
|
250 |
source.append("END"); |
|
251 |
|
|
252 |
Compiler compiler = createCompiler(); |
|
253 |
|
|
254 |
Code code = compiler.compileExpression(source.toString()); |
|
255 |
link(code); |
|
256 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
|
257 |
builder.setProperty(PROP_SQLBUILDER, new SQLBuilderBase()); |
|
258 |
checkEquals("testSelectWhereToValue3", "BLOCK(( SELECT * FROM \"countries\" WHERE (\"countries\".LASTCENSUS > 0) ))", code.toValue(builder).toString()); |
|
259 |
} |
|
260 |
|
|
261 |
public void testSelectCountWhereToValue3() { |
|
262 |
StringBuilder source = new StringBuilder(); |
|
263 |
source.append("BEGIN"); |
|
264 |
source.append(" SELECT COUNT(*) FROM countries "); |
|
265 |
source.append(" WHERE countries.LASTCENSUS > 0 ; "); |
|
266 |
source.append("END"); |
|
267 |
|
|
268 |
Compiler compiler = createCompiler(); |
|
269 |
|
|
270 |
Code code = compiler.compileExpression(source.toString()); |
|
271 |
link(code); |
|
272 |
ExpressionBuilder builder = ExpressionUtils.createExpressionBuilder(); |
|
273 |
builder.setProperty(PROP_SQLBUILDER, new SQLBuilderBase()); |
|
274 |
checkEquals("testSelectCountWhereToValue3", "BLOCK(( SELECT COUNT(*) FROM \"countries\" WHERE (\"countries\".LASTCENSUS > 0) ))", code.toValue(builder).toString()); |
|
275 |
} |
|
276 |
|
|
202 | 277 |
public void testSelectOrder() { |
203 | 278 |
StringBuilder source = new StringBuilder(); |
204 | 279 |
source.append("SELECT * FROM countries "); |
Also available in: Unified diff