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 @ 46010
History | View | Annotate | Download (5.22 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.Connection; |
27 |
import java.sql.ResultSet; |
28 |
import java.sql.SQLException; |
29 |
import java.sql.Statement; |
30 |
import org.apache.commons.lang3.StringUtils; |
31 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
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.JDBCUtils; |
39 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory.TableReference; |
40 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
41 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_JDBCHELPER; |
42 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_SYMBOLTABLE; |
43 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
44 |
import org.gvsig.tools.evaluator.Evaluator; |
45 |
|
46 |
public class TableIsEmptyOperation extends AbstractConnectionOperation { |
47 |
|
48 |
private final TableReference table; |
49 |
private final String baseFilter; |
50 |
private final FeatureQuery query; |
51 |
private final FeatureType featureType; |
52 |
|
53 |
public TableIsEmptyOperation(
|
54 |
JDBCHelper helper |
55 |
) { |
56 |
this(helper, null, null, null, null); |
57 |
} |
58 |
|
59 |
public TableIsEmptyOperation(
|
60 |
JDBCHelper helper, |
61 |
FeatureType featureType, |
62 |
TableReference table, |
63 |
String baseFilter,
|
64 |
FeatureQuery query |
65 |
) { |
66 |
super(helper);
|
67 |
this.featureType = featureType;
|
68 |
this.table = table;
|
69 |
this.baseFilter = baseFilter;
|
70 |
this.query = query;
|
71 |
} |
72 |
|
73 |
@Override
|
74 |
public final Object perform(Connection conn) throws DataException { |
75 |
return this.tableIsEmpty(conn); |
76 |
} |
77 |
|
78 |
public String getSQL() { |
79 |
JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
80 |
ExpressionBuilder expbuilder = sqlbuilder.expression(); |
81 |
|
82 |
sqlbuilder.select().column().all(); |
83 |
sqlbuilder.select().from().table() |
84 |
.database(this.table.getDatabase())
|
85 |
.schema(this.table.getSchema())
|
86 |
.name(this.table.getTable());
|
87 |
sqlbuilder.select().from().subquery(this.table.getSubquery());
|
88 |
if (!StringUtils.isEmpty(baseFilter)) {
|
89 |
sqlbuilder.select().where().set( expbuilder.custom(baseFilter) ); |
90 |
} |
91 |
|
92 |
Evaluator filter = query == null ? null : query.getFilter(); |
93 |
if (filter != null) { |
94 |
String sqlfilter = filter.getSQL();
|
95 |
if (!StringUtils.isEmpty(sqlfilter)) {
|
96 |
if (this.helper.supportFilter(this.featureType, filter)) { |
97 |
// El and() hace un set() si no hay un filtro previo
|
98 |
sqlbuilder.select().where().and(expbuilder.toValue(sqlfilter)); |
99 |
} |
100 |
} |
101 |
} |
102 |
// if (!StringUtils.isEmpty(filter)) {
|
103 |
// // El and() hace un set() si no hay un filtro previo
|
104 |
// sqlbuilder.select().where().and(expbuilder.toValue(filter));
|
105 |
// }
|
106 |
sqlbuilder.select().limit(1);
|
107 |
this.helper.processSpecialFunctions(sqlbuilder, featureType, null); |
108 |
// sqlbuilder.setProperties(
|
109 |
// ExpressionBuilder.Variable.class,
|
110 |
// PROP_TABLE, table
|
111 |
// );
|
112 |
sqlbuilder.setProperties( |
113 |
null,
|
114 |
PROP_FEATURE_TYPE, this.featureType,
|
115 |
PROP_TABLE, table, |
116 |
PROP_SYMBOLTABLE, this.query==null? null:this.query.getSymbolTable(), |
117 |
PROP_JDBCHELPER, this.helper
|
118 |
); |
119 |
String sql = sqlbuilder.select().toString();
|
120 |
return sql;
|
121 |
} |
122 |
|
123 |
public boolean tableIsEmpty(Connection conn) throws DataException { |
124 |
String sql = this.getSQL(); |
125 |
|
126 |
Statement st = null; |
127 |
ResultSet rs = null; |
128 |
try {
|
129 |
st = conn.createStatement(); |
130 |
rs = JDBCUtils.executeQuery(st, sql); |
131 |
return !rs.next();
|
132 |
|
133 |
} catch (SQLException ex) { |
134 |
throw new JDBCSQLException(ex); |
135 |
} finally {
|
136 |
JDBCUtils.closeQuietly(st); |
137 |
JDBCUtils.closeQuietly(rs); |
138 |
} |
139 |
} |
140 |
|
141 |
} |