Search in sources :

Example 1 with Operator

use of com.google.firebase.firestore.core.FieldFilter.Operator in project firebase-android-sdk by firebase.

the class Query method validateNewFieldFilter.

/**
 * Checks that adding the given field filter to the given query yields a valid query
 */
private void validateNewFieldFilter(com.google.firebase.firestore.core.Query query, com.google.firebase.firestore.core.FieldFilter fieldFilter) {
    Operator filterOp = fieldFilter.getOperator();
    if (fieldFilter.isInequality()) {
        com.google.firebase.firestore.model.FieldPath existingInequality = query.inequalityField();
        com.google.firebase.firestore.model.FieldPath newInequality = fieldFilter.getField();
        if (existingInequality != null && !existingInequality.equals(newInequality)) {
            throw new IllegalArgumentException(String.format("All where filters with an inequality (notEqualTo, notIn, lessThan, " + "lessThanOrEqualTo, greaterThan, or greaterThanOrEqualTo) must be on the " + "same field. But you have filters on '%s' and '%s'", existingInequality.canonicalString(), newInequality.canonicalString()));
        }
        com.google.firebase.firestore.model.FieldPath firstOrderByField = query.getFirstOrderByField();
        if (firstOrderByField != null) {
            validateOrderByFieldMatchesInequality(firstOrderByField, newInequality);
        }
    }
    Operator conflictingOp = findFilterWithOperator(query.getFilters(), conflictingOps(filterOp));
    if (conflictingOp != null) {
        // We special case when it's a duplicate op to give a slightly clearer error message.
        if (conflictingOp == filterOp) {
            throw new IllegalArgumentException("Invalid Query. You cannot use more than one '" + filterOp.toString() + "' filter.");
        } else {
            throw new IllegalArgumentException("Invalid Query. You cannot use '" + filterOp.toString() + "' filters with '" + conflictingOp.toString() + "' filters.");
        }
    }
}
Also used : Operator(com.google.firebase.firestore.core.FieldFilter.Operator)

Example 2 with Operator

use of com.google.firebase.firestore.core.FieldFilter.Operator in project firebase-android-sdk by firebase.

the class Query method parseFieldFilter.

/**
 * Takes a {@link Filter.UnaryFilter} object, parses the value object and returns a new {@link
 * FieldFilter} for the field, operator, and parsed value.
 *
 * @param fieldFilterData The Filter.UnaryFilter object to parse.
 * @return The created {@link FieldFilter}.
 */
private FieldFilter parseFieldFilter(Filter.UnaryFilter fieldFilterData) {
    FieldPath fieldPath = fieldFilterData.getField();
    Operator op = fieldFilterData.getOperator();
    Object value = fieldFilterData.getValue();
    checkNotNull(fieldPath, "Provided field path must not be null.");
    checkNotNull(op, "Provided op must not be null.");
    Value fieldValue;
    com.google.firebase.firestore.model.FieldPath internalPath = fieldPath.getInternalPath();
    if (internalPath.isKeyField()) {
        if (op == Operator.ARRAY_CONTAINS || op == Operator.ARRAY_CONTAINS_ANY) {
            throw new IllegalArgumentException("Invalid query. You can't perform '" + op.toString() + "' queries on FieldPath.documentId().");
        } else if (op == Operator.IN || op == Operator.NOT_IN) {
            validateDisjunctiveFilterElements(value, op);
            ArrayValue.Builder referenceList = ArrayValue.newBuilder();
            for (Object arrayValue : (List) value) {
                referenceList.addValues(parseDocumentIdValue(arrayValue));
            }
            fieldValue = Value.newBuilder().setArrayValue(referenceList).build();
        } else {
            fieldValue = parseDocumentIdValue(value);
        }
    } else {
        if (op == Operator.IN || op == Operator.NOT_IN || op == Operator.ARRAY_CONTAINS_ANY) {
            validateDisjunctiveFilterElements(value, op);
        }
        fieldValue = firestore.getUserDataReader().parseQueryValue(value, op == Operator.IN || op == Operator.NOT_IN);
    }
    FieldFilter filter = FieldFilter.create(fieldPath.getInternalPath(), op, fieldValue);
    return filter;
}
Also used : Operator(com.google.firebase.firestore.core.FieldFilter.Operator) FieldFilter(com.google.firebase.firestore.core.FieldFilter) Value(com.google.firestore.v1.Value) ArrayValue(com.google.firestore.v1.ArrayValue)

Aggregations

Operator (com.google.firebase.firestore.core.FieldFilter.Operator)2 FieldFilter (com.google.firebase.firestore.core.FieldFilter)1 ArrayValue (com.google.firestore.v1.ArrayValue)1 Value (com.google.firestore.v1.Value)1