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 @ 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 TableIsEmptyOperation extends AbstractConnectionOperation {
16

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

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

    
27
    public TableIsEmptyOperation(
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.tableIsEmpty(conn, table, baseFilter, filter);
42
    }
43

    
44
    public boolean tableIsEmpty(Connection conn,
45
            TableReference table,
46
            String baseFilter,
47
            String filter
48
        ) throws DataException {
49

    
50
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
51

    
52
        sqlbuilder.select().column().all();
53
        sqlbuilder.select().from().table()
54
                .database(this.table.getDatabase())
55
                .schema(this.table.getSchema())
56
                .name(this.table.getTable());
57
        sqlbuilder.select().from().subquery(this.table.getSubquery());
58
        if (!StringUtils.isEmpty(baseFilter)) {
59
            sqlbuilder.select().where().set( sqlbuilder.custom(baseFilter) );
60
        }
61
        if (!StringUtils.isEmpty(filter)) {
62
            // El and() hace un set() si no hay un filtro previo
63
            sqlbuilder.select().where().and(sqlbuilder.custom(filter));
64
        }
65
        sqlbuilder.select().limit(1);
66
        
67
        String sql = sqlbuilder.select().toString();
68

    
69
        Statement st = null;
70
        ResultSet rs = null;
71
        try {
72
            st = conn.createStatement();
73
            rs = JDBCUtils.executeQuery(st, sql);
74
            return !rs.next();
75

    
76
        } catch (SQLException ex) {
77
            throw new JDBCSQLException(ex);
78
        } finally {
79
            JDBCUtils.closeQuietly(st);
80
            JDBCUtils.closeQuietly(rs);
81
        }
82
    }
83

    
84
}