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

History | View | Annotate | Download (8.49 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.ResultSet;
27
import java.sql.SQLException;
28
import java.sql.Statement;
29 46050 omartinez
import java.util.ArrayList;
30 44727 jjdelcerro
import java.util.List;
31 43020 jjdelcerro
import org.apache.commons.lang3.StringUtils;
32 44198 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionBuilder;
33 46518 fdiaz
import org.gvsig.fmap.dal.SQLBuilder;
34
import static org.gvsig.fmap.dal.SQLBuilder.PROP_TABLENAME;
35 44727 jjdelcerro
import org.gvsig.fmap.dal.SQLBuilder.SelectBuilder;
36 43020 jjdelcerro
import org.gvsig.fmap.dal.exception.DataException;
37 44727 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureQuery;
38 44376 jjdelcerro
import org.gvsig.fmap.dal.feature.FeatureType;
39 43020 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException;
40 46315 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection;
41 46505 fdiaz
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
42 43020 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
43 44058 jjdelcerro
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference;
44 46505 fdiaz
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
45 46010 jjdelcerro
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE;
46
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_JDBCHELPER;
47 46050 omartinez
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_QUERY;
48 46010 jjdelcerro
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_SYMBOLTABLE;
49 44198 jjdelcerro
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE;
50 46505 fdiaz
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.getAllExtraColumns;
51
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process2_ComputedFields;
52
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process3_Where;
53
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process4_Aggregates;
54
import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process5_GroupBys;
55 43020 jjdelcerro
56
public class CountOperation extends AbstractConnectionOperation {
57
58 44058 jjdelcerro
    private final TableReference table;
59 43020 jjdelcerro
    private final String baseFilter;
60 44727 jjdelcerro
    private final FeatureQuery query;
61 44376 jjdelcerro
    private final FeatureType featureType;
62 43020 jjdelcerro
63
    public CountOperation(
64
            JDBCHelper helper
65
        ) {
66 44376 jjdelcerro
        this(helper, null, null, null, null);
67 43020 jjdelcerro
    }
68
69
    public CountOperation(
70
            JDBCHelper helper,
71 44376 jjdelcerro
            FeatureType featureType,
72 44058 jjdelcerro
            TableReference table,
73 43020 jjdelcerro
            String baseFilter,
74 44727 jjdelcerro
            FeatureQuery query
75 43020 jjdelcerro
        ) {
76
        super(helper);
77 44376 jjdelcerro
        this.featureType = featureType;
78 44058 jjdelcerro
        this.table = table;
79 43020 jjdelcerro
        this.baseFilter = baseFilter;
80 44727 jjdelcerro
        this.query = query;
81 43020 jjdelcerro
    }
82
83
    @Override
84 46315 jjdelcerro
    public final Object perform(JDBCConnection conn) throws DataException {
85 44678 jjdelcerro
        return this.count(conn);
86 43020 jjdelcerro
    }
87
88 44678 jjdelcerro
    public String getSQL() {
89 43020 jjdelcerro
        JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
90 44198 jjdelcerro
        ExpressionBuilder expbuilder = sqlbuilder.expression();
91 46517 fdiaz
92
        expbuilder.setProperty(PROP_FEATURE_TYPE, this.featureType);
93
        expbuilder.setProperty(PROP_TABLE, table);
94 46518 fdiaz
        expbuilder.setProperty(PROP_TABLENAME, table.getTable());
95 46517 fdiaz
        expbuilder.setProperty(PROP_SYMBOLTABLE, this.query==null? null:this.query.getSymbolTable());
96
        expbuilder.setProperty(PROP_JDBCHELPER, this.helper);
97
        expbuilder.setProperty(PROP_QUERY, this.query);
98 43020 jjdelcerro
99 44727 jjdelcerro
        SelectBuilder select = sqlbuilder.select();
100 46505 fdiaz
        select.from().table()
101
                .database(this.table.getDatabase())
102
                .schema(this.table.getSchema())
103
                .name(this.table.getTable());
104
        select.from().subquery(this.table.getSubquery());
105
106 46221 omartinez
        List<ExpressionBuilder.Value> valuesToRemoveFeatureType = new ArrayList<>();
107
        List<String> extraColumnNames = new ArrayList<>();
108 44727 jjdelcerro
109 46505 fdiaz
        if (this.query != null && (query.hasAggregateFunctions() || query.hasGroupByColumns())) {
110 45155 omartinez
            JDBCSQLBuilderBase subsqlbuilder = this.createSQLBuilder();
111
            SelectBuilder subselect = subsqlbuilder.select();
112 46505 fdiaz
            subselect.from().table()
113
                    .database(this.table.getDatabase())
114
                    .schema(this.table.getSchema())
115
                    .name(this.table.getTable());
116
            subselect.from().subquery(this.table.getSubquery());
117 46050 omartinez
118 46505 fdiaz
            // ?sobra?
119 46507 jjdelcerro
//            process2_ComputedFields(helper, featureType, query, sqlbuilder, subselect, extraColumnNames);
120 46505 fdiaz
121
            process3_Where(helper, featureType, query, sqlbuilder, subselect);
122
            process4_Aggregates(this.table, this.featureType, this.query, getAllExtraColumns(this.featureType, this.query), sqlbuilder, subselect, extraColumnNames);
123
            process5_GroupBys(this.table, this.featureType, this.query, getAllExtraColumns(this.featureType, this.query), sqlbuilder, subselect, extraColumnNames);
124
125
            if (!StringUtils.isEmpty(baseFilter)) {
126
                subselect.where().and(expbuilder.toValue(baseFilter));
127 46050 omartinez
            }
128 46806 jjdelcerro
            if(subselect.getColumns().isEmpty()){
129
                subselect.column().value(expbuilder.constant(1)).as("$ANY_VALUE$");
130
            }
131 46050 omartinez
132 46505 fdiaz
            subsqlbuilder.setProperties(
133
                    ExpressionBuilder.Variable.class,
134
                    PROP_FEATURE_TYPE, this.featureType,
135
                    PROP_TABLE, table,
136
                    PROP_SYMBOLTABLE, this.query == null ? null : this.query.getSymbolTable(),
137
                    PROP_JDBCHELPER, this.helper,
138
                    PROP_QUERY, this.query
139
            );
140
            for (ExpressionBuilder.Value value : valuesToRemoveFeatureType) {
141
                value.setProperty(PROP_FEATURE_TYPE, null);
142 45155 omartinez
            }
143 46505 fdiaz
            this.helper.expandCalculedColumns(subsqlbuilder);
144
            this.helper.processSpecialFunctions(subsqlbuilder, featureType, extraColumnNames);
145 46511 jjdelcerro
            String subsql = StringUtils.trim(subselect.toString());
146 45155 omartinez
            select.from().table()
147
                    .database(this.table.getDatabase())
148
                    .schema(this.table.getSchema())
149
                    .name(this.table.getTable());
150 46505 fdiaz
            select.from().subquery(subsql);
151
        } else {
152
            process3_Where(helper, featureType, query, sqlbuilder, select);
153 45155 omartinez
            if (!StringUtils.isEmpty(baseFilter)) {
154 46605 fdiaz
                sqlbuilder.select().where().and(expbuilder.custom(baseFilter));
155 44727 jjdelcerro
            }
156 43020 jjdelcerro
        }
157 45155 omartinez
158 46104 omartinez
159 44727 jjdelcerro
        select.remove_all_columns();
160
        select.column().value(sqlbuilder.count().all());
161 44198 jjdelcerro
        sqlbuilder.setProperties(
162 46010 jjdelcerro
                null,
163
                PROP_FEATURE_TYPE, this.featureType,
164
                PROP_TABLE, table,
165
                PROP_SYMBOLTABLE, this.query==null? null:this.query.getSymbolTable(),
166 46050 omartinez
                PROP_JDBCHELPER, this.helper,
167
                PROP_QUERY, this.query
168 44198 jjdelcerro
        );
169 46104 omartinez
        this.helper.expandCalculedColumns(sqlbuilder);
170
        this.helper.processSpecialFunctions(sqlbuilder, featureType, null);
171 44727 jjdelcerro
172 46104 omartinez
        select.remove_all_columns();
173
        select.column().value(sqlbuilder.count().all());
174
175 46511 jjdelcerro
        String sql = StringUtils.trim(select.toString());
176 46505 fdiaz
        LOGGER.debug(sql);
177 44678 jjdelcerro
        return sql;
178
    }
179 45155 omartinez
180 46315 jjdelcerro
    public long count(JDBCConnection conn) throws DataException {
181 43020 jjdelcerro
182 44678 jjdelcerro
        String sql = this.getSQL();
183 43020 jjdelcerro
        Statement st = null;
184
        ResultSet rs = null;
185
        try {
186 47606 fdiaz
            st = conn.createStatement(sql);
187 43020 jjdelcerro
            rs = JDBCUtils.executeQuery(st, sql);
188
            if (!rs.next()) {
189
                return 0;
190
            }
191
            return rs.getLong(1);
192
193
        } catch (SQLException ex) {
194 46148 jjdelcerro
            throw new JDBCSQLException(ex,sql);
195 43020 jjdelcerro
        } finally {
196
            JDBCUtils.closeQuietly(st);
197
            JDBCUtils.closeQuietly(rs);
198
        }
199
    }
200 46010 jjdelcerro
201 43020 jjdelcerro
}