//! Database query parameter types. //! //! This module contains types used for building dynamic SQL queries, //! including WHERE conditions, sorting, and filtering. use serde::{Deserialize, Serialize}; use serde_json::Value; use std::collections::HashMap; /// Represents a single condition in a WHERE clause. /// /// Supports multiple types of operations: /// - `eq`: Exact match (field = value) /// - `in_values`: Array membership (field IN (...)) /// - `contains`: Pattern matching (field ILIKE '%value%') /// - `fuzzy`: Fuzzy text matching (field % value) /// - `gt`: Greater than (field > value) /// - `gte`: Greater than or equal (field >= value) /// - `lt`: Less than (field < value) /// - `lte`: Less than or equal (field <= value) #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct WhereCondition { pub eq: Option, #[serde(rename = "in")] pub in_values: Option>, pub contains: Option, pub fuzzy: Option, pub gt: Option, pub gte: Option, pub lt: Option, pub lte: Option, } /// Represents a complete WHERE clause with AND/OR conditions. /// /// Supports both flat conditions (legacy) and nested and/or arrays (new). /// The main conditions map is combined with AND logic. /// The or_conditions map (if present) is combined with OR logic /// and the entire OR group is ANDed with the main conditions. /// /// New nested format supports arbitrarily nestable and/or: /// ```json /// { /// "and": [ /// { /// "or": [ /// { "artist": { "contains": "pearl jam" } }, /// { "genre": { "contains": "rock" } } /// ] /// }, /// { /// "and": [ /// { "uri": { "contains": "app.bsky" } }, /// { "uri": { "contains": "post" } } /// ] /// }, /// { "year": { "gte": 2000 } } /// ] /// } /// ``` #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct WhereClause { #[serde(flatten)] pub conditions: HashMap, #[serde(rename = "$or")] pub or_conditions: Option>, /// Nested AND conditions - array of WhereClause objects all ANDed together #[serde(rename = "and", skip_serializing_if = "Option::is_none")] pub and: Option>, /// Nested OR conditions - array of WhereClause objects all ORed together #[serde(rename = "or", skip_serializing_if = "Option::is_none")] pub or: Option>, } /// Represents a field to sort by with direction. /// /// Used for multi-field sorting in queries. #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct SortField { pub field: String, pub direction: String, }