Revision 202
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/resources/org/gvsig/oracle/dal/OracleMetadata.xml | ||
---|---|---|
1 |
<?xml version="1.0"?> |
|
2 |
<definitions> |
|
3 |
<version>1.0.0</version> |
|
4 |
<classes> |
|
5 |
<class name="Oracle" namespace="Metadata"> |
|
6 |
<extends> |
|
7 |
<class name="JDBC2" namespace="Metadata"/> |
|
8 |
</extends> |
|
9 |
<description>Metadata of a Oracle store</description> |
|
10 |
<fields> |
|
11 |
</fields> |
|
12 |
</class> |
|
13 |
|
|
14 |
</classes> |
|
15 |
</definitions> |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/resources/org/gvsig/oracle/dal/OracleParameters.xml | ||
---|---|---|
1 |
<?xml version="1.0"?> |
|
2 |
<definitions> |
|
3 |
<version>1.0.0</version> |
|
4 |
<classes> |
|
5 |
<class name="OracleResourceParameters"> |
|
6 |
<extends> |
|
7 |
<class>JDBC2ResourceParameters</class> |
|
8 |
</extends> |
|
9 |
<fields> |
|
10 |
<field name="JDBCDriverClass" type="string" mandatory="true" |
|
11 |
defaultValue="oracle.jdbc.driver.OracleDriver" group="Advanced"> |
|
12 |
<description>JDBC Driver class</description> |
|
13 |
</field> |
|
14 |
<field name="port" type="integer" mandatory="false" |
|
15 |
defaultValue="1521" group="Connection"> |
|
16 |
<description></description> |
|
17 |
</field> |
|
18 |
<field name="host" type="string" mandatory="false" |
|
19 |
defaultValue="127.0.0.1" group="Connection"> |
|
20 |
<description></description> |
|
21 |
</field> |
|
22 |
</fields> |
|
23 |
</class> |
|
24 |
|
|
25 |
<class name="OracleStoreParameters"> |
|
26 |
<extends> |
|
27 |
<class>JDBC2StoreParameters</class> |
|
28 |
<class>OracleResourceParameters</class> |
|
29 |
</extends> |
|
30 |
<fields/> |
|
31 |
</class> |
|
32 |
|
|
33 |
<class name="OracleNewStoreParameters"> |
|
34 |
<extends> |
|
35 |
<class>JDBC2NewStoreParameters</class> |
|
36 |
<class>OracleResourceParameters</class> |
|
37 |
</extends> |
|
38 |
<fields/> |
|
39 |
</class> |
|
40 |
|
|
41 |
|
|
42 |
<class name="OracleServerExplorerParameters"> |
|
43 |
<extends> |
|
44 |
<class>OracleResourceParameters</class> |
|
45 |
<class>JDBC2ServerExplorerParameters</class> |
|
46 |
</extends> |
|
47 |
<fields/> |
|
48 |
</class> |
|
49 |
|
|
50 |
|
|
51 |
</classes> |
|
52 |
</definitions> |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/resources/META-INF/services/org.gvsig.tools.library.Library | ||
---|---|---|
1 |
org.gvsig.oracle.dal.OracleLibrary |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/OracleHelper.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.oracle.dal; |
|
3 |
|
|
4 |
import java.nio.ByteBuffer; |
|
5 |
import java.nio.ByteOrder; |
|
6 |
import java.sql.Connection; |
|
7 |
import java.sql.ResultSet; |
|
8 |
import java.sql.SQLException; |
|
9 |
import java.text.MessageFormat; |
|
10 |
import java.util.ArrayList; |
|
11 |
import java.util.List; |
|
12 |
|
|
13 |
import org.apache.commons.dbcp.BasicDataSource; |
|
14 |
import org.apache.commons.lang3.StringUtils; |
|
15 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
16 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilderHelper; |
|
17 |
import static org.gvsig.fmap.dal.DataManager.FUNCTION_EXISTS; |
|
18 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
19 |
import org.gvsig.fmap.dal.exception.DataException; |
|
20 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
21 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
|
22 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
|
23 |
import org.gvsig.fmap.dal.store.jdbc.JDBCNewStoreParameters; |
|
24 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
|
25 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
|
26 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCCantFetchValueException; |
|
27 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException; |
|
28 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
|
29 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCHelperBase; |
|
30 |
import org.gvsig.fmap.geom.Geometry; |
|
31 |
import org.gvsig.oracle.dal.operations.OracleOperationsFactory; |
|
32 |
import org.slf4j.Logger; |
|
33 |
import org.slf4j.LoggerFactory; |
|
34 |
|
|
35 |
public class OracleHelper extends JDBCHelperBase { |
|
36 |
|
|
37 |
static final Logger logger = LoggerFactory.getLogger(OracleHelper.class); |
|
38 |
|
|
39 |
public static final String ORACLE_JDBC_DRIVER = "oracle.jdbc.driver.OracleDriver"; |
|
40 |
|
|
41 |
public static String getConnectionURL(OracleConnectionParameters params) { |
|
42 |
return getConnectionURL( |
|
43 |
params.getHost(), |
|
44 |
params.getPort(), |
|
45 |
params.getDBName() |
|
46 |
); |
|
47 |
} |
|
48 |
|
|
49 |
public static String getConnectionURL(String host, Integer port, String db) { |
|
50 |
if( StringUtils.isEmpty(host) ) { |
|
51 |
throw new IllegalArgumentException("Parameter 'host' can't be null."); |
|
52 |
} |
|
53 |
if( port==null ) { |
|
54 |
throw new IllegalArgumentException("Parameter 'port' can't be null."); |
|
55 |
} |
|
56 |
if( StringUtils.isEmpty(db) ) { |
|
57 |
throw new IllegalArgumentException("Parameter 'db' can't be null."); |
|
58 |
} |
|
59 |
// |
|
60 |
// http://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html |
|
61 |
// |
|
62 |
String connectionURL = MessageFormat.format( |
|
63 |
"jdbc:oracle:thin:@{0}:{1,number,######}:{2}", |
|
64 |
host, |
|
65 |
port, |
|
66 |
db |
|
67 |
); |
|
68 |
logger.debug("connectionURL: {}", connectionURL); |
|
69 |
return connectionURL; |
|
70 |
} |
|
71 |
|
|
72 |
public static class ConnectionProviderImpl { |
|
73 |
|
|
74 |
private static boolean needRegisterDriver = true; |
|
75 |
|
|
76 |
private BasicDataSource dataSource = null; |
|
77 |
|
|
78 |
private final OracleConnectionParameters connectionParameters; |
|
79 |
|
|
80 |
public ConnectionProviderImpl(OracleConnectionParameters connectionParameters) { |
|
81 |
this.connectionParameters = connectionParameters; |
|
82 |
} |
|
83 |
|
|
84 |
public Connection getConnection() throws SQLException { |
|
85 |
if (this.dataSource == null) { |
|
86 |
this.dataSource = this.createDataSource(); |
|
87 |
this.dataSource.setAccessToUnderlyingConnectionAllowed(true); |
|
88 |
} |
|
89 |
Connection conn = this.dataSource.getConnection(); |
|
90 |
return conn; |
|
91 |
} |
|
92 |
|
|
93 |
private BasicDataSource createDataSource() throws SQLException { |
|
94 |
if (!this.isRegistered()) { |
|
95 |
this.registerDriver(); |
|
96 |
} |
|
97 |
OracleConnectionParameters params = connectionParameters; |
|
98 |
|
|
99 |
BasicDataSource ds = new BasicDataSource(); |
|
100 |
ds.setDriverClassName(params.getJDBCDriverClassName()); |
|
101 |
if( !StringUtils.isEmpty(params.getUser()) ) { |
|
102 |
ds.setUsername(params.getUser()); |
|
103 |
} |
|
104 |
if( !StringUtils.isEmpty(params.getPassword()) ) { |
|
105 |
ds.setPassword(params.getPassword()); |
|
106 |
} |
|
107 |
ds.setUrl(params.getUrl()); |
|
108 |
|
|
109 |
ds.setMaxWait(60L * 1000); |
|
110 |
return ds; |
|
111 |
} |
|
112 |
|
|
113 |
private boolean isRegistered() { |
|
114 |
return needRegisterDriver; |
|
115 |
} |
|
116 |
|
|
117 |
public void registerDriver() throws SQLException { |
|
118 |
String className = this.connectionParameters.getJDBCDriverClassName(); |
|
119 |
if (className == null) { |
|
120 |
return; |
|
121 |
} |
|
122 |
try { |
|
123 |
Class theClass = Class.forName(className); |
|
124 |
if (theClass == null) { |
|
125 |
throw new JDBCDriverClassNotFoundException(OracleLibrary.NAME, className); |
|
126 |
} |
|
127 |
} catch (Exception e) { |
|
128 |
throw new SQLException("Can't register JDBC driver '" + className + "'.", e); |
|
129 |
} |
|
130 |
needRegisterDriver = false; |
|
131 |
} |
|
132 |
|
|
133 |
} |
|
134 |
|
|
135 |
private ConnectionProviderImpl connectionProvider = null; |
|
136 |
|
|
137 |
public OracleHelper(JDBCConnectionParameters connectionParameters) { |
|
138 |
super(connectionParameters); |
|
139 |
} |
|
140 |
|
|
141 |
/** |
|
142 |
* Constructor for use only for testing purposes. |
|
143 |
* @param connectionParameters |
|
144 |
* @param connectionProvider |
|
145 |
*/ |
|
146 |
public OracleHelper(JDBCConnectionParameters connectionParameters, ConnectionProviderImpl connectionProvider) { |
|
147 |
super(connectionParameters); |
|
148 |
this.connectionProvider = connectionProvider; |
|
149 |
} |
|
150 |
|
|
151 |
@Override |
|
152 |
public Connection getConnection() throws AccessResourceException { |
|
153 |
try { |
|
154 |
if (this.connectionProvider == null) { |
|
155 |
this.connectionProvider = new ConnectionProviderImpl(this.getConnectionParameters()); |
|
156 |
} |
|
157 |
return this.connectionProvider.getConnection(); |
|
158 |
} catch (SQLException ex) { |
|
159 |
throw new AccessResourceException(OracleLibrary.NAME, ex); |
|
160 |
} |
|
161 |
} |
|
162 |
|
|
163 |
@Override |
|
164 |
public OracleConnectionParameters getConnectionParameters() { |
|
165 |
return (OracleConnectionParameters) super.getConnectionParameters(); |
|
166 |
} |
|
167 |
|
|
168 |
@Override |
|
169 |
public String getConnectionURL() { |
|
170 |
return getConnectionURL(this.getConnectionParameters()); |
|
171 |
} |
|
172 |
|
|
173 |
@Override |
|
174 |
protected String getResourceType() { |
|
175 |
return OracleLibrary.NAME; |
|
176 |
} |
|
177 |
|
|
178 |
@Override |
|
179 |
public String getProviderName() { |
|
180 |
return OracleLibrary.NAME; |
|
181 |
} |
|
182 |
|
|
183 |
@Override |
|
184 |
public OracleSQLBuilder createSQLBuilder() { |
|
185 |
return new OracleSQLBuilder(this); |
|
186 |
} |
|
187 |
|
|
188 |
@Override |
|
189 |
public OperationsFactory getOperations() { |
|
190 |
if (this.operationsFactory == null) { |
|
191 |
this.operationsFactory = new OracleOperationsFactory(this); |
|
192 |
} |
|
193 |
return operationsFactory; |
|
194 |
} |
|
195 |
|
|
196 |
@Override |
|
197 |
public GeometryExpressionBuilderHelper.GeometrySupportType getGeometrySupportType() { |
|
198 |
return GeometryExpressionBuilderHelper.GeometrySupportType.WKB; |
|
199 |
} |
|
200 |
|
|
201 |
@Override |
|
202 |
public boolean hasSpatialFunctions() { |
|
203 |
return true; |
|
204 |
} |
|
205 |
|
|
206 |
@Override |
|
207 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) { |
|
208 |
return true; |
|
209 |
} |
|
210 |
|
|
211 |
@Override |
|
212 |
public String getQuoteForIdentifiers() { |
|
213 |
return "\""; |
|
214 |
} |
|
215 |
|
|
216 |
@Override |
|
217 |
public boolean allowAutomaticValues() { |
|
218 |
return true; |
|
219 |
} |
|
220 |
|
|
221 |
@Override |
|
222 |
public boolean supportOffsetInSelect() { |
|
223 |
return true; |
|
224 |
} |
|
225 |
|
|
226 |
@Override |
|
227 |
public String getQuoteForStrings() { |
|
228 |
return "\'"; |
|
229 |
} |
|
230 |
|
|
231 |
@Override |
|
232 |
public String getSourceId(JDBCStoreParameters parameters) { |
|
233 |
return parameters.getDBName() + "." + |
|
234 |
parameters.getSchema()+ "." + |
|
235 |
parameters.getTable(); |
|
236 |
} |
|
237 |
|
|
238 |
@Override |
|
239 |
public JDBCNewStoreParameters createNewStoreParameters() { |
|
240 |
return new OracleNewStoreParameters(); |
|
241 |
} |
|
242 |
|
|
243 |
@Override |
|
244 |
public JDBCStoreParameters createOpenStoreParameters() { |
|
245 |
return new OracleStoreParameters(); |
|
246 |
} |
|
247 |
|
|
248 |
@Override |
|
249 |
public JDBCServerExplorerParameters createServerExplorerParameters() { |
|
250 |
return new OracleExplorerParameters(); |
|
251 |
} |
|
252 |
|
|
253 |
@Override |
|
254 |
public Geometry getGeometryFromColumn(ResultSet rs, int index) throws DataException { |
|
255 |
try { |
|
256 |
// Oracle uses non-standard WKB encoding for rectangles (used in bounding boxes) |
|
257 |
// Correct it when needed |
|
258 |
byte[] wkb = (byte[]) rs.getBytes(index); |
|
259 |
|
|
260 |
if(wkb!=null){ |
|
261 |
ByteBuffer data = ByteBuffer.wrap(wkb); |
|
262 |
byte endian = data.get(); |
|
263 |
if (endian == 1) { |
|
264 |
data.order(ByteOrder.LITTLE_ENDIAN); |
|
265 |
} |
|
266 |
int typeword = data.getInt(); |
|
267 |
int realtype = typeword & 0x1FFFFFFF; // cut off high flag bits |
|
268 |
if (realtype==3) { // polygon |
|
269 |
int rings = data.getInt(); |
|
270 |
if (rings==1) { |
|
271 |
int count = data.getInt(); |
|
272 |
if (count==2) { // Declares 2 points but it has in fact 5 |
|
273 |
data.putInt(9, 5); |
|
274 |
} |
|
275 |
} |
|
276 |
} |
|
277 |
return this.getGeometryManager().createFrom(wkb); |
|
278 |
} |
|
279 |
} catch (Exception ex) { |
|
280 |
throw new JDBCCantFetchValueException(ex); |
|
281 |
} |
|
282 |
return null; |
|
283 |
} |
|
284 |
|
|
285 |
|
|
286 |
@Override |
|
287 |
public void processSpecialFunctions( |
|
288 |
SQLBuilder sqlbuilder, |
|
289 |
FeatureType type, |
|
290 |
List<String> extra_column_names) { |
|
291 |
replaceForeingValueFunction(sqlbuilder, type, extra_column_names); |
|
292 |
replaceExistsFunction(sqlbuilder, type, extra_column_names); |
|
293 |
} |
|
294 |
|
|
295 |
private void replaceExistsFunction( |
|
296 |
SQLBuilder sqlbuilder, |
|
297 |
FeatureType type, |
|
298 |
final List<String> extra_column_names) { |
|
299 |
|
|
300 |
// Si lse encuentra una construccion del tipo: |
|
301 |
// SELECT ... FROM ... WHERE ... EXISTS(list, 'EXISTS_ID') ... |
|
302 |
// se traslada a: |
|
303 |
// SELECT ... EXISTS(list) AS EXISTS_ID FROM ... WHERE ... EXISTS(list) ... |
|
304 |
// Y se a?ade el valor ESISTS_ID a las columnas extra a recuperar de la consulta. |
|
305 |
// |
|
306 |
final SQLBuilder.SelectBuilder select = sqlbuilder.select(); |
|
307 |
final ExpressionBuilder where = select.where(); |
|
308 |
if (where == null || where.isEmpty() || select.has_group_by()) { |
|
309 |
return; |
|
310 |
} |
|
311 |
final List<ExpressionBuilder.Value[]> value_replacements = new ArrayList<>(); |
|
312 |
where.accept(new ExpressionBuilder.Visitor() { |
|
313 |
@Override |
|
314 |
public void visit(ExpressionBuilder.Visitable value) { |
|
315 |
if (!(value instanceof ExpressionBuilder.Function)) { |
|
316 |
return; |
|
317 |
} |
|
318 |
ExpressionBuilder.Function function = (ExpressionBuilder.Function) value; |
|
319 |
if (!StringUtils.equalsIgnoreCase(function.name(), FUNCTION_EXISTS)) { |
|
320 |
return; |
|
321 |
} |
|
322 |
if (function.parameters().size() != 2) { |
|
323 |
return; |
|
324 |
} |
|
325 |
ExpressionBuilder.Value arg0 = function.parameters().get(0); |
|
326 |
ExpressionBuilder.Value arg1 = function.parameters().get(1); |
|
327 |
if (arg1 == null) { |
|
328 |
return; |
|
329 |
} |
|
330 |
String columnName = (String) ((ExpressionBuilder.Constant) arg1).value(); |
|
331 |
SQLBuilder.SelectColumnBuilder column = select.column(); |
|
332 |
|
|
333 |
|
|
334 |
// Instead of add column with "EXISTS" function, |
|
335 |
// we add a column with "CASE" function |
|
336 |
// for convert result of "EXISTS" to integer. |
|
337 |
// "CASE WHEN {FUNCTION_EXISTS} THEN 1 ELSE 0 END", |
|
338 |
|
|
339 |
column.value( |
|
340 |
sqlbuilder.expression().function( |
|
341 |
"CASE", |
|
342 |
sqlbuilder.expression().function(FUNCTION_EXISTS, arg0), |
|
343 |
sqlbuilder.expression().constant(1), |
|
344 |
sqlbuilder.expression().constant(0) |
|
345 |
) |
|
346 |
); |
|
347 |
|
|
348 |
column.as(columnName); |
|
349 |
|
|
350 |
if (extra_column_names != null) { |
|
351 |
extra_column_names.add(columnName); |
|
352 |
} |
|
353 |
} |
|
354 |
}, null); |
|
355 |
if (value_replacements.isEmpty()) { |
|
356 |
return; |
|
357 |
} |
|
358 |
// Realizamos los reemplazos calculados previamente (value_replacements). |
|
359 |
for (ExpressionBuilder.Value[] replaceValue : value_replacements) { |
|
360 |
ExpressionBuilder.Value target = replaceValue[0]; |
|
361 |
ExpressionBuilder.Value replacement = replaceValue[1]; |
|
362 |
sqlbuilder.select().replace(target, replacement); |
|
363 |
} |
|
364 |
} |
|
365 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/OracleStoreProviderFactory.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.oracle.dal; |
|
3 |
|
|
4 |
import org.gvsig.fmap.dal.DataParameters; |
|
5 |
import org.gvsig.fmap.dal.exception.InitializeException; |
|
6 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
|
7 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
|
8 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
|
9 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCHelper; |
|
10 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCStoreProvider; |
|
11 |
import org.gvsig.fmap.dal.store.jdbc2.impl.JDBCStoreProviderFactory; |
|
12 |
import org.slf4j.Logger; |
|
13 |
import org.slf4j.LoggerFactory; |
|
14 |
|
|
15 |
|
|
16 |
public class OracleStoreProviderFactory extends JDBCStoreProviderFactory { |
|
17 |
|
|
18 |
private static final Logger LOGGER = LoggerFactory.getLogger(OracleStoreProviderFactory.class); |
|
19 |
|
|
20 |
public OracleStoreProviderFactory() { |
|
21 |
super( |
|
22 |
OracleLibrary.NAME, |
|
23 |
OracleLibrary.NAME + " store" |
|
24 |
); |
|
25 |
} |
|
26 |
|
|
27 |
@Override |
|
28 |
public JDBCStoreProvider createProvider( |
|
29 |
DataParameters parameters, |
|
30 |
DataStoreProviderServices providerServices |
|
31 |
) throws InitializeException { |
|
32 |
JDBCHelper helper = new OracleHelper((JDBCConnectionParameters) parameters); |
|
33 |
JDBCStoreProvider provider = null; |
|
34 |
try { |
|
35 |
provider = helper.createProvider( |
|
36 |
(JDBCStoreParameters) parameters, |
|
37 |
providerServices |
|
38 |
); |
|
39 |
} |
|
40 |
catch (Exception exc) { |
|
41 |
LOGGER.error("Can't create Oracle provider", exc); |
|
42 |
} |
|
43 |
return provider; |
|
44 |
} |
|
45 |
|
|
46 |
@Override |
|
47 |
public JDBCStoreParameters createParameters() { |
|
48 |
JDBCStoreParameters params = new OracleStoreParameters(); |
|
49 |
return params; |
|
50 |
} |
|
51 |
|
|
52 |
public boolean preferNotToUseNonNullRestrictions() { |
|
53 |
return true; |
|
54 |
} |
|
55 |
|
|
56 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/OracleStoreParameters.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
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 2 |
|
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 |
*/ |
|
22 |
package org.gvsig.oracle.dal; |
|
23 |
|
|
24 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
25 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
|
26 |
|
|
27 |
public class OracleStoreParameters extends JDBCStoreParameters implements OracleConnectionParameters { |
|
28 |
|
|
29 |
private final OracleConnectionParametersHelper helper; |
|
30 |
|
|
31 |
public OracleStoreParameters() { |
|
32 |
super( |
|
33 |
OracleLibrary.NAME + "StoreParameters", |
|
34 |
OracleLibrary.NAME |
|
35 |
); |
|
36 |
this.helper = new OracleConnectionParametersHelper(this); |
|
37 |
} |
|
38 |
|
|
39 |
@Override |
|
40 |
public String getUrl() { |
|
41 |
return this.helper.getUrl(); |
|
42 |
} |
|
43 |
|
|
44 |
@Override |
|
45 |
public void validate() throws ValidateDataParametersException { |
|
46 |
this.helper.validate(); |
|
47 |
super.validate(); |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
public void setDynValue(String name, Object value) { |
|
52 |
if (!URL_PARAMTER_NAME.equals(name)) { |
|
53 |
// reset URL each time a parameter changes |
|
54 |
super.setDynValue(URL_PARAMTER_NAME, null); |
|
55 |
} |
|
56 |
super.setDynValue(name, value); |
|
57 |
} |
|
58 |
|
|
59 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/SpatialIndexUtils.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.oracle.dal; |
|
7 |
|
|
8 |
import java.sql.Connection; |
|
9 |
import java.sql.PreparedStatement; |
|
10 |
import java.sql.ResultSet; |
|
11 |
import org.apache.commons.lang3.StringUtils; |
|
12 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCUtils; |
|
13 |
import org.gvsig.oracle.dal.operations.OracleUpdateSpatialIndexAndMetadata; |
|
14 |
import org.slf4j.Logger; |
|
15 |
import org.slf4j.LoggerFactory; |
|
16 |
|
|
17 |
/** |
|
18 |
* |
|
19 |
* @author fdiaz |
|
20 |
*/ |
|
21 |
public class SpatialIndexUtils { |
|
22 |
|
|
23 |
private static final Logger LOGGER = LoggerFactory.getLogger(OracleUpdateSpatialIndexAndMetadata.class); |
|
24 |
|
|
25 |
|
|
26 |
public static String getSpatialIndexName(String table, String geomColumn) { |
|
27 |
return ("SDX_" + table + "_" + geomColumn).toUpperCase(); |
|
28 |
} |
|
29 |
|
|
30 |
public static String getSpatialIndex(Connection connection, String schema, String table, String geomColumnName) { |
|
31 |
ResultSet rs = null; |
|
32 |
PreparedStatement st = null; |
|
33 |
try { |
|
34 |
String sql = "SELECT SDO_INDEX_NAME " |
|
35 |
+ "FROM ALL_SDO_INDEX_METADATA tmd, ALL_SDO_INDEX_INFO tinfo " |
|
36 |
+ "WHERE tmd.SDO_INDEX_OWNER = tinfo.SDO_INDEX_OWNER " |
|
37 |
+ "AND tmd.SDO_INDEX_NAME = tinfo.INDEX_NAME " |
|
38 |
+ "AND TABLE_OWNER = ? AND TABLE_NAME = ? AND COLUMN_NAME = ?"; |
|
39 |
|
|
40 |
st = connection.prepareStatement(sql); |
|
41 |
st.setString(1, schema); |
|
42 |
st.setString(2, table); |
|
43 |
st.setString(3, geomColumnName); |
|
44 |
|
|
45 |
rs = JDBCUtils.executeQuery(st, sql); |
|
46 |
String indexName; |
|
47 |
if (rs.next()) { |
|
48 |
indexName = rs.getString(1); |
|
49 |
return indexName; |
|
50 |
} |
|
51 |
} catch (Exception ex) { |
|
52 |
LOGGER.debug("Can't get geometry type from column '" + geomColumnName + "'.", ex); |
|
53 |
} finally { |
|
54 |
JDBCUtils.closeQuietly(rs); |
|
55 |
JDBCUtils.closeQuietly(st); |
|
56 |
} |
|
57 |
return null; |
|
58 |
} |
|
59 |
|
|
60 |
public static boolean isValidSpatialIndex(Connection connection, String table) { |
|
61 |
ResultSet rs = null; |
|
62 |
PreparedStatement st = null; |
|
63 |
try { |
|
64 |
String sql = "SELECT DOMIDX_status, domidx_opstatus " |
|
65 |
+ "FROM all_indexes " |
|
66 |
+ "WHERE TABLE_NAME = ?" |
|
67 |
+ " and INDEX_TYPE = 'DOMAIN'"; |
|
68 |
|
|
69 |
|
|
70 |
st = connection.prepareStatement(sql); |
|
71 |
st.setString(1, table); |
|
72 |
|
|
73 |
rs = JDBCUtils.executeQuery(st, sql); |
|
74 |
String status; |
|
75 |
String opstatus; |
|
76 |
if (rs.next()) { |
|
77 |
status = rs.getString(1); |
|
78 |
opstatus = rs.getString(2); |
|
79 |
return StringUtils.equalsIgnoreCase(status, "VALID") && StringUtils.equalsIgnoreCase(opstatus, "VALID"); |
|
80 |
} |
|
81 |
} catch (Exception ex) { |
|
82 |
LOGGER.debug("Can't get status from index.", ex); |
|
83 |
} finally { |
|
84 |
JDBCUtils.closeQuietly(rs); |
|
85 |
JDBCUtils.closeQuietly(st); |
|
86 |
} |
|
87 |
return false; |
|
88 |
} |
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/OracleConnectionParameters.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.oracle.dal; |
|
3 |
|
|
4 |
import org.gvsig.fmap.dal.resource.db.DBParameters; |
|
5 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
|
6 |
|
|
7 |
public interface OracleConnectionParameters extends JDBCConnectionParameters, DBParameters { |
|
8 |
|
|
9 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/OracleConnectionParametersHelper.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.oracle.dal; |
|
3 |
|
|
4 |
import org.apache.commons.lang3.StringUtils; |
|
5 |
import org.gvsig.fmap.dal.DataParameters; |
|
6 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
7 |
import org.gvsig.fmap.dal.store.jdbc.JDBCConnectionParameters; |
|
8 |
|
|
9 |
|
|
10 |
public class OracleConnectionParametersHelper { |
|
11 |
|
|
12 |
private final JDBCConnectionParameters parameters; |
|
13 |
|
|
14 |
public OracleConnectionParametersHelper(JDBCConnectionParameters parameters) { |
|
15 |
this.parameters = parameters; |
|
16 |
} |
|
17 |
|
|
18 |
public String getUrl() { |
|
19 |
String url = (String) this.getDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME); |
|
20 |
if( StringUtils.isEmpty(url) ) { |
|
21 |
url = OracleHelper.getConnectionURL((OracleConnectionParameters) this.parameters); |
|
22 |
this.setDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME,url); |
|
23 |
} |
|
24 |
return url; |
|
25 |
} |
|
26 |
|
|
27 |
public void validate() throws ValidateDataParametersException { |
|
28 |
if (this.getDynValue(JDBCConnectionParameters.JDBC_DRIVER_CLASS_PARAMTER_NAME) == null) { |
|
29 |
this.setDynValue( |
|
30 |
JDBCConnectionParameters.JDBC_DRIVER_CLASS_PARAMTER_NAME, |
|
31 |
OracleHelper.ORACLE_JDBC_DRIVER |
|
32 |
); |
|
33 |
} |
|
34 |
if( this.getDynValue(JDBCConnectionParameters.PORT_PARAMTER_NAME)==null ) { |
|
35 |
this.setDynValue(JDBCConnectionParameters.PORT_PARAMTER_NAME, 1521); |
|
36 |
} |
|
37 |
if ( StringUtils.isEmpty((CharSequence) this.getDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME))) { |
|
38 |
String url = OracleHelper.getConnectionURL( |
|
39 |
parameters.getHost(), |
|
40 |
parameters.getPort(), |
|
41 |
parameters.getDBName() |
|
42 |
); |
|
43 |
this.setDynValue(JDBCConnectionParameters.URL_PARAMTER_NAME, url); |
|
44 |
} |
|
45 |
} |
|
46 |
|
|
47 |
private Object getDynValue(String name) { |
|
48 |
return ((DataParameters)this.parameters).getDynValue(name); |
|
49 |
} |
|
50 |
|
|
51 |
private void setDynValue(String name, Object value) { |
|
52 |
((DataParameters)this.parameters).setDynValue(name,value); |
|
53 |
} |
|
54 |
|
|
55 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/OracleExplorerParameters.java | ||
---|---|---|
1 |
/* gvSIG. Geographic Information System of the Valencian Government |
|
2 |
* |
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department |
|
4 |
* of the Valencian Government (CIT) |
|
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 2 |
|
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 |
*/ |
|
22 |
/** |
|
23 |
* |
|
24 |
*/ |
|
25 |
package org.gvsig.oracle.dal; |
|
26 |
|
|
27 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
|
28 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
|
29 |
|
|
30 |
public class OracleExplorerParameters extends |
|
31 |
JDBCServerExplorerParameters |
|
32 |
implements |
|
33 |
OracleConnectionParameters |
|
34 |
{ |
|
35 |
|
|
36 |
private final OracleConnectionParametersHelper helper; |
|
37 |
|
|
38 |
public OracleExplorerParameters() { |
|
39 |
super( |
|
40 |
OracleLibrary.NAME + "ServerExplorerParameters", |
|
41 |
OracleLibrary.NAME |
|
42 |
); |
|
43 |
this.helper = new OracleConnectionParametersHelper(this); |
|
44 |
} |
|
45 |
|
|
46 |
@Override |
|
47 |
public String getUrl() { |
|
48 |
return this.helper.getUrl(); |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
public void validate() throws ValidateDataParametersException { |
|
53 |
this.helper.validate(); |
|
54 |
super.validate(); |
|
55 |
} |
|
56 |
|
|
57 |
@Override |
|
58 |
public void setDynValue(String name, Object value) { |
|
59 |
if (!URL_PARAMTER_NAME.equals(name)) { |
|
60 |
// reset URL each time a parameter changes |
|
61 |
super.setDynValue(URL_PARAMTER_NAME, null); |
|
62 |
} |
|
63 |
super.setDynValue(name, value); |
|
64 |
} |
|
65 |
|
|
66 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/OracleLibrary.java | ||
---|---|---|
1 |
|
|
2 |
package org.gvsig.oracle.dal; |
|
3 |
|
|
4 |
import org.gvsig.fmap.dal.DALLibrary; |
|
5 |
import org.gvsig.fmap.dal.DALLocator; |
|
6 |
import org.gvsig.fmap.dal.spi.DataManagerProviderServices; |
|
7 |
import org.gvsig.fmap.dal.store.db.DBHelper; |
|
8 |
import org.gvsig.fmap.dal.store.jdbc2.JDBCLibrary; |
|
9 |
import org.gvsig.fmap.dal.store.jdbc2.spi.JDBCStoreProviderBase; |
|
10 |
import org.gvsig.metadata.exceptions.MetadataException; |
|
11 |
import org.gvsig.tools.library.AbstractLibrary; |
|
12 |
import org.gvsig.tools.library.LibraryException; |
|
13 |
|
|
14 |
|
|
15 |
public class OracleLibrary extends AbstractLibrary { |
|
16 |
|
|
17 |
public static final String NAME = "Oracle"; |
|
18 |
|
|
19 |
@Override |
|
20 |
public void doRegistration() { |
|
21 |
registerAsServiceOf(DALLibrary.class); |
|
22 |
require(JDBCLibrary.class); |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
protected void doInitialize() throws LibraryException { |
|
27 |
} |
|
28 |
|
|
29 |
@Override |
|
30 |
protected void doPostInitialize() throws LibraryException { |
|
31 |
LibraryException ex = null; |
|
32 |
|
|
33 |
DataManagerProviderServices dataman = |
|
34 |
(DataManagerProviderServices) DALLocator.getDataManager(); |
|
35 |
|
|
36 |
try { |
|
37 |
Class.forName(OracleHelper.ORACLE_JDBC_DRIVER); |
|
38 |
} catch(Throwable th) { |
|
39 |
OracleHelper.logger.warn("Can't load Oracle JDBC Driver.",th); |
|
40 |
} |
|
41 |
|
|
42 |
DBHelper.registerParametersDefinition( |
|
43 |
NAME + "StoreParameters", |
|
44 |
OracleStoreParameters.class, |
|
45 |
dataman.getResourceAsStream(this, NAME + "Parameters.xml") |
|
46 |
); |
|
47 |
DBHelper.registerParametersDefinition( |
|
48 |
NAME + "NewStoreParameters", |
|
49 |
OracleNewStoreParameters.class, |
|
50 |
dataman.getResourceAsStream(this, NAME + "Parameters.xml") |
|
51 |
); |
|
52 |
DBHelper.registerParametersDefinition( |
|
53 |
NAME + "ServerExplorerParameters", |
|
54 |
OracleExplorerParameters.class, |
|
55 |
dataman.getResourceAsStream(this, NAME + "Parameters.xml") |
|
56 |
); |
|
57 |
try { |
|
58 |
DBHelper.registerMetadataDefinition( |
|
59 |
NAME, |
|
60 |
JDBCStoreProviderBase.class, |
|
61 |
dataman.getResourceAsStream(this, NAME + "Metadata.xml") |
|
62 |
); |
|
63 |
} catch (MetadataException e) { |
|
64 |
ex = new LibraryException(this.getClass(), e); |
|
65 |
} |
|
66 |
|
|
67 |
if (!dataman.getStoreProviderRegister().exits(NAME)) { |
|
68 |
dataman.registerStoreProviderFactory(new OracleStoreProviderFactory()); |
|
69 |
} |
|
70 |
|
|
71 |
if (!dataman.getServerExplorerRegister().exits(NAME)) { |
|
72 |
dataman.registerServerExplorerFactory(new OracleExplorerFactory()); |
|
73 |
} |
|
74 |
if (ex != null) { |
|
75 |
throw ex; |
|
76 |
} |
|
77 |
} |
|
78 |
|
|
79 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/expressionbuilderformatter/Concat.java | ||
---|---|---|
1 |
package org.gvsig.oracle.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import java.text.MessageFormat; |
|
4 |
import java.util.List; |
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
|
|
7 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder.BinaryOperator; |
|
9 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
10 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
11 |
import org.gvsig.expressionevaluator.Formatter; |
|
12 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
13 |
import org.gvsig.oracle.dal.OracleSQLBuilder; |
|
14 |
|
|
15 |
/** |
|
16 |
* |
|
17 |
* @author jjdelcerro |
|
18 |
*/ |
|
19 |
class Concat implements Formatter<Value> { |
|
20 |
|
|
21 |
private final Formatter<Value> formatter; |
|
22 |
private final SQLBuilder builder; |
|
23 |
|
|
24 |
public Concat(SQLBuilder builder, Formatter<Value> formatter) { |
|
25 |
this.builder = builder; |
|
26 |
this.formatter = formatter; |
|
27 |
} |
|
28 |
|
|
29 |
@Override |
|
30 |
public boolean canApply(Value value) { |
|
31 |
if (value instanceof BinaryOperator) { |
|
32 |
return StringUtils.equalsIgnoreCase(ExpressionBuilder.OPERATOR_CONCAT, ((BinaryOperator) value).name()); |
|
33 |
} |
|
34 |
return false; |
|
35 |
} |
|
36 |
|
|
37 |
@Override |
|
38 |
public String format(Value function) { |
|
39 |
List<Value> parameters = ((Function) function).parameters(); |
|
40 |
String p1 = parameters.get(0).toString(formatter); |
|
41 |
String p2 = parameters.get(1).toString(formatter); |
|
42 |
String r = MessageFormat.format(OracleSQLBuilder.FORMAT_OPERATOR_CONCAT, p1, p2); |
|
43 |
return r; |
|
44 |
} |
|
45 |
|
|
46 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/expressionbuilderformatter/ST_SRID.java | ||
---|---|---|
1 |
package org.gvsig.oracle.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import java.text.MessageFormat; |
|
4 |
import java.util.List; |
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
7 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
8 |
import org.gvsig.expressionevaluator.Formatter; |
|
9 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
|
10 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
11 |
|
|
12 |
/** |
|
13 |
* |
|
14 |
* @author jjdelcerro |
|
15 |
*/ |
|
16 |
class ST_SRID implements Formatter<Value> { |
|
17 |
|
|
18 |
private final Formatter<Value> formatter; |
|
19 |
private final SQLBuilder builder; |
|
20 |
|
|
21 |
public ST_SRID(SQLBuilder builder, Formatter<Value> formatter) { |
|
22 |
this.builder = builder; |
|
23 |
this.formatter = formatter; |
|
24 |
} |
|
25 |
|
|
26 |
@Override |
|
27 |
public boolean canApply(Value value) { |
|
28 |
if (value instanceof Function) { |
|
29 |
return StringUtils.equalsIgnoreCase(GeometryExpressionBuilder.FUNCTION_ST_SRID, ((Function) value).name()); |
|
30 |
} |
|
31 |
return false; |
|
32 |
} |
|
33 |
|
|
34 |
@Override |
|
35 |
public String format(Value function) { |
|
36 |
List<Value> parameters = ((Function) function).parameters(); |
|
37 |
String p1 = parameters.get(0).toString(formatter); |
|
38 |
String r = MessageFormat.format("(({0}).SDO_SRID)", p1); |
|
39 |
return r; |
|
40 |
} |
|
41 |
|
|
42 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/expressionbuilderformatter/Getattr.java | ||
---|---|---|
1 |
package org.gvsig.oracle.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import org.apache.commons.lang3.StringUtils; |
|
4 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_GETATTR; |
|
5 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
7 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Variable; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Constant; |
|
9 |
import org.gvsig.expressionevaluator.Formatter; |
|
10 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
11 |
|
|
12 |
/** |
|
13 |
* |
|
14 |
* @author jjdelcerro |
|
15 |
*/ |
|
16 |
public class Getattr implements Formatter<Value> { |
|
17 |
|
|
18 |
private final SQLBuilder sqlbuilder; |
|
19 |
private final Formatter<Value> formatter; |
|
20 |
|
|
21 |
public Getattr(SQLBuilder sqlbuilder, Formatter<Value> formatter) { |
|
22 |
this.sqlbuilder = sqlbuilder; |
|
23 |
this.formatter = formatter; |
|
24 |
} |
|
25 |
@Override |
|
26 |
public boolean canApply(Value value) { |
|
27 |
if (value instanceof Function) { |
|
28 |
if( StringUtils.equalsIgnoreCase(FUNCTION_GETATTR, ((Function) value).name()) ) { |
|
29 |
Value obj = ((Function) value).parameters().get(0); |
|
30 |
Value attrname = ((Function) value).parameters().get(1); |
|
31 |
return obj instanceof Variable && attrname instanceof Constant; |
|
32 |
|
|
33 |
} |
|
34 |
} |
|
35 |
return false; |
|
36 |
} |
|
37 |
|
|
38 |
@Override |
|
39 |
public String format(Value function0) { |
|
40 |
Function function = (Function) function0; |
|
41 |
Variable obj = (Variable) function.parameters().get(0); |
|
42 |
Constant attrname = (Constant) function.parameters().get(1); |
|
43 |
return this.sqlbuilder.as_identifier(obj.name())+"."+this.sqlbuilder.as_identifier((String) attrname.value()); |
|
44 |
} |
|
45 |
|
|
46 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/expressionbuilderformatter/Select.java | ||
---|---|---|
1 |
package org.gvsig.oracle.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import org.apache.commons.lang3.StringUtils; |
|
4 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
5 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_TUPLE; |
|
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
7 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
8 |
import org.gvsig.expressionevaluator.Formatter; |
|
9 |
import static org.gvsig.fmap.dal.DataManager.FUNCTION_SELECT; |
|
10 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
11 |
|
|
12 |
/** |
|
13 |
* |
|
14 |
* @author jjdelcerro |
|
15 |
*/ |
|
16 |
@SuppressWarnings("UseSpecificCatch") |
|
17 |
public class Select implements Formatter<Value> { |
|
18 |
|
|
19 |
private final Formatter<Value> formatter; |
|
20 |
private final SQLBuilder builder; |
|
21 |
|
|
22 |
public Select(SQLBuilder builder, Formatter<Value> formatter) { |
|
23 |
this.builder = builder; |
|
24 |
this.formatter = formatter; |
|
25 |
} |
|
26 |
|
|
27 |
@Override |
|
28 |
public boolean canApply(ExpressionBuilder.Value value) { |
|
29 |
if (value instanceof ExpressionBuilder.Function) { |
|
30 |
return StringUtils.equalsIgnoreCase(FUNCTION_SELECT, ((Function) value).name()); |
|
31 |
} |
|
32 |
return false; |
|
33 |
} |
|
34 |
|
|
35 |
private static final int COLUMNS = 0; |
|
36 |
private static final int TABLE = 1; |
|
37 |
private static final int WHERE = 2; |
|
38 |
private static final int ORDER = 3; |
|
39 |
private static final int ORDER_MODE = 4; |
|
40 |
private static final int LIMIT = 5; |
|
41 |
|
|
42 |
private StringBuilder appendMainClause( |
|
43 |
StringBuilder builder, |
|
44 |
Function function) { |
|
45 |
|
|
46 |
String tableName = (String) function.parameters().get(TABLE).toString(formatter); |
|
47 |
Value where = function.parameters().get(WHERE); |
|
48 |
// Value order = function.parameters().get(ORDER); |
|
49 |
// Value limit = function.parameters().get(LIMIT); |
|
50 |
// Value order_mode = function.parameters().get(ORDER_MODE); |
|
51 |
|
|
52 |
|
|
53 |
builder.append("SELECT "); |
|
54 |
|
|
55 |
try { |
|
56 |
Function columns = (Function) function.parameters().get(COLUMNS); |
|
57 |
if (!StringUtils.equalsIgnoreCase(((ExpressionBuilder.Function) columns).name(), FUNCTION_TUPLE)) { |
|
58 |
throw new IllegalArgumentException(); |
|
59 |
} |
|
60 |
if (columns.parameters().isEmpty()) { |
|
61 |
builder.append("* "); |
|
62 |
} else { |
|
63 |
boolean needcomma = false; |
|
64 |
for (Value parameter : columns.parameters()) { |
|
65 |
if (needcomma) { |
|
66 |
builder.append(", "); |
|
67 |
} else { |
|
68 |
needcomma = true; |
|
69 |
} |
|
70 |
builder.append(parameter.toString(formatter)); |
|
71 |
} |
|
72 |
} |
|
73 |
} catch (Exception ex) { |
|
74 |
throw new IllegalArgumentException("Invalid columns, need a tuple function as columns."); |
|
75 |
} |
|
76 |
|
|
77 |
builder.append(" FROM "); |
|
78 |
builder.append(tableName); |
|
79 |
|
|
80 |
if (where != null) { |
|
81 |
builder.append(" WHERE "); |
|
82 |
builder.append(where.toString(formatter)); |
|
83 |
} |
|
84 |
|
|
85 |
return builder; |
|
86 |
} |
|
87 |
|
|
88 |
|
|
89 |
@Override |
|
90 |
public String format(Value function0) { |
|
91 |
Function function = (Function) function0; |
|
92 |
String tableName = (String) function.parameters().get(TABLE).toString(formatter); |
|
93 |
// Value where = function.parameters().get(WHERE); |
|
94 |
// Value order = function.parameters().get(ORDER); |
|
95 |
Value limit = function.parameters().get(LIMIT); |
|
96 |
// Value order_mode = function.parameters().get(ORDER_MODE); |
|
97 |
|
|
98 |
StringBuilder strBuilder = new StringBuilder(); |
|
99 |
|
|
100 |
strBuilder.append("("); |
|
101 |
|
|
102 |
if (limit != null) { |
|
103 |
/** |
|
104 |
* Using ROWNUM for limit & offset, compatible with any Oracle |
|
105 |
* version This requires wrapping the original query as shown in |
|
106 |
* the example: select * from ( select a.*, ROWNUM rnum from ( |
|
107 |
* <select statement with order by clause> |
|
108 |
* ) a where ROWNUM <= MAX_ROW |
|
109 |
* ) where rnum >= MIN_ROW because ROWNUM is considered before |
|
110 |
* applying order by and group by |
|
111 |
*/ |
|
112 |
strBuilder.append("SELECT * FROM ( "); |
|
113 |
this.appendMainClause( |
|
114 |
strBuilder, |
|
115 |
function); |
|
116 |
strBuilder.append(") a"); |
|
117 |
strBuilder.append(" WHERE ROWNUM <= "); |
|
118 |
strBuilder.append(limit); |
|
119 |
} else { |
|
120 |
this.appendMainClause( |
|
121 |
strBuilder, |
|
122 |
function); |
|
123 |
} |
|
124 |
|
|
125 |
strBuilder.append(")"); |
|
126 |
|
|
127 |
// builder.append("(SELECT "); |
|
128 |
// |
|
129 |
// try { |
|
130 |
// Function columns = (Function) function.parameters().get(COLUMNS); |
|
131 |
// if (!StringUtils.equalsIgnoreCase(((ExpressionBuilder.Function) columns).name(), FUNCTION_TUPLE)) { |
|
132 |
// throw new IllegalArgumentException(); |
|
133 |
// } |
|
134 |
// if (columns.parameters().isEmpty()) { |
|
135 |
// builder.append("* "); |
|
136 |
// } else { |
|
137 |
// boolean needcomma = false; |
|
138 |
// for (Value parameter : columns.parameters()) { |
|
139 |
// if (needcomma) { |
|
140 |
// builder.append(", "); |
|
141 |
// } else { |
|
142 |
// needcomma = true; |
|
143 |
// } |
|
144 |
// builder.append(parameter.toString(formatter)); |
|
145 |
// } |
|
146 |
// } |
|
147 |
// } catch (Exception ex) { |
|
148 |
// throw new IllegalArgumentException("Invalid columns, need a tuple function as columns."); |
|
149 |
// } |
|
150 |
// |
|
151 |
// builder.append(" FROM "); |
|
152 |
// builder.append(tableName); |
|
153 |
// if (where != null) { |
|
154 |
// builder.append(" WHERE "); |
|
155 |
// builder.append(where.toString(formatter)); |
|
156 |
// } |
|
157 |
// if (limit != null) { |
|
158 |
// builder.append(" LIMIT "); |
|
159 |
// builder.append(limit.toString(formatter)); |
|
160 |
// } |
|
161 |
// builder.append(")"); |
|
162 |
return strBuilder.toString(); |
|
163 |
} |
|
164 |
|
|
165 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/expressionbuilderformatter/AbstractClobOperator.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.oracle.dal.expressionbuilderformatter; |
|
7 |
|
|
8 |
import org.apache.commons.lang3.StringUtils; |
|
9 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
10 |
import org.gvsig.expressionevaluator.Formatter; |
|
11 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
|
12 |
import org.gvsig.fmap.dal.feature.FeatureType; |
|
13 |
import org.gvsig.fmap.dal.store.jdbc2.OperationsFactory; |
|
14 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_FEATURE_TYPE; |
|
15 |
import static org.gvsig.fmap.dal.store.jdbc2.spi.JDBCSQLBuilderBase.PROP_TABLE; |
|
16 |
import org.gvsig.tools.dataTypes.DataTypes; |
|
17 |
|
|
18 |
/** |
|
19 |
* |
|
20 |
* @author fdiaz |
|
21 |
*/ |
|
22 |
public abstract class AbstractClobOperator implements Formatter<ExpressionBuilder.Value> { |
|
23 |
|
|
24 |
protected boolean useLob(ExpressionBuilder.Value value) { |
|
25 |
FeatureType featureType = (FeatureType) value.getProperty(PROP_FEATURE_TYPE); |
|
26 |
if(featureType == null){ |
|
27 |
return false; |
|
28 |
} |
|
29 |
|
|
30 |
if (value instanceof ExpressionBuilder.Variable) { |
|
31 |
FeatureAttributeDescriptor descriptor = featureType.getAttributeDescriptor(((ExpressionBuilder.Variable) value).name()); |
|
32 |
if (descriptor != null) { |
|
33 |
if (descriptor.getType() == DataTypes.STRING |
|
34 |
&& (descriptor.getSize() < 0 || descriptor.getSize() >= 4096)) { |
|
35 |
return true; |
|
36 |
} |
|
37 |
} |
|
38 |
return false; |
|
39 |
} |
|
40 |
|
|
41 |
OperationsFactory.TableReference tableReference = (OperationsFactory.TableReference) value.getProperty(PROP_TABLE); |
|
42 |
if(tableReference == null){ |
|
43 |
return false; |
|
44 |
} |
|
45 |
if(value instanceof ExpressionBuilder.Function){ |
|
46 |
ExpressionBuilder.Function fn = (ExpressionBuilder.Function)value; |
|
47 |
if(StringUtils.equalsIgnoreCase(ExpressionBuilder.FUNCTION_GETATTR, fn.name())){ |
|
48 |
ExpressionBuilder.Value table = fn.parameters().get(0); |
|
49 |
|
|
50 |
if(table instanceof ExpressionBuilder.Variable){ |
|
51 |
if(((ExpressionBuilder.Variable)table).name() instanceof String && |
|
52 |
StringUtils.equals( tableReference.getTable(), (String)((ExpressionBuilder.Variable)table).name())) { |
|
53 |
if(useLobSimple(featureType, fn.parameters().get(1))){ |
|
54 |
return true; |
|
55 |
} |
|
56 |
} |
|
57 |
} |
|
58 |
if(table instanceof ExpressionBuilder.Constant){ |
|
59 |
if(((ExpressionBuilder.Constant)table).value() instanceof String && |
|
60 |
StringUtils.equals( tableReference.getTable(), (String)((ExpressionBuilder.Constant)table).value())) { |
|
61 |
if(useLobSimple(featureType, fn.parameters().get(1))){ |
|
62 |
return true; |
|
63 |
} |
|
64 |
} |
|
65 |
} |
|
66 |
} |
|
67 |
} |
|
68 |
return false; |
|
69 |
} |
|
70 |
|
|
71 |
private boolean useLobSimple(FeatureType featureType, ExpressionBuilder.Value value){ |
|
72 |
if (value instanceof ExpressionBuilder.Constant) { |
|
73 |
FeatureAttributeDescriptor descriptor = featureType.getAttributeDescriptor((String)((ExpressionBuilder.Constant) value).value()); |
|
74 |
if (descriptor != null) { |
|
75 |
if (descriptor.getType() == DataTypes.STRING |
|
76 |
&& (descriptor.getSize() < 0 || descriptor.getSize() >= 4096)) { |
|
77 |
return true; |
|
78 |
} |
|
79 |
} |
|
80 |
} |
|
81 |
return false; |
|
82 |
} |
|
83 |
|
|
84 |
|
|
85 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/expressionbuilderformatter/ST_Touches.java | ||
---|---|---|
1 |
package org.gvsig.oracle.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import java.text.MessageFormat; |
|
4 |
import java.util.List; |
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
|
|
7 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
9 |
import org.gvsig.expressionevaluator.Formatter; |
|
10 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
|
11 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
12 |
|
|
13 |
/** |
|
14 |
* |
|
15 |
* @author jjdelcerro |
|
16 |
*/ |
|
17 |
class ST_Touches implements Formatter<Value> { |
|
18 |
|
|
19 |
private final Formatter<Value> formatter; |
|
20 |
private final SQLBuilder builder; |
|
21 |
|
|
22 |
public ST_Touches(SQLBuilder builder, Formatter<Value> formatter) { |
|
23 |
this.builder = builder; |
|
24 |
this.formatter = formatter; |
|
25 |
} |
|
26 |
|
|
27 |
@Override |
|
28 |
public boolean canApply(Value value) { |
|
29 |
if (value instanceof Function) { |
|
30 |
return StringUtils.equalsIgnoreCase(GeometryExpressionBuilder.FUNCTION_ST_TOUCHES, ((Function) value).name()); |
|
31 |
} |
|
32 |
return false; |
|
33 |
} |
|
34 |
|
|
35 |
@Override |
|
36 |
public String format(Value function) { |
|
37 |
List<Value> parameters = ((Function) function).parameters(); |
|
38 |
String p1 = parameters.get(0).toString(formatter); |
|
39 |
// Le falta un par?metro ?no? |
|
40 |
// String r = MessageFormat.format("(SDO_RELATE(({0}) = ''mask=TOUCH'') = ''TRUE'')", p1); |
|
41 |
|
|
42 |
String p2 = parameters.get(1).toString(formatter); |
|
43 |
String r = MessageFormat.format("(SDO_RELATE({0}, {1}, ''mask=TOUCH'') = ''TRUE'')", p1, p2); |
|
44 |
|
|
45 |
return r; |
|
46 |
} |
|
47 |
|
|
48 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/expressionbuilderformatter/OracleGeometryParameter.java | ||
---|---|---|
1 |
/* |
|
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package org.gvsig.oracle.dal.expressionbuilderformatter; |
|
7 |
|
|
8 |
import java.text.MessageFormat; |
|
9 |
import org.cresques.cts.IProjection; |
|
10 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
11 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.PARAMETER_TYPE_CONSTANT; |
|
12 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.PARAMETER_TYPE_VARIABLE; |
|
13 |
import org.gvsig.expressionevaluator.Formatter; |
|
14 |
import org.gvsig.expressionevaluator.GeometryExpressionBuilder; |
|
15 |
import static org.gvsig.expressionevaluator.GeometryExpressionBuilderHelper.PARAMETER_TYPE_GEOMETRY; |
|
16 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
17 |
import org.gvsig.oracle.dal.OracleSQLBuilder; |
|
18 |
|
|
19 |
/** |
|
20 |
* |
|
21 |
* @author osc |
|
22 |
*/ |
|
23 |
public class OracleGeometryParameter implements Formatter<ExpressionBuilder.Value> { |
|
24 |
|
|
25 |
private final Formatter<ExpressionBuilder.Value> formatter; |
|
26 |
private final OracleSQLBuilder builder; |
|
27 |
private final GeometryExpressionBuilder expbuilder; |
|
28 |
public static final String ORACLE_GEOMETRY_PARAMETER = "oracleGeometryParameter"; |
|
29 |
|
|
30 |
public OracleGeometryParameter(SQLBuilder builder, Formatter<ExpressionBuilder.Value> formatter) { |
|
31 |
this.builder = (OracleSQLBuilder) builder; |
|
32 |
this.formatter = formatter; |
|
33 |
this.expbuilder = builder.expression(); |
|
34 |
} |
|
35 |
|
|
36 |
@Override |
|
37 |
public boolean canApply(ExpressionBuilder.Value value) { |
|
38 |
return value instanceof GeometryExpressionBuilder.GeometryParameter; |
|
39 |
} |
|
40 |
|
|
41 |
@Override |
|
42 |
public String format(ExpressionBuilder.Value param0) { |
|
43 |
GeometryExpressionBuilder.GeometryParameter param = (GeometryExpressionBuilder.GeometryParameter) param0; |
|
44 |
|
|
45 |
switch (param.type()) { |
|
46 |
case PARAMETER_TYPE_CONSTANT: |
|
47 |
case PARAMETER_TYPE_GEOMETRY: |
|
48 |
if (!(param.value() instanceof Number)) { |
|
49 |
param.setProperty(ORACLE_GEOMETRY_PARAMETER, true); |
|
50 |
return MessageFormat.format( |
|
51 |
"NVL2(({0}),SDO_GEOMETRY(({0}), ({1})),NULL)", |
|
52 |
"?", |
|
53 |
getSRS(param) |
|
54 |
); |
|
55 |
|
|
56 |
} |
|
57 |
case PARAMETER_TYPE_VARIABLE: |
|
58 |
default: |
|
59 |
return "?"; |
|
60 |
} |
|
61 |
} |
|
62 |
|
|
63 |
private String getSRS(GeometryExpressionBuilder.GeometryParameter param) { |
|
64 |
if( param.srs()!=null ) { |
|
65 |
return param.srs().toString(formatter); |
|
66 |
} |
|
67 |
IProjection proj = param.geometry().getProjection(); |
|
68 |
Object s = this.builder.srs_id(proj); |
|
69 |
if( s == null ) { |
|
70 |
throw new IllegalArgumentException("A parameter of type Geometry with an invalid SRS."); |
|
71 |
} |
|
72 |
return s.toString(); |
|
73 |
} |
|
74 |
|
|
75 |
} |
org.gvsig.oracle/tags/org.gvsig.oracle-2.0.23/org.gvsig.oracle.provider/src/main/java/org/gvsig/oracle/dal/expressionbuilderformatter/Decode.java | ||
---|---|---|
1 |
package org.gvsig.oracle.dal.expressionbuilderformatter; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
import java.util.Objects; |
|
5 |
import org.apache.commons.lang3.StringUtils; |
|
6 |
import org.gvsig.expressionevaluator.ExpressionBuilder; |
|
7 |
import static org.gvsig.expressionevaluator.ExpressionBuilder.FUNCTION_DECODE; |
|
8 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Function; |
|
9 |
import org.gvsig.expressionevaluator.ExpressionBuilder.Value; |
|
10 |
import org.gvsig.expressionevaluator.Formatter; |
|
11 |
import org.gvsig.fmap.dal.SQLBuilder; |
|
12 |
|
|
13 |
/** |
|
14 |
* |
|
15 |
* @author jjdelcerro |
|
16 |
*/ |
|
17 |
public class Decode implements Formatter<Value> { |
|
18 |
|
|
19 |
private final SQLBuilder sqlbuilder; |
|
20 |
private final Formatter<Value> formatter; |
|
21 |
|
|
22 |
public Decode(SQLBuilder sqlbuilder, Formatter<Value> formatter) { |
|
23 |
this.sqlbuilder = sqlbuilder; |
|
24 |
this.formatter = formatter; |
|
25 |
} |
|
26 |
|
|
27 |
@Override |
|
28 |
public boolean canApply(ExpressionBuilder.Value value) { |
|
29 |
if (value instanceof ExpressionBuilder.Function) { |
|
30 |
if( StringUtils.equalsIgnoreCase(FUNCTION_DECODE, ((Function) value).name()) ) { |
|
31 |
List<Value> parameters = ((Function) value).parameters(); |
|
32 |
if( parameters.size()==2) { |
|
33 |
Value p1 = parameters.get(0); |
|
34 |
Value p2 = parameters.get(1); |
|
35 |
if( p1 instanceof ExpressionBuilder.Constant && |
|
36 |
p2 instanceof ExpressionBuilder.Constant && |
|
37 |
((ExpressionBuilder.Constant)p1).value() instanceof String && |
|
38 |
((ExpressionBuilder.Constant)p2).value() instanceof String |
|
39 |
) { |
|
40 |
String p2value = ((ExpressionBuilder.Constant)p2).value().toString(); |
|
41 |
if( StringUtils.equalsIgnoreCase(p2value, "hex") ) { |
|
42 |
return true; |
|
43 |
} |
|
44 |
} |
|
45 |
|
|
46 |
} |
|
47 |
} |
|
48 |
} |
|
49 |
return false; |
Also available in: Unified diff