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 @ 44764
History | View | Annotate | Download (6.14 KB)
1 |
package org.gvsig.fmap.dal.store.jdbc2.spi.operations; |
---|---|
2 |
|
3 |
import java.sql.Connection; |
4 |
import java.sql.ResultSet; |
5 |
import java.sql.SQLException; |
6 |
import java.sql.Statement; |
7 |
import java.util.List; |
8 |
import org.apache.commons.lang3.StringUtils; |
9 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
10 |
import org.gvsig.fmap.dal.SQLBuilder.SelectBuilder; |
11 |
import org.gvsig.fmap.dal.exception.DataException; |
12 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
13 |
import org.gvsig.fmap.dal.feature.FeatureQueryOrder; |
14 |
import org.gvsig.fmap.dal.feature.FeatureType; |
15 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
16 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
17 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
18 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
19 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
20 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
21 |
import org.gvsig.tools.evaluator.Evaluator; |
22 |
|
23 |
public class CountOperation extends AbstractConnectionOperation { |
24 |
|
25 |
private final TableReference table; |
26 |
private final String baseFilter; |
27 |
private final FeatureQuery query; |
28 |
private final FeatureType featureType; |
29 |
|
30 |
public CountOperation(
|
31 |
JDBCHelper helper |
32 |
) { |
33 |
this(helper, null, null, null, null); |
34 |
} |
35 |
|
36 |
public CountOperation(
|
37 |
JDBCHelper helper, |
38 |
FeatureType featureType, |
39 |
TableReference table, |
40 |
String baseFilter,
|
41 |
FeatureQuery query |
42 |
) { |
43 |
super(helper);
|
44 |
this.featureType = featureType;
|
45 |
this.table = table;
|
46 |
this.baseFilter = baseFilter;
|
47 |
this.query = query;
|
48 |
} |
49 |
|
50 |
@Override
|
51 |
public final Object perform(Connection conn) throws DataException { |
52 |
return this.count(conn); |
53 |
} |
54 |
|
55 |
public String getSQL() { |
56 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
57 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
58 |
|
59 |
SelectBuilder select = sqlbuilder.select(); |
60 |
|
61 |
if( this.query!=null && this.query.hasGroupByColumns() ) { |
62 |
JDBCSQLBuilderBase subsqlbuilder = this.createSQLBuilder();
|
63 |
SelectBuilder subselect = subsqlbuilder.select(); |
64 |
subselect.column().value(subsqlbuilder.count().all()); |
65 |
subselect.from().table() |
66 |
.database(this.table.getDatabase())
|
67 |
.schema(this.table.getSchema())
|
68 |
.name(this.table.getTable());
|
69 |
subselect.from().subquery(this.table.getSubquery());
|
70 |
Evaluator filter = query==null? null:query.getFilter(); |
71 |
if( filter != null ) { |
72 |
String sqlfilter = filter.getSQL();
|
73 |
if( ! StringUtils.isEmpty(sqlfilter) ) {
|
74 |
if( this.helper.supportFilter(this.featureType, filter) ) { |
75 |
subselect.where().set(expbuilder.toValue(sqlfilter)); |
76 |
} |
77 |
} |
78 |
} |
79 |
if( ! StringUtils.isEmpty(baseFilter) ) {
|
80 |
subselect.where().and(expbuilder.toValue(baseFilter)); |
81 |
} |
82 |
|
83 |
FeatureQueryOrder order = query==null? null:query.getOrder(); |
84 |
if( order != null ) { |
85 |
for( FeatureQueryOrder.FeatureQueryOrderMember member : order.members() ) {
|
86 |
if( member.hasEvaluator() ) {
|
87 |
String sqlorder = member.getEvaluator().getSQL();
|
88 |
if( ! StringUtils.isEmpty(sqlorder) ) {
|
89 |
subselect.order_by().custom(sqlorder); |
90 |
} |
91 |
} else {
|
92 |
subselect.order_by() |
93 |
.column(member.getAttributeName()) |
94 |
.ascending(member.getAscending()); |
95 |
} |
96 |
} |
97 |
} |
98 |
|
99 |
List<String> groupbyColumns = query==null? null:query.getGroupByColumns(); |
100 |
if( groupbyColumns!=null && !groupbyColumns.isEmpty() ) { |
101 |
for(String columnName : groupbyColumns ) { |
102 |
subselect.group_by(expbuilder.column(columnName)); |
103 |
} |
104 |
} |
105 |
subsqlbuilder.setProperties( |
106 |
ExpressionBuilder.Variable.class, |
107 |
PROP_TABLE, table |
108 |
); |
109 |
String subsql = subselect.toString();
|
110 |
select.from().table() |
111 |
.database(this.table.getDatabase())
|
112 |
.schema(this.table.getSchema())
|
113 |
.name(this.table.getTable());
|
114 |
select.from().subquery(subsql); |
115 |
|
116 |
|
117 |
} else {
|
118 |
select.column().value(sqlbuilder.count().all()); |
119 |
select.from().table() |
120 |
.database(this.table.getDatabase())
|
121 |
.schema(this.table.getSchema())
|
122 |
.name(this.table.getTable());
|
123 |
select.from().subquery(this.table.getSubquery());
|
124 |
if (!StringUtils.isEmpty(baseFilter)) {
|
125 |
sqlbuilder.select().where().set( expbuilder.custom(baseFilter) ); |
126 |
} |
127 |
if( this.query!=null ) { |
128 |
if( this.query.getFilter()!=null && !StringUtils.isBlank(this.query.getFilter().getSQL()) ) { |
129 |
// El and() hace un set() si no hay un filtro previo
|
130 |
select.where().and(expbuilder.toValue(this.query.getFilter().getSQL()));
|
131 |
} |
132 |
} |
133 |
} |
134 |
this.helper.processSpecialFunctions(sqlbuilder, featureType, null); |
135 |
|
136 |
select.remove_all_columns(); |
137 |
select.column().value(sqlbuilder.count().all()); |
138 |
|
139 |
sqlbuilder.setProperties( |
140 |
ExpressionBuilder.Variable.class, |
141 |
PROP_TABLE, table |
142 |
); |
143 |
|
144 |
String sql = select.toString();
|
145 |
return sql;
|
146 |
} |
147 |
|
148 |
public long count(Connection conn) throws DataException { |
149 |
|
150 |
String sql = this.getSQL(); |
151 |
Statement st = null; |
152 |
ResultSet rs = null; |
153 |
try {
|
154 |
st = conn.createStatement(); |
155 |
rs = JDBCUtils.executeQuery(st, sql); |
156 |
if (!rs.next()) {
|
157 |
return 0; |
158 |
} |
159 |
return rs.getLong(1); |
160 |
|
161 |
} catch (SQLException ex) { |
162 |
throw new JDBCSQLException(ex); |
163 |
} finally {
|
164 |
JDBCUtils.closeQuietly(st); |
165 |
JDBCUtils.closeQuietly(rs); |
166 |
} |
167 |
} |
168 |
|
169 |
} |