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 43020 jjdelcerro
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 44058 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
14 43020 jjdelcerro
15
public class TableIsEmptyOperation extends AbstractConnectionOperation {
16
17 44058 jjdelcerro
    private final TableReference table;
18 43020 jjdelcerro
    private final String baseFilter;
19
    private final String filter;
20
21
    public TableIsEmptyOperation(
22
            JDBCHelper helper
23
        ) {
24 44058 jjdelcerro
        this(helper, null, null, null);
25 43020 jjdelcerro
    }
26
27
    public TableIsEmptyOperation(
28
            JDBCHelper helper,
29 44058 jjdelcerro
            TableReference table,
30 43020 jjdelcerro
            String baseFilter,
31
            String filter
32
        ) {
33
        super(helper);
34 44058 jjdelcerro
        this.table = table;
35 43020 jjdelcerro
        this.baseFilter = baseFilter;
36
        this.filter = filter;
37
    }
38
39
    @Override
40
    public final Object perform(Connection conn) throws DataException {
41 44058 jjdelcerro
        return this.tableIsEmpty(conn, table, baseFilter, filter);
42 43020 jjdelcerro
    }
43
44
    public boolean tableIsEmpty(Connection conn,
45 44058 jjdelcerro
            TableReference table,
46 43020 jjdelcerro
            String baseFilter,
47 44058 jjdelcerro
            String filter
48
        ) throws DataException {
49 43020 jjdelcerro
50
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
51
52
        sqlbuilder.select().column().all();
53 44058 jjdelcerro
        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 43020 jjdelcerro
        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
}