Revision 33287 trunk/extensions/extJDBC/src/com/iver/cit/gvsig/fmap/drivers/jdbc/postgis/PostGisDriver.java
PostGisDriver.java | ||
---|---|---|
229 | 229 |
try { |
230 | 230 |
((ConnectionJDBC)conn).getConnection().setAutoCommit(false); |
231 | 231 |
sqlOrig = "SELECT " + getTotalFields() + " FROM " |
232 |
+ getLyrDef().getComposedTableName() + " ";
|
|
233 |
// + getLyrDef().getWhereClause();
|
|
232 |
+ getLyrDef().getComposedTableName() + " "; |
|
233 |
// + getLyrDef().getWhereClause(); |
|
234 | 234 |
if (canReproject(strEPSG)) { |
235 | 235 |
completeWhere = getCompoundWhere(sqlOrig, workingArea, strEPSG); |
236 | 236 |
} else { |
... | ... | |
354 | 354 |
// ResultSet rs = st.executeQuery(sql); |
355 | 355 |
geomIterator = new PostGisFeatureIterator(((ConnectionJDBC)conn).getConnection(), provCursorName, sql); |
356 | 356 |
} catch (SQLException e) { |
357 |
// e.printStackTrace(); |
|
358 |
// e.printStackTrace(); |
|
359 |
// SqlDriveExceptionType type = new SqlDriveExceptionType(); |
|
360 |
// type.setDriverName("PostGIS Driver"); |
|
361 |
// type.setSql(sql); |
|
362 |
// type.setLayerName(getTableName()); |
|
363 |
// type.setSchema(null); |
|
364 |
throw new ReadDriverException("PostGIS Driver",e);
|
|
365 |
// throw new DriverException(e); |
|
357 |
// e.printStackTrace();
|
|
358 |
// e.printStackTrace();
|
|
359 |
// SqlDriveExceptionType type = new SqlDriveExceptionType();
|
|
360 |
// type.setDriverName("PostGIS Driver");
|
|
361 |
// type.setSql(sql);
|
|
362 |
// type.setLayerName(getTableName());
|
|
363 |
// type.setSchema(null);
|
|
364 |
throw new ReadDriverException("PostGIS Driver",e);
|
|
365 |
// throw new DriverException(e);
|
|
366 | 366 |
// return null; |
367 | 367 |
} |
368 | 368 |
|
... | ... | |
370 | 370 |
} |
371 | 371 |
|
372 | 372 |
public IFeatureIterator getFeatureIterator(Rectangle2D r, String strEPSG) |
373 |
throws ReadDriverException {
|
|
373 |
throws ReadDriverException { |
|
374 | 374 |
if (workingArea != null) |
375 | 375 |
r = r.createIntersection(workingArea); |
376 | 376 |
|
... | ... | |
404 | 404 |
double xMax = r.getMaxX(); |
405 | 405 |
double yMax = r.getMaxY(); |
406 | 406 |
String wktBox = "GeometryFromText('LINESTRING(" + xMin + " " + yMin |
407 |
+ ", " + xMax + " " + yMin + ", " + xMax + " " + yMax + ", "
|
|
408 |
+ xMin + " " + yMax + ")', " + strEPSG + ")";
|
|
407 |
+ ", " + xMax + " " + yMin + ", " + xMax + " " + yMax + ", " |
|
408 |
+ xMin + " " + yMax + ")', " + strEPSG + ")"; |
|
409 | 409 |
String sqlAux; |
410 | 410 |
if (getWhereClause().toUpperCase().indexOf("WHERE") != -1) |
411 | 411 |
sqlAux = getWhereClause() + " AND " + getLyrDef().getFieldGeometry() + " && " + wktBox; |
412 | 412 |
else |
413 | 413 |
sqlAux = "WHERE " + getLyrDef().getFieldGeometry() + " && " |
414 |
+ wktBox;
|
|
414 |
+ wktBox; |
|
415 | 415 |
return sqlAux; |
416 | 416 |
} |
417 | 417 |
|
... | ... | |
429 | 429 |
* int) |
430 | 430 |
*/ |
431 | 431 |
public Value getFieldValue(long rowIndex, int idField) |
432 |
throws ReadDriverException {
|
|
432 |
throws ReadDriverException { |
|
433 | 433 |
boolean resul; |
434 | 434 |
// EL ABSOLUTE NO HACE QUE SE VUELVAN A LEER LAS |
435 | 435 |
// FILAS, ASI QUE MONTAMOS ESTA HISTORIA PARA QUE |
... | ... | |
491 | 491 |
if (metaData.getColumnType(fieldId) == Types.TIME) |
492 | 492 |
{ |
493 | 493 |
// TODO: |
494 |
// throw new RuntimeException("TIME type not implemented yet");
|
|
495 |
return ValueFactory.createValue("NOT IMPLEMENTED YET");
|
|
496 |
}
|
|
497 |
if (metaData.getColumnType(fieldId) == Types.TIMESTAMP)
|
|
498 |
{
|
|
499 |
double segsReferredTo2000 = buf.getDouble();
|
|
500 |
long real_msecs = (long) (XTypes.NUM_msSecs2000 + segsReferredTo2000*1000);
|
|
501 |
Timestamp valTimeStamp = new Timestamp(real_msecs);
|
|
502 |
return ValueFactory.createValue(valTimeStamp);
|
|
503 |
}
|
|
494 |
// throw new RuntimeException("TIME type not implemented yet"); |
|
495 |
return ValueFactory.createValue("NOT IMPLEMENTED YET"); |
|
496 |
} |
|
497 |
if (metaData.getColumnType(fieldId) == Types.TIMESTAMP) |
|
498 |
{ |
|
499 |
double segsReferredTo2000 = buf.getDouble(); |
|
500 |
long real_msecs = (long) (XTypes.NUM_msSecs2000 + segsReferredTo2000*1000); |
|
501 |
Timestamp valTimeStamp = new Timestamp(real_msecs); |
|
502 |
return ValueFactory.createValue(valTimeStamp); |
|
503 |
} |
|
504 | 504 |
|
505 |
if (metaData.getColumnType(fieldId) == Types.NUMERIC) {
|
|
506 |
// System.out.println(metaData.getColumnName(fieldId) + " "
|
|
507 |
// + metaData.getColumnClassName(fieldId));
|
|
508 |
short ndigits = buf.getShort();
|
|
509 |
short weight = buf.getShort();
|
|
510 |
short sign = buf.getShort();
|
|
511 |
short dscale = buf.getShort();
|
|
512 |
String strAux;
|
|
513 |
if (sign == 0)
|
|
514 |
strAux = "+";
|
|
515 |
else
|
|
516 |
strAux = "-";
|
|
505 |
if (metaData.getColumnType(fieldId) == Types.NUMERIC) { |
|
506 |
// System.out.println(metaData.getColumnName(fieldId) + " " |
|
507 |
// + metaData.getColumnClassName(fieldId)); |
|
508 |
short ndigits = buf.getShort(); |
|
509 |
short weight = buf.getShort(); |
|
510 |
short sign = buf.getShort(); |
|
511 |
short dscale = buf.getShort(); |
|
512 |
String strAux; |
|
513 |
if (sign == 0) |
|
514 |
strAux = "+"; |
|
515 |
else |
|
516 |
strAux = "-"; |
|
517 | 517 |
|
518 |
for (int iDigit = 0; iDigit < ndigits; iDigit++) {
|
|
519 |
short digit = buf.getShort();
|
|
520 |
strAux = strAux + digit;
|
|
521 |
if (iDigit == weight)
|
|
522 |
strAux = strAux + ".";
|
|
518 |
for (int iDigit = 0; iDigit < ndigits; iDigit++) { |
|
519 |
short digit = buf.getShort(); |
|
520 |
strAux = strAux + digit; |
|
521 |
if (iDigit == weight) |
|
522 |
strAux = strAux + "."; |
|
523 | 523 |
|
524 |
} |
|
525 |
strAux = strAux + "0"; |
|
526 |
BigDecimal dec; |
|
527 |
dec = new BigDecimal(strAux); |
|
528 |
// System.out.println(ndigits + "_" + weight + "_" + dscale |
|
529 |
// + "_" + strAux); |
|
530 |
// System.out.println(strAux + " Big= " + dec); |
|
531 |
return ValueFactory.createValue(dec.doubleValue()); |
|
532 | 524 |
} |
533 |
|
|
525 |
strAux = strAux + "0"; |
|
526 |
BigDecimal dec; |
|
527 |
dec = new BigDecimal(strAux); |
|
528 |
// System.out.println(ndigits + "_" + weight + "_" + dscale |
|
529 |
// + "_" + strAux); |
|
530 |
// System.out.println(strAux + " Big= " + dec); |
|
531 |
return ValueFactory.createValue(dec.doubleValue()); |
|
534 | 532 |
} |
535 | 533 |
|
534 |
} |
|
535 |
|
|
536 | 536 |
return ValueFactory.createNullValue(); |
537 | 537 |
|
538 | 538 |
} |
... | ... | |
574 | 574 |
myCursorId++; |
575 | 575 |
st.execute("declare " + getTableName() + myCursorId + "_wkb_cursorAbsolutePosition binary scroll cursor with hold for " + sqlTotal); |
576 | 576 |
st.executeQuery("fetch absolute " + fetch_min |
577 |
+ " in " + getTableName() + myCursorId + "_wkb_cursorAbsolutePosition"); |
|
577 |
+ " in " + getTableName() + myCursorId + "_wkb_cursorAbsolutePosition");
|
|
578 | 578 |
|
579 | 579 |
rs = st.executeQuery("fetch forward " + FETCH_SIZE |
580 |
+ " in " + getTableName() + myCursorId + "_wkb_cursorAbsolutePosition"); |
|
580 |
+ " in " + getTableName() + myCursorId + "_wkb_cursorAbsolutePosition");
|
|
581 | 581 |
|
582 | 582 |
} |
583 | 583 |
rs.absolute(index - fetch_min + 1); |
... | ... | |
640 | 640 |
try { |
641 | 641 |
Statement stAux = ((ConnectionJDBC)conn).getConnection().createStatement(); |
642 | 642 |
|
643 |
// String sql = "SELECT * FROM GEOMETRY_COLUMNS WHERE F_TABLE_NAME = '" |
|
644 |
// + getTableName() + "' AND F_GEOMETRY_COLUMN = '" + getLyrDef().getFieldGeometry() + "'"; |
|
643 |
// String sql = "SELECT * FROM GEOMETRY_COLUMNS WHERE F_TABLE_NAME = '"
|
|
644 |
// + getTableName() + "' AND F_GEOMETRY_COLUMN = '" + getLyrDef().getFieldGeometry() + "'";
|
|
645 | 645 |
String sql; |
646 | 646 |
if (dbld.getSchema() == null || dbld.getSchema().equals("")){ |
647 | 647 |
sql = "SELECT * FROM GEOMETRY_COLUMNS WHERE F_TABLE_SCHEMA = current_schema() AND F_TABLE_NAME = '" |
648 | 648 |
+ dbld.getTableName() + "' AND F_GEOMETRY_COLUMN = '" + dbld.getFieldGeometry() + "'"; |
649 | 649 |
}else{ |
650 | 650 |
sql = "SELECT * FROM GEOMETRY_COLUMNS WHERE F_TABLE_SCHEMA = '"+ dbld.getSchema() + "' AND F_TABLE_NAME = '" |
651 |
+ dbld.getTableName() + "' AND F_GEOMETRY_COLUMN = '" + dbld.getFieldGeometry() + "'";
|
|
651 |
+ dbld.getTableName() + "' AND F_GEOMETRY_COLUMN = '" + dbld.getFieldGeometry() + "'"; |
|
652 | 652 |
} |
653 | 653 |
|
654 | 654 |
ResultSet rs = stAux.executeQuery(sql); |
... | ... | |
723 | 723 |
hashRelate = new Hashtable(); |
724 | 724 |
try { |
725 | 725 |
String strSQL = "SELECT " + getLyrDef().getFieldID() + " FROM " |
726 |
+ getLyrDef().getComposedTableName() + " ";
|
|
727 |
// + getLyrDef().getWhereClause();
|
|
726 |
+ getLyrDef().getComposedTableName() + " "; |
|
727 |
// + getLyrDef().getWhereClause(); |
|
728 | 728 |
if (canReproject(strEPSG)) { |
729 | 729 |
strSQL = strSQL |
730 |
+ getCompoundWhere(strSQL, workingArea, strEPSG);
|
|
730 |
+ getCompoundWhere(strSQL, workingArea, strEPSG); |
|
731 | 731 |
} else { |
732 | 732 |
strSQL = strSQL |
733 |
+ getCompoundWhere(strSQL, workingArea, originalEPSG);
|
|
733 |
+ getCompoundWhere(strSQL, workingArea, originalEPSG); |
|
734 | 734 |
} |
735 | 735 |
strSQL = strSQL + " ORDER BY " + getLyrDef().getFieldID(); |
736 | 736 |
Statement s = ((ConnectionJDBC)getConnection()).getConnection().createStatement( |
... | ... | |
853 | 853 |
clonedLyrDef.setFieldsDesc( (FieldDescription[])myFieldsDesc.toArray(new FieldDescription[]{}) ); |
854 | 854 |
|
855 | 855 |
String sqlProv = "SELECT " + strAux + " FROM " |
856 |
+ lyrDef.getComposedTableName() + " ";
|
|
857 |
// + getLyrDef().getWhereClause();
|
|
856 |
+ lyrDef.getComposedTableName() + " "; |
|
857 |
// + getLyrDef().getWhereClause(); |
|
858 | 858 |
|
859 | 859 |
if (canReproject(strEPSG)) { |
860 | 860 |
sqlAux = sqlProv + getCompoundWhere(sqlProv, r, strEPSG); |
... | ... | |
868 | 868 |
geomIterator.setLyrDef(clonedLyrDef); |
869 | 869 |
return geomIterator; |
870 | 870 |
} catch (Exception e) { |
871 |
// e.printStackTrace(); |
|
872 |
// SqlDriveExceptionType type = new SqlDriveExceptionType(); |
|
873 |
// type.setDriverName("PostGIS Driver"); |
|
874 |
// type.setSql(sqlAux); |
|
875 |
// type.setLayerName(getTableName()); |
|
876 |
// type.setSchema(null); |
|
877 |
throw new ReadDriverException("PostGIS Driver",e);
|
|
871 |
// e.printStackTrace();
|
|
872 |
// SqlDriveExceptionType type = new SqlDriveExceptionType();
|
|
873 |
// type.setDriverName("PostGIS Driver");
|
|
874 |
// type.setSql(sqlAux);
|
|
875 |
// type.setLayerName(getTableName());
|
|
876 |
// type.setSchema(null);
|
|
877 |
throw new ReadDriverException("PostGIS Driver",e);
|
|
878 | 878 |
|
879 |
// throw new DriverException(e); |
|
879 |
// throw new DriverException(e);
|
|
880 | 880 |
} |
881 | 881 |
} |
882 | 882 |
|
... | ... | |
918 | 918 |
|
919 | 919 |
writer.initialize(dbLyrDef); |
920 | 920 |
} |
921 |
*/ |
|
921 |
*/
|
|
922 | 922 |
public boolean isWritable() { |
923 | 923 |
// CHANGE FROM CARTOLAB |
924 | 924 |
// return true; |
... | ... | |
933 | 933 |
public String[] getGeometryFieldsCandidates(IConnection conn, String table_name) throws DBException { |
934 | 934 |
ArrayList list = new ArrayList(); |
935 | 935 |
try{ |
936 |
Statement stAux = ((ConnectionJDBC)conn).getConnection().createStatement(); |
|
937 |
String[] tokens = table_name.split("\\u002E", 2);
|
|
938 |
String sql;
|
|
939 |
if (tokens.length > 1)
|
|
940 |
{
|
|
941 |
sql = "select * from GEOMETRY_COLUMNS WHERE F_TABLE_SCHEMA = '"
|
|
942 |
+ tokens[0] + "' AND F_TABLE_NAME = '" +
|
|
943 |
tokens[1] + "'";
|
|
944 |
}
|
|
945 |
else
|
|
946 |
{
|
|
947 |
sql = "select * from GEOMETRY_COLUMNS" +
|
|
948 |
" where F_TABLE_SCHEMA = current_schema() AND F_TABLE_NAME = '" + table_name + "'";
|
|
936 |
Statement stAux = ((ConnectionJDBC)conn).getConnection().createStatement();
|
|
937 |
String[] tokens = table_name.split("\\u002E", 2);
|
|
938 |
String sql;
|
|
939 |
if (tokens.length > 1)
|
|
940 |
{
|
|
941 |
sql = "select * from GEOMETRY_COLUMNS WHERE F_TABLE_SCHEMA = '"
|
|
942 |
+ tokens[0] + "' AND F_TABLE_NAME = '" +
|
|
943 |
tokens[1] + "'";
|
|
944 |
}
|
|
945 |
else
|
|
946 |
{
|
|
947 |
sql = "select * from GEOMETRY_COLUMNS" +
|
|
948 |
" where F_TABLE_SCHEMA = current_schema() AND F_TABLE_NAME = '" + table_name + "'";
|
|
949 | 949 |
|
950 |
}
|
|
950 |
}
|
|
951 | 951 |
|
952 |
// String sql = "SELECT * FROM GEOMETRY_COLUMNS WHERE F_TABLE_NAME = '" |
|
953 |
// + table_name + "'"; |
|
952 |
// String sql = "SELECT * FROM GEOMETRY_COLUMNS WHERE F_TABLE_NAME = '"
|
|
953 |
// + table_name + "'";
|
|
954 | 954 |
|
955 |
ResultSet rs = stAux.executeQuery(sql); |
|
956 |
while (rs.next()) |
|
957 |
{ |
|
958 |
String geomCol = rs.getString("F_GEOMETRY_COLUMN"); |
|
959 |
list.add(geomCol); |
|
960 |
} |
|
961 |
rs.close(); stAux.close(); |
|
955 |
ResultSet rs = stAux.executeQuery(sql);
|
|
956 |
while (rs.next())
|
|
957 |
{
|
|
958 |
String geomCol = rs.getString("F_GEOMETRY_COLUMN");
|
|
959 |
list.add(geomCol);
|
|
960 |
}
|
|
961 |
rs.close(); stAux.close();
|
|
962 | 962 |
} catch (SQLException e) { |
963 | 963 |
throw new DBException(e); |
964 | 964 |
} |
965 | 965 |
return (String[]) list.toArray(new String[0]); |
966 | 966 |
} |
967 |
// public String[] getTableFields(IConnection conex, String table) throws DBException { |
|
968 |
// try{ |
|
969 |
// Statement st = ((ConnectionJDBC)conex).getConnection().createStatement(); |
|
970 |
// // ResultSet rs = dbmd.getTables(catalog, null, dbLayerDefinition.getTable(), null); |
|
971 |
// ResultSet rs = st.executeQuery("select * from " + table + " LIMIT 1"); |
|
972 |
// ResultSetMetaData rsmd = rs.getMetaData(); |
|
973 |
// |
|
974 |
// String[] ret = new String[rsmd.getColumnCount()]; |
|
975 |
// |
|
976 |
// for (int i = 0; i < ret.length; i++) { |
|
977 |
// ret[i] = rsmd.getColumnName(i+1); |
|
978 |
// } |
|
979 |
// |
|
980 |
// return ret; |
|
981 |
// }catch (SQLException e) { |
|
982 |
// throw new DBException(e); |
|
983 |
// } |
|
984 |
// } |
|
967 |
// public String[] getTableFields(IConnection conex, String table) throws DBException {
|
|
968 |
// try{
|
|
969 |
// Statement st = ((ConnectionJDBC)conex).getConnection().createStatement();
|
|
970 |
// // ResultSet rs = dbmd.getTables(catalog, null, dbLayerDefinition.getTable(), null);
|
|
971 |
// ResultSet rs = st.executeQuery("select * from " + table + " LIMIT 1");
|
|
972 |
// ResultSetMetaData rsmd = rs.getMetaData();
|
|
973 |
//
|
|
974 |
// String[] ret = new String[rsmd.getColumnCount()];
|
|
975 |
//
|
|
976 |
// for (int i = 0; i < ret.length; i++) {
|
|
977 |
// ret[i] = rsmd.getColumnName(i+1);
|
|
978 |
// }
|
|
979 |
//
|
|
980 |
// return ret;
|
|
981 |
// }catch (SQLException e) {
|
|
982 |
// throw new DBException(e);
|
|
983 |
// }
|
|
984 |
// }
|
|
985 | 985 |
|
986 | 986 |
private DBLayerDefinition cloneLyrDef(DBLayerDefinition lyrDef){ |
987 | 987 |
DBLayerDefinition clonedLyrDef = new DBLayerDefinition(); |
... | ... | |
1017 | 1017 |
|
1018 | 1018 |
public String getTotalFields() { |
1019 | 1019 |
StringBuilder strAux = new StringBuilder(); |
1020 |
strAux.append(getGeometryField(getLyrDef().getFieldGeometry()));
|
|
1021 |
String[] fieldNames = getLyrDef().getFieldNames();
|
|
1022 |
for (int i=0; i< fieldNames.length; i++)
|
|
1023 |
{
|
|
1024 |
strAux.append(", " + PostGIS.escapeFieldName(fieldNames[i]));
|
|
1025 |
}
|
|
1026 |
return strAux.toString();
|
|
1020 |
strAux.append(getGeometryField(getLyrDef().getFieldGeometry()));
|
|
1021 |
String[] fieldNames = getLyrDef().getFieldNames();
|
|
1022 |
for (int i=0; i< fieldNames.length; i++)
|
|
1023 |
{
|
|
1024 |
strAux.append(", " + PostGIS.escapeFieldName(fieldNames[i]));
|
|
1025 |
}
|
|
1026 |
return strAux.toString();
|
|
1027 | 1027 |
} |
1028 | 1028 |
|
1029 | 1029 |
|
1030 |
/** |
|
1031 |
* Gets all field names of a given table |
|
1032 |
* @param conn connection object |
|
1033 |
* @param table_name table name |
|
1034 |
* @return all field names of the given table |
|
1035 |
* @throws SQLException |
|
1036 |
*/ |
|
1037 |
public String[] getAllFields(IConnection conn, String table_name) throws DBException { |
|
1038 |
return super.getAllFields(conn, tableNameToComposedTableName(table_name)); |
|
1039 |
} |
|
1030 |
/** |
|
1031 |
* Gets all field names of a given table. |
|
1032 |
* |
|
1033 |
* This method comes from DefaultJDBC.java class. Postgis driver has no method to check |
|
1034 |
* the status of the connection -if it is valid or not. So, as it's not possible to assure |
|
1035 |
* its status, close the connection when an exception happens and re-open it on demand |
|
1036 |
* on the proper method will solve the problems related to an invalid status. |
|
1037 |
* |
|
1038 |
* @param conn connection object |
|
1039 |
* @param table_name table name |
|
1040 |
* @return all field names of the given table |
|
1041 |
* @throws SQLException |
|
1042 |
*/ |
|
1043 |
@Override |
|
1044 |
public String[] getAllFields(IConnection conn, String table_name) throws DBException { |
|
1045 |
Statement st = null; |
|
1046 |
ResultSet rs = null; |
|
1047 |
table_name = tableNameToComposedTableName(table_name); |
|
1040 | 1048 |
|
1041 |
public String[] getAllFieldTypeNames(IConnection conn, String table_name) throws DBException { |
|
1042 |
return super.getAllFieldTypeNames(conn, tableNameToComposedTableName(table_name)); |
|
1043 |
} |
|
1049 |
try { |
|
1050 |
st = ((ConnectionJDBC)conn).getConnection().createStatement(); |
|
1051 |
rs = st.executeQuery("SELECT * FROM " + table_name + " LIMIT 1"); |
|
1052 |
ResultSetMetaData rsmd = rs.getMetaData(); |
|
1053 |
String[] ret = new String[rsmd.getColumnCount()]; |
|
1044 | 1054 |
|
1045 |
/** |
|
1046 |
* |
|
1047 |
* @param tableName |
|
1048 |
* @return a string with the schema and the tableName quoted |
|
1049 |
*/ |
|
1050 |
private String tableNameToComposedTableName(String tableName) { |
|
1051 |
String composedTableName = null; |
|
1052 |
String[] tokens = tableName.trim().replace("\"", "").split("\\u002E"); |
|
1055 |
for (int i = 0; i < ret.length; i++) { |
|
1056 |
ret[i] = rsmd.getColumnName(i+1); |
|
1057 |
} |
|
1053 | 1058 |
|
1054 |
if (tokens.length == 1) { |
|
1055 |
composedTableName = "\"" + tokens[0] + "\""; |
|
1059 |
return ret; |
|
1060 |
} catch (SQLException e) { |
|
1061 |
// Next time getConnection() method is called it will be re-opened. |
|
1062 |
// @see com.iver.cit.gvsig.fmap.drivers.ConnectionJDBC.java; |
|
1063 |
closeConnection(conn); |
|
1064 |
throw new DBException(e); |
|
1065 |
} |
|
1066 |
finally { |
|
1067 |
closeResultSet(rs); |
|
1068 |
closeStatement(st); |
|
1069 |
} |
|
1070 |
} |
|
1056 | 1071 |
|
1057 |
} else if (tokens.length == 2) { |
|
1058 |
composedTableName = "\"" + tokens[0] + "\".\"" + tokens[1] + "\""; |
|
1059 |
} else { |
|
1060 |
// this is a not predictable case, so we return the same |
|
1061 |
composedTableName = tableName; |
|
1062 |
} |
|
1072 |
/** |
|
1073 |
* Gets all field type names of a given table. |
|
1074 |
* |
|
1075 |
* This method comes from DefaultJDBC.java class. Postgis driver has no method to check |
|
1076 |
* the status of the connection -if it is valid or not. So, as it's not possible to assure |
|
1077 |
* its status, close the connection when an exception happens and re-open it on demand |
|
1078 |
* on the proper method will solve the problems related to an invalid status. |
|
1079 |
* |
|
1080 |
* @param conn connection object |
|
1081 |
* @param table_name table name |
|
1082 |
* @return all field type names of the given table |
|
1083 |
* @throws SQLException |
|
1084 |
*/ |
|
1085 |
public String[] getAllFieldTypeNames(IConnection conn, String table_name) throws DBException { |
|
1086 |
table_name = tableNameToComposedTableName(table_name); |
|
1087 |
try { |
|
1088 |
Statement st = ((ConnectionJDBC)conn).getConnection().createStatement(); |
|
1089 |
ResultSet rs = st.executeQuery("SELECT * FROM " + table_name + " LIMIT 1"); |
|
1090 |
ResultSetMetaData rsmd = rs.getMetaData(); |
|
1091 |
String[] ret = new String[rsmd.getColumnCount()]; |
|
1063 | 1092 |
|
1064 |
return composedTableName; |
|
1065 |
} |
|
1093 |
for (int i = 0; i < ret.length; i++) { |
|
1094 |
ret[i] = rsmd.getColumnTypeName(i+1); |
|
1095 |
} |
|
1096 |
return ret; |
|
1097 |
} catch (SQLException e) { |
|
1098 |
// Next time getConnection() method is called it will be re-opened. |
|
1099 |
// @see com.iver.cit.gvsig.fmap.drivers.ConnectionJDBC.java; |
|
1100 |
closeConnection(conn); |
|
1101 |
throw new DBException(e); |
|
1102 |
} |
|
1103 |
finally{ |
|
1104 |
closeStatement(st); |
|
1105 |
closeResultSet(rs); |
|
1106 |
} |
|
1107 |
} |
|
1066 | 1108 |
|
1109 |
/** |
|
1110 |
* |
|
1111 |
* @param tableName |
|
1112 |
* @return a string with the schema and the tableName quoted |
|
1113 |
*/ |
|
1114 |
private String tableNameToComposedTableName(String tableName) { |
|
1115 |
String composedTableName = null; |
|
1116 |
// \u002E = unicode character for . |
|
1117 |
String[] tokens = tableName.trim().replace("\"", "").split("\\u002E"); |
|
1118 |
|
|
1119 |
if (tokens.length == 1) { |
|
1120 |
composedTableName = "\"" + tokens[0] + "\""; |
|
1121 |
|
|
1122 |
} else if (tokens.length == 2) { |
|
1123 |
composedTableName = "\"" + tokens[0] + "\".\"" + tokens[1] + "\""; |
|
1124 |
} else { |
|
1125 |
// this is a not predictable case, so we return the same |
|
1126 |
composedTableName = tableName; |
|
1127 |
} |
|
1128 |
|
|
1129 |
return composedTableName; |
|
1130 |
} |
|
1131 |
|
|
1132 |
/** |
|
1133 |
* Close a ResultSet |
|
1134 |
* @param rs, the resultset to be closed |
|
1135 |
* @return true if the resulset was correctly closed. false in any other case |
|
1136 |
*/ |
|
1137 |
public boolean closeResultSet(ResultSet rs) { |
|
1138 |
boolean error = false; |
|
1139 |
|
|
1140 |
if (rs != null) { |
|
1141 |
try { |
|
1142 |
rs.close(); |
|
1143 |
error = true; |
|
1144 |
} catch (SQLException e) { |
|
1145 |
logger.error(e.getMessage(), e); |
|
1146 |
} |
|
1147 |
} |
|
1148 |
|
|
1149 |
return error; |
|
1150 |
} |
|
1151 |
|
|
1152 |
/** |
|
1153 |
* Close a Statement |
|
1154 |
* @param st, the statement to be closed |
|
1155 |
* @return true if the statement was correctly closed, false in any other case |
|
1156 |
*/ |
|
1157 |
public boolean closeStatement(Statement st) { |
|
1158 |
boolean error = false; |
|
1159 |
|
|
1160 |
if (st != null) { |
|
1161 |
try { |
|
1162 |
st.close(); |
|
1163 |
error = true; |
|
1164 |
} catch (SQLException e) { |
|
1165 |
logger.error(e.getMessage(), e); |
|
1166 |
} |
|
1167 |
} |
|
1168 |
|
|
1169 |
return error; |
|
1170 |
} |
|
1171 |
|
|
1172 |
/** |
|
1173 |
* Close a Connection |
|
1174 |
* @param conn, the connection to be closed |
|
1175 |
* @return true if the connection was correctly closed, false in any other case |
|
1176 |
*/ |
|
1177 |
public boolean closeConnection(IConnection conn) { |
|
1178 |
boolean error = false; |
|
1179 |
|
|
1180 |
if (conn != null) { |
|
1181 |
try { |
|
1182 |
conn.close(); |
|
1183 |
error = true; |
|
1184 |
} catch (DBException e) { |
|
1185 |
logger.error(e.getMessage(), e); |
|
1186 |
} |
|
1187 |
} |
|
1188 |
|
|
1189 |
return error; |
|
1190 |
} |
|
1191 |
|
|
1067 | 1192 |
} |
Also available in: Unified diff