Statistics
| Revision:

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 @ 44058

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