Revision 46050 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 | ||
---|---|---|
27 | 27 |
import java.sql.ResultSet; |
28 | 28 |
import java.sql.SQLException; |
29 | 29 |
import java.sql.Statement; |
30 |
import java.util.ArrayList; |
|
30 | 31 |
import java.util.List; |
31 | 32 |
import org.apache.commons.lang3.StringUtils; |
32 | 33 |
import org.gvsig.expressionevaluator.Code; |
... | ... | |
38 | 39 |
import org.gvsig.fmap.dal.exception.DataException; |
39 | 40 |
import org.gvsig.fmap.dal.expressionevaluator.FeatureAttributeEmulatorExpression; |
40 | 41 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
42 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
43 |
import org.gvsig.fmap.dal.feature.FeatureAttributeEmulator; |
|
41 | 44 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
42 | 45 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
43 | 46 |
import org.gvsig.fmap.dal.feature.FeatureType; |
... | ... | |
48 | 51 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
49 | 52 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
50 | 53 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_JDBCHELPER; |
54 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_QUERY; |
|
51 | 55 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_SYMBOLTABLE; |
52 | 56 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
57 |
import org.gvsig.tools.dynobject.DynField; |
|
53 | 58 |
import org.gvsig.tools.evaluator.Evaluator; |
54 | 59 |
|
55 | 60 |
public class CountOperation extends AbstractConnectionOperation { |
... | ... | |
89 | 94 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
90 | 95 |
|
91 | 96 |
SelectBuilder select = sqlbuilder.select(); |
97 |
ArrayList<ExpressionBuilder.Value> valuesToRemoveFeatureType = new ArrayList<>(); |
|
92 | 98 |
|
93 | 99 |
if (this.query != null && this.query.hasGroupByColumns()) { |
94 | 100 |
JDBCSQLBuilderBase subsqlbuilder = this.createSQLBuilder(); |
95 | 101 |
SelectBuilder subselect = subsqlbuilder.select(); |
102 |
|
|
103 |
subselect.column().value(subsqlbuilder.count().all()); |
|
104 |
for (FeatureAttributeDescriptor attr : this.featureType) { |
|
105 |
if (attr.isComputed()) { |
|
106 |
if (attr.getRelationType() == DynField.RELATION_TYPE_NONE) { |
|
107 |
FeatureAttributeEmulator attrEmulator = attr.getFeatureAttributeEmulator(); |
|
108 |
if (attrEmulator instanceof FeatureAttributeEmulatorExpression) { |
|
109 |
FeatureAttributeEmulatorExpression x = (FeatureAttributeEmulatorExpression) attrEmulator; |
|
110 |
Expression exp = x.getExpression(); |
|
111 |
|
|
112 |
if (query != null && query.hasGroupByColumns()) { |
|
113 |
String aggregate = query.getAggregate(this.table.getTable(), attr.getName()); |
|
114 |
if (this.query.isAGroupByColumn(attr.getName())) { |
|
115 |
if (!subselect.has_column(attr.getName())) { |
|
116 |
subselect.column().value(exp.getCode().toValue()).as(attr.getName()); |
|
117 |
} |
|
118 |
|
|
119 |
} else if (aggregate == null) { |
|
120 |
subselect.column().value(expbuilder.constant(null)).as(attr.getName()); |
|
121 |
} else { |
|
122 |
String fn = this.query.getAggregateFunctions().get(attr.getName()); |
|
123 |
ExpressionBuilder.Function aggregateExp = expbuilder.function(fn, exp.getCode().toValue()); |
|
124 |
if (!subselect.has_column(attr.getName())) { |
|
125 |
subselect.column().value(aggregateExp).as(attr.getName()); |
|
126 |
} |
|
127 |
|
|
128 |
} |
|
129 |
} else { |
|
130 |
if (exp != null && !exp.isEmpty() && this.helper.supportExpression(this.featureType, exp.getPhrase())) { |
|
131 |
Code code = exp.getCode(); |
|
132 |
subselect.column() |
|
133 |
.value(code.toValue(expbuilder)) |
|
134 |
.as(attr.getName()); |
|
135 |
|
|
136 |
} |
|
137 |
|
|
138 |
} |
|
139 |
} |
|
140 |
} |
|
141 |
} |
|
142 |
} |
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
96 | 148 |
if (this.query.hasGroupByColumns()) { |
97 | 149 |
if (this.query.hasOrder()) { |
98 | 150 |
FeatureQueryOrder order = query.getOrder(); |
... | ... | |
113 | 165 |
} |
114 | 166 |
} |
115 | 167 |
} |
116 |
subselect.column().value(subsqlbuilder.count().all()); |
|
117 | 168 |
subselect.from().table() |
118 | 169 |
.database(this.table.getDatabase()) |
119 | 170 |
.schema(this.table.getSchema()) |
... | ... | |
171 | 222 |
this.helper.processSpecialFunctions(subsqlbuilder, featureType, null); |
172 | 223 |
subsqlbuilder.setProperties( |
173 | 224 |
ExpressionBuilder.Variable.class, |
174 |
PROP_TABLE, table |
|
225 |
PROP_FEATURE_TYPE, this.featureType, |
|
226 |
PROP_TABLE, table, |
|
227 |
PROP_SYMBOLTABLE, this.query == null ? null : this.query.getSymbolTable(), |
|
228 |
PROP_JDBCHELPER, this.helper, |
|
229 |
PROP_QUERY, this.query |
|
175 | 230 |
); |
176 | 231 |
String subsql = subselect.toString(); |
177 | 232 |
select.from().table() |
... | ... | |
211 | 266 |
PROP_FEATURE_TYPE, this.featureType, |
212 | 267 |
PROP_TABLE, table, |
213 | 268 |
PROP_SYMBOLTABLE, this.query==null? null:this.query.getSymbolTable(), |
214 |
PROP_JDBCHELPER, this.helper |
|
269 |
PROP_JDBCHELPER, this.helper, |
|
270 |
PROP_QUERY, this.query |
|
215 | 271 |
); |
216 | 272 |
|
217 | 273 |
String sql = select.toString(); |
Also available in: Unified diff