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

History | View | Annotate | Download (2.66 KB)

1
package org.gvsig.fmap.dal.store.jdbc2.spi.operations;
2

    
3
import java.sql.Connection;
4
import java.sql.DatabaseMetaData;
5
import java.sql.ResultSet;
6
import java.sql.SQLException;
7
import java.util.ArrayList;
8
import java.util.List;
9
import org.gvsig.fmap.dal.exception.DataException;
10
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
11
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
12
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
13

    
14
public class ListTablesOperation extends AbstractConnectionOperation {
15
    
16
    private static final String METADATA_COLUMN_TABLE_CATALOG = "TABLE_CAT";
17
    private static final String METADATA_COLUMN_TABLE_SCHEMA = "TABLE_SCHEM";
18
    private static final String METADATA_COLUMN_TABLE_NAME = "TABLE_NAME";
19
    
20
    private boolean informationTables = false;
21
    private final JDBCStoreParameters baseParameters;
22
    private final int mode;
23
    
24
    public ListTablesOperation(
25
            JDBCHelper helper,
26
            int mode,
27
            JDBCStoreParameters baseParameters,
28
            boolean informationTables
29
        ) {
30
        super(helper);
31
        this.mode = mode;
32
        this.informationTables = informationTables;
33
        this.baseParameters = baseParameters;
34
    }
35

    
36
    @Override
37
    public final Object perform(Connection conn) throws DataException {
38
        List<JDBCStoreParameters> tables = this.listTables(
39
                conn,
40
                mode,
41
                baseParameters,
42
                informationTables
43
        );
44
        return tables;
45
    }
46

    
47
    public List<JDBCStoreParameters> listTables(
48
            Connection conn,
49
            int mode,
50
            JDBCStoreParameters baseParameters,
51
            boolean informationTables
52
    ) {
53
        ResultSet rs = null;
54
        List<JDBCStoreParameters> tables = new ArrayList<>();
55

    
56
        try {
57
            String[] tableTypes = null;
58
            if (!informationTables) {
59
                tableTypes = new String[]{"TABLE", "VIEW"};
60
            }
61
            DatabaseMetaData metadata = conn.getMetaData();
62
            rs = metadata.getTables(null, null, null, tableTypes);
63
            while (rs.next()) {
64
                JDBCStoreParameters params = baseParameters.getCopy();
65
                params.setCatalog(rs.getString(METADATA_COLUMN_TABLE_CATALOG));
66
                params.setSchema(rs.getString(METADATA_COLUMN_TABLE_SCHEMA));
67
                params.setTable(rs.getString(METADATA_COLUMN_TABLE_NAME));
68
                tables.add(params);
69
            }
70
            return tables;
71
            
72
        } catch (SQLException ex) {
73
            throw new RuntimeException("Can't fetch tables information",ex);
74
            
75
        } finally {
76
            JDBCUtils.closeQuietly(rs);
77
        }
78

    
79
    }
80
}