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 @ 47787
History | View | Annotate | Download (8.5 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.ResultSet; |
27 |
import java.sql.SQLException; |
28 |
import java.sql.Statement; |
29 |
import java.util.ArrayList; |
30 |
import java.util.List; |
31 |
import org.apache.commons.lang3.StringUtils; |
32 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
33 |
import org.gvsig.fmap.dal.SQLBuilder; |
34 |
import static org.gvsig.fmap.dal.SQLBuilder.PROP_TABLENAME; |
35 |
import org.gvsig.fmap.dal.SQLBuilder.SelectBuilder; |
36 |
import org.gvsig.fmap.dal.exception.DataException; |
37 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
38 |
import org.gvsig.fmap.dal.feature.FeatureType; |
39 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
40 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
41 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
42 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
43 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
44 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
45 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
46 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_JDBCHELPER; |
47 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_QUERY; |
48 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_SYMBOLTABLE; |
49 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
50 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.getAllExtraColumns; |
51 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process2_ComputedFields; |
52 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process3_Where; |
53 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process4_Aggregates; |
54 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process5_GroupBys; |
55 |
|
56 |
public class CountOperation extends AbstractConnectionOperation { |
57 |
|
58 |
private final TableReference table; |
59 |
private final String baseFilter; |
60 |
private final FeatureQuery query; |
61 |
private final FeatureType featureType; |
62 |
|
63 |
public CountOperation(
|
64 |
JDBCHelper helper |
65 |
) { |
66 |
this(helper, null, null, null, null); |
67 |
} |
68 |
|
69 |
public CountOperation(
|
70 |
JDBCHelper helper, |
71 |
FeatureType featureType, |
72 |
TableReference table, |
73 |
String baseFilter,
|
74 |
FeatureQuery query |
75 |
) { |
76 |
super(helper);
|
77 |
this.featureType = featureType;
|
78 |
this.table = table;
|
79 |
this.baseFilter = baseFilter;
|
80 |
this.query = query;
|
81 |
} |
82 |
|
83 |
@Override
|
84 |
public final Object perform(JDBCConnection conn) throws DataException { |
85 |
return this.count(conn); |
86 |
} |
87 |
|
88 |
public String getSQL() { |
89 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
90 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
91 |
|
92 |
expbuilder.setProperty(PROP_FEATURE_TYPE, this.featureType);
|
93 |
expbuilder.setProperty(PROP_TABLE, table); |
94 |
expbuilder.setProperty(PROP_TABLENAME, table.getTable()); |
95 |
expbuilder.setProperty(PROP_SYMBOLTABLE, this.query==null? null:this.query.getSymbolTable()); |
96 |
expbuilder.setProperty(PROP_JDBCHELPER, this.helper);
|
97 |
expbuilder.setProperty(PROP_QUERY, this.query);
|
98 |
|
99 |
SelectBuilder select = sqlbuilder.select(); |
100 |
select.from().table() |
101 |
.database(this.table.getDatabase())
|
102 |
.schema(this.table.getSchema())
|
103 |
.name(this.table.getTable());
|
104 |
select.from().subquery(this.table.getSubquery());
|
105 |
|
106 |
List<ExpressionBuilder.Value> valuesToRemoveFeatureType = new ArrayList<>(); |
107 |
List<String> extraColumnNames = new ArrayList<>(); |
108 |
|
109 |
if (this.query != null && (query.hasAggregateFunctions() || query.hasGroupByColumns())) { |
110 |
JDBCSQLBuilderBase subsqlbuilder = this.createSQLBuilder();
|
111 |
SelectBuilder subselect = subsqlbuilder.select(); |
112 |
subselect.from().table() |
113 |
.database(this.table.getDatabase())
|
114 |
.schema(this.table.getSchema())
|
115 |
.name(this.table.getTable());
|
116 |
subselect.from().subquery(this.table.getSubquery());
|
117 |
|
118 |
// ?sobra?
|
119 |
// process2_ComputedFields(helper, featureType, query, sqlbuilder, subselect, extraColumnNames);
|
120 |
|
121 |
process3_Where(helper, featureType, query, sqlbuilder, subselect); |
122 |
process4_Aggregates(this.table, this.featureType, this.query, getAllExtraColumns(this.featureType, this.query), sqlbuilder, subselect, extraColumnNames); |
123 |
process5_GroupBys(this.table, this.featureType, this.query, getAllExtraColumns(this.featureType, this.query), sqlbuilder, subselect, extraColumnNames); |
124 |
|
125 |
if (!StringUtils.isEmpty(baseFilter)) {
|
126 |
subselect.where().and(expbuilder.toValue(baseFilter)); |
127 |
} |
128 |
if(subselect.getColumns().isEmpty()){
|
129 |
subselect.column().value(expbuilder.constant(1)).as("$ANY_VALUE$"); |
130 |
} |
131 |
|
132 |
subsqlbuilder.setProperties( |
133 |
ExpressionBuilder.Variable.class, |
134 |
PROP_FEATURE_TYPE, this.featureType,
|
135 |
PROP_TABLE, table, |
136 |
PROP_SYMBOLTABLE, this.query == null ? null : this.query.getSymbolTable(), |
137 |
PROP_JDBCHELPER, this.helper,
|
138 |
PROP_QUERY, this.query
|
139 |
); |
140 |
for (ExpressionBuilder.Value value : valuesToRemoveFeatureType) {
|
141 |
value.setProperty(PROP_FEATURE_TYPE, null);
|
142 |
} |
143 |
this.helper.expandCalculedColumns(subsqlbuilder);
|
144 |
this.helper.processSpecialFunctions(subsqlbuilder, featureType, extraColumnNames, query);
|
145 |
String subsql = StringUtils.trim(subselect.toString());
|
146 |
select.from().table() |
147 |
.database(this.table.getDatabase())
|
148 |
.schema(this.table.getSchema())
|
149 |
.name(this.table.getTable());
|
150 |
select.from().subquery(subsql); |
151 |
} else {
|
152 |
process3_Where(helper, featureType, query, sqlbuilder, select); |
153 |
if (!StringUtils.isEmpty(baseFilter)) {
|
154 |
sqlbuilder.select().where().and(expbuilder.custom(baseFilter)); |
155 |
} |
156 |
} |
157 |
|
158 |
|
159 |
select.remove_all_columns(); |
160 |
select.column().value(sqlbuilder.count().all()); |
161 |
sqlbuilder.setProperties( |
162 |
null,
|
163 |
PROP_FEATURE_TYPE, this.featureType,
|
164 |
PROP_TABLE, table, |
165 |
PROP_SYMBOLTABLE, this.query==null? null:this.query.getSymbolTable(), |
166 |
PROP_JDBCHELPER, this.helper,
|
167 |
PROP_QUERY, this.query
|
168 |
); |
169 |
this.helper.expandCalculedColumns(sqlbuilder);
|
170 |
this.helper.processSpecialFunctions(sqlbuilder, featureType, null, query); |
171 |
|
172 |
select.remove_all_columns(); |
173 |
select.column().value(sqlbuilder.count().all()); |
174 |
|
175 |
String sql = StringUtils.trim(select.toString());
|
176 |
LOGGER.debug(sql); |
177 |
return sql;
|
178 |
} |
179 |
|
180 |
public long count(JDBCConnection conn) throws DataException { |
181 |
|
182 |
String sql = this.getSQL(); |
183 |
Statement st = null; |
184 |
ResultSet rs = null; |
185 |
try {
|
186 |
st = conn.createStatement(sql); |
187 |
rs = JDBCUtils.executeQuery(st, sql); |
188 |
if (!rs.next()) {
|
189 |
return 0; |
190 |
} |
191 |
return rs.getLong(1); |
192 |
|
193 |
} catch (SQLException ex) { |
194 |
throw new JDBCSQLException(ex,sql); |
195 |
} finally {
|
196 |
JDBCUtils.closeQuietly(st); |
197 |
JDBCUtils.closeQuietly(rs); |
198 |
} |
199 |
} |
200 |
|
201 |
} |