root / trunk / extensions / extJDBC / src / com / iver / cit / gvsig / fmap / drivers / jdbc / mysql / MySQLDriver.java @ 11971
History | View | Annotate | Download (11.3 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.ResultSetMetaData; |
50 |
import java.sql.SQLException; |
51 |
import java.sql.Statement; |
52 |
|
53 |
import org.apache.log4j.Logger; |
54 |
|
55 |
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException; |
56 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
57 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
58 |
import com.iver.andami.messages.NotificationManager; |
59 |
import com.iver.cit.gvsig.fmap.SqlDriveExceptionType; |
60 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
61 |
import com.iver.cit.gvsig.fmap.drivers.ConnectionJDBC; |
62 |
import com.iver.cit.gvsig.fmap.drivers.DBException; |
63 |
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition; |
64 |
import com.iver.cit.gvsig.fmap.drivers.DefaultJDBCDriver; |
65 |
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes; |
66 |
import com.iver.cit.gvsig.fmap.drivers.IConnection; |
67 |
import com.iver.cit.gvsig.fmap.drivers.IFeatureIterator; |
68 |
import com.iver.cit.gvsig.fmap.drivers.WKBParser2; |
69 |
import com.iver.cit.gvsig.fmap.edition.IWriteable; |
70 |
import com.iver.cit.gvsig.fmap.edition.IWriter; |
71 |
|
72 |
/**
|
73 |
* @author FJP
|
74 |
*
|
75 |
* TODO To change the template for this generated type comment go to Window -
|
76 |
* Preferences - Java - Code Generation - Code and Comments
|
77 |
*/
|
78 |
public class MySQLDriver extends DefaultJDBCDriver |
79 |
implements IWriteable {
|
80 |
|
81 |
private static Logger logger = Logger |
82 |
.getLogger(MySQLDriver.class.getName()); |
83 |
|
84 |
private WKBParser2 parser = new WKBParser2(); |
85 |
|
86 |
private MySQLSpatialWriter writer = new MySQLSpatialWriter(); |
87 |
|
88 |
/*
|
89 |
* private int fetch_min=-1; private int fetch_max=-1;
|
90 |
*/
|
91 |
private Statement st; |
92 |
|
93 |
private String strAux; |
94 |
|
95 |
private String strEPSG = "-1"; |
96 |
|
97 |
private String originalEPSG; |
98 |
|
99 |
private String completeWhere; |
100 |
|
101 |
/**
|
102 |
* Don't have information about working area
|
103 |
*/
|
104 |
private String sqlOrig; |
105 |
|
106 |
/**
|
107 |
* Does have information about working area and order
|
108 |
*/
|
109 |
private String sqlTotal; |
110 |
|
111 |
/**
|
112 |
*
|
113 |
*/
|
114 |
public MySQLDriver() {
|
115 |
} |
116 |
|
117 |
/*
|
118 |
* (non-Javadoc)
|
119 |
*
|
120 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getDriverAttributes()
|
121 |
*/
|
122 |
public DriverAttributes getDriverAttributes() {
|
123 |
return null; |
124 |
} |
125 |
|
126 |
/*
|
127 |
* (non-Javadoc)
|
128 |
*
|
129 |
* @see com.hardcode.driverManager.Driver#getName()
|
130 |
*/
|
131 |
public String getName() { |
132 |
return "mySQL JDBC Driver"; |
133 |
} |
134 |
|
135 |
/**
|
136 |
* @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShape(int)
|
137 |
*/
|
138 |
public IGeometry getShape(int index) { |
139 |
IGeometry geom = null;
|
140 |
boolean resul;
|
141 |
try {
|
142 |
if (rs != null) { |
143 |
rs.absolute(index + 1);
|
144 |
byte[] data = rs.getBytes(1); |
145 |
geom = parser.parse(data); |
146 |
} |
147 |
|
148 |
} catch (SQLException e) { |
149 |
e.printStackTrace(); |
150 |
} |
151 |
|
152 |
return geom;
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* @param conn
|
157 |
*/
|
158 |
/*
|
159 |
* (non-Javadoc)
|
160 |
*
|
161 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialJDBCDriver#setData(java.sql.Connection,
|
162 |
* java.lang.String, java.lang.String, java.lang.String, int)
|
163 |
*/
|
164 |
public void setData(IConnection conn, DBLayerDefinition lyrDef) { |
165 |
this.conn = conn;
|
166 |
setLyrDef(lyrDef); |
167 |
try {
|
168 |
|
169 |
// NO ESTA LISTO ESTO AUN EN mySQL, o no s? usuarlo getTableEPSG();
|
170 |
|
171 |
sqlOrig = "SELECT " + getTotalFields() + " FROM " |
172 |
+ getLyrDef().getTableName() + " ";
|
173 |
// + getLyrDef().getWhereClause();
|
174 |
completeWhere = getCompoundWhere(workingArea, strEPSG); |
175 |
String sqlAux = sqlOrig + completeWhere + " ORDER BY " |
176 |
+ getLyrDef().getFieldID(); |
177 |
logger.info("Cadena SQL:" + sqlAux);
|
178 |
sqlTotal = sqlAux; |
179 |
|
180 |
st = ((ConnectionJDBC)conn).getConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY,
|
181 |
ResultSet.CONCUR_READ_ONLY);
|
182 |
|
183 |
rs = st.executeQuery(sqlTotal); |
184 |
metaData = rs.getMetaData(); |
185 |
// Le pegamos un primera pasada para poder relacionar
|
186 |
// un campo de identificador ?nico (parecido al OID en
|
187 |
// postgresql) con el ?ndice dentro del recordset.
|
188 |
// Esto cuando haya ediciones, no es v?lido, y hay
|
189 |
// que refrescarlo.
|
190 |
doRelateID_FID(); |
191 |
|
192 |
writer.setCreateTable(false);
|
193 |
writer.setWriteAll(false);
|
194 |
writer.initialize(lyrDef); |
195 |
|
196 |
|
197 |
} catch (SQLException e) { |
198 |
NotificationManager.addError( |
199 |
"Error al conectar a la base de datos.", e);
|
200 |
e.printStackTrace(); |
201 |
} catch (InitializeWriterException e) {
|
202 |
NotificationManager.addError( |
203 |
"Error al conectar a la base de datos.", e);
|
204 |
e.printStackTrace(); |
205 |
} |
206 |
} |
207 |
|
208 |
/*
|
209 |
* (non-Javadoc)
|
210 |
*
|
211 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.lang.String)
|
212 |
*/
|
213 |
public IFeatureIterator getFeatureIterator(String sql) |
214 |
throws ReadDriverException {
|
215 |
Statement st;
|
216 |
MySqlFeatureIterator geomIterator = null;
|
217 |
try {
|
218 |
logger.debug(sql); |
219 |
st = ((ConnectionJDBC)conn).getConnection().createStatement(); |
220 |
// st.setFetchSize(2000);
|
221 |
ResultSet rs = st.executeQuery(sql);
|
222 |
geomIterator = new MySqlFeatureIterator(rs);
|
223 |
geomIterator.setLyrDef(getLyrDef()); |
224 |
} catch (SQLException e) { |
225 |
// e.printStackTrace();
|
226 |
// SqlDriveExceptionType type = new SqlDriveExceptionType();
|
227 |
// type.setDriverName("MySQL Driver");
|
228 |
// type.setSql(sql);
|
229 |
// type.setLayerName(getTableName());
|
230 |
// type.setSchema(null);
|
231 |
throw new ReadDriverException("MySQL Driver",e); |
232 |
// throw new com.iver.cit.gvsig.fmap.DriverException(e);
|
233 |
} |
234 |
|
235 |
return geomIterator;
|
236 |
} |
237 |
|
238 |
/*
|
239 |
* (non-Javadoc)
|
240 |
*
|
241 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getGeometryIterator(java.awt.geom.Rectangle2D)
|
242 |
*/
|
243 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) |
244 |
throws ReadDriverException {
|
245 |
if (workingArea != null) |
246 |
r = r.createIntersection(workingArea); |
247 |
String sqlAux = sqlOrig + getCompoundWhere(r, strEPSG);
|
248 |
|
249 |
return getFeatureIterator(sqlAux);
|
250 |
} |
251 |
|
252 |
/**
|
253 |
* Le pasas el rect?ngulo que quieres pedir. La primera vez es el
|
254 |
* workingArea, y las siguientes una interseccion de este rectangulo con el
|
255 |
* workingArea
|
256 |
*
|
257 |
* @param r
|
258 |
* @param strEPSG
|
259 |
* @return
|
260 |
*/
|
261 |
private String getCompoundWhere(Rectangle2D r, String strEPSG) { |
262 |
if (r == null) |
263 |
return getWhereClause();
|
264 |
|
265 |
double xMin = r.getMinX();
|
266 |
double yMin = r.getMinY();
|
267 |
double xMax = r.getMaxX();
|
268 |
double yMax = r.getMaxY();
|
269 |
|
270 |
String wktBox = "GeomFromText('LINESTRING(" + xMin + " " + yMin + ", " |
271 |
+ xMax + " " + yMin + ", " + xMax + " " + yMax + ", " + xMin |
272 |
+ " " + yMax + ")', '" + strEPSG + "')"; |
273 |
String sqlAux;
|
274 |
if (getWhereClause().startsWith("WHERE")) |
275 |
sqlAux = getWhereClause() + " AND " +"MBRIntersects(" + wktBox + ", " + getLyrDef().getFieldGeometry() + ")"; |
276 |
else
|
277 |
sqlAux = "WHERE MBRIntersects(" + wktBox + ", " + getLyrDef().getFieldGeometry() + ")"; |
278 |
return sqlAux;
|
279 |
} |
280 |
|
281 |
public void open() { |
282 |
// TODO Auto-generated method stub
|
283 |
|
284 |
} |
285 |
|
286 |
/**
|
287 |
* @see com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver#getConnectionStringBeginning()
|
288 |
*/
|
289 |
public String getConnectionStringBeginning() { |
290 |
return "jdbc:mysql:"; |
291 |
} |
292 |
|
293 |
static {
|
294 |
try {
|
295 |
Class.forName("com.mysql.jdbc.Driver"); |
296 |
} catch (ClassNotFoundException e) { |
297 |
throw new RuntimeException(e); |
298 |
} |
299 |
} |
300 |
|
301 |
/**
|
302 |
* @see com.iver.cit.gvsig.fmap.drivers.IVectorialDatabaseDriver#getGeometryField(java.lang.String)
|
303 |
*/
|
304 |
public String getGeometryField(String fieldName) { |
305 |
return "ASBINARY(" + fieldName + ")"; |
306 |
} |
307 |
|
308 |
/**
|
309 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
|
310 |
*/
|
311 |
public int[] getPrimaryKeys() |
312 |
throws ReadDriverException {
|
313 |
return new int[] { getLyrDef().getIdFieldID() - 2 }; |
314 |
} |
315 |
|
316 |
/**
|
317 |
* @see com.iver.cit.gvsig.fmap.drivers.IVectorialJDBCDriver#getDefaultPort()
|
318 |
*/
|
319 |
public int getDefaultPort() { |
320 |
return 3306; |
321 |
} |
322 |
|
323 |
/**
|
324 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
|
325 |
*/
|
326 |
public void write(DataWare arg0){ |
327 |
// TODO Auto-generated method stub
|
328 |
|
329 |
} |
330 |
|
331 |
private void getTableEPSG() { |
332 |
try {
|
333 |
Statement stAux = ((ConnectionJDBC)conn).getConnection().createStatement();
|
334 |
|
335 |
String sql = "SELECT SRID(" + getLyrDef().getFieldGeometry() + ") FROM " + getTableName() |
336 |
+ " LIMIT 1;";
|
337 |
ResultSet rs = stAux.executeQuery(sql);
|
338 |
rs.next(); |
339 |
originalEPSG = "" + rs.getInt(1); |
340 |
rs.close(); |
341 |
} catch (SQLException e) { |
342 |
// TODO Auto-generated catch block
|
343 |
e.printStackTrace(); |
344 |
} |
345 |
|
346 |
} |
347 |
|
348 |
public String getSqlTotal() { |
349 |
return sqlTotal;
|
350 |
} |
351 |
|
352 |
/**
|
353 |
* @return Returns the completeWhere.
|
354 |
*/
|
355 |
public String getCompleteWhere() { |
356 |
return completeWhere;
|
357 |
} |
358 |
|
359 |
/*
|
360 |
* (non-Javadoc)
|
361 |
*
|
362 |
* @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getFeatureIterator(java.awt.geom.Rectangle2D,
|
363 |
* java.lang.String, java.lang.String[])
|
364 |
*/
|
365 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG, |
366 |
String[] alphaNumericFieldsNeeded) throws ReadDriverException { |
367 |
if (workingArea != null) |
368 |
r = r.createIntersection(workingArea); |
369 |
|
370 |
String strAux = getGeometryField(getLyrDef().getFieldGeometry());
|
371 |
boolean found = false; |
372 |
if (alphaNumericFieldsNeeded != null) { |
373 |
for (int i = 0; i < alphaNumericFieldsNeeded.length; i++) { |
374 |
strAux = strAux + ", " + alphaNumericFieldsNeeded[i];
|
375 |
if (alphaNumericFieldsNeeded[i]
|
376 |
.equals(getLyrDef().getFieldID())) |
377 |
found = true;
|
378 |
} |
379 |
} |
380 |
// Nos aseguramos de pedir siempre el campo ID
|
381 |
if (found == false) |
382 |
strAux = strAux + ", " + getLyrDef().getFieldID();
|
383 |
|
384 |
String sqlProv = "SELECT " + strAux + " FROM " |
385 |
+ getLyrDef().getTableName() + " ";
|
386 |
// + getLyrDef().getWhereClause();
|
387 |
|
388 |
String sqlAux;
|
389 |
sqlAux = sqlProv + getCompoundWhere(r, strEPSG); |
390 |
|
391 |
System.out.println("SqlAux getFeatureIterator = " + sqlAux); |
392 |
|
393 |
return getFeatureIterator(sqlAux);
|
394 |
} |
395 |
|
396 |
public boolean isWritable() { |
397 |
return true; |
398 |
} |
399 |
|
400 |
public IWriter getWriter() {
|
401 |
return writer;
|
402 |
} |
403 |
|
404 |
public String[] getTableFields(IConnection conex, String table) throws DBException { |
405 |
try{
|
406 |
Statement st = ((ConnectionJDBC)conex).getConnection().createStatement();
|
407 |
// ResultSet rs = dbmd.getTables(catalog, null, dbLayerDefinition.getTable(), null);
|
408 |
ResultSet rs = st.executeQuery("select * from " + table + " LIMIT 1"); |
409 |
ResultSetMetaData rsmd = rs.getMetaData();
|
410 |
|
411 |
String[] ret = new String[rsmd.getColumnCount()]; |
412 |
|
413 |
for (int i = 0; i < ret.length; i++) { |
414 |
ret[i] = rsmd.getColumnName(i+1);
|
415 |
} |
416 |
|
417 |
return ret;
|
418 |
}catch (SQLException e) { |
419 |
throw new DBException(e); |
420 |
} |
421 |
} |
422 |
|
423 |
|
424 |
} |