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 / ListTablesOperation.java @ 45152
History | View | Annotate | Download (3.6 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.DatabaseMetaData; |
28 |
import java.sql.ResultSet; |
29 |
import java.sql.SQLException; |
30 |
import java.util.ArrayList; |
31 |
import java.util.List; |
32 |
import org.gvsig.fmap.dal.exception.DataException; |
33 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
34 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
35 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
36 |
|
37 |
public class ListTablesOperation extends AbstractConnectionOperation { |
38 |
|
39 |
private static final String METADATA_COLUMN_TABLE_CATALOG = "TABLE_CAT"; |
40 |
private static final String METADATA_COLUMN_TABLE_SCHEMA = "TABLE_SCHEM"; |
41 |
private static final String METADATA_COLUMN_TABLE_NAME = "TABLE_NAME"; |
42 |
|
43 |
private boolean informationTables = false; |
44 |
private final JDBCStoreParameters baseParameters; |
45 |
private final int mode; |
46 |
|
47 |
public ListTablesOperation(
|
48 |
JDBCHelper helper, |
49 |
int mode,
|
50 |
JDBCStoreParameters baseParameters, |
51 |
boolean informationTables
|
52 |
) { |
53 |
super(helper);
|
54 |
this.mode = mode;
|
55 |
this.informationTables = informationTables;
|
56 |
this.baseParameters = baseParameters;
|
57 |
} |
58 |
|
59 |
@Override
|
60 |
public final Object perform(Connection conn) throws DataException { |
61 |
List<JDBCStoreParameters> tables = this.listTables( |
62 |
conn, |
63 |
mode, |
64 |
baseParameters, |
65 |
informationTables |
66 |
); |
67 |
return tables;
|
68 |
} |
69 |
|
70 |
public List<JDBCStoreParameters> listTables( |
71 |
Connection conn,
|
72 |
int mode,
|
73 |
JDBCStoreParameters baseParameters, |
74 |
boolean informationTables
|
75 |
) { |
76 |
ResultSet rs = null; |
77 |
List<JDBCStoreParameters> tables = new ArrayList<>(); |
78 |
|
79 |
try {
|
80 |
String[] tableTypes = null; |
81 |
if (!informationTables) {
|
82 |
tableTypes = new String[]{"TABLE", "VIEW"}; |
83 |
} |
84 |
DatabaseMetaData metadata = conn.getMetaData();
|
85 |
rs = metadata.getTables(null, "%", "%", tableTypes); |
86 |
while (rs.next()) {
|
87 |
JDBCStoreParameters params = baseParameters.getCopy(); |
88 |
params.setCatalog(rs.getString(METADATA_COLUMN_TABLE_CATALOG)); |
89 |
params.setSchema(rs.getString(METADATA_COLUMN_TABLE_SCHEMA)); |
90 |
params.setTable(rs.getString(METADATA_COLUMN_TABLE_NAME)); |
91 |
tables.add(params); |
92 |
} |
93 |
return tables;
|
94 |
|
95 |
} catch (SQLException ex) { |
96 |
throw new RuntimeException("Can't fetch tables information",ex); |
97 |
|
98 |
} finally {
|
99 |
JDBCUtils.closeQuietly(rs); |
100 |
} |
101 |
|
102 |
} |
103 |
} |