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