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