protected String buildSqlQuery(Query query) throws IOException {
        StringBuilder sql = new StringBuilder("SELECT ");
        
        //column names
        selectColumns(getSchema(), null, query, sql);
        sql.setLength(sql.length() - 1);
        
        // from
        sql.append(" FROM ");
        sql.append(typeName);
        
        //filtering
        Filter filter = query.getFilter();
        if (filter != null && !Filter.INCLUDE.equals(filter)) {            
            sql.append(" WHERE ");
            
            //encode filter
            CoordinateReferenceSystem queryCrs = (query.getCoordinateSystem() != null) ? query.getCoordinateSystem() : query.getCoordinateSystemReproject();
            filter(getSchema(), filter, sql, queryCrs);
        }

        //sorting
        sort(getSchema(), query.getSortBy(), null, sql);
        
        // finally encode limit/offset, if necessary
        applyLimitOffset(sql, query);
        
        return sql.toString();
    }