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