svn-gvsig-desktop / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / mysql / MySQLDriver.java @ 3095
History | View | Annotate | Download (11.8 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.cresques.cts.ICoordTrans; |
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.DefaultDBDriver; |
59 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
60 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
61 |
import com.iver.cit.gvsig.fmap.drivers.WKTParser; |
62 |
import com.iver.cit.gvsig.fmap.drivers.jdbc.WKBParser; |
63 |
import com.vividsolutions.jts.io.ParseException; |
64 |
|
65 |
/**
|
66 |
* @author FJP
|
67 |
*
|
68 |
* TODO To change the template for this generated type comment go to
|
69 |
* Window - Preferences - Java - Code Generation - Code and Comments
|
70 |
*/
|
71 |
public class MySQLDriver extends DefaultDBDriver { |
72 |
private WKBParser parser = new WKBParser(); |
73 |
private WKTParser wktParser = new WKTParser(); |
74 |
/* private int fetch_min=-1;
|
75 |
private int fetch_max=-1; */
|
76 |
private Statement st; |
77 |
private Rectangle2D fullExtent = null; |
78 |
private String strAux; |
79 |
private String strEPSG = "-1"; |
80 |
private String originalEPSG; |
81 |
/**
|
82 |
*
|
83 |
*/
|
84 |
public MySQLDriver() {
|
85 |
} |
86 |
/* (non-Javadoc)
|
87 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
|
88 |
*/
|
89 |
public DriverAttributes getDriverAttributes() {
|
90 |
return null; |
91 |
} |
92 |
|
93 |
/* (non-Javadoc)
|
94 |
* @see com.hardcode.driverManager.Driver#getName()
|
95 |
*/
|
96 |
public String getName() { |
97 |
return "mySQL JDBC Driver"; |
98 |
} |
99 |
|
100 |
/**
|
101 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
102 |
*/
|
103 |
public IGeometry getShape(int index) { |
104 |
IGeometry geom = null;
|
105 |
boolean resul;
|
106 |
try {
|
107 |
// EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS
|
108 |
// FILAS, ASI QUE MONTAMOS ESTA HISTORIA PARA QUE
|
109 |
// LO HAGA
|
110 |
// System.out.println("getShape " + index);
|
111 |
/* if (index < fetch_min)
|
112 |
{
|
113 |
rs.close();
|
114 |
|
115 |
rs = st.executeQuery(sqlOrig);
|
116 |
fetch_min = 0;
|
117 |
fetch_max = rs.getFetchSize();
|
118 |
}
|
119 |
while (index >= fetch_max)
|
120 |
{
|
121 |
rs.last();
|
122 |
// forzamos una carga
|
123 |
rs.next();
|
124 |
fetch_min = fetch_max;
|
125 |
fetch_max = fetch_max + rs.getFetchSize();
|
126 |
// System.out.println("fetchSize = " + rs.getFetchSize() + " " + fetch_min + "-" + fetch_max);
|
127 |
}
|
128 |
rs.absolute(index+1 - fetch_min); */
|
129 |
rs.absolute(index+1);
|
130 |
// strAux = rs.getString(1);
|
131 |
// geom = parser.read(strAux);
|
132 |
byte[] data = rs.getBytes(1); |
133 |
geom = parser.parse(data); |
134 |
|
135 |
|
136 |
} catch (SQLException e) { |
137 |
e.printStackTrace(); |
138 |
} |
139 |
|
140 |
return geom;
|
141 |
} |
142 |
/**
|
143 |
* @param conn
|
144 |
* @param tableName
|
145 |
* @param fields OJO: EL PRIMER CAMPO HA DE SER EL DE GEOMETRIA
|
146 |
* @param whereClause
|
147 |
*/
|
148 |
/* (non-Javadoc)
|
149 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#setData(java.sql.Connection, java.lang.String, java.lang.String, java.lang.String, int)
|
150 |
*/
|
151 |
public void setData(Connection conn, String tableName, String fields, String whereClause, int id_FID_field) |
152 |
{ |
153 |
this.conn = conn;
|
154 |
this.tableName = tableName;
|
155 |
this.fields = fields;
|
156 |
this.whereClause = whereClause;
|
157 |
this.sqlOrig = "SELECT " + fields + " FROM " + tableName + " " + whereClause; |
158 |
this.idFID_FieldName = id_FID_field;
|
159 |
try {
|
160 |
|
161 |
// NO ESTA LISTO ESTO AUN EN mySQL, o no s? usuarlo getTableEPSG();
|
162 |
|
163 |
st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); |
164 |
|
165 |
String sqlAux = getCompoundWhere(workingArea, strEPSG);
|
166 |
System.out.println("mySQL: setData con sql=" + sqlAux); |
167 |
rs = st.executeQuery(sqlAux); |
168 |
metaData = rs.getMetaData(); |
169 |
// Le pegamos un primera pasada para poder relacionar
|
170 |
// un campo de identificador ?nico (parecido al OID en
|
171 |
// postgresql) con el ?ndice dentro del recordset.
|
172 |
// Esto cuando haya ediciones, no es v?lido, y hay
|
173 |
// que refrescarlo.
|
174 |
doRelateID_FID(); |
175 |
|
176 |
} catch (SQLException e) { |
177 |
NotificationManager.addError("Error al conectar a la base de datos.",e);
|
178 |
e.printStackTrace(); |
179 |
} |
180 |
} |
181 |
|
182 |
/**
|
183 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
|
184 |
*/
|
185 |
public Rectangle2D getFullExtent(){ |
186 |
/*
|
187 |
* NO SE PUEDE HACER AS?. DA EL EXTENT PARA CADA GEOMETRIA,
|
188 |
* NO DA EL GLOBAL. HABR? QUE ESPERAR A QUE LO PONGAN.
|
189 |
* if (fullExtent == null)
|
190 |
{
|
191 |
try
|
192 |
{
|
193 |
Statement s = conn.createStatement();
|
194 |
|
195 |
ResultSet r = s.executeQuery("SELECT AsText(Envelope(ogc_geom)) AS FullExtent FROM " + tableName);
|
196 |
r.next();
|
197 |
String strAux = r.getString(1);
|
198 |
System.out.println("fullExtent = " + strAux);
|
199 |
// byte[] data = rs.getBytes(1);
|
200 |
// IGeometry geom = parser.parse(data);
|
201 |
IGeometry geom = wktParser.read(strAux);
|
202 |
|
203 |
fullExtent = geom.getBounds2D();
|
204 |
}
|
205 |
catch (SQLException e)
|
206 |
{
|
207 |
System.err.println(e.getMessage());
|
208 |
} catch (ParseException e) {
|
209 |
// TODO Auto-generated catch block
|
210 |
e.printStackTrace();
|
211 |
}
|
212 |
|
213 |
}
|
214 |
return fullExtent;
|
215 |
*/
|
216 |
|
217 |
if (fullExtent == null) |
218 |
{ |
219 |
try
|
220 |
{ |
221 |
IFeatureIterator itGeom = getFeatureIterator("SELECT ASBINARY(ogc_geom) AS the_geom FROM " + tableName);
|
222 |
IGeometry geom; |
223 |
int cont = 0; |
224 |
while (itGeom.hasNext())
|
225 |
{ |
226 |
geom = itGeom.next().getGeometry(); |
227 |
if (cont==0) |
228 |
fullExtent = geom.getBounds2D(); |
229 |
else
|
230 |
fullExtent.add(geom.getBounds2D()); |
231 |
cont++; |
232 |
} |
233 |
} |
234 |
catch (SQLException e) |
235 |
{ |
236 |
System.err.println(e.getMessage());
|
237 |
} catch (DriverException e) {
|
238 |
// TODO Auto-generated catch block
|
239 |
e.printStackTrace(); |
240 |
} |
241 |
|
242 |
} |
243 |
return fullExtent;
|
244 |
} |
245 |
/* (non-Javadoc)
|
246 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.lang.String)
|
247 |
*/
|
248 |
public IFeatureIterator getFeatureIterator(String sql) throws com.iver.cit.gvsig.fmap.DriverException { |
249 |
Statement st;
|
250 |
MySqlFeatureIterator geomIterator = null;
|
251 |
try {
|
252 |
st = conn.createStatement(); |
253 |
// st.setFetchSize(2000);
|
254 |
ResultSet rs = st.executeQuery(sql);
|
255 |
geomIterator = new MySqlFeatureIterator(rs);
|
256 |
} catch (SQLException e) { |
257 |
e.printStackTrace(); |
258 |
throw new com.iver.cit.gvsig.fmap.DriverException(e); |
259 |
} |
260 |
|
261 |
return geomIterator;
|
262 |
} |
263 |
/* (non-Javadoc)
|
264 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.awt.geom.Rectangle2D)
|
265 |
*/
|
266 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) throws DriverException { |
267 |
if (workingArea != null) |
268 |
r = r.createIntersection(workingArea); |
269 |
String sqlAux = getCompoundWhere(r, strEPSG);
|
270 |
|
271 |
return getFeatureIterator(sqlAux);
|
272 |
} |
273 |
|
274 |
/**
|
275 |
* Le pasas el rect?ngulo que quieres pedir. La primera
|
276 |
* vez es el workingArea, y las siguientes una interseccion
|
277 |
* de este rectangulo con el workingArea
|
278 |
* @param r
|
279 |
* @param strEPSG
|
280 |
* @return
|
281 |
*/
|
282 |
private String getCompoundWhere(Rectangle2D r, String strEPSG) { |
283 |
if (r==null) |
284 |
return this.sqlOrig; |
285 |
|
286 |
double xMin = r.getMinX();
|
287 |
double yMin = r.getMinY();
|
288 |
double xMax = r.getMaxX();
|
289 |
double yMax = r.getMaxY();
|
290 |
|
291 |
String wktBox = "GeomFromText('LINESTRING(" + xMin + " " + yMin + ", " |
292 |
+ xMax + " " + yMin + ", " |
293 |
+ xMax + " " + yMax + ", " |
294 |
+ xMin + " " + yMax + ")', " |
295 |
+ strEPSG + ")";
|
296 |
String sqlAux;
|
297 |
if (getWhereClause().startsWith("WHERE")) |
298 |
sqlAux = sqlOrig + " MBRIntersects(" + wktBox + ",ogc_geom);" ; |
299 |
else
|
300 |
sqlAux = sqlOrig + "WHERE MBRIntersects(" + wktBox + ",ogc_geom);" ; |
301 |
return sqlAux;
|
302 |
} |
303 |
|
304 |
|
305 |
public void open() { |
306 |
// TODO Auto-generated method stub
|
307 |
|
308 |
} |
309 |
/**
|
310 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getConnectionStringBeginning()
|
311 |
*/
|
312 |
public String getConnectionStringBeginning() { |
313 |
return "jdbc:mysql:"; |
314 |
} |
315 |
|
316 |
static{
|
317 |
try {
|
318 |
Class.forName("com.mysql.jdbc.Driver"); |
319 |
} catch (ClassNotFoundException e) { |
320 |
throw new RuntimeException(e); |
321 |
} |
322 |
} |
323 |
|
324 |
/**
|
325 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryField(java.lang.String)
|
326 |
*/
|
327 |
public String getGeometryField(String fieldName) { |
328 |
return "ASBINARY(" + fieldName +")"; |
329 |
} |
330 |
/**
|
331 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
|
332 |
*/
|
333 |
public int[] getPrimaryKeys() throws com.hardcode.gdbms.engine.data.driver.DriverException { |
334 |
return new int[]{idFID_FieldName - 2}; |
335 |
} |
336 |
/**
|
337 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#getDefaultPort()
|
338 |
*/
|
339 |
public int getDefaultPort() { |
340 |
return 3306; |
341 |
} |
342 |
/**
|
343 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
|
344 |
*/
|
345 |
public void write(DataWare arg0) throws com.hardcode.gdbms.engine.data.driver.DriverException { |
346 |
// TODO Auto-generated method stub
|
347 |
|
348 |
} |
349 |
|
350 |
private void getTableEPSG() |
351 |
{ |
352 |
try {
|
353 |
Statement stAux = conn.createStatement();
|
354 |
|
355 |
String sql = "SELECT SRID(ogc_geom) FROM " + |
356 |
getTableName() + " LIMIT 1;";
|
357 |
ResultSet rs = stAux.executeQuery(sql);
|
358 |
rs.next(); |
359 |
originalEPSG = "" + rs.getInt(1); |
360 |
rs.close(); |
361 |
} catch (SQLException e) { |
362 |
// TODO Auto-generated catch block
|
363 |
e.printStackTrace(); |
364 |
} |
365 |
|
366 |
} |
367 |
|
368 |
|
369 |
} |