root / branches / v2_0_0_prep / libraries / libFMap_daldb / src / org / gvsig / fmap / dal / store / mysql / MySQLStoreProvider.java @ 29326
History | View | Annotate | Download (8.41 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.mysql; |
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.FeatureStore; |
45 |
import org.gvsig.fmap.dal.feature.FeatureType; |
46 |
import org.gvsig.fmap.dal.feature.spi.FeatureSetProvider; |
47 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
48 |
import org.gvsig.fmap.dal.store.jdbc.JDBCHelper; |
49 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProviderWriter; |
50 |
import org.gvsig.fmap.geom.Geometry; |
51 |
import org.gvsig.tools.ToolsLocator; |
52 |
import org.gvsig.tools.dynobject.DynClass; |
53 |
import org.gvsig.tools.dynobject.DynObjectManager; |
54 |
import org.slf4j.Logger; |
55 |
import org.slf4j.LoggerFactory; |
56 |
|
57 |
public class MySQLStoreProvider extends JDBCStoreProviderWriter { |
58 |
|
59 |
public final static Logger logger = LoggerFactory |
60 |
.getLogger(MySQLStoreProvider.class); |
61 |
|
62 |
public static String NAME = "MySQL"; |
63 |
public static String DESCRIPTION = "MySQL source"; |
64 |
private static final String DYNCLASS_NAME = "MySQLStore"; |
65 |
private static DynClass DYNCLASS = null; |
66 |
|
67 |
|
68 |
protected static void registerDynClass() { |
69 |
DynObjectManager dynman = ToolsLocator.getDynObjectManager(); |
70 |
DynClass dynClass; |
71 |
if (DYNCLASS == null) { |
72 |
dynClass = dynman.add(DYNCLASS_NAME, DESCRIPTION); |
73 |
|
74 |
dynClass.extend(dynman.get(FeatureStore.DYNCLASS_NAME)); |
75 |
DYNCLASS = dynClass; |
76 |
} |
77 |
} |
78 |
|
79 |
|
80 |
public MySQLStoreProvider(MySQLStoreParameters params,
|
81 |
DataStoreProviderServices storeServices) |
82 |
throws InitializeException {
|
83 |
super(params, storeServices, ToolsLocator.getDynObjectManager()
|
84 |
.createDynObject(DYNCLASS)); |
85 |
|
86 |
} |
87 |
|
88 |
private MySQLStoreParameters getMySQLParamters() {
|
89 |
return (MySQLStoreParameters) this.getParameters(); |
90 |
} |
91 |
|
92 |
protected JDBCHelper createHelper() throws InitializeException { |
93 |
return new MySQLHelper(this, getMySQLParamters()); |
94 |
} |
95 |
|
96 |
protected String fixFilter(String filter) { |
97 |
if (filter == null) { |
98 |
return null; |
99 |
} |
100 |
|
101 |
// Transform SRS to code
|
102 |
// GeomFromText\s*\(\s*'[^']*'\s*,\s*('[^']*')\s*\)
|
103 |
Pattern pattern = Pattern |
104 |
.compile("GeomFromText\\s*\\(\\s*'[^']*'\\s*,\\s*'([^']*)'\\s*\\)");
|
105 |
Matcher matcher = pattern.matcher(filter);
|
106 |
StringBuilder strb = new StringBuilder(); |
107 |
int pos = 0; |
108 |
String srsCode;
|
109 |
while (matcher.find(pos)) {
|
110 |
strb.append(filter.substring(pos, matcher.start(1)));
|
111 |
srsCode = matcher.group(1).trim();
|
112 |
if (srsCode.startsWith("'")) { |
113 |
srsCode = srsCode.substring(1);
|
114 |
} |
115 |
if (srsCode.endsWith("'")) { |
116 |
srsCode = srsCode.substring(0, srsCode.length() - 1); |
117 |
} |
118 |
// strb.append(helper.getProviderSRID(srsCode));
|
119 |
strb.append(filter.substring(matcher.end(1), matcher.end()));
|
120 |
pos = matcher.end(); |
121 |
|
122 |
} |
123 |
strb.append(filter.substring(pos)); |
124 |
|
125 |
return strb.toString();
|
126 |
} |
127 |
|
128 |
public String getName() { |
129 |
return NAME;
|
130 |
} |
131 |
|
132 |
public FeatureSetProvider createSet(FeatureQuery query,
|
133 |
FeatureType featureType) throws DataException {
|
134 |
|
135 |
return new MySQLSetProvider(this, query, featureType); |
136 |
} |
137 |
|
138 |
|
139 |
public DataServerExplorer getExplorer() throws ReadException { |
140 |
DataManager manager = DALLocator.getDataManager(); |
141 |
MySQLServerExplorerParameters exParams; |
142 |
MySQLStoreParameters params = getMySQLParamters(); |
143 |
try {
|
144 |
exParams = (MySQLServerExplorerParameters) manager |
145 |
.createServerExplorerParameters(MySQLServerExplorer.NAME); |
146 |
exParams.setUrl(params.getUrl()); |
147 |
exParams.setHost(params.getHost()); |
148 |
exParams.setPort(params.getPort()); |
149 |
exParams.setDBName(params.getDBName()); |
150 |
exParams.setUser(params.getUser()); |
151 |
exParams.setPassword(params.getPassword()); |
152 |
exParams.setCatalog(params.getCatalog()); |
153 |
exParams.setSchema(params.getSchema()); |
154 |
exParams.setJDBCDriverClassName(params.getJDBCDriverClassName()); |
155 |
exParams.setUseSSL(params.getUseSSL()); |
156 |
|
157 |
return manager.createServerExplorer(exParams);
|
158 |
} catch (DataException e) {
|
159 |
throw new ReadException(this.getName(), e); |
160 |
} catch (ValidateDataParametersException e) {
|
161 |
// TODO Auto-generated catch block
|
162 |
throw new ReadException(this.getName(), e); |
163 |
} |
164 |
} |
165 |
|
166 |
public boolean allowAutomaticValues() { |
167 |
return true; |
168 |
} |
169 |
|
170 |
|
171 |
public boolean hasGeometrySupport() { |
172 |
return true; |
173 |
} |
174 |
|
175 |
// ************************************************************************************//
|
176 |
|
177 |
|
178 |
// ************************************************************************************//
|
179 |
|
180 |
|
181 |
|
182 |
protected MySQLHelper getMyHelper() {
|
183 |
return (MySQLHelper) getHelper();
|
184 |
} |
185 |
|
186 |
// ************************************************************************************//
|
187 |
|
188 |
// ************************************************************************************//
|
189 |
|
190 |
|
191 |
|
192 |
public boolean canWriteGeometry(int geometryType, int geometrySubtype) |
193 |
throws DataException {
|
194 |
FeatureType fType = this.getFeatureStore().getDefaultFeatureType();
|
195 |
FeatureAttributeDescriptor geomAttr = fType |
196 |
.getAttributeDescriptor(fType.getDefaultGeometryAttributeName()); |
197 |
if (geomAttr == null) { |
198 |
return false; |
199 |
} |
200 |
if (geometrySubtype != Geometry.SUBTYPES.GEOM2D) {
|
201 |
return false; |
202 |
} |
203 |
|
204 |
switch (geomAttr.getGeometryType()) {
|
205 |
case Geometry.TYPES.GEOMETRY:
|
206 |
return true; |
207 |
|
208 |
case Geometry.TYPES.MULTISURFACE:
|
209 |
return geometryType == Geometry.TYPES.MULTISURFACE
|
210 |
|| geometryType == Geometry.TYPES.SURFACE; |
211 |
|
212 |
case Geometry.TYPES.MULTIPOINT:
|
213 |
return geometryType == Geometry.TYPES.MULTIPOINT
|
214 |
|| geometryType == Geometry.TYPES.POINT; |
215 |
|
216 |
case Geometry.TYPES.MULTICURVE:
|
217 |
return geometryType == Geometry.TYPES.MULTICURVE
|
218 |
|| geometryType == Geometry.TYPES.CURVE; |
219 |
|
220 |
case Geometry.TYPES.MULTISOLID:
|
221 |
return geometryType == Geometry.TYPES.MULTISOLID
|
222 |
|| geometryType == Geometry.TYPES.SOLID; |
223 |
|
224 |
default:
|
225 |
return geometryType == geomAttr.getGeometryType();
|
226 |
} |
227 |
|
228 |
} |
229 |
|
230 |
|
231 |
protected void prepareAttributeForInsert( |
232 |
FeatureAttributeDescriptor attr, List fields, List values) { |
233 |
|
234 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
235 |
fields.add(helper.escapeFieldName(attr.getName())); |
236 |
values.add("GeomFromWKB(?)");
|
237 |
} else {
|
238 |
super.prepareAttributeForInsert(attr, fields, values);
|
239 |
} |
240 |
|
241 |
} |
242 |
|
243 |
protected void prepareAttributeForUpdate(FeatureAttributeDescriptor attr, |
244 |
List values) {
|
245 |
if (attr.getDataType() == DataTypes.GEOMETRY) {
|
246 |
values.add(helper.escapeFieldName(attr.getName()) |
247 |
+ " = GeomFromWKB(?)");
|
248 |
} else {
|
249 |
super.prepareAttributeForUpdate(attr, values);
|
250 |
} |
251 |
} |
252 |
|
253 |
protected List getSqlStatementAlterField( |
254 |
FeatureAttributeDescriptor attrOrg, |
255 |
FeatureAttributeDescriptor attrTrg, List additionalStatement)
|
256 |
throws DataException {
|
257 |
//
|
258 |
List actions = super.getSqlStatementAlterField(attrOrg, attrTrg, |
259 |
additionalStatement); |
260 |
StringBuilder strb;
|
261 |
if (attrOrg.getDataType() != attrTrg.getDataType()) {
|
262 |
if (attrOrg.getDataType() == DataTypes.GEOMETRY) {
|
263 |
// FIXME
|
264 |
//additionalStatement.add(getSqlGeometyFieldDrop(attrOrg));
|
265 |
} |
266 |
if (attrTrg.getDataType() == DataTypes.GEOMETRY) {
|
267 |
// FIXME
|
268 |
// additionalStatement.addAll(((MySQLHelper) helper)
|
269 |
// .getSqlGeometyFieldAdd(attrTrg, params.getTable(),
|
270 |
// params.getSchema()));
|
271 |
} |
272 |
} |
273 |
if (attrOrg.getDataType() == attrTrg.getDataType()
|
274 |
&& attrTrg.getDataType() == DataTypes.GEOMETRY) { |
275 |
// TODO Checks SRS and GeomType/Subtype
|
276 |
} |
277 |
|
278 |
return actions;
|
279 |
} |
280 |
|
281 |
} |