root / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / dal / store / postgresql / PostgreSQLStoreProvider.java @ 33331
History | View | Annotate | Download (10 KB)
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 |
* AUTHORS (In addition to CIT):
|
25 |
* 2009 IVER T.I {{Task}}
|
26 |
*/
|
27 |
|
28 |
package org.gvsig.fmap.dal.store.postgresql; |
29 |
|
30 |
import java.util.List; |
31 |
import java.util.regex.Matcher; |
32 |
import java.util.regex.Pattern; |
33 |
|
34 |
import org.gvsig.fmap.dal.DALLocator; |
35 |
import org.gvsig.fmap.dal.DataManager; |
36 |
import org.gvsig.fmap.dal.DataServerExplorer; |
37 |
import org.gvsig.fmap.dal.DataTypes; |
38 |
import org.gvsig.fmap.dal.exception.DataException; |
39 |
import org.gvsig.fmap.dal.exception.InitializeException; |
40 |
import org.gvsig.fmap.dal.exception.ReadException; |
41 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
42 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
43 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
44 |
import org.gvsig.fmap.dal.feature.FeatureType; |
45 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
46 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
47 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
48 |
import org.gvsig.fmap.dal.store.db.DBHelper; |
49 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
50 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProviderWriter; |
51 |
import org.gvsig.fmap.geom.Geometry; |
52 |
import org.slf4j.Logger; |
53 |
import org.slf4j.LoggerFactory; |
54 |
|
55 |
public class PostgreSQLStoreProvider extends JDBCStoreProviderWriter { |
56 |
|
57 |
public final static Logger logger = LoggerFactory |
58 |
.getLogger(PostgreSQLStoreProvider.class); |
59 |
|
60 |
public static final String NAME = "PostgreSQL"; |
61 |
public static final String DESCRIPTION = "PostgreSQL source"; |
62 |
|
63 |
public static final String METADATA_DEFINITION_NAME = NAME; |
64 |
|
65 |
|
66 |
public PostgreSQLStoreProvider(PostgreSQLStoreParameters params,
|
67 |
DataStoreProviderServices storeServices) |
68 |
throws InitializeException {
|
69 |
super(params, storeServices, DBHelper.newMetadataContainer(METADATA_DEFINITION_NAME));
|
70 |
} |
71 |
|
72 |
private PostgreSQLStoreParameters getPGParameters() {
|
73 |
return (PostgreSQLStoreParameters) this.getParameters(); |
74 |
} |
75 |
|
76 |
protected JDBCHelper createHelper() throws InitializeException { |
77 |
return new PostgreSQLHelper(this, getPGParameters()); |
78 |
} |
79 |
|
80 |
protected String fixFilter(String filter) { |
81 |
if (filter == null) { |
82 |
return null; |
83 |
} |
84 |
|
85 |
// Transform SRS to code
|
86 |
// GeomFromText\s*\(\s*'[^']*'\s*,\s*('[^']*')\s*\)
|
87 |
Pattern pattern = Pattern |
88 |
.compile("GeomFromText\\s*\\(\\s*'[^']*'\\s*,\\s*'([^']*)'\\s*\\)");
|
89 |
Matcher matcher = pattern.matcher(filter);
|
90 |
StringBuilder strb = new StringBuilder(); |
91 |
int pos = 0; |
92 |
String srsCode;
|
93 |
while (matcher.find(pos)) {
|
94 |
strb.append(filter.substring(pos, matcher.start(1)));
|
95 |
srsCode = matcher.group(1).trim();
|
96 |
if (srsCode.startsWith("'")) { |
97 |
srsCode = srsCode.substring(1);
|
98 |
} |
99 |
if (srsCode.endsWith("'")) { |
100 |
srsCode = srsCode.substring(0, srsCode.length() - 1); |
101 |
} |
102 |
strb.append(helper.getProviderSRID(srsCode)); |
103 |
strb.append(filter.substring(matcher.end(1), matcher.end()));
|
104 |
pos = matcher.end(); |
105 |
|
106 |
} |
107 |
strb.append(filter.substring(pos)); |
108 |
|
109 |
return strb.toString();
|
110 |
} |
111 |
|
112 |
public String getName() { |
113 |
return NAME;
|
114 |
} |
115 |
|
116 |
public FeatureSetProvider createSet(FeatureQuery query,
|
117 |
FeatureType featureType) throws DataException {
|
118 |
|
119 |
return new PostgreSQLSetProvider(this, query, featureType); |
120 |
} |
121 |
|
122 |
|
123 |
public DataServerExplorer getExplorer() throws ReadException { |
124 |
DataManager manager = DALLocator.getDataManager(); |
125 |
PostgreSQLServerExplorerParameters exParams; |
126 |
PostgreSQLStoreParameters params = getPGParameters(); |
127 |
try {
|
128 |
exParams = (PostgreSQLServerExplorerParameters) manager |
129 |
.createServerExplorerParameters(PostgreSQLServerExplorer.NAME); |
130 |
exParams.setUrl(params.getUrl()); |
131 |
exParams.setHost(params.getHost()); |
132 |
exParams.setPort(params.getPort()); |
133 |
exParams.setDBName(params.getDBName()); |
134 |
exParams.setUser(params.getUser()); |
135 |
exParams.setPassword(params.getPassword()); |
136 |
exParams.setCatalog(params.getCatalog()); |
137 |
exParams.setSchema(params.getSchema()); |
138 |
exParams.setJDBCDriverClassName(params.getJDBCDriverClassName()); |
139 |
exParams.setUseSSL(params.getUseSSL()); |
140 |
|
141 |
return manager.openServerExplorer(PostgreSQLServerExplorer.NAME, exParams);
|
142 |
} catch (DataException e) {
|
143 |
throw new ReadException(this.getName(), e); |
144 |
} catch (ValidateDataParametersException e) {
|
145 |
throw new ReadException(this.getName(), e); |
146 |
} |
147 |
} |
148 |
|
149 |
public boolean allowAutomaticValues() { |
150 |
return true; |
151 |
} |
152 |
|
153 |
|
154 |
public boolean hasGeometrySupport() { |
155 |
return true; |
156 |
} |
157 |
|
158 |
// ************************************************************************************//
|
159 |
|
160 |
|
161 |
// ************************************************************************************//
|
162 |
|
163 |
|
164 |
|
165 |
protected PostgreSQLHelper getPgHelper() {
|
166 |
return (PostgreSQLHelper) getHelper();
|
167 |
} |
168 |
|
169 |
// ************************************************************************************//
|
170 |
|
171 |
// ************************************************************************************//
|
172 |
|
173 |
|
174 |
|
175 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) |
176 |
throws DataException {
|
177 |
FeatureType type = getFeatureStore().getDefaultFeatureType(); |
178 |
FeatureAttributeDescriptor geomAttr = type.getAttributeDescriptor(type |
179 |
.getDefaultGeometryAttributeName()); |
180 |
if (geomAttr == null) { |
181 |
return false; |
182 |
} |
183 |
if (geometrySubtype != geomAttr.getGeometrySubType()) {
|
184 |
return false; |
185 |
} |
186 |
switch (geomAttr.getGeometryType()) {
|
187 |
case Geometry.TYPES.GEOMETRY:
|
188 |
return true; |
189 |
|
190 |
case Geometry.TYPES.MULTISURFACE:
|
191 |
return geometryType == Geometry.TYPES.MULTISURFACE
|
192 |
|| geometryType == Geometry.TYPES.SURFACE; |
193 |
|
194 |
case Geometry.TYPES.MULTIPOINT:
|
195 |
return geometryType == Geometry.TYPES.MULTIPOINT
|
196 |
|| geometryType == Geometry.TYPES.POINT; |
197 |
|
198 |
case Geometry.TYPES.MULTICURVE:
|
199 |
return geometryType == Geometry.TYPES.MULTICURVE
|
200 |
|| geometryType == Geometry.TYPES.CURVE; |
201 |
|
202 |
case Geometry.TYPES.MULTISOLID:
|
203 |
return geometryType == Geometry.TYPES.MULTISOLID
|
204 |
|| geometryType == Geometry.TYPES.SOLID; |
205 |
|
206 |
default:
|
207 |
return geometryType == geomAttr.getGeometryType();
|
208 |
} |
209 |
|
210 |
} |
211 |
|
212 |
|
213 |
protected void addToListFeatureValues(FeatureProvider featureProvider, |
214 |
FeatureAttributeDescriptor attrOfList, |
215 |
FeatureAttributeDescriptor attr, |
216 |
List<Object> values) throws DataException { |
217 |
|
218 |
super.addToListFeatureValues(featureProvider, attrOfList, attr, values);
|
219 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
220 |
values.add(helper.getProviderSRID(attr.getSRS())); |
221 |
} |
222 |
} |
223 |
|
224 |
protected void prepareAttributeForInsert( |
225 |
FeatureAttributeDescriptor attr, List<String> fields, List<String> values) { |
226 |
|
227 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
228 |
fields.add(helper.escapeFieldName(attr.getName())); |
229 |
values.add("GeomFromWKB(?,?)");
|
230 |
} else {
|
231 |
super.prepareAttributeForInsert(attr, fields, values);
|
232 |
} |
233 |
|
234 |
} |
235 |
|
236 |
protected void prepareAttributeForUpdate(FeatureAttributeDescriptor attr, |
237 |
List<String> values) { |
238 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
239 |
values.add(helper.escapeFieldName(attr.getName()) |
240 |
+ " = GeomFromWKB(?,?)");
|
241 |
} else {
|
242 |
super.prepareAttributeForUpdate(attr, values);
|
243 |
} |
244 |
} |
245 |
|
246 |
protected String getSqlStatementAddField(FeatureAttributeDescriptor attr, |
247 |
List<String> additionalStatement) throws DataException { |
248 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
249 |
PostgreSQLStoreParameters params = getPGParameters(); |
250 |
additionalStatement.addAll( ((PostgreSQLHelper) helper) |
251 |
.getSqlGeometyFieldAdd(attr, params.getTable(), params |
252 |
.getSchema())); |
253 |
|
254 |
} |
255 |
return super.getSqlStatementAddField(attr, additionalStatement); |
256 |
|
257 |
} |
258 |
private String getSqlGeometyFieldDrop(FeatureAttributeDescriptor attr) { |
259 |
StringBuilder strb = new StringBuilder(); |
260 |
PostgreSQLStoreParameters params = getPGParameters(); |
261 |
strb.append("Delete from geometry_columns where f_geometry_column = '");
|
262 |
strb.append(attr.getName()); |
263 |
strb.append("' and f_table_nam = '");
|
264 |
strb.append(params.getTable()); |
265 |
strb.append("' and f_table_schema = ");
|
266 |
if (params.getSchema() == null || params.getSchema().length() == 0) { |
267 |
strb.append("current_schema()");
|
268 |
} else {
|
269 |
strb.append("'");
|
270 |
strb.append(params.getSchema()); |
271 |
strb.append("'");
|
272 |
} |
273 |
if (params.getCatalog() != null && params.getCatalog().length() > 0) { |
274 |
strb.append(" and f_table_catalog = '");
|
275 |
strb.append(params.getCatalog()); |
276 |
strb.append("'");
|
277 |
} |
278 |
return strb.toString();
|
279 |
} |
280 |
|
281 |
protected String getSqlStatementDropField(FeatureAttributeDescriptor attr, |
282 |
List<String> additionalStatement) { |
283 |
String result = super.getSqlStatementDropField(attr, |
284 |
additionalStatement); |
285 |
if (attr.getType() == DataTypes.GEOMETRY) {
|
286 |
additionalStatement.add(getSqlGeometyFieldDrop(attr)); |
287 |
} |
288 |
return result;
|
289 |
} |
290 |
|
291 |
protected List<String> getSqlStatementAlterField( |
292 |
FeatureAttributeDescriptor attrOrg, |
293 |
FeatureAttributeDescriptor attrTrg, List<String> additionalStatement) |
294 |
throws DataException {
|
295 |
//
|
296 |
List<String> actions = super.getSqlStatementAlterField(attrOrg, attrTrg, |
297 |
additionalStatement); |
298 |
PostgreSQLStoreParameters params = getPGParameters(); |
299 |
if (attrOrg.getDataType() != attrTrg.getDataType()) {
|
300 |
if (attrOrg.getType() == DataTypes.GEOMETRY) {
|
301 |
additionalStatement.add(getSqlGeometyFieldDrop(attrOrg)); |
302 |
} |
303 |
if (attrTrg.getType() == DataTypes.GEOMETRY) {
|
304 |
additionalStatement.addAll(((PostgreSQLHelper) helper) |
305 |
.getSqlGeometyFieldAdd(attrTrg, params.getTable(), |
306 |
params.getSchema())); |
307 |
} |
308 |
} |
309 |
if (attrOrg.getDataType() == attrTrg.getDataType()
|
310 |
&& attrTrg.getType() == DataTypes.GEOMETRY) { |
311 |
// TODO Checks SRS and GeomType/Subtype
|
312 |
} |
313 |
|
314 |
return actions;
|
315 |
} |
316 |
|
317 |
} |