gvsig-oracle / org.gvsig.oracle / trunk / org.gvsig.oracle / org.gvsig.oracle.provider / src / main / java / org / gvsig / oracle / dal / operations / OracleListTablesOperation.java @ 74
History | View | Annotate | Download (4.49 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, informationTables); |
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(" AND ");
|
66 |
} |
67 |
else {
|
68 |
builder.append(" WHERE ");
|
69 |
} |
70 |
builder.append(METADATA_COLUMN_TABLE_OWNER); |
71 |
builder.append(" = '");
|
72 |
builder.append(schema); |
73 |
builder.append("'");
|
74 |
} |
75 |
builder.append(" UNION ");
|
76 |
builder.append(ALL_VIEWS_QUERY); |
77 |
if (!includeSystemTbls) {
|
78 |
builder.append(" WHERE ");
|
79 |
where = true;
|
80 |
builder.append(EXCLUDE_SYSTEM_VIEWS_FILTER); |
81 |
} |
82 |
else {
|
83 |
where = false;
|
84 |
} |
85 |
if (schema!=null) { |
86 |
if (where) {
|
87 |
builder.append(" AND ");
|
88 |
} |
89 |
else {
|
90 |
builder.append(" WHERE ");
|
91 |
} |
92 |
builder.append(METADATA_COLUMN_TABLE_OWNER); |
93 |
builder.append(" = '");
|
94 |
builder.append(schema); |
95 |
builder.append("'");
|
96 |
} |
97 |
Statement st = null; |
98 |
ResultSet rs = null; |
99 |
|
100 |
try {
|
101 |
st = this.getConnection().createStatement();
|
102 |
rs = JDBCUtils.executeQuery(st, builder.toString()); |
103 |
while (rs.next()) {
|
104 |
JDBCStoreParameters params = baseParameters.getCopy(); |
105 |
params.setSchema(rs.getString(METADATA_COLUMN_TABLE_OWNER)); |
106 |
params.setTable(rs.getString(METADATA_COLUMN_TABLE_NAME)); |
107 |
tables.add(params); |
108 |
} |
109 |
} finally {
|
110 |
JDBCUtils.closeQuietly(rs); |
111 |
JDBCUtils.closeQuietly(st); |
112 |
} |
113 |
} catch (Exception ex) { |
114 |
logger.debug("Can't get the list of tables accessible by the user.",ex);
|
115 |
throw new SQLException(ex); |
116 |
} |
117 |
return tables;
|
118 |
|
119 |
} |
120 |
} |