svn-gvsig-desktop / 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 @ 45711
History | View | Annotate | Download (10.3 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.fmap.dal.store.jdbc2.spi.operations; |
25 |
|
26 |
import java.sql.Connection; |
27 |
import java.sql.ResultSet; |
28 |
import java.sql.SQLException; |
29 |
import java.sql.Statement; |
30 |
import java.util.List; |
31 |
import org.apache.commons.lang3.StringUtils; |
32 |
import org.gvsig.expressionevaluator.Code; |
33 |
import org.gvsig.expressionevaluator.Expression; |
34 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
35 |
import org.gvsig.expressionevaluator.ExpressionUtils; |
36 |
import org.gvsig.fmap.dal.SQLBuilder.SelectBuilder; |
37 |
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.FeatureQuery; |
41 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
42 |
import org.gvsig.fmap.dal.feature.FeatureType; |
43 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
44 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
45 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
46 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
47 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
48 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
49 |
import org.gvsig.tools.evaluator.Evaluator; |
50 |
|
51 |
public class CountOperation extends AbstractConnectionOperation { |
52 |
|
53 |
private final TableReference table; |
54 |
private final String baseFilter; |
55 |
private final FeatureQuery query; |
56 |
private final FeatureType featureType; |
57 |
|
58 |
public CountOperation(
|
59 |
JDBCHelper helper |
60 |
) { |
61 |
this(helper, null, null, null, null); |
62 |
} |
63 |
|
64 |
public CountOperation(
|
65 |
JDBCHelper helper, |
66 |
FeatureType featureType, |
67 |
TableReference table, |
68 |
String baseFilter,
|
69 |
FeatureQuery query |
70 |
) { |
71 |
super(helper);
|
72 |
this.featureType = featureType;
|
73 |
this.table = table;
|
74 |
this.baseFilter = baseFilter;
|
75 |
this.query = query;
|
76 |
} |
77 |
|
78 |
@Override
|
79 |
public final Object perform(Connection conn) throws DataException { |
80 |
return this.count(conn); |
81 |
} |
82 |
|
83 |
public String getSQL() { |
84 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
85 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
86 |
|
87 |
SelectBuilder select = sqlbuilder.select(); |
88 |
|
89 |
if (this.query != null && this.query.hasGroupByColumns()) { |
90 |
JDBCSQLBuilderBase subsqlbuilder = this.createSQLBuilder();
|
91 |
SelectBuilder subselect = subsqlbuilder.select(); |
92 |
if (this.query.hasGroupByColumns()) { |
93 |
if (this.query.hasOrder()) { |
94 |
FeatureQueryOrder order = query.getOrder(); |
95 |
if (order != null) { |
96 |
for (FeatureQueryOrder.FeatureQueryOrderMember member : order.members()) {
|
97 |
if (member.hasEvaluator()) {
|
98 |
String sqlorder = member.getEvaluator().getSQL();
|
99 |
if (!StringUtils.isEmpty(sqlorder)) {
|
100 |
subselect.order_by() |
101 |
.value(expbuilder.toValue(sqlorder)) |
102 |
.ascending(member.getAscending()); |
103 |
} |
104 |
} else {
|
105 |
subselect.order_by() |
106 |
.column(member.getAttributeName()) |
107 |
.ascending(member.getAscending()); |
108 |
} |
109 |
} |
110 |
} |
111 |
} |
112 |
subselect.column().value(subsqlbuilder.count().all()); |
113 |
subselect.from().table() |
114 |
.database(this.table.getDatabase())
|
115 |
.schema(this.table.getSchema())
|
116 |
.name(this.table.getTable());
|
117 |
subselect.from().subquery(this.table.getSubquery());
|
118 |
Evaluator filter = query == null ? null : query.getFilter(); |
119 |
if (filter != null) { |
120 |
String sqlfilter = filter.getSQL();
|
121 |
if (!StringUtils.isEmpty(sqlfilter)) {
|
122 |
if (this.helper.supportFilter(this.featureType, filter)) { |
123 |
subselect.where().set(expbuilder.toValue(sqlfilter)); |
124 |
} |
125 |
} |
126 |
} |
127 |
if (!StringUtils.isEmpty(baseFilter)) {
|
128 |
subselect.where().and(expbuilder.toValue(baseFilter)); |
129 |
} |
130 |
|
131 |
|
132 |
List<String> groupbyColumns = query == null ? null : query.getGroupByColumns(); |
133 |
if (groupbyColumns != null && !groupbyColumns.isEmpty()) { |
134 |
for (String columnName : groupbyColumns) { |
135 |
if (this.featureType.getAttributeDescriptor(columnName) != null) { |
136 |
subselect.group_by(expbuilder.column(columnName)); |
137 |
} else if(this.featureType.getExtraColumns().get(columnName)!=null) { |
138 |
EditableFeatureAttributeDescriptor attr = this.featureType.getExtraColumns().get(columnName);
|
139 |
subselect.group_by(expbuilder.column(columnName)); |
140 |
Expression exp = ((FeatureAttributeEmulatorExpression) attr.getFeatureAttributeEmulator()).getExpression();
|
141 |
subselect.column().value(exp.getCode().toValue()).as(columnName); |
142 |
} else {
|
143 |
try {
|
144 |
try {
|
145 |
Code groupByColumnCode = ExpressionUtils.compile(columnName); |
146 |
// if (groupByColumnCode.code() == Code.CALLABLE) {
|
147 |
// Code.Callable callable = (Code.Callable) groupByColumnCode;
|
148 |
// if (callable.name().equalsIgnoreCase(FUNCTION_LET)) {
|
149 |
// Code exp = callable.parameters().get(1);
|
150 |
// Code name = callable.parameters().get(0);
|
151 |
// subselect.column().value(exp.toValue())
|
152 |
// .as((String) ((Code.Constant) name).value());
|
153 |
// // nombre que se pone en la parte del groupby debe de ser el nombre de la var del set
|
154 |
// groupByColumnCode = exp;
|
155 |
// }
|
156 |
// }
|
157 |
subselect.group_by(groupByColumnCode.toValue()); |
158 |
} catch (Exception ex) { |
159 |
throw new RuntimeException("Not able to create column by expression in groupby query", ex); |
160 |
} |
161 |
} catch (Exception ex) { |
162 |
throw new RuntimeException("Not able to create column by expression in groupby query", ex); |
163 |
} |
164 |
} |
165 |
} |
166 |
} |
167 |
this.helper.processSpecialFunctions(subsqlbuilder, featureType, null); |
168 |
subsqlbuilder.setProperties( |
169 |
ExpressionBuilder.Variable.class, |
170 |
PROP_TABLE, table |
171 |
); |
172 |
String subsql = subselect.toString();
|
173 |
select.from().table() |
174 |
.database(this.table.getDatabase())
|
175 |
.schema(this.table.getSchema())
|
176 |
.name(this.table.getTable());
|
177 |
select.from().subquery(subsql); |
178 |
} |
179 |
} else {
|
180 |
select.column().value(sqlbuilder.count().all()); |
181 |
select.from().table() |
182 |
.database(this.table.getDatabase())
|
183 |
.schema(this.table.getSchema())
|
184 |
.name(this.table.getTable());
|
185 |
select.from().subquery(this.table.getSubquery());
|
186 |
if (!StringUtils.isEmpty(baseFilter)) {
|
187 |
sqlbuilder.select().where().set(expbuilder.custom(baseFilter)); |
188 |
} |
189 |
if (this.query != null) { |
190 |
if (this.query.getFilter() != null && !StringUtils.isBlank(this.query.getFilter().getSQL())) { |
191 |
// El and() hace un set() si no hay un filtro previo
|
192 |
select.where().and(expbuilder.toValue(this.query.getFilter().getSQL()));
|
193 |
} |
194 |
} |
195 |
} |
196 |
this.helper.processSpecialFunctions(sqlbuilder, featureType, null); |
197 |
|
198 |
select.remove_all_columns(); |
199 |
select.column().value(sqlbuilder.count().all()); |
200 |
|
201 |
sqlbuilder.setProperties( |
202 |
ExpressionBuilder.Variable.class, |
203 |
PROP_TABLE, table |
204 |
); |
205 |
|
206 |
String sql = select.toString();
|
207 |
return sql;
|
208 |
} |
209 |
|
210 |
public long count(Connection conn) throws DataException { |
211 |
|
212 |
String sql = this.getSQL(); |
213 |
Statement st = null; |
214 |
ResultSet rs = null; |
215 |
try {
|
216 |
st = conn.createStatement(); |
217 |
rs = JDBCUtils.executeQuery(st, sql); |
218 |
if (!rs.next()) {
|
219 |
return 0; |
220 |
} |
221 |
return rs.getLong(1); |
222 |
|
223 |
} catch (SQLException ex) { |
224 |
throw new JDBCSQLException(ex); |
225 |
} finally {
|
226 |
JDBCUtils.closeQuietly(st); |
227 |
JDBCUtils.closeQuietly(rs); |
228 |
} |
229 |
} |
230 |
|
231 |
} |