Revision 3236 trunk/libraries/libFMap/src/com/iver/cit/gvsig/fmap/drivers/DefaultDBDriver.java
DefaultDBDriver.java | ||
---|---|---|
80 | 80 |
private static Hashtable poolPassw = new Hashtable(); |
81 | 81 |
|
82 | 82 |
protected Connection conn; |
83 |
protected String tableName; |
|
84 |
protected String whereClause; |
|
85 |
protected String fields; |
|
86 |
protected String sqlOrig; |
|
83 |
// protected String tableName; |
|
84 |
// protected String whereClause; |
|
85 |
// protected String fields; |
|
86 |
// protected String sqlOrig; |
|
87 |
private DBLayerDefinition lyrDef = null; |
|
87 | 88 |
protected ResultSet rs; |
88 | 89 |
protected boolean bCursorActivo = false; |
89 | 90 |
protected Statement st; |
90 | 91 |
protected int numReg=-1; |
91 | 92 |
|
92 |
protected String strFID_FieldName; |
|
93 |
protected int idFID_FieldName;
|
|
93 |
// protected String strFID_FieldName;
|
|
94 |
// protected String idFID_FieldName;
|
|
94 | 95 |
|
95 | 96 |
protected Hashtable hashRelate; |
96 | 97 |
|
... | ... | |
98 | 99 |
protected ResultSetMetaData metaData = null; |
99 | 100 |
protected Rectangle2D workingArea; |
100 | 101 |
|
101 |
abstract public void setData(Connection conn, String tableName, String fields, String whereClause, int fidField);
|
|
102 |
abstract public void setData(Connection conn, DBLayerDefinition lyrDef);
|
|
102 | 103 |
|
103 | 104 |
/** |
104 | 105 |
* @return devuelve la Conexi?n a la base de datos, para que |
... | ... | |
114 | 115 |
} |
115 | 116 |
public String[] getFields() |
116 | 117 |
{ |
117 |
StringTokenizer tokenizer = new StringTokenizer(fields, ","); |
|
118 |
/* StringTokenizer tokenizer = new StringTokenizer(fields, ",");
|
|
118 | 119 |
String[] arrayFields = new String[tokenizer.countTokens()]; |
119 | 120 |
int i=0; |
120 | 121 |
while (tokenizer.hasMoreTokens()) |
... | ... | |
122 | 123 |
arrayFields[i] = tokenizer.nextToken(); |
123 | 124 |
i++; |
124 | 125 |
} |
125 |
return arrayFields; |
|
126 |
return arrayFields; */ |
|
127 |
return lyrDef.getFieldNames(); |
|
128 |
|
|
126 | 129 |
} |
130 |
/** |
|
131 |
* First, the geometry field. After, the rest of fields |
|
132 |
* @return |
|
133 |
*/ |
|
134 |
public String getTotalFields() |
|
135 |
{ |
|
136 |
String strAux = getGeometryField(getLyrDef().getFieldGeometry()); |
|
137 |
String[] fieldNames = getLyrDef().getFieldNames(); |
|
138 |
for (int i=0; i< fieldNames.length; i++) |
|
139 |
{ |
|
140 |
strAux = strAux + ", " + fieldNames[i]; |
|
141 |
} |
|
142 |
return strAux; |
|
143 |
} |
|
144 |
|
|
127 | 145 |
public String getWhereClause() |
128 | 146 |
{ |
129 |
return whereClause;
|
|
147 |
return lyrDef.getWhereClause();
|
|
130 | 148 |
} |
131 | 149 |
public String getTableName() |
132 | 150 |
{ |
133 |
return tableName;
|
|
151 |
return lyrDef.getTableName();
|
|
134 | 152 |
} |
135 | 153 |
|
136 | 154 |
|
... | ... | |
145 | 163 |
try |
146 | 164 |
{ |
147 | 165 |
Statement s = conn.createStatement(); |
148 |
ResultSet r = s.executeQuery("SELECT COUNT(*) AS NUMREG FROM " + tableName + " " + whereClause);
|
|
166 |
ResultSet r = s.executeQuery("SELECT COUNT(*) AS NUMREG FROM " + lyrDef.getTableName() + " " + lyrDef.getWhereClause());
|
|
149 | 167 |
r.next(); |
150 | 168 |
numReg = r.getInt(1); |
151 | 169 |
System.err.println("numReg = " + numReg); |
... | ... | |
322 | 340 |
|
323 | 341 |
while (rs.next()) |
324 | 342 |
{ |
325 |
Value aux = getFieldValue(index, idFID_FieldName-2);
|
|
343 |
Value aux = getFieldValue(index, lyrDef.getIdFieldID()-2);
|
|
326 | 344 |
hashRelate.put(aux, new Integer(index)); |
327 | 345 |
index++; |
328 | 346 |
} |
... | ... | |
344 | 362 |
public int getRowIndexByFID(IFeature FID) |
345 | 363 |
{ |
346 | 364 |
int resul; |
347 |
Object obj = FID.getAttribute(idFID_FieldName -2);
|
|
365 |
Object obj = FID.getAttribute(lyrDef.getIdFieldID() -2);
|
|
348 | 366 |
// System.err.println("Mirando si existe " + obj.toString()); |
349 | 367 |
Integer rowIndex = (Integer) hashRelate.get(obj); |
350 | 368 |
resul = rowIndex.intValue(); |
... | ... | |
358 | 376 |
{ |
359 | 377 |
String className = xml.getStringProperty("className"); |
360 | 378 |
String dbUrl = xml.getStringProperty("dbURL"); |
379 |
String catalogName = xml.getStringProperty("catalog"); |
|
361 | 380 |
String userName =xml.getStringProperty("username"); |
362 | 381 |
String driverClass =xml.getStringProperty("driverclass"); |
363 | 382 |
String tableName = xml.getStringProperty("tablename"); |
364 |
String fields = xml.getStringProperty("fields");
|
|
383 |
String[] fields = xml.getStringArrayProperty("fields");
|
|
365 | 384 |
int idFIDfield = xml.getIntProperty("FID"); |
385 |
String geometryField = xml.getStringProperty("THE_GEOM"); |
|
366 | 386 |
String whereClause = xml.getStringProperty("whereclause"); |
387 |
String strSRID = xml.getStringProperty("SRID"); |
|
367 | 388 |
|
368 | 389 |
try { |
369 | 390 |
Class.forName(driverClass); |
... | ... | |
390 | 411 |
newConn = DriverManager.getConnection(dbUrl, userName, clave); |
391 | 412 |
newConn.setAutoCommit(false); |
392 | 413 |
|
393 |
setData(newConn, tableName, fields, whereClause, idFIDfield); |
|
414 |
DBLayerDefinition lyrDef = new DBLayerDefinition(); |
|
415 |
lyrDef.setCatalogName(catalogName); |
|
416 |
lyrDef.setTableName(tableName); |
|
417 |
lyrDef.setFieldNames(fields); |
|
418 |
lyrDef.setFieldID(fields[idFIDfield]); |
|
419 |
lyrDef.setFieldGeometry(geometryField); |
|
420 |
lyrDef.setWhereClause(whereClause); |
|
421 |
// lyrDef.setClassToInstantiate(driverClass); |
|
422 |
if (workingArea != null) |
|
423 |
lyrDef.setWorkingArea(workingArea); |
|
424 |
|
|
425 |
lyrDef.setSRID_EPSG(strSRID); |
|
426 |
|
|
427 |
|
|
428 |
setData(newConn, lyrDef); |
|
394 | 429 |
} catch (ClassNotFoundException e) { |
395 | 430 |
logger.debug(e); |
396 | 431 |
throw new XMLException(e); |
... | ... | |
410 | 445 |
try { |
411 | 446 |
DatabaseMetaData metadata = getConnection().getMetaData(); |
412 | 447 |
xml.putProperty("dbURL", metadata.getURL()); |
448 |
xml.putProperty("catalog", getLyrDef().getCatalogName()); |
|
413 | 449 |
// TODO: NO DEBEMOS GUARDAR EL NOMBRE DE USUARIO Y CONTRASE?A |
414 | 450 |
// AQUI. Hay que utilizar un pool de conexiones |
415 | 451 |
// y pedir al usuario que conecte a la base de datos |
... | ... | |
426 | 462 |
xml.putProperty("driverclass", drv.getClass().getName()); |
427 | 463 |
|
428 | 464 |
xml.putProperty("tablename", getTableName()); |
429 |
xml.putProperty("fields", getFields()); |
|
430 |
xml.putProperty("FID", idFID_FieldName); |
|
431 |
|
|
432 |
// NOTA: El campo de geometr?a se supone que es |
|
433 |
// el ?ltimo de la lista de campos. |
|
434 |
|
|
465 |
xml.putProperty("fields", lyrDef.getFieldNames()); |
|
466 |
xml.putProperty("FID", lyrDef.getFieldID()); |
|
467 |
xml.putProperty("THE_GEOM", lyrDef.getFieldGeometry()); |
|
435 | 468 |
xml.putProperty("whereclause", getWhereClause()); |
469 |
xml.putProperty("SRID", lyrDef.getSRID_EPSG()); |
|
436 | 470 |
|
437 | 471 |
} catch (SQLException e) { |
438 | 472 |
// TODO Auto-generated catch block |
... | ... | |
465 | 499 |
// TODO Auto-generated method stub |
466 | 500 |
|
467 | 501 |
} |
502 |
|
|
503 |
/** |
|
504 |
* @return Returns the lyrDef. |
|
505 |
*/ |
|
506 |
public DBLayerDefinition getLyrDef() { |
|
507 |
return lyrDef; |
|
508 |
} |
|
509 |
|
|
510 |
/** |
|
511 |
* @param lyrDef The lyrDef to set. |
|
512 |
*/ |
|
513 |
public void setLyrDef(DBLayerDefinition lyrDef) { |
|
514 |
this.lyrDef = lyrDef; |
|
515 |
} |
|
468 | 516 |
|
469 | 517 |
} |
Also available in: Unified diff