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 / TableIsEmptyOperation.java @ 44376

History | View | Annotate | Download (3.33 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 TableIsEmptyOperation 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 TableIsEmptyOperation(
27
            JDBCHelper helper
28
        ) {
29
        this(helper, null, null, null, null);
30
    }
31

    
32
    public TableIsEmptyOperation(
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.tableIsEmpty(conn, featureType, table, baseFilter, filter);
49
    }
50

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

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

    
82
        Statement st = null;
83
        ResultSet rs = null;
84
        try {
85
            st = conn.createStatement();
86
            rs = JDBCUtils.executeQuery(st, sql);
87
            return !rs.next();
88

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

    
97
}