Statistics
| Revision:

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 @ 45165

History | View | Annotate | Download (3.73 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.JDBCServerExplorerParameters;
34
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
35
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
36
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
37

    
38
public class ListTablesOperation extends AbstractConnectionOperation {
39
    
40
    private static final String METADATA_COLUMN_TABLE_CATALOG = "TABLE_CAT";
41
    private static final String METADATA_COLUMN_TABLE_SCHEMA = "TABLE_SCHEM";
42
    private static final String METADATA_COLUMN_TABLE_NAME = "TABLE_NAME";
43
    
44
    private boolean informationTables = false;
45
    private final JDBCServerExplorerParameters serverParameters;
46
    private final int mode;
47
    
48
    public ListTablesOperation(
49
            JDBCHelper helper,
50
            int mode,
51
            JDBCServerExplorerParameters serverParameters,
52
            boolean informationTables
53
        ) {
54
        super(helper);
55
        this.mode = mode;
56
        this.informationTables = informationTables;
57
        this.serverParameters = serverParameters;
58
    }
59

    
60
    @Override
61
    public final Object perform(Connection conn) throws DataException {
62
        List<JDBCStoreParameters> tables = this.listTables(
63
                conn,
64
                mode,
65
                serverParameters,
66
                informationTables
67
        );
68
        return tables;
69
    }
70

    
71
    public List<JDBCStoreParameters> listTables(
72
            Connection conn,
73
            int mode,
74
            JDBCServerExplorerParameters serverParameters,
75
            boolean informationTables
76
    ) {
77
        ResultSet rs = null;
78
        List<JDBCStoreParameters> tables = new ArrayList<>();
79

    
80
        try {
81
            String[] tableTypes = null;
82
            if (!informationTables) {
83
                tableTypes = new String[]{"TABLE", "VIEW"};
84
            }
85
            DatabaseMetaData metadata = conn.getMetaData();
86
            rs = metadata.getTables(null, "%", "%", tableTypes);
87
            while (rs.next()) {
88
                JDBCStoreParameters params = this.helper.createOpenStoreParameters(serverParameters);
89
                params.setCatalog(rs.getString(METADATA_COLUMN_TABLE_CATALOG));
90
                params.setSchema(rs.getString(METADATA_COLUMN_TABLE_SCHEMA));
91
                params.setTable(rs.getString(METADATA_COLUMN_TABLE_NAME));
92
                tables.add(params);
93
            }
94
            return tables;
95
            
96
        } catch (SQLException ex) {
97
            throw new RuntimeException("Can't fetch tables information",ex);
98
            
99
        } finally {
100
            JDBCUtils.closeQuietly(rs);
101
        }
102

    
103
    }
104
}