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

History | View | Annotate | Download (3.34 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.feature.FeatureType;
11
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
12
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
13
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
14
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
15
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
16
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE;
17
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE;
18

    
19
public class CountOperation extends AbstractConnectionOperation {
20

    
21
    private final TableReference table;
22
    private final String baseFilter;
23
    private final String filter;
24
    private final FeatureType featureType;
25

    
26
    public CountOperation(
27
            JDBCHelper helper
28
        ) {
29
        this(helper, null, null, null, null);
30
    }
31

    
32
    public CountOperation(
33
            JDBCHelper helper,
34
            FeatureType featureType,
35
            TableReference table,
36
            String baseFilter,
37
            String filter
38
        ) {
39
        super(helper);
40
        this.featureType = featureType;
41
        this.table = table;
42
        this.baseFilter = baseFilter;
43
        this.filter = filter;
44
    }
45

    
46
    @Override
47
    public final Object perform(Connection conn) throws DataException {
48
        return this.count(conn, featureType, table, baseFilter, filter);
49
    }
50

    
51
    public long count(Connection conn,
52
            FeatureType featureType,
53
            TableReference table,
54
            String baseFilter,
55
            String filter) throws DataException {
56

    
57
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
58
        ExpressionBuilder expbuilder = sqlbuilder.expression();
59

    
60
        sqlbuilder.select().column().value(sqlbuilder.count().all());
61
        sqlbuilder.select().from().table()
62
                .database(this.table.getDatabase())
63
                .schema(this.table.getSchema())
64
                .name(this.table.getTable());
65
        sqlbuilder.select().from().subquery(this.table.getSubquery());
66
        if (!StringUtils.isEmpty(baseFilter)) {
67
            sqlbuilder.select().where().set( expbuilder.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(expbuilder.toValue(filter));
72
        }
73
        this.helper.replaceForeingValueFunction(sqlbuilder, featureType);
74
        
75
        sqlbuilder.setProperties(
76
                ExpressionBuilder.Variable.class, 
77
                PROP_TABLE, table
78
        );
79
        String sql = sqlbuilder.select().toString();
80

    
81
        Statement st = null;
82
        ResultSet rs = null;
83
        try {
84
            st = conn.createStatement();
85
            rs = JDBCUtils.executeQuery(st, sql);
86
            if (!rs.next()) {
87
                return 0;
88
            }
89
            return rs.getLong(1);
90

    
91
        } catch (SQLException ex) {
92
            throw new JDBCSQLException(ex);
93
        } finally {
94
            JDBCUtils.closeQuietly(st);
95
            JDBCUtils.closeQuietly(rs);
96
        }
97
    }
98

    
99
}