svn-gvsig-desktop / tags / v1_1_Build_1002 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / DefaultJDBCDriver.java @ 12070
History | View | Annotate | Download (31.9 KB)
1 | 11926 | caballero | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | *
|
||
19 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | * Av. Blasco Ib??ez, 50
|
||
24 | * 46010 VALENCIA
|
||
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | package com.iver.cit.gvsig.fmap.drivers; |
||
42 | |||
43 | import java.awt.geom.Rectangle2D; |
||
44 | import java.io.IOException; |
||
45 | //import java.sql.Connection;
|
||
46 | import java.sql.DatabaseMetaData; |
||
47 | import java.sql.Driver; |
||
48 | import java.sql.DriverManager; |
||
49 | import java.sql.ResultSet; |
||
50 | import java.sql.ResultSetMetaData; |
||
51 | import java.sql.SQLException; |
||
52 | import java.sql.Statement; |
||
53 | import java.sql.Types; |
||
54 | import java.util.ArrayList; |
||
55 | import java.util.Hashtable; |
||
56 | import java.util.TreeMap; |
||
57 | |||
58 | import org.apache.log4j.Logger; |
||
59 | |||
60 | import com.hardcode.driverManager.DriverEvent; |
||
61 | import com.hardcode.driverManager.DriverEventListener; |
||
62 | import com.hardcode.driverManager.IDelayedDriver; |
||
63 | import com.hardcode.gdbms.engine.data.DataSourceFactory; |
||
64 | import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
||
65 | import com.hardcode.gdbms.engine.values.Value; |
||
66 | import com.hardcode.gdbms.engine.values.ValueFactory; |
||
67 | import com.iver.cit.gvsig.fmap.DriverException; |
||
68 | import com.iver.cit.gvsig.fmap.DriverJdbcNotFoundExceptionType; |
||
69 | import com.iver.cit.gvsig.fmap.MapContext; |
||
70 | import com.iver.cit.gvsig.fmap.Messages; |
||
71 | import com.iver.cit.gvsig.fmap.SqlDriveExceptionType; |
||
72 | import com.iver.cit.gvsig.fmap.core.IFeature; |
||
73 | import com.iver.cit.gvsig.fmap.core.IGeometry; |
||
74 | 11928 | caballero | import com.iver.cit.gvsig.fmap.drivers.db.utils.ConnectionWithParams; |
75 | import com.iver.cit.gvsig.fmap.drivers.db.utils.SingleVectorialDBConnectionManager; |
||
76 | 11926 | caballero | import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
77 | import com.iver.cit.gvsig.fmap.layers.XMLException; |
||
78 | import com.iver.utiles.XMLEntity; |
||
79 | import com.iver.utiles.swing.JPasswordDlg; |
||
80 | |||
81 | |||
82 | |||
83 | /**
|
||
84 | * Clase abstracta qu
|
||
85 | */
|
||
86 | public abstract class DefaultJDBCDriver implements IVectorialJDBCDriver, ObjectDriver { |
||
87 | private static Logger logger = Logger.getLogger(SelectableDataSource.class.getName()); |
||
88 | protected static Hashtable poolPassw = new Hashtable(); |
||
89 | |||
90 | protected IConnection conn;
|
||
91 | // protected String tableName;
|
||
92 | // protected String whereClause;
|
||
93 | // protected String fields;
|
||
94 | // protected String sqlOrig;
|
||
95 | protected DBLayerDefinition lyrDef = null; |
||
96 | protected ResultSet rs; |
||
97 | protected boolean bCursorActivo = false; |
||
98 | protected Statement st; |
||
99 | protected int numReg=-1; |
||
100 | |||
101 | protected Rectangle2D fullExtent = null; |
||
102 | |||
103 | // protected String strFID_FieldName;
|
||
104 | // protected String idFID_FieldName;
|
||
105 | |||
106 | protected Hashtable hashRelate; |
||
107 | |||
108 | |||
109 | protected ResultSetMetaData metaData = null; |
||
110 | protected Rectangle2D workingArea; |
||
111 | protected String driverClass; |
||
112 | protected String userName; |
||
113 | protected String dbUrl; |
||
114 | protected String className; |
||
115 | protected String catalogName; |
||
116 | protected String schema; |
||
117 | protected String tableName; |
||
118 | protected String[] fields; |
||
119 | protected String FIDfield; |
||
120 | protected String geometryField; |
||
121 | protected String whereClause; |
||
122 | protected String strSRID; |
||
123 | //private double flatness;
|
||
124 | |||
125 | protected String host, port, dbName, connName; |
||
126 | |||
127 | protected ArrayList driverEventListeners = new ArrayList(); |
||
128 | |||
129 | |||
130 | abstract public void setData(IConnection conn, DBLayerDefinition lyrDef); |
||
131 | |||
132 | /**
|
||
133 | * @return devuelve la Conexi?n a la base de datos, para que
|
||
134 | * el usuario pueda hacer la consulta que quiera, si lo desea.
|
||
135 | * Por ejemplo, esto puede ser ?til para abrir un cuadro de dialogo
|
||
136 | * avanazado y lanzar peticiones del tipo "Devuelveme un buffer
|
||
137 | * a las autopistas", y con el resultset que te venga, escribir
|
||
138 | * un shape, o cosas as?.
|
||
139 | */
|
||
140 | public IConnection getConnection()
|
||
141 | { |
||
142 | return conn;
|
||
143 | } |
||
144 | public String[] getFields() |
||
145 | { |
||
146 | /* StringTokenizer tokenizer = new StringTokenizer(fields, ",");
|
||
147 | String[] arrayFields = new String[tokenizer.countTokens()];
|
||
148 | int i=0;
|
||
149 | while (tokenizer.hasMoreTokens())
|
||
150 | {
|
||
151 | arrayFields[i] = tokenizer.nextToken();
|
||
152 | i++;
|
||
153 | }
|
||
154 | return arrayFields; */
|
||
155 | return lyrDef.getFieldNames();
|
||
156 | |||
157 | } |
||
158 | /**
|
||
159 | * First, the geometry field. After, the rest of fields
|
||
160 | * @return
|
||
161 | */
|
||
162 | public String getTotalFields() |
||
163 | { |
||
164 | String strAux = getGeometryField(getLyrDef().getFieldGeometry());
|
||
165 | String[] fieldNames = getLyrDef().getFieldNames(); |
||
166 | for (int i=0; i< fieldNames.length; i++) |
||
167 | { |
||
168 | strAux = strAux + ", " + fieldNames[i];
|
||
169 | } |
||
170 | return strAux;
|
||
171 | } |
||
172 | |||
173 | /* (non-Javadoc)
|
||
174 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getWhereClause()
|
||
175 | */
|
||
176 | public String getWhereClause() |
||
177 | { |
||
178 | return lyrDef.getWhereClause().toUpperCase();
|
||
179 | } |
||
180 | public String getTableName() |
||
181 | { |
||
182 | return lyrDef.getTableName();
|
||
183 | } |
||
184 | |||
185 | |||
186 | /**
|
||
187 | * @throws DriverIOException
|
||
188 | * @throws DriverException
|
||
189 | * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeCount()
|
||
190 | */
|
||
191 | public int getShapeCount() throws IOException { |
||
192 | if (numReg == -1) |
||
193 | { |
||
194 | try
|
||
195 | { |
||
196 | Statement s = ((ConnectionJDBC)conn).getConnection().createStatement();
|
||
197 | ResultSet r = s.executeQuery("SELECT COUNT(*) AS NUMREG FROM " + lyrDef.getTableName() + " " + getCompleteWhere()); |
||
198 | r.next(); |
||
199 | numReg = r.getInt(1);
|
||
200 | System.err.println("numReg = " + numReg); |
||
201 | } |
||
202 | catch (SQLException e) |
||
203 | { |
||
204 | throw new IOException(e.getMessage()); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | return numReg;
|
||
209 | } |
||
210 | |||
211 | /**
|
||
212 | * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getFullExtent()
|
||
213 | */
|
||
214 | public Rectangle2D getFullExtent(){ |
||
215 | // Por defecto recorremos todas las geometrias.
|
||
216 | // Las bases de datos como PostGIS pueden y deben
|
||
217 | // sobreescribir este m?todo.
|
||
218 | if (fullExtent == null) |
||
219 | { |
||
220 | try
|
||
221 | { |
||
222 | IFeatureIterator itGeom = getFeatureIterator("SELECT " +
|
||
223 | getGeometryField(getLyrDef().getFieldGeometry()) + ", " + getLyrDef().getFieldID() + " FROM " + |
||
224 | getLyrDef().getComposedTableName() + " " + getCompleteWhere());
|
||
225 | IGeometry geom; |
||
226 | int cont = 0; |
||
227 | while (itGeom.hasNext())
|
||
228 | { |
||
229 | geom = itGeom.next().getGeometry(); |
||
230 | if (cont==0) |
||
231 | fullExtent = geom.getBounds2D(); |
||
232 | else
|
||
233 | fullExtent.add(geom.getBounds2D()); |
||
234 | cont++; |
||
235 | } |
||
236 | } |
||
237 | catch (DriverException e) {
|
||
238 | // TODO Auto-generated catch block
|
||
239 | e.printStackTrace(); |
||
240 | } |
||
241 | |||
242 | } |
||
243 | return fullExtent;
|
||
244 | } |
||
245 | |||
246 | |||
247 | /**
|
||
248 | * @see com.iver.cit.gvsig.fmap.layers.ReadableVectorial#getShapeType()
|
||
249 | */
|
||
250 | public int getShapeType() { |
||
251 | /* IGeometry geom;
|
||
252 | if (shapeType == -1)
|
||
253 | {
|
||
254 | shapeType = FShape.MULTI;
|
||
255 | try {
|
||
256 | geom = getShape(0);
|
||
257 | if (geom != null)
|
||
258 | shapeType = geom.getGeometryType();
|
||
259 | } catch (IOException e) {
|
||
260 | // e.printStackTrace();
|
||
261 | }
|
||
262 | }
|
||
263 | return shapeType; */
|
||
264 | return lyrDef.getShapeType();
|
||
265 | } |
||
266 | |||
267 | public int getFieldType(int idField) throws com.hardcode.gdbms.engine.data.driver.DriverException |
||
268 | { |
||
269 | String str = ""; |
||
270 | try {
|
||
271 | int i = idField + 2; // idField viene basado en 1, y |
||
272 | // adem?s nos saltamos el campo de geometry
|
||
273 | str = metaData.getColumnClassName(i); |
||
274 | if (metaData.getColumnType(i) == Types.VARCHAR) |
||
275 | return Types.VARCHAR; |
||
276 | if (metaData.getColumnType(i) == Types.FLOAT) |
||
277 | return Types.FLOAT; |
||
278 | if (metaData.getColumnType(i) == Types.DOUBLE) |
||
279 | return Types.DOUBLE; |
||
280 | if (metaData.getColumnType(i) == Types.INTEGER) |
||
281 | return Types.INTEGER; |
||
282 | if (metaData.getColumnType(i) == Types.SMALLINT) |
||
283 | return Types.SMALLINT; |
||
284 | if (metaData.getColumnType(i) == Types.TINYINT) |
||
285 | return Types.TINYINT; |
||
286 | if (metaData.getColumnType(i) == Types.BIGINT) |
||
287 | return Types.BIGINT; |
||
288 | if (metaData.getColumnType(i) == Types.BIT) |
||
289 | return Types.BIT; |
||
290 | if (metaData.getColumnType(i) == Types.DATE) |
||
291 | return Types.DATE; |
||
292 | if (metaData.getColumnType(i) == Types.DECIMAL) |
||
293 | return Types.DOUBLE; |
||
294 | if (metaData.getColumnType(i) == Types.NUMERIC) |
||
295 | return Types.DOUBLE; |
||
296 | if (metaData.getColumnType(i) == Types.DATE) |
||
297 | return Types.DATE; |
||
298 | if (metaData.getColumnType(i) == Types.TIME) |
||
299 | return Types.TIME; |
||
300 | if (metaData.getColumnType(i) == Types.TIMESTAMP) |
||
301 | return Types.TIMESTAMP; |
||
302 | |||
303 | } catch (SQLException e) { |
||
304 | throw new com.hardcode.gdbms.engine.data.driver.DriverException(e); |
||
305 | } |
||
306 | return Types.OTHER; |
||
307 | // throw new com.hardcode.gdbms.engine.data.driver.DriverException("Tipo no soportado: " + str);
|
||
308 | } |
||
309 | /**
|
||
310 | * Obtiene el valor que se encuentra en la fila y columna indicada
|
||
311 | * Esta es la implementaci?n por defecto. Si lo del absolute
|
||
312 | * no va bien, como es el caso del PostGis, el driver lo
|
||
313 | * tiene que reimplementar
|
||
314 | *
|
||
315 | * @param rowIndex fila
|
||
316 | * @param fieldId columna
|
||
317 | *
|
||
318 | * @return subclase de Value con el valor del origen de datos
|
||
319 | *
|
||
320 | * @throws DriverException Si se produce un error accediendo al DataSource
|
||
321 | */
|
||
322 | public Value getFieldValue(long rowIndex, int idField) |
||
323 | throws com.hardcode.gdbms.engine.data.driver.DriverException
|
||
324 | { |
||
325 | int i = (int) (rowIndex + 1); |
||
326 | int fieldId = idField+2; |
||
327 | try {
|
||
328 | rs.absolute(i); |
||
329 | if (metaData.getColumnType(fieldId) == Types.VARCHAR) |
||
330 | { |
||
331 | String strAux = rs.getString(fieldId);
|
||
332 | if (strAux == null) strAux = ""; |
||
333 | return ValueFactory.createValue(strAux);
|
||
334 | } |
||
335 | if (metaData.getColumnType(fieldId) == Types.FLOAT) |
||
336 | return ValueFactory.createValue(rs.getFloat(fieldId));
|
||
337 | if (metaData.getColumnType(fieldId) == Types.DOUBLE) |
||
338 | return ValueFactory.createValue(rs.getDouble(fieldId));
|
||
339 | if (metaData.getColumnType(fieldId) == Types.INTEGER) |
||
340 | return ValueFactory.createValue(rs.getInt(fieldId));
|
||
341 | if (metaData.getColumnType(fieldId) == Types.BIGINT) |
||
342 | return ValueFactory.createValue(rs.getLong(fieldId));
|
||
343 | if (metaData.getColumnType(fieldId) == Types.BIT) |
||
344 | return ValueFactory.createValue(rs.getBoolean(fieldId));
|
||
345 | if (metaData.getColumnType(fieldId) == Types.DATE) |
||
346 | return ValueFactory.createValue(rs.getDate(fieldId));
|
||
347 | } catch (SQLException e) { |
||
348 | throw new com.hardcode.gdbms.engine.data.driver.DriverException("Tipo no soportado: columna " + fieldId ); |
||
349 | } |
||
350 | return null; |
||
351 | |||
352 | |||
353 | } |
||
354 | |||
355 | /**
|
||
356 | * Obtiene el n?mero de campos del DataSource
|
||
357 | *
|
||
358 | * @return
|
||
359 | *
|
||
360 | * @throws DriverException Si se produce alg?n error accediendo al
|
||
361 | * DataSource
|
||
362 | */
|
||
363 | public int getFieldCount() throws com.hardcode.gdbms.engine.data.driver.DriverException |
||
364 | { |
||
365 | try {
|
||
366 | // Suponemos que el primer campo es el de las geometries, y no lo
|
||
367 | // contamos
|
||
368 | return rs.getMetaData().getColumnCount()-1; |
||
369 | } catch (SQLException e) { |
||
370 | throw new com.hardcode.gdbms.engine.data.driver.DriverException(e); |
||
371 | } |
||
372 | |||
373 | } |
||
374 | |||
375 | /**
|
||
376 | * Devuelve el nombre del campo fieldId-?simo
|
||
377 | *
|
||
378 | * @param fieldId ?ndice del campo cuyo nombre se quiere obtener
|
||
379 | *
|
||
380 | * @return
|
||
381 | * @throws com.hardcode.gdbms.engine.data.driver.DriverException
|
||
382 | *
|
||
383 | * @throws DriverException Si se produce alg?n error accediendo al
|
||
384 | * DataSource
|
||
385 | */
|
||
386 | public String getFieldName(int fieldId) throws com.hardcode.gdbms.engine.data.driver.DriverException |
||
387 | { |
||
388 | try {
|
||
389 | return rs.getMetaData().getColumnName(fieldId+2); |
||
390 | } catch (SQLException e) { |
||
391 | throw new com.hardcode.gdbms.engine.data.driver.DriverException(e); |
||
392 | } |
||
393 | } |
||
394 | |||
395 | /**
|
||
396 | * Obtiene el n?mero de registros del DataSource
|
||
397 | *
|
||
398 | * @return
|
||
399 | *
|
||
400 | * @throws DriverException Si se produce alg?n error accediendo al
|
||
401 | * DataSource
|
||
402 | */
|
||
403 | public long getRowCount() |
||
404 | { |
||
405 | try {
|
||
406 | return getShapeCount();
|
||
407 | } catch (IOException e) { |
||
408 | // TODO Auto-generated catch block
|
||
409 | e.printStackTrace(); |
||
410 | } |
||
411 | return -1; |
||
412 | } |
||
413 | |||
414 | public void close() { |
||
415 | } |
||
416 | |||
417 | /**
|
||
418 | * Recorre el recordset creando una tabla Hash que usaremos para
|
||
419 | * relacionar el n?mero de un registro con su identificador ?nico.
|
||
420 | * Debe ser llamado en el setData justo despu?s de crear el recorset
|
||
421 | * principal
|
||
422 | * @throws SQLException
|
||
423 | */
|
||
424 | protected void doRelateID_FID() throws SQLException |
||
425 | { |
||
426 | hashRelate = new Hashtable(); |
||
427 | |||
428 | |||
429 | String strSQL = "SELECT " + getLyrDef().getFieldID() + " FROM " + getLyrDef().getComposedTableName() |
||
430 | + " " + getCompleteWhere() + " ORDER BY " + getLyrDef().getFieldID(); |
||
431 | Statement s = ((ConnectionJDBC)getConnection()).getConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); |
||
432 | ResultSet r = s.executeQuery(strSQL);
|
||
433 | int id=0; |
||
434 | int gid;
|
||
435 | int index = 0; |
||
436 | while (r.next())
|
||
437 | { |
||
438 | String aux = r.getString(1); |
||
439 | Value val = ValueFactory.createValue(aux); |
||
440 | hashRelate.put(val, new Integer(index)); |
||
441 | System.out.println("ASOCIANDO CLAVE " + aux + " CON VALOR " + index); |
||
442 | index++; |
||
443 | } |
||
444 | numReg = index; |
||
445 | r.close(); |
||
446 | // rs.beforeFirst();
|
||
447 | |||
448 | } |
||
449 | |||
450 | /* (non-Javadoc)
|
||
451 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getRowIndexByFID(java.lang.Object)
|
||
452 | */
|
||
453 | public int getRowIndexByFID(IFeature FID) |
||
454 | { |
||
455 | int resul;
|
||
456 | // Object obj = FID.getAttribute(lyrDef.getIdFieldID());
|
||
457 | String theId = FID.getID();
|
||
458 | Value aux = ValueFactory.createValue(theId); |
||
459 | // System.err.println("Mirando si existe " + aux.toString());
|
||
460 | if (hashRelate.containsKey(aux))
|
||
461 | { |
||
462 | Integer rowIndex = (Integer) hashRelate.get(aux); |
||
463 | resul = rowIndex.intValue(); |
||
464 | // System.err.println("Row asociada a " + aux.toString() + ":" + resul);
|
||
465 | return resul;
|
||
466 | } |
||
467 | else
|
||
468 | return -1; |
||
469 | } |
||
470 | |||
471 | /* (non-Javadoc)
|
||
472 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#setXMLEntity(com.iver.utiles.XMLEntity)
|
||
473 | */
|
||
474 | public void setXMLEntity(XMLEntity xml) throws XMLException |
||
475 | { |
||
476 | |||
477 | className = xml.getStringProperty("className");
|
||
478 | |||
479 | catalogName = xml.getStringProperty("catalog");
|
||
480 | userName =xml.getStringProperty("username");
|
||
481 | driverClass =xml.getStringProperty("driverclass");
|
||
482 | tableName = xml.getStringProperty("tablename");
|
||
483 | if (xml.contains("schema")){ |
||
484 | schema = xml.getStringProperty("schema");
|
||
485 | } |
||
486 | fields = xml.getStringArrayProperty("fields");
|
||
487 | FIDfield = xml.getStringProperty("FID");
|
||
488 | geometryField = xml.getStringProperty("THE_GEOM");
|
||
489 | whereClause = xml.getStringProperty("whereclause");
|
||
490 | strSRID = xml.getStringProperty("SRID");
|
||
491 | |||
492 | if (xml.contains("host")) |
||
493 | { |
||
494 | host = xml.getStringProperty("host");
|
||
495 | port = xml.getStringProperty("port");
|
||
496 | dbName = xml.getStringProperty("dbName");
|
||
497 | connName = xml.getStringProperty("connName");
|
||
498 | } |
||
499 | else
|
||
500 | { |
||
501 | // Por compatibilidad con versiones anteriores
|
||
502 | dbUrl = xml.getStringProperty("dbURL");
|
||
503 | extractParamsFromDbUrl(dbUrl); |
||
504 | |||
505 | } |
||
506 | if (xml.contains("minXworkArea")) |
||
507 | { |
||
508 | double x = xml.getDoubleProperty("minXworkArea"); |
||
509 | double y = xml.getDoubleProperty("minYworkArea"); |
||
510 | double H = xml.getDoubleProperty("HworkArea"); |
||
511 | double W = xml.getDoubleProperty("WworkArea"); |
||
512 | workingArea = new Rectangle2D.Double(x,y,W,H); |
||
513 | } |
||
514 | |||
515 | DBLayerDefinition lyrDef = new DBLayerDefinition();
|
||
516 | lyrDef.setCatalogName(catalogName); |
||
517 | lyrDef.setSchema(schema); |
||
518 | lyrDef.setTableName(tableName); |
||
519 | lyrDef.setFieldNames(fields); |
||
520 | lyrDef.setFieldID(FIDfield); |
||
521 | lyrDef.setFieldGeometry(geometryField); |
||
522 | lyrDef.setWhereClause(whereClause); |
||
523 | // lyrDef.setClassToInstantiate(driverClass);
|
||
524 | if (workingArea != null) |
||
525 | lyrDef.setWorkingArea(workingArea); |
||
526 | |||
527 | lyrDef.setSRID_EPSG(strSRID); |
||
528 | |||
529 | setLyrDef(lyrDef); |
||
530 | |||
531 | } |
||
532 | |||
533 | private void extractParamsFromDbUrl(String dbUrl2) { |
||
534 | //jdbc:postgres://localhost:5431/latin1
|
||
535 | int iDbName = dbUrl2.lastIndexOf('/'); |
||
536 | dbName = dbUrl2.substring(iDbName+1);
|
||
537 | int iLast2points = dbUrl2.lastIndexOf(':'); |
||
538 | port = dbUrl2.substring(iLast2points+1, iDbName);
|
||
539 | int iHost = dbUrl2.indexOf("//"); |
||
540 | host = dbUrl2.substring(iHost + 2, iLast2points);
|
||
541 | connName = dbUrl2; |
||
542 | } |
||
543 | |||
544 | public void load() throws DriverException { |
||
545 | try {
|
||
546 | if (driverClass != null) |
||
547 | Class.forName(driverClass);
|
||
548 | |||
549 | String _drvName = getName();
|
||
550 | |||
551 | String keyPool = _drvName.toLowerCase() + "_" + host.toLowerCase() |
||
552 | + "_" + port + "_" + dbName.toLowerCase() |
||
553 | + "_" + userName.toLowerCase();
|
||
554 | |||
555 | IConnection newConn = null;
|
||
556 | String clave = null; |
||
557 | ConnectionWithParams cwp = null;
|
||
558 | |||
559 | if (poolPassw.containsKey(keyPool)) {
|
||
560 | |||
561 | clave = (String) poolPassw.get(keyPool);
|
||
562 | cwp = SingleVectorialDBConnectionManager.instance().getConnection( |
||
563 | _drvName, userName, clave, connName, |
||
564 | host, port, dbName, true);
|
||
565 | |||
566 | } else {
|
||
567 | |||
568 | cwp = SingleVectorialDBConnectionManager.instance().getConnection( |
||
569 | _drvName, userName, null, connName,
|
||
570 | host, port, dbName, false);
|
||
571 | |||
572 | if (cwp.isConnected()) {
|
||
573 | |||
574 | poolPassw.put(keyPool, cwp.getPw()); |
||
575 | |||
576 | } else {
|
||
577 | |||
578 | JPasswordDlg dlg = new JPasswordDlg();
|
||
579 | String strMessage = Messages.getString("conectar_jdbc"); |
||
580 | String strPassword = Messages.getString("password"); |
||
581 | dlg.setMessage(strMessage |
||
582 | + " ["
|
||
583 | + _drvName + ", "
|
||
584 | + host + ", "
|
||
585 | + port + ", "
|
||
586 | + dbName + ", "
|
||
587 | + userName + "]. "
|
||
588 | + strPassword |
||
589 | + "?");
|
||
590 | dlg.show(); |
||
591 | clave = dlg.getPassword(); |
||
592 | if (clave == null) |
||
593 | return;
|
||
594 | poolPassw.put(keyPool, clave); |
||
595 | |||
596 | cwp.connect(clave); |
||
597 | } |
||
598 | } |
||
599 | |||
600 | newConn = cwp.getConnection(); |
||
601 | // newConn.setAutoCommit(false);
|
||
602 | |||
603 | DBLayerDefinition lyrDef = new DBLayerDefinition();
|
||
604 | if (getLyrDef() == null) { |
||
605 | lyrDef.setCatalogName(catalogName); |
||
606 | lyrDef.setSchema(schema); |
||
607 | lyrDef.setTableName(tableName); |
||
608 | lyrDef.setFieldNames(fields); |
||
609 | lyrDef.setFieldID(FIDfield); |
||
610 | lyrDef.setFieldGeometry(geometryField); |
||
611 | lyrDef.setWhereClause(whereClause); |
||
612 | // lyrDef.setClassToInstantiate(driverClass);
|
||
613 | if (workingArea != null) |
||
614 | lyrDef.setWorkingArea(workingArea); |
||
615 | |||
616 | lyrDef.setSRID_EPSG(strSRID); |
||
617 | } else {
|
||
618 | lyrDef = getLyrDef(); |
||
619 | } |
||
620 | |||
621 | setData(newConn, lyrDef); |
||
622 | } catch (ClassNotFoundException e) { |
||
623 | logger.debug(e); |
||
624 | DriverJdbcNotFoundExceptionType type = |
||
625 | new DriverJdbcNotFoundExceptionType();
|
||
626 | type.setDriverJdbcClassName(driverClass); |
||
627 | type.setLayerName(this.getTableName());
|
||
628 | throw new DriverException("Driver JDBC no encontrado", e, type); |
||
629 | } catch (DBException e) {
|
||
630 | |||
631 | throw new DriverException(e.getMessage()); |
||
632 | |||
633 | } |
||
634 | } |
||
635 | /* (non-Javadoc)
|
||
636 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialDatabaseDriver#getXMLEntity()
|
||
637 | */
|
||
638 | public XMLEntity getXMLEntity()
|
||
639 | { |
||
640 | XMLEntity xml = new XMLEntity();
|
||
641 | xml.putProperty("className",this.getClass().getName()); |
||
642 | try {
|
||
643 | DatabaseMetaData metadata = ((ConnectionJDBC)getConnection()).getConnection().getMetaData();
|
||
644 | xml.putProperty("catalog", getLyrDef().getCatalogName());
|
||
645 | |||
646 | // TODO: NO DEBEMOS GUARDAR EL NOMBRE DE USUARIO Y CONTRASE?A
|
||
647 | // AQUI. Hay que utilizar un pool de conexiones
|
||
648 | // y pedir al usuario que conecte a la base de datos
|
||
649 | // en la primera capa. En el resto, usar la conexi?n
|
||
650 | // creada con anterioridad.
|
||
651 | String userName = metadata.getUserName();
|
||
652 | int aux = userName.indexOf("@"); |
||
653 | if (aux != -1) |
||
654 | userName = userName.substring(0,aux);
|
||
655 | xml.putProperty("username", userName);
|
||
656 | |||
657 | Driver drv = DriverManager.getDriver(metadata.getURL()); |
||
658 | // System.out.println(drv.getClass().getName());
|
||
659 | xml.putProperty("driverclass", drv.getClass().getName());
|
||
660 | xml.putProperty("schema", lyrDef.getSchema());
|
||
661 | xml.putProperty("tablename", getTableName());
|
||
662 | xml.putProperty("fields", lyrDef.getFieldNames());
|
||
663 | xml.putProperty("FID", lyrDef.getFieldID());
|
||
664 | xml.putProperty("THE_GEOM", lyrDef.getFieldGeometry());
|
||
665 | xml.putProperty("whereclause", getWhereClause());
|
||
666 | xml.putProperty("SRID", lyrDef.getSRID_EPSG());
|
||
667 | |||
668 | ConnectionWithParams cwp = |
||
669 | SingleVectorialDBConnectionManager.instance().findConnection(getConnection()); |
||
670 | |||
671 | //FIXME:(Chema) Estos cambios los hago porque da errores as persistencia
|
||
672 | if (cwp != null){ |
||
673 | xml.putProperty("host", cwp.getHost());
|
||
674 | xml.putProperty("port", cwp.getPort());
|
||
675 | xml.putProperty("dbName", cwp.getDb());
|
||
676 | xml.putProperty("connName", cwp.getName());
|
||
677 | } else {
|
||
678 | xml.putProperty("dbURL",metadata.getURL());
|
||
679 | } |
||
680 | // Chema
|
||
681 | |||
682 | if (getWorkingArea() != null) |
||
683 | { |
||
684 | xml.putProperty("minXworkArea", getWorkingArea().getMinX());
|
||
685 | xml.putProperty("minYworkArea", getWorkingArea().getMinY());
|
||
686 | xml.putProperty("HworkArea", getWorkingArea().getHeight());
|
||
687 | xml.putProperty("WworkArea", getWorkingArea().getWidth());
|
||
688 | } |
||
689 | |||
690 | } catch (SQLException e) { |
||
691 | // TODO Auto-generated catch block
|
||
692 | e.printStackTrace(); |
||
693 | } |
||
694 | |||
695 | |||
696 | return xml;
|
||
697 | |||
698 | } |
||
699 | |||
700 | /**
|
||
701 | * @see com.iver.cit.gvsig.fmap.drivers.IVectorialJDBCDriver#setWorkingArea(java.awt.geom.Rectangle2D)
|
||
702 | */
|
||
703 | public void setWorkingArea(Rectangle2D rect) { |
||
704 | this.workingArea = rect;
|
||
705 | } |
||
706 | |||
707 | /**
|
||
708 | * @see com.iver.cit.gvsig.fmap.drivers.IVectorialJDBCDriver#getWorkingArea()
|
||
709 | */
|
||
710 | public Rectangle2D getWorkingArea() { |
||
711 | return workingArea;
|
||
712 | } |
||
713 | |||
714 | /* (non-Javadoc)
|
||
715 | * @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
|
||
716 | */
|
||
717 | public void setDataSourceFactory(DataSourceFactory arg0) { |
||
718 | // TODO Auto-generated method stub
|
||
719 | |||
720 | } |
||
721 | |||
722 | /**
|
||
723 | * @return Returns the lyrDef.
|
||
724 | */
|
||
725 | public DBLayerDefinition getLyrDef() {
|
||
726 | if (this.conn != null){ |
||
727 | if (lyrDef.getConnection() != this.conn){ |
||
728 | lyrDef.setConnection(this.conn);
|
||
729 | } |
||
730 | } |
||
731 | return lyrDef;
|
||
732 | } |
||
733 | |||
734 | /**
|
||
735 | * @param lyrDef The lyrDef to set.
|
||
736 | */
|
||
737 | public void setLyrDef(DBLayerDefinition lyrDef) { |
||
738 | this.lyrDef = lyrDef;
|
||
739 | } |
||
740 | |||
741 | abstract public String getSqlTotal(); |
||
742 | |||
743 | /**
|
||
744 | * @return Returns the completeWhere. WITHOUT order by clause!!
|
||
745 | */
|
||
746 | abstract public String getCompleteWhere(); |
||
747 | |||
748 | /* (non-Javadoc)
|
||
749 | * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#reLoad()
|
||
750 | */
|
||
751 | public void reload() throws IOException |
||
752 | { |
||
753 | try {
|
||
754 | if ((conn == null) || (((ConnectionJDBC)conn).getConnection().isClosed())) |
||
755 | { |
||
756 | this.load();
|
||
757 | } |
||
758 | |||
759 | ((ConnectionJDBC)conn).getConnection().commit(); |
||
760 | |||
761 | setData(conn, lyrDef); |
||
762 | } catch (SQLException e) { |
||
763 | throw new IOException(e.getMessage()); |
||
764 | } catch (DriverException e) {
|
||
765 | throw new IOException(e.getMessage()); |
||
766 | } |
||
767 | |||
768 | } |
||
769 | |||
770 | public int getFieldWidth(int fieldId) |
||
771 | { |
||
772 | int i = -1; |
||
773 | try {
|
||
774 | int aux = fieldId + 2; // idField viene basado en 1, y |
||
775 | i = rs.getMetaData().getColumnDisplaySize(aux); |
||
776 | } catch (SQLException e) { |
||
777 | e.printStackTrace(); |
||
778 | } |
||
779 | // SUN define que getColumnDisplaySize devuelve numeros negativos cuando el campo es de tipo Text o Vartext
|
||
780 | // sin ancho. Nosotros vamos a devolver 255 para que fucione, por lo menos al exportar a DBF.
|
||
781 | // Nota: Si se truncan cadenas, este es el sitio que lo provoca.
|
||
782 | if (i <0) i=255; |
||
783 | return i;
|
||
784 | } |
||
785 | |||
786 | // public void setFlatness(double flatness) {
|
||
787 | // this.flatness = flatness;
|
||
788 | // }
|
||
789 | |||
790 | // -----------------------------------------------------------
|
||
791 | // ---- EXT JDBC NUEVA ---
|
||
792 | // -----------------------------------------------------------
|
||
793 | |||
794 | /**
|
||
795 | * Gets the drivers connection string given its parameters (this can be different for
|
||
796 | * different driver, so it should be overwritten in that case.)
|
||
797 | */
|
||
798 | public String getConnectionString( |
||
799 | String host,
|
||
800 | String port,
|
||
801 | String dbname,
|
||
802 | String user,
|
||
803 | String pw) {
|
||
804 | |||
805 | String resp = getConnectionStringBeginning() + "//" + host.toLowerCase(); |
||
806 | |||
807 | if (dbname.trim().length() > 0) { |
||
808 | resp += ":" + port;
|
||
809 | } else {
|
||
810 | resp += ":" + getDefaultPort();
|
||
811 | } |
||
812 | |||
813 | resp += "/" + dbname.toLowerCase();
|
||
814 | return resp;
|
||
815 | } |
||
816 | |||
817 | /**
|
||
818 | * Gets available table names. Should be overwritten by subclasses if its
|
||
819 | * not compatible or if it can be refined
|
||
820 | *
|
||
821 | * @param conn connection object
|
||
822 | * @param catalog catalog name
|
||
823 | * @return array of strings with available table names
|
||
824 | * @throws SQLException
|
||
825 | */
|
||
826 | public String[] getTableNames(IConnection conn, String catalog) throws DBException { |
||
827 | TreeMap ret = new TreeMap(); |
||
828 | try{
|
||
829 | DatabaseMetaData dbmd = ((ConnectionJDBC)conn).getConnection().getMetaData();
|
||
830 | String[] types = {"TABLE", "VIEW"}; |
||
831 | ResultSet rs = dbmd.getTables(catalog, null, null, types); |
||
832 | while (rs.next()){
|
||
833 | // ret.put(rs.getString("TABLE_NAME"), rs.getString("TABLE_NAME"));
|
||
834 | // As suggested by Jorge Agudo, to allow charging tables from other schemas
|
||
835 | ret.put((rs.getString("TABLE_SCHEM")!=null?(rs.getString("TABLE_SCHEM") + "."): "") + rs.getString("TABLE_NAME"), (rs.getString("TABLE_SCHEM")!=null?(rs.getString("TABLE_SCHEM") + "."): "") + rs.getString("TABLE_NAME")); |
||
836 | } |
||
837 | rs.close(); |
||
838 | } catch (SQLException e) { |
||
839 | throw new DBException(e); |
||
840 | } |
||
841 | return (String[]) ret.keySet().toArray(new String[0]); |
||
842 | } |
||
843 | |||
844 | |||
845 | /**
|
||
846 | * Gets all field names of a given table
|
||
847 | * @param conn connection object
|
||
848 | * @param table_name table name
|
||
849 | * @return all field names of the given table
|
||
850 | * @throws SQLException
|
||
851 | */
|
||
852 | public String[] getAllFields(IConnection conn, String table_name) throws DBException { |
||
853 | String[] ret = null; |
||
854 | try{
|
||
855 | Statement st = ((ConnectionJDBC)conn).getConnection().createStatement();
|
||
856 | ResultSet rs = st.executeQuery("select * from " + table_name + " LIMIT 1"); |
||
857 | ResultSetMetaData rsmd = rs.getMetaData();
|
||
858 | ret = new String[rsmd.getColumnCount()]; |
||
859 | |||
860 | for (int i = 0; i < ret.length; i++) { |
||
861 | ret[i] = rsmd.getColumnName(i+1);
|
||
862 | } |
||
863 | rs.close(); st.close(); |
||
864 | } catch (SQLException e) { |
||
865 | // TODO Auto-generated catch block
|
||
866 | e.printStackTrace(); |
||
867 | } |
||
868 | return ret;
|
||
869 | } |
||
870 | |||
871 | /**
|
||
872 | * Gets all field type names of a given table
|
||
873 | * @param conn connection object
|
||
874 | * @param table_name table name
|
||
875 | * @return all field type names of the given table
|
||
876 | * @throws SQLException
|
||
877 | */
|
||
878 | public String[] getAllFieldTypeNames(IConnection conn, String table_name) throws DBException { |
||
879 | String[] ret = null; |
||
880 | try{
|
||
881 | Statement st = ((ConnectionJDBC)conn).getConnection().createStatement();
|
||
882 | ResultSet rs = st.executeQuery("select * from " + table_name + " LIMIT 1"); |
||
883 | ResultSetMetaData rsmd = rs.getMetaData();
|
||
884 | ret = new String[rsmd.getColumnCount()]; |
||
885 | |||
886 | for (int i = 0; i < ret.length; i++) { |
||
887 | ret[i] = rsmd.getColumnTypeName(i+1);
|
||
888 | } |
||
889 | rs.close(); st.close(); |
||
890 | } catch (SQLException e) { |
||
891 | throw new DBException(e); |
||
892 | } |
||
893 | return ret;
|
||
894 | } |
||
895 | |||
896 | /**
|
||
897 | * Gets the table's possible id fields. By default, all fields can be id.
|
||
898 | * It should be overwritten by subclasses.
|
||
899 | *
|
||
900 | * @param conn conenction object
|
||
901 | * @param table_name table name
|
||
902 | * @return the table's possible id fields
|
||
903 | * @throws SQLException
|
||
904 | */
|
||
905 | public String[] getIdFieldsCandidates(IConnection conn, String table_name) throws DBException { |
||
906 | return getAllFields(conn, table_name);
|
||
907 | } |
||
908 | |||
909 | /**
|
||
910 | * Gets the table's possible geometry fields. By default, all fields can be geometry
|
||
911 | * fields. It should be overwritten by subclasses.
|
||
912 | *
|
||
913 | * @param conn conenction object
|
||
914 | * @param table_name table name
|
||
915 | * @return the table's possible geometry fields
|
||
916 | * @throws SQLException
|
||
917 | */
|
||
918 | public String[] getGeometryFieldsCandidates(IConnection conn, String table_name) throws DBException { |
||
919 | return getAllFields(conn, table_name);
|
||
920 | } |
||
921 | |||
922 | /**
|
||
923 | * Tells if it's an empty table (with no records)
|
||
924 | * @param conn conenction object
|
||
925 | * @param tableName rtable name
|
||
926 | * @return whether it's an empty table (with no records) or not
|
||
927 | */
|
||
928 | public boolean isEmptyTable(IConnection conn, String tableName) { |
||
929 | |||
930 | boolean res = true; |
||
931 | |||
932 | try {
|
||
933 | Statement st = ((ConnectionJDBC)conn).getConnection().createStatement();
|
||
934 | ResultSet rs = null; |
||
935 | rs = st.executeQuery("select * from " + tableName + " LIMIT 1"); |
||
936 | res = !rs.next(); |
||
937 | rs.close(); st.close(); |
||
938 | } catch (Exception ex) { |
||
939 | res = true;
|
||
940 | } |
||
941 | return res;
|
||
942 | } |
||
943 | |||
944 | |||
945 | |||
946 | /**
|
||
947 | * Utility method to allow the driver to do something with the geometry field.
|
||
948 | * By default does nothing.
|
||
949 | *
|
||
950 | * @param flds user-selected fields
|
||
951 | * @param geomField geometry field
|
||
952 | * @return new user-selected fields
|
||
953 | */
|
||
954 | public String[] manageGeometryField(String[] flds, String geomField) { |
||
955 | return flds;
|
||
956 | } |
||
957 | |||
958 | /**
|
||
959 | * Empty method called when the layer is going to be removed from the view.
|
||
960 | * Subclasses can overwrite it if needed.
|
||
961 | *
|
||
962 | */
|
||
963 | public void remove() { |
||
964 | |||
965 | } |
||
966 | |||
967 | public void addDriverEventListener(DriverEventListener listener) { |
||
968 | if (!driverEventListeners.contains(listener))
|
||
969 | driverEventListeners.add(listener); |
||
970 | |||
971 | } |
||
972 | |||
973 | public void removeDriverEventListener(DriverEventListener listener) { |
||
974 | driverEventListeners.remove(listener); |
||
975 | |||
976 | } |
||
977 | |||
978 | public void notifyDriverEndLoaded() { |
||
979 | DriverEvent event = new DriverEvent(DriverEvent.DRIVER_EVENT_LOADING_END);
|
||
980 | for (int i=0; i < driverEventListeners.size(); i++) |
||
981 | { |
||
982 | DriverEventListener aux = (DriverEventListener) driverEventListeners.get(i); |
||
983 | aux.driverNotification(event); |
||
984 | } |
||
985 | } |
||
986 | |||
987 | } |