Search in sources :

Example 1 with SpatialOperatorName

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;
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseMetaData(java.sql.DatabaseMetaData) SpatialOperatorName(org.apache.sis.internal.geoapi.filter.SpatialOperatorName) Filter(org.apache.sis.filter.Filter) ResultSet(java.sql.ResultSet)

Aggregations

Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 Filter (org.apache.sis.filter.Filter)1 SpatialOperatorName (org.apache.sis.internal.geoapi.filter.SpatialOperatorName)1