Revision 46 trunk/org.gvsig.postgresql/org.gvsig.postgresql.provider/src/main/java/org/gvsig/fmap/dal/store/postgresql/PostgreSQLStoreProvider.java
PostgreSQLStoreProvider.java | ||
---|---|---|
27 | 27 |
|
28 | 28 |
package org.gvsig.fmap.dal.store.postgresql; |
29 | 29 |
|
30 |
import java.sql.DatabaseMetaData; |
|
31 |
import java.sql.ResultSet; |
|
32 |
import java.sql.SQLException; |
|
33 |
import java.sql.Statement; |
|
34 |
import java.util.Iterator; |
|
30 | 35 |
import java.util.List; |
36 |
import java.util.Properties; |
|
31 | 37 |
import java.util.regex.Matcher; |
32 | 38 |
import java.util.regex.Pattern; |
33 | 39 |
|
... | ... | |
44 | 50 |
import org.gvsig.fmap.dal.feature.FeatureType; |
45 | 51 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
46 | 52 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
53 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
|
47 | 54 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
48 | 55 |
import org.gvsig.fmap.dal.store.db.DBHelper; |
49 | 56 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
50 | 57 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProviderWriter; |
58 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCExecuteSQLException; |
|
59 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
|
51 | 60 |
import org.gvsig.fmap.geom.Geometry; |
52 | 61 |
import org.slf4j.Logger; |
53 | 62 |
import org.slf4j.LoggerFactory; |
... | ... | |
61 | 70 |
public static final String DESCRIPTION = "PostgreSQL source"; |
62 | 71 |
|
63 | 72 |
public static final String METADATA_DEFINITION_NAME = NAME; |
64 |
|
|
65 |
|
|
73 |
|
|
66 | 74 |
public PostgreSQLStoreProvider(PostgreSQLStoreParameters params, |
67 | 75 |
DataStoreProviderServices storeServices) |
68 | 76 |
throws InitializeException { |
... | ... | |
74 | 82 |
} |
75 | 83 |
|
76 | 84 |
protected JDBCHelper createHelper() throws InitializeException { |
77 |
return new PostgreSQLHelper(this, getPGParameters()); |
|
85 |
JDBCHelper resp = new PostgreSQLHelper(this, getPGParameters()); |
|
86 |
|
|
87 |
return resp; |
|
78 | 88 |
} |
79 | 89 |
|
80 |
protected String fixFilter(String filter) { |
|
81 |
if (filter == null) { |
|
90 |
|
|
91 |
|
|
92 |
protected String fixFilter(String _filter) { |
|
93 |
if (_filter == null) { |
|
82 | 94 |
return null; |
83 | 95 |
} |
96 |
|
|
97 |
String filter = fixFunctionNames(_filter); |
|
84 | 98 |
|
85 | 99 |
// Transform SRS to code |
86 | 100 |
// GeomFromText\s*\(\s*'[^']*'\s*,\s*('[^']*')\s*\) |
101 |
|
|
102 |
String geom_from_text = this.getFunctionName("ST_GeomFromText"); |
|
87 | 103 |
Pattern pattern = Pattern |
88 |
.compile("ST_GeomFromText\\s*\\(\\s*'[^']*'\\s*,\\s*'([^']*)'\\s*\\)");
|
|
104 |
.compile(geom_from_text + "\\s*\\(\\s*'[^']*'\\s*,\\s*'([^']*)'\\s*\\)");
|
|
89 | 105 |
Matcher matcher = pattern.matcher(filter); |
90 | 106 |
StringBuilder strb = new StringBuilder(); |
91 | 107 |
int pos = 0; |
... | ... | |
109 | 125 |
return strb.toString(); |
110 | 126 |
} |
111 | 127 |
|
112 |
public String getName() { |
|
128 |
|
|
129 |
public String getName() { |
|
113 | 130 |
return NAME; |
114 | 131 |
} |
115 | 132 |
|
... | ... | |
155 | 172 |
return true; |
156 | 173 |
} |
157 | 174 |
|
158 |
// ************************************************************************************// |
|
159 | 175 |
|
160 |
|
|
161 |
// ************************************************************************************// |
|
162 |
|
|
163 |
|
|
164 |
|
|
165 | 176 |
protected PostgreSQLHelper getPgHelper() { |
166 | 177 |
return (PostgreSQLHelper) getHelper(); |
167 | 178 |
} |
168 | 179 |
|
169 |
// ************************************************************************************// |
|
170 | 180 |
|
171 |
// ************************************************************************************// |
|
172 | 181 |
|
173 |
|
|
174 |
|
|
175 | 182 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) |
176 | 183 |
throws DataException { |
177 | 184 |
FeatureType type = getFeatureStore().getDefaultFeatureType(); |
... | ... | |
226 | 233 |
|
227 | 234 |
if (attr.getType() == DataTypes.GEOMETRY) { |
228 | 235 |
fields.add(helper.escapeFieldName(attr.getName())); |
229 |
values.add("ST_GeomFromWKB(?,?)");
|
|
236 |
values.add(getFunctionName("ST_GeomFromWKB") + "(?,?)");
|
|
230 | 237 |
} else { |
231 | 238 |
super.prepareAttributeForInsert(attr, fields, values); |
232 | 239 |
} |
... | ... | |
237 | 244 |
List<String> values) { |
238 | 245 |
if (attr.getType() == DataTypes.GEOMETRY) { |
239 | 246 |
values.add(helper.escapeFieldName(attr.getName()) |
240 |
+ " = ST_GeomFromWKB(?,?)");
|
|
247 |
+ " = " + getFunctionName("ST_GeomFromWKB") + "(?,?)");
|
|
241 | 248 |
} else { |
242 | 249 |
super.prepareAttributeForUpdate(attr, values); |
243 | 250 |
} |
... | ... | |
313 | 320 |
|
314 | 321 |
return actions; |
315 | 322 |
} |
323 |
|
|
316 | 324 |
|
325 |
private String getFunctionName(String newFunctionName) { |
|
326 |
|
|
327 |
PostgreSQLHelper hpr = getPgHelper(); |
|
328 |
if (hpr == null) { |
|
329 |
logger.info("Unable to get PG helper.", new Exception("Helper is null")); |
|
330 |
return newFunctionName; |
|
331 |
} else { |
|
332 |
return hpr.getFunctionName(newFunctionName); |
|
333 |
} |
|
334 |
} |
|
335 |
|
|
336 |
private String fixFunctionNames(String _filter) { |
|
337 |
|
|
338 |
Properties props = this.getPgHelper().getBeforePostgis13Properties(); |
|
339 |
Iterator iter = props.keySet().iterator(); |
|
340 |
String kstr = null; |
|
341 |
String vstr = null; |
|
342 |
|
|
343 |
String resp = _filter; |
|
344 |
|
|
345 |
while (iter.hasNext()) { |
|
346 |
kstr = (String) iter.next(); |
|
347 |
vstr = getPgHelper().getFunctionName(kstr); |
|
348 |
resp = replace(resp, kstr, vstr); |
|
349 |
} |
|
350 |
return resp; |
|
351 |
} |
|
352 |
|
|
353 |
private String replace(String str, String oldstr, String newstr) { |
|
354 |
|
|
355 |
if (oldstr == null || newstr == null || |
|
356 |
oldstr.length() == 0 || oldstr.equals(newstr)) { |
|
357 |
return str; |
|
358 |
} |
|
359 |
|
|
360 |
String lowerstr = str.toLowerCase(); |
|
361 |
String lowerold = oldstr.toLowerCase(); |
|
362 |
|
|
363 |
if (lowerstr.indexOf(lowerold) == -1) { |
|
364 |
// nothing to do |
|
365 |
return str; |
|
366 |
} |
|
367 |
|
|
368 |
Pattern p = Pattern.compile(lowerold, Pattern.LITERAL); |
|
369 |
String[] parts = p.split(lowerstr); |
|
370 |
|
|
371 |
StringBuffer resp = new StringBuffer(); |
|
372 |
int auxind = 0; |
|
373 |
resp.append(str.subSequence(0, parts[0].length())); |
|
374 |
for (int i=1; i<parts.length; i++) { |
|
375 |
resp.append(newstr); |
|
376 |
auxind = getIndex(parts, i-1, oldstr.length()); |
|
377 |
resp.append(str.subSequence(auxind, auxind + parts[i].length())); |
|
378 |
} |
|
379 |
return resp.toString(); |
|
380 |
} |
|
381 |
|
|
382 |
/** |
|
383 |
* This method gets the index where the n-th part (0-based) |
|
384 |
* starts in the original string |
|
385 |
* |
|
386 |
* @param parts |
|
387 |
* @param n |
|
388 |
* @param length |
|
389 |
* @return |
|
390 |
*/ |
|
391 |
private int getIndex(String[] parts, int till_n, int length) { |
|
392 |
|
|
393 |
int resp = 0; |
|
394 |
for (int i=0; i<(till_n+1); i++) { |
|
395 |
resp = resp + parts[i].length(); |
|
396 |
resp = resp + length; |
|
397 |
} |
|
398 |
return resp; |
|
399 |
} |
|
400 |
|
|
401 |
|
|
317 | 402 |
} |
Also available in: Unified diff