compileCondition function

CompiledFilter compileCondition(
  1. FilterCondition condition,
  2. ColumnType? type
)

Compiles one condition, coercing its operands to type when it is known.

Exposed for a data source that compiles conditions itself; most callers want Filter.compile.

Implementation

CompiledFilter compileCondition(FilterCondition condition, ColumnType? type) {
  if (condition.isEmpty) return const _AlwaysTrue();
  final field = condition.field;
  final folded = !condition.caseSensitive;
  final dateOnly = condition.dateOnly;

  Object? coerce(Object? operand) =>
      coerceFilterOperand(operand, type, dateOnly: dateOnly);

  int compare(Object? a, Object? b) {
    if (dateOnly && (a is DateTime || b is DateTime)) {
      return compareValues(_dateOnly(a), _dateOnly(b));
    }
    return folded ? compareValuesFolded(a, b) : compareValues(a, b);
  }

  Object? normalise(Object? value) =>
      dateOnly && value is DateTime ? _dateOnly(value) : value;

  final operand = coerce(condition.value);
  final second = coerce(condition.secondValue);

  switch (condition.operator) {
    case FilterOperator.equals:
      return _CompiledPredicate(field, (a) => compare(a, operand) == 0);
    case FilterOperator.notEquals:
      return _CompiledPredicate(field, (a) => compare(a, operand) != 0);
    case FilterOperator.contains:
      final needle = _textOf(operand, folded);
      return _CompiledPredicate(
        field,
        (a) => _textOf(a, folded).contains(needle),
      );
    case FilterOperator.notContains:
      final needle = _textOf(operand, folded);
      return _CompiledPredicate(
        field,
        (a) => !_textOf(a, folded).contains(needle),
      );
    case FilterOperator.startsWith:
      final needle = _textOf(operand, folded);
      return _CompiledPredicate(
        field,
        (a) => _textOf(a, folded).startsWith(needle),
      );
    case FilterOperator.endsWith:
      final needle = _textOf(operand, folded);
      return _CompiledPredicate(
        field,
        (a) => _textOf(a, folded).endsWith(needle),
      );
    case FilterOperator.greaterThan:
      return _CompiledPredicate(
        field,
        (a) => a != null && compare(a, operand) > 0,
      );
    case FilterOperator.greaterOrEqual:
      return _CompiledPredicate(
        field,
        (a) => a != null && compare(a, operand) >= 0,
      );
    case FilterOperator.lessThan:
      return _CompiledPredicate(
        field,
        (a) => a != null && compare(a, operand) < 0,
      );
    case FilterOperator.lessOrEqual:
      return _CompiledPredicate(
        field,
        (a) => a != null && compare(a, operand) <= 0,
      );
    case FilterOperator.between:
      // Bounds typed backwards still describe the same range.
      final ordered = compare(operand, second) <= 0;
      final low = ordered ? operand : second;
      final high = ordered ? second : operand;
      return _CompiledPredicate(
        field,
        (a) => a != null && compare(a, low) >= 0 && compare(a, high) <= 0,
      );
    case FilterOperator.isIn:
      final raw = condition.value;
      if (raw is! Iterable) return const _AlwaysTrue();
      // The whole point of compiling: one hash lookup per row instead of a
      // walk over every selected value.
      final keys = HashSet<Object?>();
      for (final value in raw) {
        keys.add(_setKey(normalise(coerce(value)), folded));
      }
      return _CompiledPredicate(
        field,
        (a) => keys.contains(_setKey(normalise(a), folded)),
      );
    case FilterOperator.isNull:
      return _CompiledPredicate(field, (a) => a == null);
    case FilterOperator.isNotNull:
      return _CompiledPredicate(field, (a) => a != null);
    case FilterOperator.isEmpty:
      return _CompiledPredicate(
        field,
        (a) => a == null || _textOf(a, false).isEmpty,
      );
    case FilterOperator.isNotEmpty:
      return _CompiledPredicate(
        field,
        (a) => a != null && _textOf(a, false).isNotEmpty,
      );
  }
}