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