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 @ 44202
History | View | Annotate | Download (3.04 KB)
1 | 43020 | jjdelcerro | 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 org.apache.commons.lang3.StringUtils; |
||
8 | 44198 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionBuilder; |
9 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
10 | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
||
11 | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
||
12 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
||
13 | import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
||
14 | 44058 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
15 | 44198 | jjdelcerro | import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
16 | import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
||
17 | 43020 | jjdelcerro | |
18 | public class CountOperation extends AbstractConnectionOperation { |
||
19 | |||
20 | 44058 | jjdelcerro | private final TableReference table; |
21 | 43020 | jjdelcerro | private final String baseFilter; |
22 | private final String filter; |
||
23 | |||
24 | public CountOperation(
|
||
25 | JDBCHelper helper |
||
26 | ) { |
||
27 | 44058 | jjdelcerro | this(helper, null, null, null); |
28 | 43020 | jjdelcerro | } |
29 | |||
30 | public CountOperation(
|
||
31 | JDBCHelper helper, |
||
32 | 44058 | jjdelcerro | TableReference table, |
33 | 43020 | jjdelcerro | String baseFilter,
|
34 | String filter
|
||
35 | ) { |
||
36 | super(helper);
|
||
37 | 44058 | jjdelcerro | this.table = table;
|
38 | 43020 | jjdelcerro | this.baseFilter = baseFilter;
|
39 | this.filter = filter;
|
||
40 | } |
||
41 | |||
42 | @Override
|
||
43 | public final Object perform(Connection conn) throws DataException { |
||
44 | 44058 | jjdelcerro | return this.count(conn, table, baseFilter, filter); |
45 | 43020 | jjdelcerro | } |
46 | |||
47 | public long count(Connection conn, |
||
48 | 44058 | jjdelcerro | TableReference table, |
49 | 43020 | jjdelcerro | String baseFilter,
|
50 | String filter) throws DataException { |
||
51 | |||
52 | JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
||
53 | 44198 | jjdelcerro | ExpressionBuilder expbuilder = sqlbuilder.expression(); |
54 | 43020 | jjdelcerro | |
55 | sqlbuilder.select().column().value(sqlbuilder.count().all()); |
||
56 | 44058 | jjdelcerro | sqlbuilder.select().from().table() |
57 | .database(this.table.getDatabase())
|
||
58 | .schema(this.table.getSchema())
|
||
59 | .name(this.table.getTable());
|
||
60 | sqlbuilder.select().from().subquery(this.table.getSubquery());
|
||
61 | 43020 | jjdelcerro | if (!StringUtils.isEmpty(baseFilter)) {
|
62 | 44198 | jjdelcerro | sqlbuilder.select().where().set( expbuilder.custom(baseFilter) ); |
63 | 43020 | jjdelcerro | } |
64 | if (!StringUtils.isEmpty(filter)) {
|
||
65 | // El and() hace un set() si no hay un filtro previo
|
||
66 | 44202 | jjdelcerro | sqlbuilder.select().where().and(expbuilder.toValue(filter)); |
67 | 43020 | jjdelcerro | } |
68 | 44198 | jjdelcerro | sqlbuilder.setProperties( |
69 | ExpressionBuilder.Variable.class, |
||
70 | PROP_TABLE, table |
||
71 | ); |
||
72 | 43020 | jjdelcerro | String sql = sqlbuilder.select().toString();
|
73 | |||
74 | Statement st = null; |
||
75 | ResultSet rs = null; |
||
76 | try {
|
||
77 | st = conn.createStatement(); |
||
78 | rs = JDBCUtils.executeQuery(st, sql); |
||
79 | if (!rs.next()) {
|
||
80 | return 0; |
||
81 | } |
||
82 | return rs.getLong(1); |
||
83 | |||
84 | } catch (SQLException ex) { |
||
85 | throw new JDBCSQLException(ex); |
||
86 | } finally {
|
||
87 | JDBCUtils.closeQuietly(st); |
||
88 | JDBCUtils.closeQuietly(rs); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | } |