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
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
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
14

    
15
public class CountOperation extends AbstractConnectionOperation {
16

    
17
    private final TableReference table;
18
    private final String baseFilter;
19
    private final String filter;
20

    
21
    public CountOperation(
22
            JDBCHelper helper
23
        ) {
24
        this(helper, null, null, null);
25
    }
26

    
27
    public CountOperation(
28
            JDBCHelper helper,
29
            TableReference table,
30
            String baseFilter,
31
            String filter
32
        ) {
33
        super(helper);
34
        this.table = table;
35
        this.baseFilter = baseFilter;
36
        this.filter = filter;
37
    }
38

    
39
    @Override
40
    public final Object perform(Connection conn) throws DataException {
41
        return this.count(conn, table, baseFilter, filter);
42
    }
43

    
44
    public long count(Connection conn,
45
            TableReference table,
46
            String baseFilter,
47
            String filter) throws DataException {
48

    
49
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
50

    
51
        sqlbuilder.select().column().value(sqlbuilder.count().all());
52
        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
        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
}