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 |
/**
|
---|---|
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 |
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 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
31 |
import org.gvsig.fmap.dal.SQLBuilder; |
32 |
import org.gvsig.fmap.dal.exception.DataException; |
33 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
34 |
import org.gvsig.fmap.dal.feature.FeatureType; |
35 |
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 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
39 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
40 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
41 |
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 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_QUERY; |
44 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_SYMBOLTABLE; |
45 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
46 |
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 |
import org.gvsig.tools.evaluator.Evaluator; |
51 |
|
52 |
public class TableIsEmptyOperation extends AbstractConnectionOperation { |
53 |
|
54 |
private final TableReference table; |
55 |
private final String baseFilter; |
56 |
private final FeatureQuery query; |
57 |
private final FeatureType featureType; |
58 |
|
59 |
public TableIsEmptyOperation(
|
60 |
JDBCHelper helper |
61 |
) { |
62 |
this(helper, null, null, null, null); |
63 |
} |
64 |
|
65 |
public TableIsEmptyOperation(
|
66 |
JDBCHelper helper, |
67 |
FeatureType featureType, |
68 |
TableReference table, |
69 |
String baseFilter,
|
70 |
FeatureQuery query |
71 |
) { |
72 |
super(helper);
|
73 |
this.featureType = featureType;
|
74 |
this.table = table;
|
75 |
this.baseFilter = baseFilter;
|
76 |
this.query = query;
|
77 |
} |
78 |
|
79 |
@Override
|
80 |
public final Object perform(JDBCConnection conn) throws DataException { |
81 |
return this.tableIsEmpty(conn); |
82 |
} |
83 |
|
84 |
public String getSQL() { |
85 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
86 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
87 |
|
88 |
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 |
.database(this.table.getDatabase())
|
98 |
.schema(this.table.getSchema())
|
99 |
.name(this.table.getTable());
|
100 |
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 |
if (!StringUtils.isEmpty(baseFilter)) {
|
105 |
select.where().set( expbuilder.custom(baseFilter) ); |
106 |
} |
107 |
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 |
|
112 |
sqlbuilder.select().limit(1);
|
113 |
|
114 |
sqlbuilder.setProperties( |
115 |
null,
|
116 |
PROP_FEATURE_TYPE, this.featureType,
|
117 |
PROP_TABLE, table, |
118 |
PROP_SYMBOLTABLE, this.query==null? null:this.query.getSymbolTable(), |
119 |
PROP_JDBCHELPER, this.helper,
|
120 |
PROP_QUERY, this.query
|
121 |
); |
122 |
|
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 |
String sql = sqlbuilder.select().toString();
|
129 |
return sql;
|
130 |
} |
131 |
|
132 |
public boolean tableIsEmpty(JDBCConnection conn) throws DataException { |
133 |
String sql = this.getSQL(); |
134 |
|
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 |
throw new JDBCSQLException(ex,sql); |
144 |
} finally {
|
145 |
JDBCUtils.closeQuietly(st); |
146 |
JDBCUtils.closeQuietly(rs); |
147 |
} |
148 |
} |
149 |
|
150 |
} |