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