svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / dal / store / postgresql / PostgreSQLStoreProvider.java @ 34129
History | View | Annotate | Download (10 KB)
1 | 26790 | jmvivo | /* 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 | 27919 | jmvivo | package org.gvsig.fmap.dal.store.postgresql; |
29 | 26790 | jmvivo | |
30 | 28909 | jmvivo | import java.util.List; |
31 | 28046 | jmvivo | import java.util.regex.Matcher; |
32 | import java.util.regex.Pattern; |
||
33 | 26790 | jmvivo | |
34 | 27906 | jmvivo | import org.gvsig.fmap.dal.DALLocator; |
35 | import org.gvsig.fmap.dal.DataManager; |
||
36 | 26790 | jmvivo | import org.gvsig.fmap.dal.DataServerExplorer; |
37 | 28909 | jmvivo | import org.gvsig.fmap.dal.DataTypes; |
38 | 26790 | jmvivo | import org.gvsig.fmap.dal.exception.DataException; |
39 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
40 | import org.gvsig.fmap.dal.exception.ReadException; |
||
41 | 27906 | jmvivo | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
42 | 28909 | jmvivo | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
43 | 26790 | jmvivo | import org.gvsig.fmap.dal.feature.FeatureQuery; |
44 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
45 | 29289 | jmvivo | import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
46 | 26790 | jmvivo | import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
47 | 29326 | jmvivo | import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
48 | 32880 | jjdelcerro | import org.gvsig.fmap.dal.store.db.DBHelper; |
49 | 28784 | jmvivo | import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
50 | 28909 | jmvivo | import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProviderWriter; |
51 | import org.gvsig.fmap.geom.Geometry; |
||
52 | 27906 | jmvivo | import org.slf4j.Logger; |
53 | import org.slf4j.LoggerFactory; |
||
54 | 26790 | jmvivo | |
55 | 28909 | jmvivo | public class PostgreSQLStoreProvider extends JDBCStoreProviderWriter { |
56 | 26790 | jmvivo | |
57 | 28909 | jmvivo | public final static Logger logger = LoggerFactory |
58 | 27906 | jmvivo | .getLogger(PostgreSQLStoreProvider.class); |
59 | |||
60 | 32880 | jjdelcerro | public static final String NAME = "PostgreSQL"; |
61 | public static final String DESCRIPTION = "PostgreSQL source"; |
||
62 | 26790 | jmvivo | |
63 | 32880 | jjdelcerro | public static final String METADATA_DEFINITION_NAME = NAME; |
64 | 27525 | jmvivo | |
65 | 26790 | jmvivo | |
66 | 29326 | jmvivo | public PostgreSQLStoreProvider(PostgreSQLStoreParameters params,
|
67 | DataStoreProviderServices storeServices) |
||
68 | 26790 | jmvivo | throws InitializeException {
|
69 | 32880 | jjdelcerro | super(params, storeServices, DBHelper.newMetadataContainer(METADATA_DEFINITION_NAME));
|
70 | 26790 | jmvivo | } |
71 | |||
72 | 29326 | jmvivo | private PostgreSQLStoreParameters getPGParameters() {
|
73 | return (PostgreSQLStoreParameters) this.getParameters(); |
||
74 | 26790 | jmvivo | } |
75 | |||
76 | 28784 | jmvivo | protected JDBCHelper createHelper() throws InitializeException { |
77 | 29326 | jmvivo | return new PostgreSQLHelper(this, getPGParameters()); |
78 | 26790 | jmvivo | } |
79 | |||
80 | 28784 | jmvivo | protected String fixFilter(String filter) { |
81 | 28046 | jmvivo | 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 | 28047 | jmvivo | srsCode = matcher.group(1).trim();
|
96 | 28046 | jmvivo | if (srsCode.startsWith("'")) { |
97 | srsCode = srsCode.substring(1);
|
||
98 | } |
||
99 | if (srsCode.endsWith("'")) { |
||
100 | srsCode = srsCode.substring(0, srsCode.length() - 1); |
||
101 | } |
||
102 | 28784 | jmvivo | strb.append(helper.getProviderSRID(srsCode)); |
103 | 28046 | jmvivo | 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 | 26790 | jmvivo | public String getName() { |
113 | return NAME;
|
||
114 | } |
||
115 | |||
116 | 27595 | jmvivo | public FeatureSetProvider createSet(FeatureQuery query,
|
117 | FeatureType featureType) throws DataException {
|
||
118 | |||
119 | return new PostgreSQLSetProvider(this, query, featureType); |
||
120 | } |
||
121 | |||
122 | 27672 | jmvivo | |
123 | 27906 | jmvivo | public DataServerExplorer getExplorer() throws ReadException { |
124 | DataManager manager = DALLocator.getDataManager(); |
||
125 | PostgreSQLServerExplorerParameters exParams; |
||
126 | 29326 | jmvivo | PostgreSQLStoreParameters params = getPGParameters(); |
127 | 27906 | jmvivo | try {
|
128 | exParams = (PostgreSQLServerExplorerParameters) manager |
||
129 | .createServerExplorerParameters(PostgreSQLServerExplorer.NAME); |
||
130 | 28784 | jmvivo | exParams.setUrl(params.getUrl()); |
131 | 27906 | jmvivo | exParams.setHost(params.getHost()); |
132 | exParams.setPort(params.getPort()); |
||
133 | 27908 | jmvivo | exParams.setDBName(params.getDBName()); |
134 | 27906 | jmvivo | 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 | 29326 | jmvivo | exParams.setUseSSL(params.getUseSSL()); |
140 | 27595 | jmvivo | |
141 | 32880 | jjdelcerro | return manager.openServerExplorer(PostgreSQLServerExplorer.NAME, exParams);
|
142 | 27906 | jmvivo | } catch (DataException e) {
|
143 | throw new ReadException(this.getName(), e); |
||
144 | } catch (ValidateDataParametersException e) {
|
||
145 | throw new ReadException(this.getName(), e); |
||
146 | } |
||
147 | } |
||
148 | 27595 | jmvivo | |
149 | 28136 | jmvivo | public boolean allowAutomaticValues() { |
150 | 27595 | jmvivo | return true; |
151 | } |
||
152 | |||
153 | 27906 | jmvivo | |
154 | 28909 | jmvivo | public boolean hasGeometrySupport() { |
155 | return true; |
||
156 | 27906 | jmvivo | } |
157 | |||
158 | 28909 | jmvivo | // ************************************************************************************//
|
159 | |||
160 | |||
161 | // ************************************************************************************//
|
||
162 | |||
163 | |||
164 | |||
165 | protected PostgreSQLHelper getPgHelper() {
|
||
166 | return (PostgreSQLHelper) getHelper();
|
||
167 | 27595 | jmvivo | } |
168 | |||
169 | 28909 | jmvivo | // ************************************************************************************//
|
170 | 27595 | jmvivo | |
171 | 28909 | jmvivo | // ************************************************************************************//
|
172 | 27595 | jmvivo | |
173 | 28909 | jmvivo | |
174 | |||
175 | 29289 | jmvivo | public boolean canWriteGeometry(int geometryType, int geometrySubtype) |
176 | throws DataException {
|
||
177 | 29326 | jmvivo | FeatureType type = getFeatureStore().getDefaultFeatureType(); |
178 | FeatureAttributeDescriptor geomAttr = type.getAttributeDescriptor(type |
||
179 | 28909 | jmvivo | .getDefaultGeometryAttributeName()); |
180 | if (geomAttr == null) { |
||
181 | return false; |
||
182 | } |
||
183 | 29289 | jmvivo | if (geometrySubtype != geomAttr.getGeometrySubType()) {
|
184 | return false; |
||
185 | } |
||
186 | 28909 | jmvivo | 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 | 27595 | jmvivo | } |
211 | |||
212 | |||
213 | 29292 | jmvivo | protected void addToListFeatureValues(FeatureProvider featureProvider, |
214 | 28909 | jmvivo | FeatureAttributeDescriptor attrOfList, |
215 | FeatureAttributeDescriptor attr, |
||
216 | 32880 | jjdelcerro | List<Object> values) throws DataException { |
217 | 28909 | jmvivo | |
218 | 29292 | jmvivo | super.addToListFeatureValues(featureProvider, attrOfList, attr, values);
|
219 | 33331 | jjdelcerro | if (attr.getType() == DataTypes.GEOMETRY) {
|
220 | 28909 | jmvivo | values.add(helper.getProviderSRID(attr.getSRS())); |
221 | } |
||
222 | 27595 | jmvivo | } |
223 | |||
224 | 28909 | jmvivo | protected void prepareAttributeForInsert( |
225 | 32880 | jjdelcerro | FeatureAttributeDescriptor attr, List<String> fields, List<String> values) { |
226 | 28909 | jmvivo | |
227 | 33331 | jjdelcerro | if (attr.getType() == DataTypes.GEOMETRY) {
|
228 | 28909 | jmvivo | fields.add(helper.escapeFieldName(attr.getName())); |
229 | values.add("GeomFromWKB(?,?)");
|
||
230 | } else {
|
||
231 | super.prepareAttributeForInsert(attr, fields, values);
|
||
232 | } |
||
233 | |||
234 | 28676 | jmvivo | } |
235 | 27595 | jmvivo | |
236 | 28909 | jmvivo | protected void prepareAttributeForUpdate(FeatureAttributeDescriptor attr, |
237 | 32880 | jjdelcerro | List<String> values) { |
238 | 33331 | jjdelcerro | if (attr.getType() == DataTypes.GEOMETRY) {
|
239 | 28909 | jmvivo | values.add(helper.escapeFieldName(attr.getName()) |
240 | + " = GeomFromWKB(?,?)");
|
||
241 | } else {
|
||
242 | super.prepareAttributeForUpdate(attr, values);
|
||
243 | } |
||
244 | 28784 | jmvivo | } |
245 | |||
246 | 28909 | jmvivo | protected String getSqlStatementAddField(FeatureAttributeDescriptor attr, |
247 | 32880 | jjdelcerro | List<String> additionalStatement) throws DataException { |
248 | 33331 | jjdelcerro | if (attr.getType() == DataTypes.GEOMETRY) {
|
249 | 29326 | jmvivo | PostgreSQLStoreParameters params = getPGParameters(); |
250 | 32880 | jjdelcerro | additionalStatement.addAll( ((PostgreSQLHelper) helper) |
251 | 28909 | jmvivo | .getSqlGeometyFieldAdd(attr, params.getTable(), params |
252 | .getSchema())); |
||
253 | 27595 | jmvivo | |
254 | 28909 | jmvivo | } |
255 | return super.getSqlStatementAddField(attr, additionalStatement); |
||
256 | 27595 | jmvivo | |
257 | 28909 | jmvivo | } |
258 | 32880 | jjdelcerro | private String getSqlGeometyFieldDrop(FeatureAttributeDescriptor attr) { |
259 | 28909 | jmvivo | StringBuilder strb = new StringBuilder(); |
260 | 29326 | jmvivo | PostgreSQLStoreParameters params = getPGParameters(); |
261 | 28909 | jmvivo | 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 | 28676 | jmvivo | |
281 | 28909 | jmvivo | protected String getSqlStatementDropField(FeatureAttributeDescriptor attr, |
282 | 32880 | jjdelcerro | List<String> additionalStatement) { |
283 | 28909 | jmvivo | String result = super.getSqlStatementDropField(attr, |
284 | additionalStatement); |
||
285 | 33331 | jjdelcerro | if (attr.getType() == DataTypes.GEOMETRY) {
|
286 | 28909 | jmvivo | additionalStatement.add(getSqlGeometyFieldDrop(attr)); |
287 | } |
||
288 | return result;
|
||
289 | } |
||
290 | 28676 | jmvivo | |
291 | 32880 | jjdelcerro | protected List<String> getSqlStatementAlterField( |
292 | 28909 | jmvivo | FeatureAttributeDescriptor attrOrg, |
293 | 32880 | jjdelcerro | FeatureAttributeDescriptor attrTrg, List<String> additionalStatement) |
294 | 28909 | jmvivo | throws DataException {
|
295 | //
|
||
296 | 32880 | jjdelcerro | List<String> actions = super.getSqlStatementAlterField(attrOrg, attrTrg, |
297 | 28909 | jmvivo | additionalStatement); |
298 | 29326 | jmvivo | PostgreSQLStoreParameters params = getPGParameters(); |
299 | 28909 | jmvivo | if (attrOrg.getDataType() != attrTrg.getDataType()) {
|
300 | 33331 | jjdelcerro | if (attrOrg.getType() == DataTypes.GEOMETRY) {
|
301 | 28909 | jmvivo | additionalStatement.add(getSqlGeometyFieldDrop(attrOrg)); |
302 | } |
||
303 | 33331 | jjdelcerro | if (attrTrg.getType() == DataTypes.GEOMETRY) {
|
304 | 28909 | jmvivo | additionalStatement.addAll(((PostgreSQLHelper) helper) |
305 | .getSqlGeometyFieldAdd(attrTrg, params.getTable(), |
||
306 | params.getSchema())); |
||
307 | } |
||
308 | } |
||
309 | if (attrOrg.getDataType() == attrTrg.getDataType()
|
||
310 | 33331 | jjdelcerro | && attrTrg.getType() == DataTypes.GEOMETRY) { |
311 | 28909 | jmvivo | // TODO Checks SRS and GeomType/Subtype
|
312 | } |
||
313 | |||
314 | return actions;
|
||
315 | 27595 | jmvivo | } |
316 | 28909 | jmvivo | |
317 | 26790 | jmvivo | } |