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 @ 43020
History | View | Annotate | Download (2.87 KB)
1 | 43020 | jjdelcerro | package org.gvsig.fmap.dal.store.jdbc2.spi.operations; |
---|---|---|---|
2 | |||
3 | import java.sql.Connection; |
||
4 | import java.sql.ResultSet; |
||
5 | import java.sql.SQLException; |
||
6 | import java.sql.Statement; |
||
7 | import org.apache.commons.lang3.StringUtils; |
||
8 | import org.gvsig.fmap.dal.exception.DataException; |
||
9 | import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
||
10 | import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase; |
||
11 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
||
12 | import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
||
13 | |||
14 | public class TableIsEmptyOperation extends AbstractConnectionOperation { |
||
15 | |||
16 | private final String schemaName; |
||
17 | private final String tableName; |
||
18 | private final String subquery; |
||
19 | private final String baseFilter; |
||
20 | private final String filter; |
||
21 | private final String dbName; |
||
22 | |||
23 | public TableIsEmptyOperation(
|
||
24 | JDBCHelper helper |
||
25 | ) { |
||
26 | this(helper, null, null, null, null, null, null); |
||
27 | } |
||
28 | |||
29 | public TableIsEmptyOperation(
|
||
30 | JDBCHelper helper, |
||
31 | String dbName,
|
||
32 | String schemaName,
|
||
33 | String tableName,
|
||
34 | String subquery,
|
||
35 | String baseFilter,
|
||
36 | String filter
|
||
37 | ) { |
||
38 | super(helper);
|
||
39 | this.dbName = dbName;
|
||
40 | this.schemaName = schemaName;
|
||
41 | this.tableName = tableName;
|
||
42 | this.subquery = subquery;
|
||
43 | this.baseFilter = baseFilter;
|
||
44 | this.filter = filter;
|
||
45 | } |
||
46 | |||
47 | @Override
|
||
48 | public final Object perform(Connection conn) throws DataException { |
||
49 | return this.tableIsEmpty(conn, |
||
50 | dbName, schemaName, tableName, subquery, baseFilter, filter); |
||
51 | } |
||
52 | |||
53 | public boolean tableIsEmpty(Connection conn, |
||
54 | String database,
|
||
55 | String schema,
|
||
56 | String table,
|
||
57 | String subquery,
|
||
58 | String baseFilter,
|
||
59 | String filter) throws DataException { |
||
60 | |||
61 | JDBCSQLBuilderBase sqlbuilder = this.createSQLBuilder();
|
||
62 | |||
63 | sqlbuilder.select().column().all(); |
||
64 | sqlbuilder.select().from().table().database(database).schema(schema).name(table); |
||
65 | sqlbuilder.select().from().subquery(subquery); |
||
66 | if (!StringUtils.isEmpty(baseFilter)) {
|
||
67 | sqlbuilder.select().where().set( sqlbuilder.custom(baseFilter) ); |
||
68 | } |
||
69 | if (!StringUtils.isEmpty(filter)) {
|
||
70 | // El and() hace un set() si no hay un filtro previo
|
||
71 | sqlbuilder.select().where().and(sqlbuilder.custom(filter)); |
||
72 | } |
||
73 | sqlbuilder.select().limit(1);
|
||
74 | |||
75 | String sql = sqlbuilder.select().toString();
|
||
76 | |||
77 | Statement st = null; |
||
78 | ResultSet rs = null; |
||
79 | try {
|
||
80 | st = conn.createStatement(); |
||
81 | rs = JDBCUtils.executeQuery(st, sql); |
||
82 | return !rs.next();
|
||
83 | |||
84 | } catch (SQLException ex) { |
||
85 | throw new JDBCSQLException(ex); |
||
86 | } finally {
|
||
87 | JDBCUtils.closeQuietly(st); |
||
88 | JDBCUtils.closeQuietly(rs); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | } |