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

History | View | Annotate | Download (4.1 KB)

1 45065 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2020 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24 43020 jjdelcerro
package org.gvsig.fmap.dal.store.jdbc2.spi.operations;
25
26
import java.sql.Connection;
27
import java.sql.ResultSet;
28
import java.sql.SQLException;
29
import java.sql.Statement;
30
import org.apache.commons.lang3.StringUtils;
31 44198 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder;
32 43020 jjdelcerro
import org.gvsig.fmap.dal.exception.DataException;
33 44376 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
34 43020 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
35
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
36
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
37
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
38 44058 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
39 44198 jjdelcerro
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE;
40 43020 jjdelcerro
41
public class TableIsEmptyOperation extends AbstractConnectionOperation {
42
43 44058 jjdelcerro
    private final TableReference table;
44 43020 jjdelcerro
    private final String baseFilter;
45
    private final String filter;
46 44376 jjdelcerro
    private final FeatureType featureType;
47 43020 jjdelcerro
48
    public TableIsEmptyOperation(
49
            JDBCHelper helper
50
        ) {
51 44376 jjdelcerro
        this(helper, null, null, null, null);
52 43020 jjdelcerro
    }
53
54
    public TableIsEmptyOperation(
55
            JDBCHelper helper,
56 44376 jjdelcerro
            FeatureType featureType,
57 44058 jjdelcerro
            TableReference table,
58 43020 jjdelcerro
            String baseFilter,
59
            String filter
60
        ) {
61
        super(helper);
62 44376 jjdelcerro
        this.featureType = featureType;
63 44058 jjdelcerro
        this.table = table;
64 43020 jjdelcerro
        this.baseFilter = baseFilter;
65
        this.filter = filter;
66
    }
67
68
    @Override
69
    public final Object perform(Connection conn) throws DataException {
70 44678 jjdelcerro
        return this.tableIsEmpty(conn);
71 43020 jjdelcerro
    }
72
73 44678 jjdelcerro
    public String getSQL() {
74 43020 jjdelcerro
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
75 44198 jjdelcerro
        ExpressionBuilder expbuilder = sqlbuilder.expression();
76
77 43020 jjdelcerro
        sqlbuilder.select().column().all();
78 44058 jjdelcerro
        sqlbuilder.select().from().table()
79
                .database(this.table.getDatabase())
80
                .schema(this.table.getSchema())
81
                .name(this.table.getTable());
82
        sqlbuilder.select().from().subquery(this.table.getSubquery());
83 43020 jjdelcerro
        if (!StringUtils.isEmpty(baseFilter)) {
84 44198 jjdelcerro
            sqlbuilder.select().where().set( expbuilder.custom(baseFilter) );
85 43020 jjdelcerro
        }
86
        if (!StringUtils.isEmpty(filter)) {
87
            // El and() hace un set() si no hay un filtro previo
88 44202 jjdelcerro
            sqlbuilder.select().where().and(expbuilder.toValue(filter));
89 43020 jjdelcerro
        }
90
        sqlbuilder.select().limit(1);
91 44748 jjdelcerro
        this.helper.processSpecialFunctions(sqlbuilder, featureType, null);
92 44198 jjdelcerro
        sqlbuilder.setProperties(
93
                ExpressionBuilder.Variable.class,
94
                PROP_TABLE, table
95
        );
96 43020 jjdelcerro
        String sql = sqlbuilder.select().toString();
97 44678 jjdelcerro
        return sql;
98
    }
99
100
    public boolean tableIsEmpty(Connection conn) throws DataException {
101
        String sql = this.getSQL();
102 43020 jjdelcerro
103
        Statement st = null;
104
        ResultSet rs = null;
105
        try {
106
            st = conn.createStatement();
107
            rs = JDBCUtils.executeQuery(st, sql);
108
            return !rs.next();
109
110
        } catch (SQLException ex) {
111
            throw new JDBCSQLException(ex);
112
        } finally {
113
            JDBCUtils.closeQuietly(st);
114
            JDBCUtils.closeQuietly(rs);
115
        }
116
    }
117
118
}