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