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