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 |
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 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
9 |
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 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
15 |
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 |
|
18 |
public class CountOperation extends AbstractConnectionOperation { |
19 |
|
20 |
private final TableReference table; |
21 |
private final String baseFilter; |
22 |
private final String filter; |
23 |
|
24 |
public CountOperation(
|
25 |
JDBCHelper helper |
26 |
) { |
27 |
this(helper, null, null, null); |
28 |
} |
29 |
|
30 |
public CountOperation(
|
31 |
JDBCHelper helper, |
32 |
TableReference table, |
33 |
String baseFilter,
|
34 |
String filter
|
35 |
) { |
36 |
super(helper);
|
37 |
this.table = table;
|
38 |
this.baseFilter = baseFilter;
|
39 |
this.filter = filter;
|
40 |
} |
41 |
|
42 |
@Override
|
43 |
public final Object perform(Connection conn) throws DataException { |
44 |
return this.count(conn, table, baseFilter, filter); |
45 |
} |
46 |
|
47 |
public long count(Connection conn, |
48 |
TableReference table, |
49 |
String baseFilter,
|
50 |
String filter) throws DataException { |
51 |
|
52 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
53 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
54 |
|
55 |
sqlbuilder.select().column().value(sqlbuilder.count().all()); |
56 |
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 |
if (!StringUtils.isEmpty(baseFilter)) {
|
62 |
sqlbuilder.select().where().set( expbuilder.custom(baseFilter) ); |
63 |
} |
64 |
if (!StringUtils.isEmpty(filter)) {
|
65 |
// El and() hace un set() si no hay un filtro previo
|
66 |
sqlbuilder.select().where().and(expbuilder.toValue(filter)); |
67 |
} |
68 |
sqlbuilder.setProperties( |
69 |
ExpressionBuilder.Variable.class, |
70 |
PROP_TABLE, table |
71 |
); |
72 |
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 |
} |