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

History | View | Annotate | Download (3.31 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_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
    private final FeatureType featureType;
24

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

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

    
45
    @Override
46
    public final Object perform(Connection conn) throws DataException {
47
        return this.count(conn);
48
    }
49

    
50
    public String getSQL() {
51
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
52
        ExpressionBuilder expbuilder = sqlbuilder.expression();
53

    
54
        sqlbuilder.select().column().value(sqlbuilder.count().all());
55
        sqlbuilder.select().from().table()
56
                .database(this.table.getDatabase())
57
                .schema(this.table.getSchema())
58
                .name(this.table.getTable());
59
        sqlbuilder.select().from().subquery(this.table.getSubquery());
60
        if (!StringUtils.isEmpty(baseFilter)) {
61
            sqlbuilder.select().where().set( expbuilder.custom(baseFilter) );
62
        }
63
        if (!StringUtils.isEmpty(filter)) {
64
            // El and() hace un set() si no hay un filtro previo
65
            sqlbuilder.select().where().and(expbuilder.toValue(filter));
66
        }
67
        this.helper.replaceForeingValueFunction(sqlbuilder, featureType);
68
        
69
        sqlbuilder.select().remove_all_columns();
70
        sqlbuilder.select().column().value(sqlbuilder.count().all());
71
        
72
        sqlbuilder.setProperties(
73
                ExpressionBuilder.Variable.class, 
74
                PROP_TABLE, table
75
        );
76
        String sql = sqlbuilder.select().toString();
77
        return sql;
78
    }
79
    
80
    public long count(Connection conn) throws DataException {
81

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

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

    
101
}