gvsig-oracle / org.gvsig.oracle / trunk / org.gvsig.oracle / org.gvsig.oracle.provider / src / main / java / org / gvsig / oracle / dal / operations / OracleListTablesOperation.java @ 915
History | View | Annotate | Download (10 KB)
1 |
package org.gvsig.oracle.dal.operations; |
---|---|
2 |
|
3 |
import java.sql.PreparedStatement; |
4 |
import java.sql.ResultSet; |
5 |
import java.sql.SQLException; |
6 |
import java.sql.Statement; |
7 |
import java.util.ArrayList; |
8 |
import java.util.List; |
9 |
import org.apache.commons.lang3.StringUtils; |
10 |
import org.cresques.cts.IProjection; |
11 |
import org.gvsig.fmap.crs.CRSFactory; |
12 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
13 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParametersBase; |
14 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
15 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParametersBase; |
16 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCConnection; |
17 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
18 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCServerExplorer; |
19 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
20 |
import org.gvsig.fmap.dal.store.jdbc2.spi.operations.ListTablesOperation; |
21 |
import org.gvsig.oracle.dal.OracleStoreParameters; |
22 |
|
23 |
@SuppressWarnings("UseSpecificCatch") |
24 |
public class OracleListTablesOperation extends ListTablesOperation { |
25 |
|
26 |
private static final String METADATA_COLUMN_TABLE_OWNER = "OWNER"; |
27 |
private static final String METADATA_COLUMN_TABLE_NAME = "TABLE_NAME"; |
28 |
private static final String METADATA_COLUMN_AUTHORITY = "AUTHORITY"; |
29 |
private static final String METADATA_COLUMN_SRID = "SRID"; |
30 |
private static final String ALL_TABLES_NAME = "ALL_TABLES"; |
31 |
private static final String ALL_VIEWS_NAME = "ALL_VIEWS"; |
32 |
|
33 |
private static final String ALL_TABLES_QUERY = "SELECT ALL_TABLES.OWNER, ALL_TABLES.TABLE_NAME, coordref.DATA_SOURCE as AUTHORITY, meta.srid as SRID FROM ALL_TABLES\n" + |
34 |
"left join MDSYS.ALL_SDO_GEOM_METADATA meta on meta.TABLE_NAME = ALL_TABLES.TABLE_NAME \n" +
|
35 |
"left join SDO_COORD_REF_SYS coordRef ON coordRef.SRID = meta.SRID"; //SELECT OWNER, TABLE_NAME FROM ALL_TABLES"; |
36 |
private static final String ALL_VIEWS_QUERY = "SELECT ALL_VIEWS.OWNER, ALL_VIEWS.VIEW_NAME TABLE_NAME, coordref.DATA_SOURCE as AUTHORITY, meta.srid as SRID FROM ALL_VIEWS\n" + |
37 |
"left join MDSYS.ALL_SDO_GEOM_METADATA meta on meta.TABLE_NAME = ALL_VIEWS.VIEW_NAME\n" +
|
38 |
"left join SDO_COORD_REF_SYS coordRef ON coordRef.SRID = meta.SRID"; //SELECT OWNER, VIEW_NAME TABLE_NAME FROM ALL_VIEWS"; |
39 |
private static final String EXCLUDE_SYSTEM_TABLES_FILTER = "ALL_TABLES.OWNER != 'MDSYS' AND ALL_TABLES.OWNER != 'CTXSYS' AND ALL_TABLES.OWNER != 'EXFSYS' AND ALL_TABLES.OWNER != 'OLAPSYS' AND ALL_TABLES.OWNER != 'ORDDATA' AND ALL_TABLES.OWNER != 'ORDSYS' AND ALL_TABLES.OWNER != 'SYS' AND ALL_TABLES.OWNER != 'SYSTEM' AND ALL_TABLES.OWNER != 'WMSYS' AND ALL_TABLES.OWNER != 'XDB' AND ALL_TABLES.OWNER NOT LIKE 'APEX_%' AND ALL_TABLES.TABLE_NAME NOT LIKE '%$%'"; |
40 |
private static final String EXCLUDE_SYSTEM_VIEWS_FILTER = "ALL_VIEWS.OWNER != 'MDSYS' AND ALL_VIEWS.OWNER != 'CTXSYS' AND ALL_VIEWS.OWNER != 'EXFSYS' AND ALL_VIEWS.OWNER != 'OLAPSYS' AND ALL_VIEWS.OWNER != 'ORDDATA' AND ALL_VIEWS.OWNER != 'ORDSYS' AND ALL_VIEWS.OWNER != 'SYS' AND ALL_VIEWS.OWNER != 'SYSTEM' AND ALL_VIEWS.OWNER != 'WMSYS' AND ALL_VIEWS.OWNER != 'XDB' AND ALL_VIEWS.OWNER NOT LIKE 'APEX_%' AND ALL_VIEWS.VIEW_NAME NOT LIKE '%$%'"; |
41 |
|
42 |
|
43 |
public OracleListTablesOperation(JDBCHelper helper, int mode, JDBCServerExplorerParameters baseParameters, |
44 |
boolean informationTables, int tablesOrViews) { |
45 |
super(helper, mode, baseParameters, informationTables, tablesOrViews);
|
46 |
} |
47 |
|
48 |
@Override
|
49 |
public List<JDBCStoreParameters> listTables( |
50 |
JDBCConnection conn, |
51 |
int mode,
|
52 |
JDBCServerExplorerParameters serverParameters, |
53 |
boolean informationTables,
|
54 |
int tablesOrViews
|
55 |
) { |
56 |
try {
|
57 |
String schema = serverParameters.getSchema();
|
58 |
return this.getAllTablesAndViews(serverParameters, schema, informationTables, tablesOrViews); |
59 |
|
60 |
} catch (SQLException ex) { |
61 |
throw new RuntimeException("Can't fetch tables information", ex); |
62 |
|
63 |
} |
64 |
} |
65 |
|
66 |
protected List<JDBCStoreParameters> getAllTablesAndViews( |
67 |
JDBCServerExplorerParameters serverParameters, |
68 |
String schema,
|
69 |
boolean includeSystemTbls,
|
70 |
int tablesOrViews) throws SQLException { |
71 |
List<JDBCStoreParameters> tables = new ArrayList<>(); |
72 |
try {
|
73 |
StringBuilder builder = new StringBuilder(); |
74 |
boolean showTables = (tablesOrViews == JDBCServerExplorer.SHOW_TABLES_AND_VIEWS || tablesOrViews == JDBCServerExplorer.SHOW_TABLES);
|
75 |
boolean showViews = (tablesOrViews == JDBCServerExplorer.SHOW_TABLES_AND_VIEWS || tablesOrViews == JDBCServerExplorer.SHOW_VIEWS);
|
76 |
boolean showTablesAndViews = (tablesOrViews == JDBCServerExplorer.SHOW_TABLES_AND_VIEWS);
|
77 |
|
78 |
String owner = schema;
|
79 |
if (owner == null) { |
80 |
owner = serverParameters.getUser(); |
81 |
} |
82 |
|
83 |
boolean where = false; |
84 |
|
85 |
if(showTables){
|
86 |
builder.append(ALL_TABLES_QUERY); |
87 |
if (!includeSystemTbls) {
|
88 |
builder.append(" WHERE ");
|
89 |
where = true;
|
90 |
builder.append(EXCLUDE_SYSTEM_TABLES_FILTER); |
91 |
} |
92 |
if (owner != null) { |
93 |
if (where) {
|
94 |
builder.append(" AND ");
|
95 |
} else {
|
96 |
builder.append(" WHERE ");
|
97 |
} |
98 |
builder.append(ALL_TABLES_NAME); |
99 |
builder.append(".");
|
100 |
builder.append(METADATA_COLUMN_TABLE_OWNER); |
101 |
builder.append(" = '");
|
102 |
builder.append(owner); |
103 |
builder.append("'");
|
104 |
} |
105 |
} |
106 |
if(showTablesAndViews){
|
107 |
builder.append(" UNION ");
|
108 |
} |
109 |
|
110 |
if(showViews){
|
111 |
builder.append(ALL_VIEWS_QUERY); |
112 |
if (!includeSystemTbls) {
|
113 |
builder.append(" WHERE ");
|
114 |
where = true;
|
115 |
builder.append(EXCLUDE_SYSTEM_VIEWS_FILTER); |
116 |
} else {
|
117 |
where = false;
|
118 |
} |
119 |
if (owner != null) { |
120 |
if (where) {
|
121 |
builder.append(" AND ");
|
122 |
} else {
|
123 |
builder.append(" WHERE ");
|
124 |
} |
125 |
builder.append(ALL_VIEWS_NAME); |
126 |
builder.append(".");
|
127 |
builder.append(METADATA_COLUMN_TABLE_OWNER); |
128 |
builder.append(" = '");
|
129 |
builder.append(owner); |
130 |
builder.append("'");
|
131 |
} |
132 |
} |
133 |
Statement st = null; |
134 |
ResultSet rs = null; |
135 |
|
136 |
try {
|
137 |
st = this.getConnection().createStatement();
|
138 |
rs = JDBCUtils.executeQuery(st, builder.toString()); |
139 |
while (rs.next()) {
|
140 |
OracleStoreParameters params = (OracleStoreParameters) this.helper.createOpenStoreParameters(serverParameters);
|
141 |
params.setSchema(rs.getString(METADATA_COLUMN_TABLE_OWNER)); |
142 |
String table = rs.getString(METADATA_COLUMN_TABLE_NAME);
|
143 |
params.setTable(table); |
144 |
if( StringUtils.isNotBlank(table) && !table.equals(table.toUpperCase()) ) {
|
145 |
// !aaa.equals(AAA) -> !false --> true
|
146 |
// !AAA.equals(AAA) -> !true --> false
|
147 |
params.setForceUppercaseInTableName(false);
|
148 |
} |
149 |
String authority = rs.getString(METADATA_COLUMN_AUTHORITY);
|
150 |
String srs = rs.getString(METADATA_COLUMN_SRID);
|
151 |
if(StringUtils.isBlank(authority)) {
|
152 |
if(srs!=null){ |
153 |
IProjection projection = this.helper.getSRSSolver().getProjection(this.getConnection(), Integer.valueOf(srs)); |
154 |
if (projection != null) { |
155 |
params.setCRS(projection); |
156 |
} |
157 |
} |
158 |
} else if(StringUtils.isNotBlank(srs)) { |
159 |
try {
|
160 |
IProjection projection = CRSFactory.getCRS(authority + ":" + srs);
|
161 |
if (projection != null) { |
162 |
params.setCRS(projection); |
163 |
} |
164 |
} catch (Exception e) { |
165 |
//Si falla continuamos cargando el resto de tablas dejando esta sin proyeccion
|
166 |
LOGGER.trace("Can't get projection from table '"+table+"'",e); |
167 |
} |
168 |
} |
169 |
tables.add(params); |
170 |
} |
171 |
} finally {
|
172 |
JDBCUtils.closeQuietly(rs); |
173 |
JDBCUtils.closeQuietly(st); |
174 |
} |
175 |
} catch (Exception ex) { |
176 |
LOGGER.trace("Can't get the list of tables accessible by the user.", ex);
|
177 |
throw new SQLException(ex); |
178 |
} |
179 |
return tables;
|
180 |
|
181 |
} |
182 |
|
183 |
private IProjection getProjection(String table) { |
184 |
StringBuilder srsQuery = new StringBuilder(); |
185 |
|
186 |
srsQuery.append("select coordRef.DATA_SOURCE, meta.SRID from MDSYS.ALL_SDO_GEOM_METADATA meta left join SDO_COORD_REF_SYS coordRef ON coordRef.SRID = meta.SRID where TABLE_NAME = UPPER(?)");
|
187 |
|
188 |
PreparedStatement st = null; |
189 |
String authority;
|
190 |
String srs;
|
191 |
IProjection projection = null;
|
192 |
try {
|
193 |
String sql = srsQuery.toString();
|
194 |
st = conn.prepareStatement(sql); |
195 |
st.setString(1, table);
|
196 |
|
197 |
ResultSet rs = JDBCUtils.executeQuery(st, sql);
|
198 |
if (rs.next()) {
|
199 |
authority = rs.getString(1);
|
200 |
srs = rs.getString(2);
|
201 |
projection = CRSFactory.getCRS(authority + ":" + srs);
|
202 |
|
203 |
} |
204 |
} catch (SQLException ex) { |
205 |
throw new RuntimeException("Can't get srs from table " + table, ex); |
206 |
} finally {
|
207 |
JDBCUtils.closeQuietly(st); |
208 |
} |
209 |
return projection;
|
210 |
|
211 |
} |
212 |
} |