use of org.apache.sis.internal.geoapi.filter.SpatialOperatorName in project sis by apache.
the class SelectionClauseWriter method removeUnsupportedFunctions.
/**
* Returns a writer without the functions that are unsupported by the database software.
* If the database supports all functions, then this method returns {@code this}.
* Otherwise it returns a copy of {@code this} with unsupported functions removed.
* This method should be invoked at most once for a {@link Database} instance.
*
* @param database information about the database software.
* @return a writer with unsupported functions removed.
*/
final SelectionClauseWriter removeUnsupportedFunctions(final Database<?> database) {
final Map<String, SpatialOperatorName> unsupported = new HashMap<>();
try (Connection c = database.source.getConnection()) {
final DatabaseMetaData metadata = c.getMetaData();
/*
* Get the names of all spatial functions for which a handler is registered.
* All those handlers should be instances of `Function`, otherwise we do not
* know how to determine whether the function is supported or not.
*/
final boolean lowerCase = metadata.storesLowerCaseIdentifiers();
final boolean upperCase = metadata.storesUpperCaseIdentifiers();
for (final SpatialOperatorName type : SpatialOperatorName.values()) {
final BiConsumer<Filter<AbstractFeature>, SelectionClause> function = getFilterHandler(type);
if (function instanceof Function) {
String name = ((Function) function).name;
if (lowerCase)
name = name.toLowerCase(Locale.US);
if (upperCase)
name = name.toUpperCase(Locale.US);
unsupported.put(name, type);
}
}
/*
* Remove from above map all functions that are supported by the database.
* This list is potentially large so we do not put those items in a map.
*/
final String pattern = (lowerCase ? "st_%" : "ST\\_%").replace("\\", metadata.getSearchStringEscape());
try (ResultSet r = metadata.getFunctions(database.catalogOfSpatialTables, database.schemaOfSpatialTables, pattern)) {
while (r.next()) {
unsupported.remove(r.getString("FUNCTION_NAME"));
}
}
} catch (SQLException e) {
/*
* If this exception happens before `unsupported` entries were removed,
* this is equivalent to assuming that all functions are unsupported.
*/
database.listeners.warning(e);
}
/*
* Remaining functions are unsupported functions.
*/
if (unsupported.isEmpty()) {
return this;
}
final SelectionClauseWriter copy = duplicate();
copy.removeFilterHandlers(unsupported.values());
return copy;
}
Aggregations