svn-gvsig-desktop / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / mysql / MySQLDriver.java @ 5007
History | View | Annotate | Download (9.57 KB)
1 |
/*
|
---|---|
2 |
* Created on 04-mar-2005
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
package com.iver.cit.gvsig.fmap.drivers.jdbc.mysql; |
45 |
|
46 |
import java.awt.geom.Rectangle2D; |
47 |
import java.sql.Connection; |
48 |
import java.sql.ResultSet; |
49 |
import java.sql.SQLException; |
50 |
import java.sql.Statement; |
51 |
|
52 |
import org.apache.log4j.Logger; |
53 |
|
54 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
55 |
import com.iver.andami.messages.NotificationManager; |
56 |
import com.iver.cit.gvsig.fmap.DriverException; |
57 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
58 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
59 |
import com.iver.cit.gvsig.fmap.drivers.DefaultDBDriver; |
60 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
61 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
62 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.WKBParser2; |
63 |
|
64 |
/**
|
65 |
* @author FJP
|
66 |
*
|
67 |
* TODO To change the template for this generated type comment go to Window -
|
68 |
* Preferences - Java - Code Generation - Code and Comments
|
69 |
*/
|
70 |
public class MySQLDriver extends DefaultDBDriver { |
71 |
private static Logger logger = Logger |
72 |
.getLogger(MySQLDriver.class.getName()); |
73 |
|
74 |
private WKBParser2 parser = new WKBParser2(); |
75 |
|
76 |
// private WKTParser wktParser = new WKTParser();
|
77 |
|
78 |
/*
|
79 |
* private int fetch_min=-1; private int fetch_max=-1;
|
80 |
*/
|
81 |
private Statement st; |
82 |
|
83 |
private String strAux; |
84 |
|
85 |
private String strEPSG = "-1"; |
86 |
|
87 |
private String originalEPSG; |
88 |
|
89 |
private String completeWhere; |
90 |
|
91 |
/**
|
92 |
* Don't have information about working area
|
93 |
*/
|
94 |
private String sqlOrig; |
95 |
|
96 |
/**
|
97 |
* Does have information about working area and order
|
98 |
*/
|
99 |
private String sqlTotal; |
100 |
|
101 |
/**
|
102 |
*
|
103 |
*/
|
104 |
public MySQLDriver() {
|
105 |
} |
106 |
|
107 |
/*
|
108 |
* (non-Javadoc)
|
109 |
*
|
110 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
|
111 |
*/
|
112 |
public DriverAttributes getDriverAttributes() {
|
113 |
return null; |
114 |
} |
115 |
|
116 |
/*
|
117 |
* (non-Javadoc)
|
118 |
*
|
119 |
* @see com.hardcode.driverManager.Driver#getName()
|
120 |
*/
|
121 |
public String getName() { |
122 |
return "mySQL JDBC Driver"; |
123 |
} |
124 |
|
125 |
/**
|
126 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
127 |
*/
|
128 |
public IGeometry getShape(int index) { |
129 |
IGeometry geom = null;
|
130 |
boolean resul;
|
131 |
try {
|
132 |
if (rs != null) { |
133 |
rs.absolute(index + 1);
|
134 |
byte[] data = rs.getBytes(1); |
135 |
geom = parser.parse(data); |
136 |
} |
137 |
|
138 |
} catch (SQLException e) { |
139 |
e.printStackTrace(); |
140 |
} |
141 |
|
142 |
return geom;
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* @param conn
|
147 |
*/
|
148 |
/*
|
149 |
* (non-Javadoc)
|
150 |
*
|
151 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#setData(java.sql.Connection,
|
152 |
* java.lang.String, java.lang.String, java.lang.String, int)
|
153 |
*/
|
154 |
public void setData(Connection conn, DBLayerDefinition lyrDef) { |
155 |
this.conn = conn;
|
156 |
setLyrDef(lyrDef); |
157 |
try {
|
158 |
|
159 |
// NO ESTA LISTO ESTO AUN EN mySQL, o no s? usuarlo getTableEPSG();
|
160 |
|
161 |
sqlOrig = "SELECT " + getTotalFields() + " FROM " |
162 |
+ getLyrDef().getTableName() + " "
|
163 |
+ getLyrDef().getWhereClause(); |
164 |
completeWhere = getCompoundWhere(workingArea, strEPSG); |
165 |
completeWhere = completeWhere; |
166 |
String sqlAux = sqlOrig + completeWhere + " ORDER BY " |
167 |
+ getLyrDef().getFieldID(); |
168 |
completeWhere = getLyrDef().getWhereClause() + completeWhere; |
169 |
logger.info("Cadena SQL:" + sqlAux);
|
170 |
sqlTotal = sqlAux; |
171 |
|
172 |
st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
|
173 |
ResultSet.CONCUR_READ_ONLY);
|
174 |
|
175 |
rs = st.executeQuery(sqlTotal); |
176 |
metaData = rs.getMetaData(); |
177 |
// Le pegamos un primera pasada para poder relacionar
|
178 |
// un campo de identificador ?nico (parecido al OID en
|
179 |
// postgresql) con el ?ndice dentro del recordset.
|
180 |
// Esto cuando haya ediciones, no es v?lido, y hay
|
181 |
// que refrescarlo.
|
182 |
doRelateID_FID(); |
183 |
|
184 |
} catch (SQLException e) { |
185 |
NotificationManager.addError( |
186 |
"Error al conectar a la base de datos.", e);
|
187 |
e.printStackTrace(); |
188 |
} |
189 |
} |
190 |
|
191 |
/*
|
192 |
* (non-Javadoc)
|
193 |
*
|
194 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.lang.String)
|
195 |
*/
|
196 |
public IFeatureIterator getFeatureIterator(String sql) |
197 |
throws com.iver.cit.gvsig.fmap.DriverException {
|
198 |
Statement st;
|
199 |
MySqlFeatureIterator geomIterator = null;
|
200 |
try {
|
201 |
logger.debug(sql); |
202 |
st = conn.createStatement(); |
203 |
// st.setFetchSize(2000);
|
204 |
ResultSet rs = st.executeQuery(sql);
|
205 |
geomIterator = new MySqlFeatureIterator(rs);
|
206 |
geomIterator.setLyrDef(getLyrDef()); |
207 |
} catch (SQLException e) { |
208 |
e.printStackTrace(); |
209 |
throw new com.iver.cit.gvsig.fmap.DriverException(e); |
210 |
} |
211 |
|
212 |
return geomIterator;
|
213 |
} |
214 |
|
215 |
/*
|
216 |
* (non-Javadoc)
|
217 |
*
|
218 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.awt.geom.Rectangle2D)
|
219 |
*/
|
220 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) |
221 |
throws DriverException {
|
222 |
if (workingArea != null) |
223 |
r = r.createIntersection(workingArea); |
224 |
String sqlAux = sqlOrig + getCompoundWhere(r, strEPSG);
|
225 |
|
226 |
return getFeatureIterator(sqlAux);
|
227 |
} |
228 |
|
229 |
/**
|
230 |
* Le pasas el rect?ngulo que quieres pedir. La primera vez es el
|
231 |
* workingArea, y las siguientes una interseccion de este rectangulo con el
|
232 |
* workingArea
|
233 |
*
|
234 |
* @param r
|
235 |
* @param strEPSG
|
236 |
* @return
|
237 |
*/
|
238 |
private String getCompoundWhere(Rectangle2D r, String strEPSG) { |
239 |
if (r == null) |
240 |
return ""; |
241 |
|
242 |
double xMin = r.getMinX();
|
243 |
double yMin = r.getMinY();
|
244 |
double xMax = r.getMaxX();
|
245 |
double yMax = r.getMaxY();
|
246 |
|
247 |
String wktBox = "GeomFromText('LINESTRING(" + xMin + " " + yMin + ", " |
248 |
+ xMax + " " + yMin + ", " + xMax + " " + yMax + ", " + xMin |
249 |
+ " " + yMax + ")', " + strEPSG + ")"; |
250 |
String sqlAux;
|
251 |
if (getWhereClause().startsWith("WHERE")) |
252 |
sqlAux = " MBRIntersects(" + wktBox + ",ogc_geom)"; |
253 |
else
|
254 |
sqlAux = "WHERE MBRIntersects(" + wktBox + ",ogc_geom)"; |
255 |
return sqlAux;
|
256 |
} |
257 |
|
258 |
public void open() { |
259 |
// TODO Auto-generated method stub
|
260 |
|
261 |
} |
262 |
|
263 |
/**
|
264 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getConnectionStringBeginning()
|
265 |
*/
|
266 |
public String getConnectionStringBeginning() { |
267 |
return "jdbc:mysql:"; |
268 |
} |
269 |
|
270 |
static {
|
271 |
try {
|
272 |
Class.forName("com.mysql.jdbc.Driver"); |
273 |
} catch (ClassNotFoundException e) { |
274 |
throw new RuntimeException(e); |
275 |
} |
276 |
} |
277 |
|
278 |
/**
|
279 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryField(java.lang.String)
|
280 |
*/
|
281 |
public String getGeometryField(String fieldName) { |
282 |
return "ASBINARY(" + fieldName + ")"; |
283 |
} |
284 |
|
285 |
/**
|
286 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
|
287 |
*/
|
288 |
public int[] getPrimaryKeys() |
289 |
throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
290 |
return new int[] { getLyrDef().getIdFieldID() - 2 }; |
291 |
} |
292 |
|
293 |
/**
|
294 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#getDefaultPort()
|
295 |
*/
|
296 |
public int getDefaultPort() { |
297 |
return 3306; |
298 |
} |
299 |
|
300 |
/**
|
301 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
|
302 |
*/
|
303 |
public void write(DataWare arg0) |
304 |
throws com.hardcode.gdbms.engine.data.driver.DriverException {
|
305 |
// TODO Auto-generated method stub
|
306 |
|
307 |
} |
308 |
|
309 |
private void getTableEPSG() { |
310 |
try {
|
311 |
Statement stAux = conn.createStatement();
|
312 |
|
313 |
String sql = "SELECT SRID(ogc_geom) FROM " + getTableName() |
314 |
+ " LIMIT 1;";
|
315 |
ResultSet rs = stAux.executeQuery(sql);
|
316 |
rs.next(); |
317 |
originalEPSG = "" + rs.getInt(1); |
318 |
rs.close(); |
319 |
} catch (SQLException e) { |
320 |
// TODO Auto-generated catch block
|
321 |
e.printStackTrace(); |
322 |
} |
323 |
|
324 |
} |
325 |
|
326 |
public String getSqlTotal() { |
327 |
return sqlTotal;
|
328 |
} |
329 |
|
330 |
/**
|
331 |
* @return Returns the completeWhere.
|
332 |
*/
|
333 |
public String getCompleteWhere() { |
334 |
return completeWhere;
|
335 |
} |
336 |
|
337 |
/*
|
338 |
* (non-Javadoc)
|
339 |
*
|
340 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getFeatureIterator(java.awt.geom.Rectangle2D,
|
341 |
* java.lang.String, java.lang.String[])
|
342 |
*/
|
343 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG, |
344 |
String[] alphaNumericFieldsNeeded) throws DriverException { |
345 |
if (workingArea != null) |
346 |
r = r.createIntersection(workingArea); |
347 |
|
348 |
String strAux = getGeometryField(getLyrDef().getFieldGeometry());
|
349 |
boolean found = false; |
350 |
if (alphaNumericFieldsNeeded != null) { |
351 |
for (int i = 0; i < alphaNumericFieldsNeeded.length; i++) { |
352 |
strAux = strAux + ", " + alphaNumericFieldsNeeded[i];
|
353 |
if (alphaNumericFieldsNeeded[i]
|
354 |
.equals(getLyrDef().getFieldID())) |
355 |
found = true;
|
356 |
} |
357 |
} |
358 |
// Nos aseguramos de pedir siempre el campo ID
|
359 |
if (found == false) |
360 |
strAux = strAux + ", " + getLyrDef().getFieldID();
|
361 |
|
362 |
String sqlProv = "SELECT " + strAux + " FROM " |
363 |
+ getLyrDef().getTableName() + " "
|
364 |
+ getLyrDef().getWhereClause(); |
365 |
|
366 |
String sqlAux;
|
367 |
sqlAux = sqlProv + getCompoundWhere(r, strEPSG); |
368 |
|
369 |
System.out.println("SqlAux getFeatureIterator = " + sqlAux); |
370 |
|
371 |
return getFeatureIterator(sqlAux);
|
372 |
} |
373 |
|
374 |
} |