Statistics
| Revision:

gvsig-oracle / org.gvsig.oracle / trunk / org.gvsig.oracle / org.gvsig.oracle.provider / src / main / java / org / gvsig / oracle / dal / operations / OracleListTablesOperation.java @ 61

History | View | Annotate | Download (4.31 KB)

1
package org.gvsig.oracle.dal.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.sql.Statement;
8
import java.util.ArrayList;
9
import java.util.List;
10

    
11
import org.apache.commons.lang3.StringUtils;
12
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters;
13
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper;
14
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils;
15
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase;
16
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ListTablesOperation;
17
import org.gvsig.fmap.geom.type.GeometryType;
18

    
19
public class OracleListTablesOperation extends ListTablesOperation {
20
    private static final String METADATA_COLUMN_TABLE_OWNER = "OWNER";
21
    private static final String METADATA_COLUMN_TABLE_NAME = "TABLE_NAME";
22
    
23
    private static final String ALL_VIEWS_QUERY = "SELECT OWNER, VIEW_NAME TABLE_NAME FROM ALL_VIEWS";
24
    private static final String ALL_TABLES_QUERY = "SELECT OWNER, TABLE_NAME FROM ALL_TABLES";
25
    private static final String EXCLUDE_SYSTEM_VIEWS_FILTER = "OWNER != 'MDSYS' AND OWNER != 'CTXSYS' AND OWNER != 'EXFSYS' AND OWNER != 'OLAPSYS' AND OWNER != 'ORDDATA' AND OWNER != 'ORDSYS' AND OWNER != 'SYS' AND OWNER != 'SYSTEM' AND OWNER != 'WMSYS' AND OWNER != 'XDB' AND OWNER NOT LIKE 'APEX_%' AND VIEW_NAME NOT LIKE '%$%'";
26
    private static final String EXCLUDE_SYSTEM_TABLES_FILTER = "OWNER != 'MDSYS' AND OWNER != 'CTXSYS' AND OWNER != 'EXFSYS' AND OWNER != 'OLAPSYS' AND OWNER != 'ORDDATA' AND OWNER != 'ORDSYS' AND OWNER != 'SYS' AND OWNER != 'SYSTEM' AND OWNER != 'WMSYS' AND OWNER != 'XDB' AND OWNER NOT LIKE 'APEX_%' AND TABLE_NAME NOT LIKE '%$%'";
27

    
28
        public OracleListTablesOperation(JDBCHelper helper, int mode, JDBCStoreParameters baseParameters,
29
                        boolean informationTables) {
30
                super(helper, mode, baseParameters, informationTables);
31
        }
32
        
33
    public List<JDBCStoreParameters> listTables(
34
            Connection conn,
35
            int mode,
36
            JDBCStoreParameters baseParameters,
37
            boolean informationTables
38
    ) {
39
        try {
40
                String schema = baseParameters.getSchema();
41
                return this.getAllTablesAndViews(baseParameters, schema, false);                
42
            
43
        } catch (SQLException ex) {
44
            throw new RuntimeException("Can't fetch tables information",ex);
45
            
46
        }
47
    }
48
    
49
    protected List<JDBCStoreParameters> getAllTablesAndViews(
50
                    JDBCStoreParameters baseParameters,
51
                    String schema,
52
                    boolean includeSystemTbls) throws SQLException {
53
            List<JDBCStoreParameters> tables = new ArrayList<>();
54
            try {
55
                    StringBuilder builder = new StringBuilder();
56
                    builder.append(ALL_TABLES_QUERY);
57
                    boolean where = false;
58
                    if (!includeSystemTbls) {
59
                            builder.append(" WHERE ");
60
                            where = true;
61
                            builder.append(EXCLUDE_SYSTEM_TABLES_FILTER);
62
                    }
63
                    if (schema!=null) {
64
                            if (!where) {
65
                                    builder.append(" WHERE ");
66
                            }
67
                            builder.append(METADATA_COLUMN_TABLE_OWNER);
68
                            builder.append(" = ");
69
                            builder.append(schema);
70
                    }
71
                    builder.append(" UNION ");
72
                    builder.append(ALL_VIEWS_QUERY);
73
                    if (!includeSystemTbls) {
74
                            builder.append(" WHERE ");
75
                            where = true;
76
                            builder.append(EXCLUDE_SYSTEM_VIEWS_FILTER);
77
                    }
78
                    else {
79
                            where = false;
80
                    }
81
                    if (schema!=null) {
82
                            if (!where) {
83
                                    builder.append(" WHERE ");
84
                            }
85
                            builder.append(METADATA_COLUMN_TABLE_OWNER);
86
                            builder.append(" = ");
87
                            builder.append(schema);
88
                    }
89
                Statement st = null;
90
                ResultSet rs = null;
91

    
92
                try {
93
                    st = this.getConnection().createStatement();
94
                    rs = JDBCUtils.executeQuery(st, builder.toString());
95
                    while (rs.next()) {
96
                        JDBCStoreParameters params = baseParameters.getCopy();
97
                        params.setSchema(rs.getString(METADATA_COLUMN_TABLE_OWNER));
98
                        params.setTable(rs.getString(METADATA_COLUMN_TABLE_NAME));
99
                        tables.add(params);
100
                    }
101
                } finally {
102
                    JDBCUtils.closeQuietly(rs);
103
                    JDBCUtils.closeQuietly(st);
104
                }
105
            } catch (Exception ex) {
106
                logger.debug("Can't get the list of tables accessible by the user.",ex);
107
                throw new SQLException(ex);
108
            }
109
            return tables;
110

    
111
    }
112
}