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