filter.rs

Overview

This file implements filtering utilities designed to construct SQL-like WHERE clauses dynamically from structured filter inputs. It defines several typed filter structs (for booleans, floats, integers, and strings) that can be serialized and converted into SQL condition expressions. The core functionality is encapsulated in a trait that converts filter criteria into SQL condition strings, supports null-value pruning, and handles nested logical operators such as AND and OR.

The filtering constructs are intended for use in query-building contexts where dynamic query conditions are required based on user input or API parameters.


Core Components

Trait: WhereOp

Purpose

Provides methods to manipulate JSON-like filter data and convert it into SQL WHERE clause fragments.

Methods


Struct: Filter

An empty struct implementing the WhereOp trait to provide default behavior for filtering operations. It serves as the main utility to invoke filtering methods.


Filter Structs for Different Data Types

These structs represent typed filters that specify possible operations on fields of different primitive types. Each struct derives serialization traits and is compatible with GraphQL input objects.

Each filter type also has an associated optional alias type, e.g., OptBooleanFilter = Option<BooleanFilter>.


Important Implementation Details


Interaction with Other Parts of the System


Usage Examples

// Create a filter to select rows where `age` is greater than 30
let int_filter = IntFilter {
    gt: Some(30),
    ..Default::default()
};
let where_clause = int_filter.to_where(); // Some("WHERE age > 30")

// Create a filter to select rows where `status` is either "active" or "pending"
let string_filter = StringFilter {
    include: Some(vec![Some("active".to_string()), Some("pending".to_string())]),
    ..Default::default()
};
let where_clause = string_filter.to_where(); // Some("WHERE status IN (\"active\",\"pending\")")

Testing

The included tests module contains unit tests validating:


Diagram: Filter Module Structure

classDiagram
class WhereOp {
+skip_nulls()
+into_str()
+to_where()
}
class Filter {
}
Filter ..|> WhereOp
class BooleanFilter {
+eq: Option<bool>
+ne: Option<bool>
}
class FloatFilter {
+eq: Option<f64>
+ne: Option<f64>
+gt: Option<f64>
+lt: Option<f64>
+ge: Option<f64>
+le: Option<f64>
+include: Option<Vec<Option<f64>>>
+notIn: Option<Vec<Option<f64>>>
}
class IntFilter {
+eq: Option<i32>
+ne: Option<i32>
+gt: Option<i32>
+lt: Option<i32>
+ge: Option<i32>
+le: Option<i32>
+include: Option<Vec<Option<i32>>>
+notIn: Option<Vec<Option<i32>>>
}
class StringFilter {
+eq: Option<String>
+ne: Option<String>
+ge: Option<String>
+le: Option<String>
+include: Option<Vec<Option<String>>>
+notIn: Option<Vec<Option<String>>>
}
WhereOp <|.. Filter

This diagram illustrates the key trait and its implementation, along with the various filter types that provide the input data structure for query filtering.