params.put(PORT.key, port);
params.put(USER.key, username);
params.put(PASSWD.key, password);
params.put(DATABASE.key, database);
BasicDataSource dataSource = null;
Connection connection = null;
try {
dataSource = factory.createDataSource(params);
connection = dataSource.getConnection();
Statement statement = connection.createStatement();
if (!hasWritableTable("geometry_columns", "f_geometry_column", statement)) { //$NON-NLS-1$
error = "Database is not a Postgis Database.\nThe 'geometry_columns' table is either missing or not accessible";
return;
}
if (!hasWritableTable("spatial_ref_sys", "srid", statement)) { //$NON-NLS-1$
error = "Database is not a Postgis Database.\nThe 'srid' table is either missing or not accessible";
return;
}
// Pair is schema, table name
List<Pair<String, String>> tableNames = new ArrayList<Pair<String, String>>();
ResultSet resultSet = statement
.executeQuery("SELECT schemaname, tablename FROM pg_tables ORDER BY schemaname, tablename;");
while( resultSet.next() ) {
String schema = resultSet.getString("schemaname"); //$NON-NLS-1$
String table = resultSet.getString("tablename"); //$NON-NLS-1$
tableNames.add(Pair.create(schema, table));
}
Collection<TableDescriptor> results = lookupGeometryColumn(tableNames, connection);
tables.addAll(results);
statement.close();
} catch (SQLException e) {
error = "An error occurred when querying the database about the data it contains. Please talk to the administrator: "
+ e.getMessage();
}
catch (IOException io ){
error = "An error occurred when querying the database about the data it contains. Please talk to the administrator: "
+ io.getMessage();
} finally {
if( connection != null ){
connection.close();
}
if( dataSource != null ){
dataSource.close();
}
}
}