FatEcto.Query.OperatorApplier (FatEcto v1.4.0)

View Source

Provides helper functions to apply dynamic query operators for Ecto queries.

This module centralizes the logic for applying various query operators such as $LIKE, $ILIKE, $LT, $GT, etc., as well as their negated counterparts (e.g., $NOT_LIKE, $NOT_ILIKE). It is designed to work with FatDynamics and FatNotDynamics modules to generate dynamic Ecto query conditions.

Supported Operators

Positive Operators

  • $LIKE: Matches a pattern in a string.
  • $NOT_LIKE: Excludes rows that match a pattern in a string.
  • $ILIKE: Case-insensitive match of a pattern in a string.
  • $NOT_ILIKE: Case-insensitive exclusion of rows that match a pattern in a string.
  • $NULL: Matches rows where the field is null.
  • $NOT_NULL: Matches rows where the field is not null.
  • $CAST_TO_DATE_EQUAL: Casts field to date and matches a specific date value.
  • $CAST_TO_DATE_NOT_EQUAL: Casts field to date and excludes a specific date value.
  • $CAST_TO_DATE_GTE: Casts field to date and matches dates greater than or equal to a value.
  • $CAST_TO_DATE_GT: Casts field to date and matches dates greater than a value.
  • $CAST_TO_DATE_LT: Casts field to date and matches dates less than a value.
  • $CAST_TO_DATE_LTE: Casts field to date and matches dates less than or equal to a value.
  • $LT: Less than.
  • $LTE: Less than or equal to.
  • $GT: Greater than.
  • $GTE: Greater than or equal to.
  • $BETWEEN: Matches values between a range (exclusive).
  • $BETWEEN_EQUAL: Matches values between a range (inclusive).
  • $NOT_BETWEEN: Excludes values between a range (exclusive).
  • $NOT_BETWEEN_EQUAL: Excludes values between a range (inclusive).
  • $IN: Matches values in a list.
  • $NOT_IN: Excludes values in a list.
  • $EQUAL: Matches a specific value.
  • $NOT_EQUAL: Excludes a specific value.

Negated Operators (for $NOT conditions)

  • $LIKE: Negates the $LIKE condition.
  • $ILIKE: Negates the $ILIKE condition.
  • $LT: Negates the $LT condition.
  • $LTE: Negates the $LTE condition.
  • $GT: Negates the $GT condition.
  • $GTE: Negates the $GTE condition.
  • $EQUAL: Negates the $EQUAL condition.

Join Filters (Named Bindings)

All operators support an optional binding parameter for filtering on joined tables. When a binding is provided, the dynamic uses a named binding instead of the default [q].

# Direct filter (no binding)
OperatorApplier.apply_operator("$EQUAL", :name, "John")
# => dynamic([q], q.name == ^"John")

# Join filter (with binding)
OperatorApplier.apply_operator("$EQUAL", :subject_id, "uuid", :classifications)
# => dynamic([classifications: j], j.subject_id == ^"uuid")

Usage

This module is typically used internally by FatEcto.FatQuery.FatWhere and FatEcto.FatQuery.WhereOr to construct dynamic queries. It abstracts away the complexity of applying operators and ensures consistency across the codebase.

Example

# Applying a `$LIKE` operator
OperatorApplier.apply_operator("$LIKE", :name, "%John%")

# Applying a negated `$EQUAL` operator
OperatorApplier.apply_not_condition("$EQUAL", :age, 30)

# Applying a `$EQUAL` operator on a joined table
OperatorApplier.apply_operator("$EQUAL", :subject_id, "uuid", :classifications)

Summary

Functions

allowed_not_operators()

@spec allowed_not_operators() :: [String.t(), ...]

allowed_operators()

@spec allowed_operators() :: [String.t(), ...]

apply_nil_operator(operator, field)

@spec apply_nil_operator(String.t(), atom()) :: nil | Ecto.Query.dynamic_expr()

apply_not_condition(arg1, field, value)

@spec apply_not_condition(String.t(), atom(), any()) ::
  nil | Ecto.Query.dynamic_expr()

apply_operator(operator, field, value, binding \\ nil)

@spec apply_operator(String.t(), atom() | String.t(), any(), atom() | nil) ::
  nil | Ecto.Query.dynamic_expr()