Revision 46505 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.db/org.gvsig.fmap.dal.db.jdbc/src/main/java/org/gvsig/fmap/dal/store/jdbc2/spi/operations/CountOperation.java
CountOperation.java | ||
---|---|---|
29 | 29 |
import java.util.ArrayList; |
30 | 30 |
import java.util.List; |
31 | 31 |
import org.apache.commons.lang3.StringUtils; |
32 |
import org.gvsig.expressionevaluator.Code; |
|
33 |
import org.gvsig.expressionevaluator.Expression; |
|
34 | 32 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
35 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
|
36 | 33 |
import org.gvsig.fmap.dal.SQLBuilder.SelectBuilder; |
37 | 34 |
import org.gvsig.fmap.dal.exception.DataException; |
38 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
|
39 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
|
40 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
41 |
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
|
42 |
import org.gvsig.fmap.dal.feature.FeatureExtraColumns; |
|
43 | 35 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
44 | 36 |
import org.gvsig.fmap.dal.feature.FeatureType; |
45 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
|
46 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
|
47 | 37 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
48 | 38 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
39 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
|
49 | 40 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
50 | 41 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
42 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
|
51 | 43 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
52 | 44 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_JDBCHELPER; |
53 | 45 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_QUERY; |
54 | 46 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_SYMBOLTABLE; |
55 | 47 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
56 |
import org.gvsig.tools.dynobject.DynField; |
|
57 |
import org.gvsig.tools.evaluator.Evaluator; |
|
48 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.getAllExtraColumns; |
|
49 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process2_ComputedFields; |
|
50 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process3_Where; |
|
51 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process4_Aggregates; |
|
52 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process5_GroupBys; |
|
58 | 53 |
|
59 | 54 |
public class CountOperation extends AbstractConnectionOperation { |
60 | 55 |
|
... | ... | |
93 | 88 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
94 | 89 |
|
95 | 90 |
SelectBuilder select = sqlbuilder.select(); |
91 |
select.from().table() |
|
92 |
.database(this.table.getDatabase()) |
|
93 |
.schema(this.table.getSchema()) |
|
94 |
.name(this.table.getTable()); |
|
95 |
select.from().subquery(this.table.getSubquery()); |
|
96 |
|
|
96 | 97 |
List<ExpressionBuilder.Value> valuesToRemoveFeatureType = new ArrayList<>(); |
97 | 98 |
List<String> extraColumnNames = new ArrayList<>(); |
98 | 99 |
|
99 |
if (this.query != null && (query.hasGroupByColumns() || query.hasAggregateFunctions())) {
|
|
100 |
if (this.query != null && (query.hasAggregateFunctions() || query.hasGroupByColumns())) {
|
|
100 | 101 |
JDBCSQLBuilderBase subsqlbuilder = this.createSQLBuilder(); |
101 | 102 |
SelectBuilder subselect = subsqlbuilder.select(); |
102 |
|
|
103 |
subselect.column().value(subsqlbuilder.count().all()); |
|
104 |
for (FeatureAttributeDescriptor attr : this.featureType) { |
|
105 |
if (attr.isComputed()) { |
|
106 |
if (extraColumnNames != null && !extraColumnNames.contains(attr.getName())) { |
|
107 |
extraColumnNames.add(attr.getName()); |
|
108 |
} |
|
109 |
if (attr.getRelationType() == DynField.RELATION_TYPE_NONE) { |
|
110 |
FeatureAttributeEmulator attrEmulator = attr.getFeatureAttributeEmulator(); |
|
111 |
if (attrEmulator instanceof FeatureAttributeEmulatorExpression) { |
|
112 |
FeatureAttributeEmulatorExpression x = (FeatureAttributeEmulatorExpression) attrEmulator; |
|
113 |
Expression exp = x.getExpression(); |
|
103 |
subselect.from().table() |
|
104 |
.database(this.table.getDatabase()) |
|
105 |
.schema(this.table.getSchema()) |
|
106 |
.name(this.table.getTable()); |
|
107 |
subselect.from().subquery(this.table.getSubquery()); |
|
114 | 108 |
|
115 |
// if (query != null && (query.hasGroupByColumns() || query.hasAggregateFunctions())) { |
|
116 |
String aggregate = query.getAggregate(this.table.getTable(), attr.getName()); |
|
117 |
if (this.query.isAGroupByColumn(attr.getName())) { |
|
118 |
if (!subselect.has_column(attr.getName())) { |
|
119 |
subselect.column().value(exp.getCode().toValue()).as(attr.getName()); |
|
120 |
} |
|
121 |
|
|
122 |
} else if (aggregate == null) { |
|
123 |
subselect.column().value(expbuilder.constant(null)).as(attr.getName()); |
|
124 |
} else { |
|
125 |
String fn = this.query.getAggregateFunctions().get(attr.getName()); |
|
126 |
ExpressionBuilder.Function aggregateExp = expbuilder.function(fn, exp.getCode().toValue()); |
|
127 |
if (!subselect.has_column(attr.getName())) { |
|
128 |
subselect.column().value(aggregateExp).as(attr.getName()); |
|
129 |
} |
|
130 |
|
|
131 |
} |
|
132 |
// } else { |
|
133 |
// if (exp != null && !exp.isEmpty() && this.helper.supportExpression(this.featureType, exp.getPhrase())) { |
|
134 |
// Code code = exp.getCode(); |
|
135 |
// subselect.column() |
|
136 |
// .value(code.toValue(expbuilder)) |
|
137 |
// .as(attr.getName()); |
|
138 |
// |
|
139 |
// } |
|
140 |
// |
|
141 |
// } |
|
142 |
} |
|
143 |
} |
|
144 |
} |
|
109 |
// ?sobra? |
|
110 |
process2_ComputedFields(helper, featureType, query, sqlbuilder, subselect, extraColumnNames); |
|
111 |
|
|
112 |
process3_Where(helper, featureType, query, sqlbuilder, subselect); |
|
113 |
process4_Aggregates(this.table, this.featureType, this.query, getAllExtraColumns(this.featureType, this.query), sqlbuilder, subselect, extraColumnNames); |
|
114 |
process5_GroupBys(this.table, this.featureType, this.query, getAllExtraColumns(this.featureType, this.query), sqlbuilder, subselect, extraColumnNames); |
|
115 |
|
|
116 |
if (!StringUtils.isEmpty(baseFilter)) { |
|
117 |
subselect.where().and(expbuilder.toValue(baseFilter)); |
|
145 | 118 |
} |
146 |
if (this.query != null) { |
|
147 |
FeatureExtraColumns extraColumns = this.query.getExtraColumn(); |
|
148 |
if (extraColumns != null && !extraColumns.isEmpty()) { |
|
149 |
for (EditableFeatureAttributeDescriptor attr : extraColumns.getColumns()) { |
|
150 |
if (!attr.isComputed()) { |
|
151 |
continue; |
|
152 |
} |
|
153 |
if (extraColumnNames != null && !extraColumnNames.contains(attr.getName())) { |
|
154 |
extraColumnNames.add(attr.getName()); |
|
155 |
} |
|
156 |
} |
|
157 |
} |
|
158 |
} |
|
159 | 119 |
|
160 |
if (this.query.hasGroupByColumns() || query.hasAggregateFunctions()) { |
|
161 |
subselect.from().table() |
|
162 |
.database(this.table.getDatabase()) |
|
163 |
.schema(this.table.getSchema()) |
|
164 |
.name(this.table.getTable()); |
|
165 |
subselect.from().subquery(this.table.getSubquery()); |
|
166 |
Evaluator filter = query == null ? null : query.getFilter(); |
|
167 |
if (filter != null) { |
|
168 |
String sqlfilter = filter.getSQL(); |
|
169 |
if (!StringUtils.isEmpty(sqlfilter)) { |
|
170 |
if (this.helper.supportFilter(this.featureType, filter)) { |
|
171 |
subselect.where().set(expbuilder.toValue(sqlfilter)); |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
if (!StringUtils.isEmpty(baseFilter)) { |
|
176 |
subselect.where().and(expbuilder.toValue(baseFilter)); |
|
177 |
} |
|
178 |
|
|
179 |
|
|
180 |
List<String> groupbyColumns = query == null ? null : query.getGroupByColumns(); |
|
181 |
if (groupbyColumns != null && !groupbyColumns.isEmpty()) { |
|
182 |
for (String columnName : groupbyColumns) { |
|
183 |
if (this.featureType.getAttributeDescriptor(columnName) != null) { |
|
184 |
subselect.group_by(expbuilder.column(columnName)); |
|
185 |
} else if(this.featureType.getExtraColumns().get(columnName)!=null) { |
|
186 |
ExpressionBuilder.Variable col = expbuilder.column(columnName); |
|
187 |
subselect.group_by(col); |
|
188 |
EditableFeatureAttributeDescriptor attr = this.featureType.getExtraColumns().get(columnName); // TODO no deberia? |
|
189 |
Expression exp = ((FeatureAttributeEmulatorExpression) attr.getFeatureAttributeEmulator()).getExpression(); |
|
190 |
subselect.column().value(exp.getCode().toValue()).as(columnName); |
|
191 |
} else { |
|
192 |
try { |
|
193 |
try { |
|
194 |
Code groupByColumnCode = ExpressionUtils.compile(columnName); |
|
195 |
// if (groupByColumnCode.code() == Code.CALLABLE) { |
|
196 |
// Code.Callable callable = (Code.Callable) groupByColumnCode; |
|
197 |
// if (callable.name().equalsIgnoreCase(FUNCTION_LET)) { |
|
198 |
// Code exp = callable.parameters().get(1); |
|
199 |
// Code name = callable.parameters().get(0); |
|
200 |
// subselect.column().value(exp.toValue()) |
|
201 |
// .as((String) ((Code.Constant) name).value()); |
|
202 |
// // nombre que se pone en la parte del groupby debe de ser el nombre de la var del set |
|
203 |
// groupByColumnCode = exp; |
|
204 |
// } |
|
205 |
// } |
|
206 |
subselect.group_by(groupByColumnCode.toValue()); |
|
207 |
} catch (Exception ex) { |
|
208 |
throw new RuntimeException("Not able to create column by expression in groupby query", ex); |
|
209 |
} |
|
210 |
} catch (Exception ex) { |
|
211 |
throw new RuntimeException("Not able to create column by expression in groupby query", ex); |
|
212 |
} |
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
subsqlbuilder.setProperties( |
|
217 |
ExpressionBuilder.Variable.class, |
|
218 |
PROP_FEATURE_TYPE, this.featureType, |
|
219 |
PROP_TABLE, table, |
|
220 |
PROP_SYMBOLTABLE, this.query == null ? null : this.query.getSymbolTable(), |
|
221 |
PROP_JDBCHELPER, this.helper, |
|
222 |
PROP_QUERY, this.query |
|
223 |
); |
|
224 |
for (ExpressionBuilder.Value value : valuesToRemoveFeatureType) { |
|
225 |
value.setProperty(PROP_FEATURE_TYPE, null); |
|
226 |
} |
|
227 |
this.helper.expandCalculedColumns(subsqlbuilder); |
|
228 |
this.helper.processSpecialFunctions(subsqlbuilder, featureType, extraColumnNames); |
|
229 |
String subsql = subselect.toString(); |
|
230 |
select.from().table() |
|
231 |
.database(this.table.getDatabase()) |
|
232 |
.schema(this.table.getSchema()) |
|
233 |
.name(this.table.getTable()); |
|
234 |
select.from().subquery(subsql); |
|
120 |
subsqlbuilder.setProperties( |
|
121 |
ExpressionBuilder.Variable.class, |
|
122 |
PROP_FEATURE_TYPE, this.featureType, |
|
123 |
PROP_TABLE, table, |
|
124 |
PROP_SYMBOLTABLE, this.query == null ? null : this.query.getSymbolTable(), |
|
125 |
PROP_JDBCHELPER, this.helper, |
|
126 |
PROP_QUERY, this.query |
|
127 |
); |
|
128 |
for (ExpressionBuilder.Value value : valuesToRemoveFeatureType) { |
|
129 |
value.setProperty(PROP_FEATURE_TYPE, null); |
|
235 | 130 |
} |
236 |
} else { |
|
237 |
select.column().value(sqlbuilder.count().all()); |
|
131 |
this.helper.expandCalculedColumns(subsqlbuilder); |
|
132 |
this.helper.processSpecialFunctions(subsqlbuilder, featureType, extraColumnNames); |
|
133 |
String subsql = subselect.toString(); |
|
238 | 134 |
select.from().table() |
239 | 135 |
.database(this.table.getDatabase()) |
240 | 136 |
.schema(this.table.getSchema()) |
241 | 137 |
.name(this.table.getTable()); |
242 |
select.from().subquery(this.table.getSubquery()); |
|
138 |
select.from().subquery(subsql); |
|
139 |
} else { |
|
140 |
process3_Where(helper, featureType, query, sqlbuilder, select); |
|
243 | 141 |
if (!StringUtils.isEmpty(baseFilter)) { |
244 | 142 |
sqlbuilder.select().where().set(expbuilder.custom(baseFilter)); |
245 | 143 |
} |
246 |
if (this.query != null) { |
|
247 |
if (this.query.getFilter() != null && !StringUtils.isBlank(this.query.getFilter().getSQL())) { |
|
248 |
// El and() hace un set() si no hay un filtro previo |
|
249 |
select.where().and(expbuilder.toValue(this.query.getFilter().getSQL())); |
|
250 |
} |
|
251 |
} |
|
252 | 144 |
} |
253 | 145 |
|
254 | 146 |
|
255 | 147 |
select.remove_all_columns(); |
256 | 148 |
select.column().value(sqlbuilder.count().all()); |
257 |
// sqlbuilder.setProperties( |
|
258 |
// ExpressionBuilder.Variable.class, |
|
259 |
// PROP_TABLE, table |
|
260 |
// ); |
|
261 | 149 |
sqlbuilder.setProperties( |
262 | 150 |
null, |
263 | 151 |
PROP_FEATURE_TYPE, this.featureType, |
... | ... | |
273 | 161 |
select.column().value(sqlbuilder.count().all()); |
274 | 162 |
|
275 | 163 |
String sql = select.toString(); |
164 |
LOGGER.debug(sql); |
|
276 | 165 |
return sql; |
277 | 166 |
} |
278 | 167 |
|
Also available in: Unified diff