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