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 @ 46517
History | View | Annotate | Download (6.17 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 | import org.apache.commons.lang3.StringUtils; |
||
30 | 44198 | jjdelcerro | import org.gvsig.expressionevaluator.ExpressionBuilder; |
31 | 46517 | fdiaz | import org.gvsig.fmap.dal.SQLBuilder; |
32 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.exception.DataException; |
33 | 46010 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureQuery; |
34 | 44376 | jjdelcerro | import org.gvsig.fmap.dal.feature.FeatureType; |
35 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
36 | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
||
37 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
||
38 | 46315 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
39 | 43020 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
40 | 44058 | jjdelcerro | import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
41 | 46010 | jjdelcerro | import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
42 | import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_JDBCHELPER; |
||
43 | 46517 | fdiaz | import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_QUERY; |
44 | 46010 | jjdelcerro | import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_SYMBOLTABLE; |
45 | 44198 | jjdelcerro | import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
46 | 46517 | fdiaz | import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.getAllExtraColumns; |
47 | import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process3_Where; |
||
48 | import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process4_Aggregates; |
||
49 | import static org.gvsig.fmap.dal.store.jdbc2.spi.operations.ResultSetForSetProviderOperation.process5_GroupBys; |
||
50 | 46010 | jjdelcerro | import org.gvsig.tools.evaluator.Evaluator; |
51 | 43020 | jjdelcerro | |
52 | public class TableIsEmptyOperation extends AbstractConnectionOperation { |
||
53 | |||
54 | 44058 | jjdelcerro | private final TableReference table; |
55 | 43020 | jjdelcerro | private final String baseFilter; |
56 | 46010 | jjdelcerro | private final FeatureQuery query; |
57 | 44376 | jjdelcerro | private final FeatureType featureType; |
58 | 43020 | jjdelcerro | |
59 | public TableIsEmptyOperation(
|
||
60 | JDBCHelper helper |
||
61 | ) { |
||
62 | 44376 | jjdelcerro | this(helper, null, null, null, null); |
63 | 43020 | jjdelcerro | } |
64 | |||
65 | public TableIsEmptyOperation(
|
||
66 | JDBCHelper helper, |
||
67 | 44376 | jjdelcerro | FeatureType featureType, |
68 | 44058 | jjdelcerro | TableReference table, |
69 | 43020 | jjdelcerro | String baseFilter,
|
70 | 46010 | jjdelcerro | FeatureQuery query |
71 | 43020 | jjdelcerro | ) { |
72 | super(helper);
|
||
73 | 44376 | jjdelcerro | this.featureType = featureType;
|
74 | 44058 | jjdelcerro | this.table = table;
|
75 | 43020 | jjdelcerro | this.baseFilter = baseFilter;
|
76 | 46010 | jjdelcerro | this.query = query;
|
77 | 43020 | jjdelcerro | } |
78 | |||
79 | @Override
|
||
80 | 46315 | jjdelcerro | public final Object perform(JDBCConnection conn) throws DataException { |
81 | 44678 | jjdelcerro | return this.tableIsEmpty(conn); |
82 | 43020 | jjdelcerro | } |
83 | |||
84 | 44678 | jjdelcerro | public String getSQL() { |
85 | 43020 | jjdelcerro | JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
86 | 44198 | jjdelcerro | ExpressionBuilder expbuilder = sqlbuilder.expression(); |
87 | |||
88 | 46517 | fdiaz | expbuilder.setProperty(PROP_FEATURE_TYPE, this.featureType);
|
89 | expbuilder.setProperty(PROP_TABLE, table); |
||
90 | expbuilder.setProperty(PROP_SYMBOLTABLE, this.query==null? null:this.query.getSymbolTable()); |
||
91 | expbuilder.setProperty(PROP_JDBCHELPER, this.helper);
|
||
92 | expbuilder.setProperty(PROP_QUERY, this.query);
|
||
93 | |||
94 | SQLBuilder.SelectBuilder select = sqlbuilder.select(); |
||
95 | // select.column().all();
|
||
96 | select.from().table() |
||
97 | 44058 | jjdelcerro | .database(this.table.getDatabase())
|
98 | .schema(this.table.getSchema())
|
||
99 | .name(this.table.getTable());
|
||
100 | 46517 | fdiaz | select.from().subquery(this.table.getSubquery());
|
101 | |||
102 | // ResultSetForSetProviderOperation.process2_ComputedFields(helper, featureType, query, sqlbuilder, select, null);
|
||
103 | process3_Where(helper, featureType, query, sqlbuilder, select); |
||
104 | 43020 | jjdelcerro | if (!StringUtils.isEmpty(baseFilter)) {
|
105 | 46517 | fdiaz | select.where().set( expbuilder.custom(baseFilter) ); |
106 | 43020 | jjdelcerro | } |
107 | 46517 | fdiaz | if (this.query != null && (query.hasAggregateFunctions() || query.hasGroupByColumns())) { |
108 | process4_Aggregates(this.table, this.featureType, this.query, getAllExtraColumns(this.featureType, this.query), sqlbuilder, select, null); |
||
109 | process5_GroupBys(this.table, this.featureType, this.query, getAllExtraColumns(this.featureType, this.query), sqlbuilder, select, null); |
||
110 | } |
||
111 | 46010 | jjdelcerro | |
112 | 43020 | jjdelcerro | sqlbuilder.select().limit(1);
|
113 | 46517 | fdiaz | |
114 | 44198 | jjdelcerro | sqlbuilder.setProperties( |
115 | 46010 | jjdelcerro | null,
|
116 | PROP_FEATURE_TYPE, this.featureType,
|
||
117 | PROP_TABLE, table, |
||
118 | PROP_SYMBOLTABLE, this.query==null? null:this.query.getSymbolTable(), |
||
119 | 46517 | fdiaz | PROP_JDBCHELPER, this.helper,
|
120 | PROP_QUERY, this.query
|
||
121 | 44198 | jjdelcerro | ); |
122 | 46517 | fdiaz | |
123 | this.helper.expandCalculedColumns(sqlbuilder);
|
||
124 | this.helper.processSpecialFunctions(sqlbuilder, featureType, null); |
||
125 | if(select.getColumns().isEmpty()){
|
||
126 | select.column().value(expbuilder.constant(1));
|
||
127 | } |
||
128 | 43020 | jjdelcerro | String sql = sqlbuilder.select().toString();
|
129 | 44678 | jjdelcerro | return sql;
|
130 | } |
||
131 | |||
132 | 46315 | jjdelcerro | public boolean tableIsEmpty(JDBCConnection conn) throws DataException { |
133 | 44678 | jjdelcerro | String sql = this.getSQL(); |
134 | 43020 | jjdelcerro | |
135 | Statement st = null; |
||
136 | ResultSet rs = null; |
||
137 | try {
|
||
138 | st = conn.createStatement(); |
||
139 | rs = JDBCUtils.executeQuery(st, sql); |
||
140 | return !rs.next();
|
||
141 | |||
142 | } catch (SQLException ex) { |
||
143 | 46148 | jjdelcerro | throw new JDBCSQLException(ex,sql); |
144 | 43020 | jjdelcerro | } finally {
|
145 | JDBCUtils.closeQuietly(st); |
||
146 | JDBCUtils.closeQuietly(rs); |
||
147 | } |
||
148 | } |
||
149 | |||
150 | } |