Skip to main content

bottle_orm/
query_builder.rs

1//! # Query Builder Module
2//!
3//! This module provides a fluent interface for constructing and executing SQL queries.
4//! It handles SELECT, INSERT, filtering (WHERE), pagination (LIMIT/OFFSET), and ordering operations
5//! with type-safe parameter binding across different database drivers.
6//!
7//! ## Features
8//!
9//! - **Fluent API**: Chainable methods for building complex queries
10//! - **Type-Safe Binding**: Automatic parameter binding with support for multiple types
11//! - **Multi-Driver Support**: Works with PostgreSQL, MySQL, and SQLite
12//! - **UUID Support**: Full support for UUID versions 1-7
13//! - **Pagination**: Built-in LIMIT/OFFSET support with helper methods
14//! - **Custom Filters**: Support for manual SQL construction with closures
15//!
16//! ## Example Usage
17//!
18//! ```rust,ignore
19//! use bottle_orm::{Database, Model};
20//! 
21//!
22//! // Simple query
23//! let users: Vec<User> = db.model::<User>()
24//!     .filter("age", ">=", 18)
25//!     .order("created_at DESC")
26//!     .limit(10)
27//!     .scan()
28//!     .await?;
29//!
30//! // Query with UUID filter
31//! let user_id = Uuid::new_v4();
32//! let user: User = db.model::<User>()
33//!     .filter("id", "=", user_id)
34//!     .first()
35//!     .await?;
36//!
37//! // Insert a new record
38//! let new_user = User {
39//!     id: Uuid::new_v7(uuid::Timestamp::now(uuid::NoContext)),
40//!     username: "john_doe".to_string(),
41//!     age: 25,
42//! };
43//! db.model::<User>().insert(&new_user).await?;
44//! ```
45
46// ============================================================================
47// External Crate Imports
48// ============================================================================
49
50use futures::future::BoxFuture;
51use heck::ToSnakeCase;
52use sqlx::{Any, Arguments, Decode, Encode, Type, any::AnyArguments};
53use std::marker::PhantomData;
54
55
56// ============================================================================
57// Internal Crate Imports
58// ============================================================================
59
60use crate::{
61    AnyImpl, Error,
62    any_struct::FromAnyRow,
63    database::{Connection, Drivers},
64    model::{ColumnInfo, Model},
65    temporal::{self, is_temporal_type},
66    value_binding::ValueBinder,
67};
68
69// ============================================================================
70// Type Aliases
71// ============================================================================
72
73/// A type alias for filter closures that support manual SQL construction and argument binding.
74///
75/// Filter functions receive the following parameters:
76/// 1. `&mut String` - The SQL query buffer being built
77/// 2. `&mut AnyArguments` - The argument container for binding values
78/// 3. `&Drivers` - The current database driver (determines placeholder syntax)
79/// 4. `&mut usize` - The argument counter (for PostgreSQL `$n` placeholders)
80///
81/// ## Example
82///
83/// ```rust,ignore
84/// let custom_filter: FilterFn = Box::new(|query, args, driver, counter| {
85///     query.push_str(" AND age > ");
86///     match driver {
87///         Drivers::Postgres => {
88///             query.push_str(&format!("${}", counter));
89///             *counter += 1;
90///         }
91///         _ => query.push('?'),
92///     }
93///     args.add(18);
94/// });
95/// });\n/// ```
96pub type FilterFn = Box<dyn Fn(&mut String, &mut AnyArguments<'_>, &Drivers, &mut usize) + Send + Sync>;
97
98// ============================================================================
99// Comparison Operators Enum
100// ============================================================================
101
102/// Type-safe comparison operators for filter conditions.
103///
104/// Use these instead of string operators for autocomplete support and type safety.
105///
106/// # Example
107///
108/// ```rust,ignore
109/// use bottle_orm::Op;
110///
111/// db.model::<User>()
112///     .filter(user_fields::AGE, Op::Gte, 18)
113///     .filter(user_fields::NAME, Op::Like, "%John%")
114///     .scan()
115///     .await?;
116/// ```
117#[derive(Debug, Clone, Copy, PartialEq, Eq)]
118pub enum Op {
119    /// Equal: `=`
120    Eq,
121    /// Not Equal: `!=` or `<>`
122    Ne,
123    /// Greater Than: `>`
124    Gt,
125    /// Greater Than or Equal: `>=`
126    Gte,
127    /// Less Than: `<`
128    Lt,
129    /// Less Than or Equal: `<=`
130    Lte,
131    /// SQL LIKE pattern matching
132    Like,
133    /// SQL NOT LIKE pattern matching
134    NotLike,
135    /// SQL IN (for arrays/lists)
136    In,
137    /// SQL NOT IN
138    NotIn,
139    /// SQL BETWEEN
140    Between,
141    /// SQL NOT BETWEEN
142    NotBetween,
143}
144
145impl Op {
146    /// Converts the operator to its SQL string representation.
147    pub fn as_sql(&self) -> &'static str {
148        match self {
149            Op::Eq => "=",
150            Op::Ne => "!=",
151            Op::Gt => ">",
152            Op::Gte => ">=",
153            Op::Lt => "<",
154            Op::Lte => "<=",
155            Op::Like => "LIKE",
156            Op::NotLike => "NOT LIKE",
157            Op::In => "IN",
158            Op::NotIn => "NOT IN",
159            Op::Between => "BETWEEN",
160            Op::NotBetween => "NOT BETWEEN",
161        }
162    }
163}
164
165// ============================================================================
166// QueryBuilder Struct
167// ============================================================================
168
169/// A fluent Query Builder for constructing SQL queries.
170///
171/// `QueryBuilder` provides a type-safe, ergonomic interface for building and executing
172/// SQL queries across different database backends. It supports filtering, ordering,
173/// pagination, and both SELECT and INSERT operations.
174///
175/// ## Type Parameter
176///
177/// * `'a` - Lifetime of the database reference (used for PhantomData)
178/// * `T` - The Model type this query operates on
179/// * `E` - The connection type (Database or Transaction)
180///
181/// ## Fields
182///
183/// * `db` - Reference to the database connection pool or transaction
184/// * `table_name` - Static string containing the table name
185/// * `columns_info` - Metadata about each column in the table
186/// * `columns` - List of column names in snake_case format
187/// * `select_columns` - Specific columns to select (empty = SELECT *)
188/// * `where_clauses` - List of filter functions to apply
189/// * `order_clauses` - List of ORDER BY clauses
190/// * `limit` - Maximum number of rows to return
191/// * `offset` - Number of rows to skip (for pagination)
192/// * `_marker` - PhantomData to bind the generic type T
193pub struct QueryBuilder<T, E> {
194    /// Reference to the database connection pool
195    pub(crate) tx: E,
196
197    /// Database driver type
198    pub(crate) driver: Drivers,
199
200    /// Name of the database table (in original case)
201    pub(crate) table_name: &'static str,
202
203    pub(crate) alias: Option<String>,
204
205    /// Metadata information about each column
206    pub(crate) columns_info: Vec<ColumnInfo>,
207
208    /// List of column names (in snake_case)
209    pub(crate) columns: Vec<String>,
210
211    /// Specific columns to select (empty means SELECT *)
212    pub(crate) select_columns: Vec<String>,
213
214    /// Collection of WHERE clause filter functions
215    pub(crate) where_clauses: Vec<FilterFn>,
216
217    /// Collection of ORDER BY clauses
218    pub(crate) order_clauses: Vec<String>,
219
220    /// Collection of JOIN clause to filter entry tables
221    pub(crate) joins_clauses: Vec<FilterFn>,
222
223    /// Map of table names to their aliases used in JOINS
224    pub(crate) join_aliases: std::collections::HashMap<String, String>,
225
226    /// Maximum number of rows to return (LIMIT)
227    pub(crate) limit: Option<usize>,
228
229    /// Number of rows to skip (OFFSET)
230    pub(crate) offset: Option<usize>,
231
232    /// Activate debug mode in query
233    pub(crate) debug_mode: bool,
234
235    /// Clauses for GROUP BY
236    pub(crate) group_by_clauses: Vec<String>,
237
238    /// Clauses for HAVING
239    pub(crate) having_clauses: Vec<FilterFn>,
240
241    /// Distinct flag
242    pub(crate) is_distinct: bool,
243
244    /// Columns to omit from the query results (inverse of select_columns)
245    pub(crate) omit_columns: Vec<String>,
246
247    /// Whether to include soft-deleted records in query results
248    pub(crate) with_deleted: bool,
249
250    /// PhantomData to bind the generic type T
251    pub(crate) _marker: PhantomData<T>,
252}
253
254// ============================================================================
255// QueryBuilder Implementation
256// ============================================================================
257
258impl<T, E> QueryBuilder<T, E>
259where
260    T: Model + Send + Sync + Unpin,
261    E: Connection,
262{
263    // ========================================================================
264    // Constructor
265    // ========================================================================
266
267    /// Creates a new QueryBuilder instance.
268    ///
269    /// This constructor is typically called internally via `db.model::<T>()`.
270    /// You rarely need to call this directly.
271    ///
272    /// # Arguments
273    ///
274    /// * `db` - Reference to the database connection
275    /// * `table_name` - Name of the table to query
276    /// * `columns_info` - Metadata about table columns
277    /// * `columns` - List of column names
278    ///
279    /// # Returns
280    ///
281    /// A new `QueryBuilder` instance ready for query construction
282    ///
283    /// # Example
284    ///
285    /// ```rust,ignore
286    /// // Usually called via db.model::<User>()
287    /// let query = db.model::<User>();
288    /// ```
289    pub fn new(
290        tx: E,
291        driver: Drivers,
292        table_name: &'static str,
293        columns_info: Vec<ColumnInfo>,
294        columns: Vec<String>,
295    ) -> Self {
296        // Pre-populate omit_columns with globally omitted columns (from #[orm(omit)] attribute)
297        let omit_columns: Vec<String> =
298            columns_info.iter().filter(|c| c.omit).map(|c| c.name.to_snake_case()).collect();
299
300        Self {
301            tx,
302            alias: None,
303            driver,
304            table_name,
305            columns_info,
306            columns,
307            debug_mode: false,
308            select_columns: Vec::new(),
309            where_clauses: Vec::new(),
310            order_clauses: Vec::new(),
311            joins_clauses: Vec::new(),
312            join_aliases: std::collections::HashMap::new(),
313            group_by_clauses: Vec::new(),
314            having_clauses: Vec::new(),
315            is_distinct: false,
316            omit_columns,
317            limit: None,
318            offset: None,
319            with_deleted: false,
320            _marker: PhantomData,
321        }
322    }
323
324    /// Returns the table name or alias if set.
325    pub(crate) fn get_table_identifier(&self) -> String {
326        self.alias.clone().unwrap_or_else(|| self.table_name.to_snake_case())
327    }
328
329    // ========================================================================
330    // Query Building Methods
331    // ========================================================================
332
333    /// Internal helper to add a WHERE clause with a specific join operator.
334    fn filter_internal<V>(mut self, joiner: &str, col: &'static str, op: Op, value: V) -> Self
335    where
336        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
337    {
338        let op_str = op.as_sql();
339        let table_id = self.get_table_identifier();
340        // Check if the column exists in the main table to avoid ambiguous references in JOINS
341        let is_main_col = self.columns.contains(&col.to_snake_case());
342        let joiner_owned = joiner.to_string();
343        let clause: FilterFn = Box::new(move |query, args, driver, arg_counter| {
344            query.push_str(&joiner_owned);
345            if let Some((table, column)) = col.split_once(".") {
346                // If explicit table prefix is provided, use it
347                query.push_str(&format!("\"{}\".\"{}\"", table, column));
348            } else if is_main_col {
349                // If it's a known column of the main table, apply the table name/alias prefix
350                query.push_str(&format!("\"{}\".\"{}\"", table_id, col));
351            } else {
352                // Otherwise leave it unqualified so the DB can resolve it (or fail if ambiguous)
353                query.push_str(&format!("\"{}\"", col));
354            }
355            query.push(' ');
356            query.push_str(op_str);
357            query.push(' ');
358
359            // Handle different placeholder syntaxes based on database driver
360            match driver {
361                // PostgreSQL uses numbered placeholders: $1, $2, $3, ...
362                Drivers::Postgres => {
363                    query.push_str(&format!("${}", arg_counter));
364                    *arg_counter += 1;
365                }
366                // MySQL and SQLite use question mark placeholders: ?
367                _ => query.push('?'),
368            }
369
370            // Bind the value to the query
371            let _ = args.add(value.clone());
372        });
373
374        self.where_clauses.push(clause);
375        self
376    }
377
378    /// Adds a WHERE clause to the query.
379    ///
380    /// This method adds a filter condition to the query. Multiple filters can be chained
381    /// and will be combined with AND operators. The value is bound as a parameter to
382    /// prevent SQL injection.
383    ///
384    /// # Type Parameters
385    ///
386    /// * `V` - The type of the value to filter by. Must be encodable for SQL queries.
387    ///
388    /// # Arguments
389    ///
390    /// * `col` - The column name to filter on
391    /// * `op` - The comparison operator (e.g., "=", ">", "LIKE", "IN")
392    /// * `value` - The value to compare against
393    ///
394    /// # Example
395    ///
396    /// ```rust,ignore
397    /// query.filter("age", Op::Gte, 18)
398    /// ```
399    pub fn filter<V>(self, col: &'static str, op: Op, value: V) -> Self
400    where
401        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
402    {
403        self.filter_internal(" AND ", col, op, value)
404    }
405
406    /// Adds an OR WHERE clause to the query.
407    ///
408    /// # Example
409    ///
410    /// ```rust,ignore
411    /// query.filter("age", Op::Lt, 18).or_filter("active", Op::Eq, false)
412    /// ```
413    pub fn or_filter<V>(self, col: &'static str, op: Op, value: V) -> Self
414    where
415        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
416    {
417        self.filter_internal(" OR ", col, op, value)
418    }
419
420    /// Adds an AND NOT WHERE clause to the query.
421    pub fn not_filter<V>(self, col: &'static str, op: Op, value: V) -> Self
422    where
423        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
424    {
425        self.filter_internal(" AND NOT ", col, op, value)
426    }
427
428    /// Adds an OR NOT WHERE clause to the query.
429    pub fn or_not_filter<V>(self, col: &'static str, op: Op, value: V) -> Self
430    where
431        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
432    {
433        self.filter_internal(" OR NOT ", col, op, value)
434    }
435
436    /// Adds a BETWEEN clause to the query.
437    ///
438    /// # Arguments
439    ///
440    /// * `col` - The column name
441    /// * `start` - The start value of the range
442    /// * `end` - The end value of the range
443    ///
444    /// # Example
445    ///
446    /// ```rust,ignore
447    /// query.between("age", 18, 30)
448    /// // SQL: AND "age" BETWEEN 18 AND 30
449    /// ```
450    pub fn between<V>(mut self, col: &'static str, start: V, end: V) -> Self
451    where
452        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
453    {
454        let table_id = self.get_table_identifier();
455        let is_main_col = self.columns.contains(&col.to_snake_case());
456        let clause: FilterFn = Box::new(move |query, args, driver, arg_counter| {
457            query.push_str(" AND ");
458            if let Some((table, column)) = col.split_once(".") {
459                query.push_str(&format!("\"{}\".\"{}\"", table, column));
460            } else if is_main_col {
461                query.push_str(&format!("\"{}\".\"{}\"", table_id, col));
462            } else {
463                query.push_str(&format!("\"{}\"", col));
464            }
465            query.push_str(" BETWEEN ");
466
467            match driver {
468                Drivers::Postgres => {
469                    query.push_str(&format!("${} AND ${}", arg_counter, *arg_counter + 1));
470                    *arg_counter += 2;
471                }
472                _ => query.push_str("? AND ?"),
473            }
474
475            let _ = args.add(start.clone());
476            let _ = args.add(end.clone());
477        });
478        self.where_clauses.push(clause);
479        self
480    }
481
482    /// Adds an OR BETWEEN clause to the query.
483    ///
484    /// # Example
485    ///
486    /// ```rust,ignore
487    /// query.between("age", 18, 30).or_between("salary", 5000, 10000)
488    /// ```
489    pub fn or_between<V>(mut self, col: &'static str, start: V, end: V) -> Self
490    where
491        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
492    {
493        let table_id = self.get_table_identifier();
494        let is_main_col = self.columns.contains(&col.to_snake_case());
495        let clause: FilterFn = Box::new(move |query, args, driver, arg_counter| {
496            query.push_str(" OR ");
497            if let Some((table, column)) = col.split_once(".") {
498                query.push_str(&format!("\"{}\".\"{}\"", table, column));
499            } else if is_main_col {
500                query.push_str(&format!("\"{}\".\"{}\"", table_id, col));
501            } else {
502                query.push_str(&format!("\"{}\"", col));
503            }
504            query.push_str(" BETWEEN ");
505
506            match driver {
507                Drivers::Postgres => {
508                    query.push_str(&format!("${} AND ${}", arg_counter, *arg_counter + 1));
509                    *arg_counter += 2;
510                }
511                _ => query.push_str("? AND ?"),
512            }
513
514            let _ = args.add(start.clone());
515            let _ = args.add(end.clone());
516        });
517        self.where_clauses.push(clause);
518        self
519    }
520
521    /// Adds an IN list clause to the query.
522    ///
523    /// # Example
524    ///
525    /// ```rust,ignore
526    /// query.in_list("status", vec!["active", "pending"])
527    /// // SQL: AND "status" IN ('active', 'pending')
528    /// ```
529    pub fn in_list<V>(mut self, col: &'static str, values: Vec<V>) -> Self
530    where
531        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
532    {
533        if values.is_empty() {
534            // WHERE 1=0 to ensure empty result
535            let clause: FilterFn = Box::new(|query, _, _, _| {
536                query.push_str(" AND 1=0");
537            });
538            self.where_clauses.push(clause);
539            return self;
540        }
541
542        let table_id = self.get_table_identifier();
543        let is_main_col = self.columns.contains(&col.to_snake_case());
544        let clause: FilterFn = Box::new(move |query, args, driver, arg_counter| {
545            query.push_str(" AND ");
546            if let Some((table, column)) = col.split_once(".") {
547                query.push_str(&format!("\"{}\".\"{}\"", table, column));
548            } else if is_main_col {
549                query.push_str(&format!("\"{}\".\"{}\"", table_id, col));
550            } else {
551                query.push_str(&format!("\"{}\"", col));
552            }
553            query.push_str(" IN (");
554
555            let mut placeholders = Vec::new();
556            for _ in &values {
557                match driver {
558                    Drivers::Postgres => {
559                        placeholders.push(format!("${}", arg_counter));
560                        *arg_counter += 1;
561                    }
562                    _ => placeholders.push("?".to_string()),
563                }
564            }
565            query.push_str(&placeholders.join(", "));
566            query.push(')');
567
568            for val in &values {
569                let _ = args.add(val.clone());
570            }
571        });
572        self.where_clauses.push(clause);
573        self
574    }
575
576    /// Adds an OR IN list clause to the query.
577    pub fn or_in_list<V>(mut self, col: &'static str, values: Vec<V>) -> Self
578    where
579        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
580    {
581        if values.is_empty() {
582            return self;
583        }
584
585        let table_id = self.get_table_identifier();
586        let is_main_col = self.columns.contains(&col.to_snake_case());
587        let clause: FilterFn = Box::new(move |query, args, driver, arg_counter| {
588            query.push_str(" OR ");
589            if let Some((table, column)) = col.split_once(".") {
590                query.push_str(&format!("\"{}\".\"{}\"", table, column));
591            } else if is_main_col {
592                query.push_str(&format!("\"{}\".\"{}\"", table_id, col));
593            } else {
594                query.push_str(&format!("\"{}\"", col));
595            }
596            query.push_str(" IN (");
597
598            let mut placeholders = Vec::new();
599            for _ in &values {
600                match driver {
601                    Drivers::Postgres => {
602                        placeholders.push(format!("${}", arg_counter));
603                        *arg_counter += 1;
604                    }
605                    _ => placeholders.push("?".to_string()),
606                }
607            }
608            query.push_str(&placeholders.join(", "));
609            query.push(')');
610
611            for val in &values {
612                let _ = args.add(val.clone());
613            }
614        });
615        self.where_clauses.push(clause);
616        self
617    }
618
619    /// Groups filters inside parentheses with an AND operator.
620    pub fn group<F>(mut self, f: F) -> Self
621    where
622        F: FnOnce(Self) -> Self,
623    {
624        let old_clauses = std::mem::take(&mut self.where_clauses);
625        self = f(self);
626        let group_clauses = std::mem::take(&mut self.where_clauses);
627        self.where_clauses = old_clauses;
628
629        if !group_clauses.is_empty() {
630            let clause: FilterFn = Box::new(move |query, args, driver, arg_counter| {
631                query.push_str(" AND (1=1");
632                for c in &group_clauses {
633                    c(query, args, driver, arg_counter);
634                }
635                query.push_str(")");
636            });
637            self.where_clauses.push(clause);
638        }
639        self
640    }
641
642    /// Groups filters inside parentheses with an OR operator.
643    pub fn or_group<F>(mut self, f: F) -> Self
644    where
645        F: FnOnce(Self) -> Self,
646    {
647        let old_clauses = std::mem::take(&mut self.where_clauses);
648        self = f(self);
649        let group_clauses = std::mem::take(&mut self.where_clauses);
650        self.where_clauses = old_clauses;
651
652        if !group_clauses.is_empty() {
653            let clause: FilterFn = Box::new(move |query, args, driver, arg_counter| {
654                query.push_str(" OR (1=1");
655                for c in &group_clauses {
656                    c(query, args, driver, arg_counter);
657                }
658                query.push_str(")");
659            });
660            self.where_clauses.push(clause);
661        }
662        self
663    }
664
665    /// Adds a raw WHERE clause with a placeholder and a single value.
666    ///
667    /// This allows writing raw SQL conditions with a `?` placeholder.
668    /// To use multiple placeholders with different types, chain multiple `where_raw` calls.
669    ///
670    /// # Arguments
671    ///
672    /// * `sql` - Raw SQL string with one `?` placeholder (e.g., "age > ?")
673    /// * `value` - Value to bind
674    ///
675    /// # Example
676    ///
677    /// ```rust,ignore
678    /// db.model::<User>()
679    ///     .where_raw("name = ?", "Alice".to_string())
680    ///     .where_raw("age >= ?", 18)
681    ///     .scan()
682    ///     .await?;
683    /// ```
684    pub fn where_raw<V>(mut self, sql: &str, value: V) -> Self
685    where
686        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
687    {
688        self.where_clauses.push(self.create_raw_clause(" AND ", sql, value));
689        self
690    }
691
692    /// Adds a raw OR WHERE clause with a placeholder.
693    pub fn or_where_raw<V>(mut self, sql: &str, value: V) -> Self
694    where
695        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
696    {
697        self.where_clauses.push(self.create_raw_clause(" OR ", sql, value));
698        self
699    }
700
701    /// Internal helper to create a raw SQL clause with a single value.
702    fn create_raw_clause<V>(&self, joiner: &'static str, sql: &str, value: V) -> FilterFn
703    where
704        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
705    {
706        let sql_owned = sql.to_string();
707        Box::new(move |query, args, driver, arg_counter| {
708            query.push_str(joiner);
709            
710            let mut processed_sql = sql_owned.clone();
711            if let Some(pos) = processed_sql.find('?') {
712                let placeholder = match driver {
713                    Drivers::Postgres => {
714                        let p = format!("${}", arg_counter);
715                        *arg_counter += 1;
716                        p
717                    }
718                    _ => "?".to_string(),
719                };
720                processed_sql.replace_range(pos..pos + 1, &placeholder);
721            }
722            
723            query.push_str(&processed_sql);
724            let _ = args.add(value.clone());
725        })
726    }
727
728    /// Adds an equality filter to the query.
729    ///
730    /// This is a convenience wrapper around `filter()` for simple equality checks.
731    /// It is equivalent to calling `filter(col, "=", value)`.
732    ///
733    /// # Type Parameters
734    ///
735    /// * `V` - The type of the value to compare against.
736    ///
737    /// # Arguments
738    ///
739    /// * `col` - The column name to filter on.
740    /// * `value` - The value to match.
741    ///
742    /// # Example
743    ///
744    /// ```rust,ignore
745    /// // Equivalent to filter("age", Op::Eq, 18)
746    /// query.equals("age", 18)
747    /// ```
748    pub fn equals<V>(self, col: &'static str, value: V) -> Self
749    where
750        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
751    {
752        self.filter(col, Op::Eq, value)
753    }
754
755    /// Adds an ORDER BY clause to the query.
756    ///
757    /// Specifies the sort order for the query results. Multiple order clauses
758    /// can be added and will be applied in the order they were added.
759    ///
760    /// # Arguments
761    ///
762    /// * `order` - The ORDER BY expression (e.g., "created_at DESC", "age ASC, name DESC")
763    ///
764    /// # Example
765    ///
766    /// ```rust,ignore
767    /// // Single column ascending (ASC is default)
768    /// query.order("age")
769    ///
770    /// // Single column descending
771    /// query.order("created_at DESC")
772    ///
773    /// // Multiple columns
774    /// query.order("age DESC, username ASC")
775    ///
776    /// // Chain multiple order clauses
777    /// query
778    ///     .order("priority DESC")
779    ///     .order("created_at ASC")
780    /// ```
781    pub fn order(mut self, order: &str) -> Self {
782        self.order_clauses.push(order.to_string());
783        self
784    }
785
786    /// Defines a SQL alias for the primary table in the query.
787    ///
788    /// This method allows you to set a short alias for the model's underlying table.
789    /// It is highly recommended when writing complex queries with multiple `JOIN` clauses,
790    /// preventing the need to repeat the full table name in `.filter()`, `.equals()`, or `.select()`.
791    ///
792    /// # Arguments
793    ///
794    /// * `alias` - A string slice representing the alias to be used (e.g., "u", "rp").
795    ///
796    /// # Example
797    ///
798    /// ```rust,ignore
799    /// // Using 'u' as an alias for the User table
800    /// let results = db.model::<User>()
801    ///     .alias("u")
802    ///     .join("role_permissions rp", "rp.role_id = u.role")
803    ///     .equals("u.id", user_id)
804    ///     .select("u.username, rp.permission_id")
805    ///     .scan_as::<UserPermissionDTO>()
806    ///     .await?;
807    /// ```
808    pub fn alias(mut self, alias: &str) -> Self {
809        self.alias = Some(alias.to_string());
810        self
811    }
812
813    /// Placeholder for eager loading relationships (preload).
814    ///
815    /// This method is reserved for future implementation of relationship preloading.
816    /// Currently, it returns `self` unchanged to maintain the fluent interface.
817    ///
818    /// # Future Implementation
819    ///
820    /// Will support eager loading of related models to avoid N+1 query problems:
821    ///
822    /// ```rust,ignore
823    /// // Future usage example
824    /// query.preload("posts").preload("comments")
825    /// ```
826    // pub fn preload(self) -> Self {
827    //     // TODO: Implement relationship preloading
828    //     self
829    // }
830
831    /// Activates debug mode for this query.
832    ///
833    /// When enabled, the generated SQL query will be logged using the `log` crate
834    /// at the `DEBUG` level before execution.
835    ///
836    /// # Note
837    ///
838    /// To see the output, you must initialize a logger in your application (e.g., using `env_logger`)
839    /// and configure it to display `debug` logs for `bottle_orm`.
840    ///
841    /// # Example
842    ///
843    /// ```rust,ignore
844    /// db.model::<User>()
845    ///     .filter("active", "=", true)
846    ///     .debug() // Logs SQL: SELECT * FROM "user" WHERE "active" = $1
847    ///     .scan()
848    ///     .await?;
849    /// ```
850    pub fn debug(mut self) -> Self {
851        self.debug_mode = true;
852        self
853    }
854
855    /// Adds an IS NULL filter for the specified column.
856    ///
857    /// # Arguments
858    ///
859    /// * `col` - The column name to check for NULL
860    ///
861    /// # Example
862    ///
863    /// ```rust,ignore
864    /// db.model::<User>()
865    ///     .is_null("deleted_at")
866    ///     .scan()
867    ///     .await?;
868    /// // SQL: SELECT * FROM "user" WHERE "deleted_at" IS NULL
869    /// ```
870    pub fn is_null(mut self, col: &str) -> Self {
871        let col_owned = col.to_string();
872        let table_id = self.get_table_identifier();
873        let is_main_col = self.columns.contains(&col_owned.to_snake_case());
874        let clause: FilterFn = Box::new(move |query, _args, _driver, _arg_counter| {
875            query.push_str(" AND ");
876            if let Some((table, column)) = col_owned.split_once(".") {
877                query.push_str(&format!("\"{}\".\"{}\"", table, column));
878            } else if is_main_col {
879                query.push_str(&format!("\"{}\".\"{}\"", table_id, col_owned));
880            } else {
881                query.push_str(&format!("\"{}\"", col_owned));
882            }
883            query.push_str(" IS NULL");
884        });
885        self.where_clauses.push(clause);
886        self
887    }
888
889    /// Adds an IS NOT NULL filter for the specified column.
890    ///
891    /// # Arguments
892    ///
893    /// * `col` - The column name to check for NOT NULL
894    ///
895    /// # Example
896    ///
897    /// ```rust,ignore
898    /// db.model::<User>()
899    ///     .is_not_null("email")
900    ///     .scan()
901    ///     .await?;
902    /// // SQL: SELECT * FROM "user" WHERE "email" IS NOT NULL
903    /// ```
904    pub fn is_not_null(mut self, col: &str) -> Self {
905        let col_owned = col.to_string();
906        let table_id = self.get_table_identifier();
907        let is_main_col = self.columns.contains(&col_owned.to_snake_case());
908        let clause: FilterFn = Box::new(move |query, _args, _driver, _arg_counter| {
909            query.push_str(" AND ");
910            if let Some((table, column)) = col_owned.split_once(".") {
911                query.push_str(&format!("\"{}\".\"{}\"", table, column));
912            } else if is_main_col {
913                query.push_str(&format!("\"{}\".\"{}\"", table_id, col_owned));
914            } else {
915                query.push_str(&format!("\"{}\"", col_owned));
916            }
917            query.push_str(" IS NOT NULL");
918        });
919        self.where_clauses.push(clause);
920        self
921    }
922
923    /// Includes soft-deleted records in query results.
924    ///
925    /// By default, queries on models with a `#[orm(soft_delete)]` column exclude
926    /// records where that column is not NULL. This method disables that filter.
927    ///
928    /// # Example
929    ///
930    /// ```rust,ignore
931    /// // Get all users including deleted ones
932    /// db.model::<User>()
933    ///     .with_deleted()
934    ///     .scan()
935    ///     .await?;
936    /// ```
937    pub fn with_deleted(mut self) -> Self {
938        self.with_deleted = true;
939        self
940    }
941
942    /// Placeholder for JOIN operations.
943    ///
944    /// This method is reserved for future implementation of SQL JOINs.
945    /// Currently, it returns `self` unchanged to maintain the fluent interface.
946    ///
947    /// # Future Implementation
948    ///
949    /// Will support various types of JOINs (INNER, LEFT, RIGHT, FULL):
950    ///
951    /// ```rust,ignore
952    /// Adds a JOIN clause to the query.
953    ///
954    /// # Arguments
955    ///
956    /// * `table` - The name of the table to join.
957    /// * `s_query` - The ON clause condition (e.g., "users.id = posts.user_id").
958    ///
959    /// # Example
960    ///
961    /// ```rust,ignore
962    /// query.join("posts", "users.id = posts.user_id")
963    /// ```
964    pub fn join(self, table: &str, s_query: &str) -> Self {
965        self.join_generic("", table, s_query)
966    }
967
968    /// Internal helper for specific join types
969    fn join_generic(mut self, join_type: &str, table: &str, s_query: &str) -> Self {
970        let table_owned = table.to_string();
971        let join_type_owned = join_type.to_string();
972        
973        let trimmed_value = s_query.replace(" ", "");
974        let values = trimmed_value.split_once("=");
975        let mut parsed_query = s_query.to_string();
976        
977        if let Some((first, second)) = values {
978            // Try to parse table.column = table.column
979            if let Some((t1, c1)) = first.split_once('.') {
980                if let Some((t2, c2)) = second.split_once('.') {
981                    parsed_query = format!("\"{}\".\"{}\" = \"{}\".\"{}\"", t1, c1, t2, c2);
982                }
983            }
984        }
985
986        if let Some((table_name, alias)) = table.split_once(" ") {
987            self.join_aliases.insert(table_name.to_snake_case(), alias.to_string());
988        }
989
990        self.joins_clauses.push(Box::new(move |query, _args, _driver, _arg_counter| {
991            if let Some((table_name, alias)) = table_owned.split_once(" ") {
992                query.push_str(&format!("{} JOIN \"{}\" {} ON {}", join_type_owned, table_name, alias, parsed_query));
993            } else {
994                query.push_str(&format!("{} JOIN \"{}\" ON {}", join_type_owned, table_owned, parsed_query));
995            }
996        }));
997        self
998    }
999
1000    /// Adds a raw JOIN clause with a placeholder and a bound value.
1001    ///
1002    /// This is useful for joining tables with conditions that involve external values.
1003    ///
1004    /// # Example
1005    ///
1006    /// ```rust,ignore
1007    /// db.model::<Permissions>()
1008    ///     .join_raw("role_permissions rp", "rp.role_id = ?", role_id)
1009    ///     .scan()
1010    ///     .await?;
1011    /// ```
1012    pub fn join_raw<V>(mut self, table: &str, on: &str, value: V) -> Self
1013    where
1014        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
1015    {
1016        self.join_generic_raw("", table, on, value)
1017    }
1018
1019    /// Adds a raw LEFT JOIN clause with a placeholder and a bound value.
1020    ///
1021    /// # Example
1022    ///
1023    /// ```rust,ignore
1024    /// query.left_join_raw("posts", "posts.user_id = ?", user_id)
1025    /// ```
1026    pub fn left_join_raw<V>(mut self, table: &str, on: &str, value: V) -> Self
1027    where
1028        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
1029    {
1030        self.join_generic_raw("LEFT", table, on, value)
1031    }
1032
1033    /// Adds a raw RIGHT JOIN clause with a placeholder and a bound value.
1034    ///
1035    /// # Example
1036    ///
1037    /// ```rust,ignore
1038    /// query.right_join_raw("users", "users.id = ?", user_id)
1039    /// ```
1040    pub fn right_join_raw<V>(mut self, table: &str, on: &str, value: V) -> Self
1041    where
1042        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
1043    {
1044        self.join_generic_raw("RIGHT", table, on, value)
1045    }
1046
1047    /// Adds a raw INNER JOIN clause with a placeholder and a bound value.
1048    ///
1049    /// # Example
1050    ///
1051    /// ```rust,ignore
1052    /// query.inner_join_raw("accounts", "accounts.user_id = ?", user_id)
1053    /// ```
1054    pub fn inner_join_raw<V>(mut self, table: &str, on: &str, value: V) -> Self
1055    where
1056        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
1057    {
1058        self.join_generic_raw("INNER", table, on, value)
1059    }
1060
1061    /// Adds a raw FULL JOIN clause with a placeholder and a bound value.
1062    ///
1063    /// # Example
1064    ///
1065    /// ```rust,ignore
1066    /// query.full_join_raw("profiles", "profiles.user_id = ?", user_id)
1067    /// ```
1068    pub fn full_join_raw<V>(mut self, table: &str, on: &str, value: V) -> Self
1069    where
1070        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
1071    {
1072        self.join_generic_raw("FULL", table, on, value)
1073    }
1074
1075    /// Internal helper for raw join types
1076    fn join_generic_raw<V>(mut self, join_type: &str, table: &str, on: &str, value: V) -> Self
1077    where
1078        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
1079    {
1080        let table_owned = table.to_string();
1081        let on_owned = on.to_string();
1082        let join_type_owned = join_type.to_string();
1083        
1084        if let Some((table_name, alias)) = table.split_once(" ") {
1085            self.join_aliases.insert(table_name.to_snake_case(), alias.to_string());
1086        }
1087
1088        self.joins_clauses.push(Box::new(move |query, args, driver, arg_counter| {
1089            if let Some((table_name, alias)) = table_owned.split_once(" ") {
1090                query.push_str(&format!("{} JOIN \"{}\" {} ON ", join_type_owned, table_name, alias));
1091            } else {
1092                query.push_str(&format!("{} JOIN \"{}\" ON ", join_type_owned, table_owned));
1093            }
1094
1095            let mut processed_on = on_owned.clone();
1096            if let Some(pos) = processed_on.find('?') {
1097                let placeholder = match driver {
1098                    Drivers::Postgres => {
1099                        let p = format!("${}", arg_counter);
1100                        *arg_counter += 1;
1101                        p
1102                    }
1103                    _ => "?".to_string(),
1104                };
1105                processed_on.replace_range(pos..pos + 1, &placeholder);
1106            }
1107            
1108            query.push_str(&processed_on);
1109            let _ = args.add(value.clone());
1110        }));
1111        self
1112    }
1113
1114    /// Adds a LEFT JOIN clause.
1115    ///
1116    /// Performs a LEFT JOIN with another table. Returns all records from the left table,
1117    /// and the matched records from the right table (or NULL if no match).
1118    ///
1119    /// # Arguments
1120    ///
1121    /// * `table` - The name of the table to join with
1122    /// * `on` - The join condition (e.g., "users.id = posts.user_id")
1123    ///
1124    /// # Example
1125    ///
1126    /// ```rust,ignore
1127    /// // Get all users and their posts (if any)
1128    /// let users_with_posts = db.model::<User>()
1129    ///     .left_join("posts", "users.id = posts.user_id")
1130    ///     .scan()
1131    ///     .await?;
1132    /// ```
1133    pub fn left_join(self, table: &str, on: &str) -> Self {
1134        self.join_generic("LEFT", table, on)
1135    }
1136
1137    /// Adds a RIGHT JOIN clause.
1138    ///
1139    /// Performs a RIGHT JOIN with another table. Returns all records from the right table,
1140    /// and the matched records from the left table (or NULL if no match).
1141    ///
1142    /// # Arguments
1143    ///
1144    /// * `table` - The name of the table to join with
1145    /// * `on` - The join condition
1146    ///
1147    /// # Example
1148    ///
1149    /// ```rust,ignore
1150    /// let posts_with_users = db.model::<Post>()
1151    ///     .right_join("users", "posts.user_id = users.id")
1152    ///     .scan()
1153    ///     .await?;
1154    /// ```
1155    pub fn right_join(self, table: &str, on: &str) -> Self {
1156        self.join_generic("RIGHT", table, on)
1157    }
1158
1159    /// Adds an INNER JOIN clause.
1160    ///
1161    /// Performs an INNER JOIN with another table. Returns records that have matching
1162    /// values in both tables.
1163    ///
1164    /// # Arguments
1165    ///
1166    /// * `table` - The name of the table to join with
1167    /// * `on` - The join condition
1168    ///
1169    /// # Example
1170    ///
1171    /// ```rust,ignore
1172    /// // Get only users who have posts
1173    /// let active_users = db.model::<User>()
1174    ///     .inner_join("posts", "users.id = posts.user_id")
1175    ///     .scan()
1176    ///     .await?;
1177    /// ```
1178    pub fn inner_join(self, table: &str, on: &str) -> Self {
1179        self.join_generic("INNER", table, on)
1180    }
1181
1182    /// Adds a FULL JOIN clause.
1183    ///
1184    /// Performs a FULL OUTER JOIN. Returns all records when there is a match in
1185    /// either left or right table.
1186    ///
1187    /// # Arguments
1188    ///
1189    /// * `table` - The name of the table to join with
1190    /// * `on` - The join condition
1191    ///
1192    /// # Example
1193    ///
1194    /// ```rust,ignore
1195    /// query.full_join("profiles", "profiles.user_id = users.id")
1196    /// ```
1197    pub fn full_join(self, table: &str, on: &str) -> Self {
1198        self.join_generic("FULL", table, on)
1199    }
1200
1201    /// Marks the query to return DISTINCT results.
1202    ///
1203    /// Adds the `DISTINCT` keyword to the SELECT statement, ensuring that unique
1204    /// rows are returned.
1205    ///
1206    /// # Example
1207    ///
1208    /// ```rust,ignore
1209    /// // Get unique ages of users
1210    /// let unique_ages: Vec<i32> = db.model::<User>()
1211    ///     .select("age")
1212    ///     .distinct()
1213    ///     .scan()
1214    ///     .await?;
1215    /// ```
1216    pub fn distinct(mut self) -> Self {
1217        self.is_distinct = true;
1218        self
1219    }
1220
1221    /// Adds a GROUP BY clause to the query.
1222    ///
1223    /// Groups rows that have the same values into summary rows. Often used with
1224    /// aggregate functions (COUNT, MAX, MIN, SUM, AVG).
1225    ///
1226    /// # Arguments
1227    ///
1228    /// * `columns` - Comma-separated list of columns to group by
1229    ///
1230    /// # Example
1231    ///
1232    /// ```rust,ignore
1233    /// // Count users by age group
1234    /// let stats: Vec<(i32, i64)> = db.model::<User>()
1235    ///     .select("age, COUNT(*)")
1236    ///     .group_by("age")
1237    ///     .scan()
1238    ///     .await?;
1239    /// ```
1240    pub fn group_by(mut self, columns: &str) -> Self {
1241        self.group_by_clauses.push(columns.to_string());
1242        self
1243    }
1244
1245    /// Adds a HAVING clause to the query.
1246    ///
1247    /// Used to filter groups created by `group_by`. Similar to `filter` (WHERE),
1248    /// but operates on grouped records and aggregate functions.
1249    ///
1250    /// # Arguments
1251    ///
1252    /// * `col` - The column or aggregate function to filter on
1253    /// * `op` - Comparison operator
1254    /// * `value` - Value to compare against
1255    ///
1256    /// # Example
1257    ///
1258    /// ```rust,ignore
1259    /// // Get ages with more than 5 users
1260    /// let popular_ages = db.model::<User>()
1261    ///     .select("age, COUNT(*)")
1262    ///     .group_by("age")
1263    ///     .having("COUNT(*)", Op::Gt, 5)
1264    ///     .scan()
1265    ///     .await?;
1266    /// ```
1267    pub fn having<V>(mut self, col: &'static str, op: Op, value: V) -> Self
1268    where
1269        V: 'static + for<'q> Encode<'q, Any> + Type<Any> + Send + Sync + Clone,
1270    {
1271        let op_str = op.as_sql();
1272        let clause: FilterFn = Box::new(move |query, args, driver, arg_counter| {
1273            query.push_str(" AND ");
1274            query.push_str(col);
1275            query.push(' ');
1276            query.push_str(op_str);
1277            query.push(' ');
1278
1279            match driver {
1280                Drivers::Postgres => {
1281                    query.push_str(&format!("${}", arg_counter));
1282                    *arg_counter += 1;
1283                }
1284                _ => query.push('?'),
1285            }
1286            let _ = args.add(value.clone());
1287        });
1288
1289        self.having_clauses.push(clause);
1290        self
1291    }
1292
1293    /// Returns the COUNT of rows matching the query.
1294    ///
1295    /// A convenience method that automatically sets `SELECT COUNT(*)` and returns
1296    /// the result as an `i64`.
1297    ///
1298    /// # Returns
1299    ///
1300    /// * `Ok(i64)` - The count of rows
1301    /// * `Err(sqlx::Error)` - Database error
1302    ///
1303    /// # Example
1304    ///
1305    /// ```rust,ignore
1306    /// let user_count = db.model::<User>().count().await?;
1307    /// ```
1308    pub async fn count(mut self) -> Result<i64, sqlx::Error> {
1309        self.select_columns = vec!["COUNT(*)".to_string()];
1310        self.scalar::<i64>().await
1311    }
1312
1313    /// Returns the SUM of the specified column.
1314    ///
1315    /// Calculates the sum of a numeric column.
1316    ///
1317    /// # Arguments
1318    ///
1319    /// * `column` - The column to sum
1320    ///
1321    /// # Example
1322    ///
1323    /// ```rust,ignore
1324    /// let total_age: i64 = db.model::<User>().sum("age").await?;
1325    /// ```
1326    pub async fn sum<N>(mut self, column: &str) -> Result<N, sqlx::Error>
1327    where
1328        N: FromAnyRow + for<'r> Decode<'r, Any> + Type<Any> + Send + Unpin,
1329    {
1330        self.select_columns = vec![format!("SUM({})", column)];
1331        self.scalar::<N>().await
1332    }
1333
1334    /// Returns the AVG of the specified column.
1335    ///
1336    /// Calculates the average value of a numeric column.
1337    ///
1338    /// # Arguments
1339    ///
1340    /// * `column` - The column to average
1341    ///
1342    /// # Example
1343    ///
1344    /// ```rust,ignore
1345    /// let avg_age: f64 = db.model::<User>().avg("age").await?;
1346    /// ```
1347    pub async fn avg<N>(mut self, column: &str) -> Result<N, sqlx::Error>
1348    where
1349        N: FromAnyRow + for<'r> Decode<'r, Any> + Type<Any> + Send + Unpin,
1350    {
1351        self.select_columns = vec![format!("AVG({})", column)];
1352        self.scalar::<N>().await
1353    }
1354
1355    /// Returns the MIN of the specified column.
1356    ///
1357    /// Finds the minimum value in a column.
1358    ///
1359    /// # Arguments
1360    ///
1361    /// * `column` - The column to check
1362    ///
1363    /// # Example
1364    ///
1365    /// ```rust,ignore
1366    /// let min_age: i32 = db.model::<User>().min("age").await?;
1367    /// ```
1368    pub async fn min<N>(mut self, column: &str) -> Result<N, sqlx::Error>
1369    where
1370        N: FromAnyRow + for<'r> Decode<'r, Any> + Type<Any> + Send + Unpin,
1371    {
1372        self.select_columns = vec![format!("MIN({})", column)];
1373        self.scalar::<N>().await
1374    }
1375
1376    /// Returns the MAX of the specified column.
1377    ///
1378    /// Finds the maximum value in a column.
1379    ///
1380    /// # Arguments
1381    ///
1382    /// * `column` - The column to check
1383    ///
1384    /// # Example
1385    ///
1386    /// ```rust,ignore
1387    /// let max_age: i32 = db.model::<User>().max("age").await?;
1388    /// ```
1389    pub async fn max<N>(mut self, column: &str) -> Result<N, sqlx::Error>
1390    where
1391        N: FromAnyRow + for<'r> Decode<'r, Any> + Type<Any> + Send + Unpin,
1392    {
1393        self.select_columns = vec![format!("MAX({})", column)];
1394        self.scalar::<N>().await
1395    }
1396
1397    /// Applies pagination with validation and limits.
1398    ///
1399    /// This is a convenience method that combines `limit()` and `offset()` with
1400    /// built-in validation and maximum value enforcement for safer pagination.
1401    ///
1402    /// # Arguments
1403    ///
1404    /// * `max_value` - Maximum allowed items per page
1405    /// * `default` - Default value if `value` exceeds `max_value`
1406    /// * `page` - Zero-based page number
1407    /// * `value` - Requested items per page
1408    ///
1409    /// # Returns
1410    ///
1411    /// * `Ok(Self)` - The updated QueryBuilder with pagination applied
1412    /// * `Err(Error)` - If `value` is negative
1413    ///
1414    /// # Pagination Logic
1415    ///
1416    /// 1. Validates that `value` is non-negative
1417    /// 2. If `value` > `max_value`, uses `default` instead
1418    /// 3. Calculates offset as: `value * page`
1419    /// 4. Sets limit to `value`
1420    ///
1421    /// # Example
1422    ///
1423    /// ```rust,ignore
1424    /// // Page 0 with 10 items (page 1 in 1-indexed systems)
1425    /// query.pagination(100, 20, 0, 10)?  // LIMIT 10 OFFSET 0
1426    ///
1427    /// // Page 2 with 25 items (page 3 in 1-indexed systems)
1428    /// query.pagination(100, 20, 2, 25)?  // LIMIT 25 OFFSET 50
1429    ///
1430    /// // Request too many items, falls back to default
1431    /// query.pagination(100, 20, 0, 150)? // LIMIT 20 OFFSET 0 (150 > 100)
1432    ///
1433    /// // Error: negative value
1434    /// query.pagination(100, 20, 0, -10)? // Returns Error
1435    /// ```
1436    pub fn pagination(mut self, max_value: usize, default: usize, page: usize, value: isize) -> Result<Self, Error> {
1437        // Validate that value is non-negative
1438        if value < 0 {
1439            return Err(Error::InvalidArgument("value cannot be negative".into()));
1440        }
1441
1442        let mut f_value = value as usize;
1443
1444        // Enforce maximum value limit
1445        if f_value > max_value {
1446            f_value = default;
1447        }
1448
1449        // Apply offset and limit
1450        self = self.offset(f_value * page);
1451        self = self.limit(f_value);
1452
1453        Ok(self)
1454    }
1455
1456    /// Selects specific columns to return.
1457    ///
1458    /// By default, queries use `SELECT *` to return all columns. This method
1459    /// allows you to specify exactly which columns should be returned.
1460    ///
1461    /// **Note:** Columns are pushed exactly as provided, without automatic
1462    /// snake_case conversion, allowing for aliases and raw SQL fragments.
1463    ///
1464    /// # Arguments
1465    ///
1466    /// * `columns` - Comma-separated list of column names to select
1467    ///
1468    /// # Example
1469    ///
1470    /// ```rust,ignore
1471    /// // Select single column
1472    /// query.select("id")
1473    ///
1474    /// // Select multiple columns
1475    /// query.select("id, username, email")
1476    ///
1477    /// // Select with SQL functions and aliases (now supported)
1478    /// query.select("COUNT(*) as total_count")
1479    /// ```
1480    pub fn select(mut self, columns: &str) -> Self {
1481        self.select_columns.push(columns.to_string());
1482        self
1483    }
1484
1485    /// Excludes specific columns from the query results.
1486    ///
1487    /// This is the inverse of `select()`. Instead of specifying which columns to include,
1488    /// you specify which columns to exclude. All other columns will be returned.
1489    ///
1490    /// # Arguments
1491    ///
1492    /// * `columns` - Comma-separated list of column names to exclude
1493    ///
1494    /// # Priority
1495    ///
1496    /// If both `select()` and `omit()` are used, `select()` takes priority.
1497    ///
1498    /// # Example
1499    ///
1500    /// ```rust,ignore
1501    /// // Exclude password from results
1502    /// let user = db.model::<User>()
1503    ///     .omit("password")
1504    ///     .first()
1505    ///     .await?;
1506    ///
1507    /// // Exclude multiple fields
1508    /// let user = db.model::<User>()
1509    ///     .omit("password, secret_token")
1510    ///     .first()
1511    ///     .await?;
1512    ///
1513    /// // Using with generated field constants (autocomplete support)
1514    /// let user = db.model::<User>()
1515    ///     .omit(user_fields::PASSWORD)
1516    ///     .first()
1517    ///     .await?;
1518    /// ```
1519    pub fn omit(mut self, columns: &str) -> Self {
1520        for col in columns.split(',') {
1521            self.omit_columns.push(col.trim().to_snake_case());
1522        }
1523        self
1524    }
1525
1526    /// Sets the query offset (pagination).
1527    ///
1528    /// Specifies the number of rows to skip before starting to return rows.
1529    /// Commonly used in combination with `limit()` for pagination.
1530    ///
1531    /// # Arguments
1532    ///
1533    /// * `offset` - Number of rows to skip
1534    ///
1535    /// # Example
1536    ///
1537    /// ```rust,ignore
1538    /// // Skip first 20 rows
1539    /// query.offset(20)
1540    ///
1541    /// // Pagination: page 3 with 10 items per page
1542    /// query.limit(10).offset(20)  // Skip 2 pages = 20 items
1543    /// ```
1544    pub fn offset(mut self, offset: usize) -> Self {
1545        self.offset = Some(offset);
1546        self
1547    }
1548
1549    /// Sets the maximum number of records to return.
1550    ///
1551    /// Limits the number of rows returned by the query. Essential for pagination
1552    /// and preventing accidentally fetching large result sets.
1553    ///
1554    /// # Arguments
1555    ///
1556    /// * `limit` - Maximum number of rows to return
1557    ///
1558    /// # Example
1559    ///
1560    /// ```rust,ignore
1561    /// // Return at most 10 rows
1562    /// query.limit(10)
1563    ///
1564    /// // Pagination: 50 items per page
1565    /// query.limit(50).offset(page * 50)
1566    /// ```
1567    pub fn limit(mut self, limit: usize) -> Self {
1568        self.limit = Some(limit);
1569        self
1570    }
1571
1572    // ========================================================================
1573    // Insert Operation
1574    // ========================================================================
1575
1576    /// Inserts a new record into the database based on the model instance.
1577    ///
1578    /// This method serializes the model into a SQL INSERT statement with proper
1579    /// type handling for primitives, dates, UUIDs, and other supported types.
1580    ///
1581    /// # Type Binding Strategy
1582    ///
1583    /// The method uses string parsing as a temporary solution for type binding.
1584    /// Values are converted to strings via the model's `to_map()` method, then
1585    /// parsed back to their original types for proper SQL binding.
1586    ///
1587    /// # Supported Types for Insert
1588    ///
1589    /// - **Integers**: `i32`, `i64` (INTEGER, BIGINT)
1590    /// - **Boolean**: `bool` (BOOLEAN)
1591    /// - **Float**: `f64` (DOUBLE PRECISION)
1592    /// - **Text**: `String` (TEXT, VARCHAR)
1593    /// - **UUID**: `Uuid` (UUID) - All versions 1-7 supported
1594    /// - **DateTime**: `DateTime<Utc>` (TIMESTAMPTZ)
1595    /// - **NaiveDateTime**: (TIMESTAMP)
1596    /// - **NaiveDate**: (DATE)
1597    /// - **NaiveTime**: (TIME)
1598    ///
1599    /// # Arguments
1600    ///
1601    /// * `model` - Reference to the model instance to insert
1602    ///
1603    /// # Returns
1604    ///
1605    /// * `Ok(&Self)` - Reference to self for method chaining
1606    /// * `Err(sqlx::Error)` - Database error during insertion
1607    ///
1608    /// # Example
1609    ///
1610    /// ```rust,ignore
1611    /// 
1612    /// use chrono::Utc;
1613    ///
1614    /// let new_user = User {
1615    ///     id: Uuid::new_v4(),
1616    ///     username: "john_doe".to_string(),
1617    ///     email: "john@example.com".to_string(),
1618    ///     age: 25,
1619    ///     active: true,
1620    ///     created_at: Utc::now(),
1621    /// };
1622    ///
1623    /// db.model::<User>().insert(&new_user).await?;
1624    /// ```
1625    pub fn insert<'b>(&'b mut self, model: &'b T) -> BoxFuture<'b, Result<(), sqlx::Error>> {
1626        Box::pin(async move {
1627            // Serialize model to a HashMap of column_name -> string_value
1628            let data_map = model.to_map();
1629
1630            // Early return if no data to insert
1631            if data_map.is_empty() {
1632                return Ok(());
1633            }
1634
1635            let table_name = self.table_name.to_snake_case();
1636            let columns_info = T::columns();
1637
1638            let mut target_columns = Vec::new();
1639            let mut bindings: Vec<(String, &str)> = Vec::new();
1640
1641            // Build column list and collect values with their SQL types
1642            for (col_name, value) in data_map {
1643                // Strip the "r#" prefix if present (for Rust keywords used as field names)
1644                let col_name_clean = col_name.strip_prefix("r#").unwrap_or(&col_name).to_snake_case();
1645                target_columns.push(format!("\"{}\"", col_name_clean));
1646
1647                // Find the SQL type for this column
1648                let sql_type = columns_info.iter().find(|c| c.name == col_name).map(|c| c.sql_type).unwrap_or("TEXT");
1649
1650                bindings.push((value, sql_type));
1651            }
1652
1653            // Generate placeholders with proper type casting for PostgreSQL
1654            let placeholders: Vec<String> = bindings
1655                .iter()
1656                .enumerate()
1657                .map(|(i, (_, sql_type))| match self.driver {
1658                    Drivers::Postgres => {
1659                        let idx = i + 1;
1660                        // PostgreSQL requires explicit type casting for some types
1661                        if temporal::is_temporal_type(sql_type) {
1662                            // Use temporal module for type casting
1663                            format!("${}{}", idx, temporal::get_postgres_type_cast(sql_type))
1664                        } else {
1665                            match *sql_type {
1666                                "UUID" => format!("${}::UUID", idx),
1667                                "JSONB" | "jsonb" => format!("${}::JSONB", idx),
1668                                _ => format!("${}", idx),
1669                            }
1670                        }
1671                    }
1672                    // MySQL and SQLite use simple ? placeholders
1673                    _ => "?".to_string(),
1674                })
1675                .collect();
1676
1677            // Construct the INSERT query
1678            let query_str = format!(
1679                "INSERT INTO \"{}\" ({}) VALUES ({})",
1680                table_name,
1681                target_columns.join(", "),
1682                placeholders.join(", ")
1683            );
1684
1685            if self.debug_mode {
1686                log::debug!("SQL: {}", query_str);
1687            }
1688
1689            let mut args = AnyArguments::default();
1690
1691            // Bind values using the optimized value_binding module
1692            for (val_str, sql_type) in bindings {
1693                if args.bind_value(&val_str, sql_type, &self.driver).is_err() {
1694                    let _ = args.add(val_str);
1695                }
1696            }
1697
1698            // Execute the INSERT query
1699            self.tx.execute(&query_str, args).await?;
1700            Ok(())
1701        })
1702    }
1703
1704    /// Inserts multiple records into the database in a single batch operation.
1705    ///
1706    /// This is significantly faster than performing individual inserts in a loop
1707    /// as it generates a single SQL statement with multiple VALUES groups.
1708    ///
1709    /// # Type Binding Strategy
1710    ///
1711    /// Similar to the single record `insert`, this method uses string parsing for
1712    /// type binding. It ensures that all columns defined in the model are included
1713    /// in the insert statement, providing NULL for any missing optional values.
1714    ///
1715    /// # Arguments
1716    ///
1717    /// * `models` - A slice of model instances to insert
1718    ///
1719    /// # Returns
1720    ///
1721    /// * `Ok(())` - Successfully inserted all records
1722    /// * `Err(sqlx::Error)` - Database error during insertion
1723    ///
1724    /// # Example
1725    ///
1726    /// ```rust,ignore
1727    /// let users = vec![
1728    ///     User { username: "alice".to_string(), ... },
1729    ///     User { username: "bob".to_string(), ... },
1730    /// ];
1731    ///
1732    /// db.model::<User>().batch_insert(&users).await?;
1733    /// ```
1734    pub fn batch_insert<'b>(&'b mut self, models: &'b [T]) -> BoxFuture<'b, Result<(), sqlx::Error>> {
1735        Box::pin(async move {
1736            if models.is_empty() {
1737                return Ok(());
1738            }
1739
1740            let table_name = self.table_name.to_snake_case();
1741            let columns_info = T::columns();
1742
1743            // Collect all column names for the INSERT statement
1744            // We use all columns defined in the model to ensure consistency across the batch
1745            let target_columns: Vec<String> = columns_info
1746                .iter()
1747                .map(|c| {
1748                    let col_name_clean = c.name.strip_prefix("r#").unwrap_or(c.name).to_snake_case();
1749                    format!("\"{}\"", col_name_clean)
1750                })
1751                .collect();
1752
1753            let mut value_groups = Vec::new();
1754            let mut bind_index = 1;
1755
1756            // Generate placeholders for all models
1757            for _ in models {
1758                let mut placeholders = Vec::new();
1759                for col in &columns_info {
1760                    match self.driver {
1761                        Drivers::Postgres => {
1762                            let p = if temporal::is_temporal_type(col.sql_type) {
1763                                format!("${}{}", bind_index, temporal::get_postgres_type_cast(col.sql_type))
1764                            } else {
1765                                match col.sql_type {
1766                                    "UUID" => format!("${}::UUID", bind_index),
1767                                    "JSONB" | "jsonb" => format!("${}::JSONB", bind_index),
1768                                    _ => format!("${}", bind_index),
1769                                }
1770                            };
1771                            placeholders.push(p);
1772                            bind_index += 1;
1773                        }
1774                        _ => {
1775                            placeholders.push("?".to_string());
1776                        }
1777                    }
1778                }
1779                value_groups.push(format!("({})", placeholders.join(", ")));
1780            }
1781
1782            let query_str = format!(
1783                "INSERT INTO \"{}\" ({}) VALUES {}",
1784                table_name,
1785                target_columns.join(", "),
1786                value_groups.join(", ")
1787            );
1788
1789            if self.debug_mode {
1790                log::debug!("SQL Batch: {}", query_str);
1791            }
1792
1793            let mut args = AnyArguments::default();
1794
1795            for model in models {
1796                let data_map = model.to_map();
1797                for col in &columns_info {
1798                    let val_opt = data_map.get(col.name);
1799                    let sql_type = col.sql_type;
1800
1801                    if let Some(val_str) = val_opt {
1802                        if args.bind_value(val_str, sql_type, &self.driver).is_err() {
1803                            let _ = args.add(val_str.clone());
1804                        }
1805                    } else {
1806                        // Bind NULL for missing values
1807                        let _ = args.add(None::<String>);
1808                    }
1809                }
1810            }
1811
1812            // Execute the batch INSERT query
1813            self.tx.execute(&query_str, args).await?;
1814            Ok(())
1815        })
1816    }
1817
1818    // ========================================================================
1819    // Query Execution Methods
1820    // ========================================================================
1821
1822    /// Returns the generated SQL string for debugging purposes.
1823    ///
1824    /// This method constructs the SQL query string without executing it.
1825    /// Useful for debugging and logging query construction. Note that this
1826    /// shows placeholders (?, $1, etc.) rather than actual bound values.
1827    ///
1828    /// # Returns
1829    ///
1830    /// A `String` containing the SQL query that would be executed
1831    ///
1832    /// # Example
1833    ///
1834    /// ```rust,ignore
1835    /// let query = db.model::<User>()
1836    ///     .filter("age", ">=", 18)
1837    ///     .order("created_at DESC")
1838    ///     .limit(10);
1839    ///
1840    /// println!("SQL: {}", query.to_sql());
1841    /// // Output: SELECT * FROM "user" WHERE 1=1 AND "age" >= $1 ORDER BY created_at DESC
1842    /// ```
1843    pub fn to_sql(&self) -> String {
1844        let mut query = String::from("SELECT ");
1845
1846        if self.is_distinct {
1847            query.push_str("DISTINCT ");
1848        }
1849
1850        // Handle column selection
1851        if self.select_columns.is_empty() {
1852            query.push('*');
1853        } else {
1854            query.push_str(&self.select_columns.join(", "));
1855        }
1856
1857        query.push_str(" FROM \"");
1858        query.push_str(&self.table_name.to_snake_case());
1859        query.push_str("\" ");
1860
1861        if let Some(alias) = &self.alias {
1862            query.push_str(&format!("{} ", alias));
1863        }
1864
1865        // Apply WHERE clauses with dummy arguments
1866        let mut dummy_args = AnyArguments::default();
1867        let mut dummy_counter = 1;
1868
1869        if !self.joins_clauses.is_empty() {
1870            for join_clause in &self.joins_clauses {
1871                query.push(' ');
1872                join_clause(&mut query, &mut dummy_args, &self.driver, &mut dummy_counter);
1873            }
1874        }
1875
1876        query.push_str(" WHERE 1=1");
1877
1878        for clause in &self.where_clauses {
1879            clause(&mut query, &mut dummy_args, &self.driver, &mut dummy_counter);
1880        }
1881
1882        // Apply GROUP BY
1883        if !self.group_by_clauses.is_empty() {
1884            query.push_str(&format!(" GROUP BY {}", self.group_by_clauses.join(", ")));
1885        }
1886
1887        // Apply HAVING
1888        if !self.having_clauses.is_empty() {
1889            query.push_str(" HAVING 1=1");
1890            for clause in &self.having_clauses {
1891                clause(&mut query, &mut dummy_args, &self.driver, &mut dummy_counter);
1892            }
1893        }
1894
1895        // Apply ORDER BY if present
1896        if !self.order_clauses.is_empty() {
1897            query.push_str(&format!(" ORDER BY {}", &self.order_clauses.join(", ")));
1898        }
1899
1900        query
1901    }
1902
1903    /// Generates the list of column selection SQL arguments.
1904    ///
1905    /// This helper function constructs the column list for the SELECT statement.
1906    /// It handles:
1907    /// 1. Mapping specific columns if `select_columns` is set.
1908    /// 2. Defaulting to all columns from the struct `R` if no columns are specified.
1909    /// 3. applying `to_json(...)` casting for temporal types when using `AnyImpl` structs,
1910    ///    ensuring compatibility with the `FromAnyRow` deserialization logic.
1911    fn select_args_sql<R: AnyImpl>(&self) -> Vec<String> {
1912        let struct_cols = R::columns();
1913        let table_id = self.get_table_identifier();
1914
1915        if !struct_cols.is_empty() {
1916            if !self.select_columns.is_empty() {
1917                let mut args = Vec::new();
1918
1919                // Flatten potential multi-column strings like "col1, col2"
1920                // This ensures each column is processed individually for prefixes and temporal types
1921                let mut flat_selects = Vec::new();
1922                for s in &self.select_columns {
1923                    if s.contains(',') {
1924                        for sub in s.split(',') {
1925                            flat_selects.push(sub.trim().to_string());
1926                        }
1927                    } else {
1928                        flat_selects.push(s.trim().to_string());
1929                    }
1930                }
1931
1932                for col_info in struct_cols {
1933                    let col_snake = col_info.column.to_snake_case();
1934                    let sql_type = col_info.sql_type;
1935
1936                    // Check if this column (or table.column) is in our select list
1937                    // We check against the column name alone OR the table-qualified name
1938                    let is_selected = flat_selects.iter().any(|s| {
1939                        if s == &col_snake {
1940                            return true;
1941                        }
1942                        if let Some((t, c)) = s.split_once('.') {
1943                            let t_clean = t.trim().trim_matches('"');
1944                            let c_clean = c.trim().trim_matches('"');
1945                            // Matches if the table prefix is either the original table name or the alias
1946                            return (t_clean == table_id || t_clean == self.table_name.to_snake_case())
1947                                && c_clean == col_snake;
1948                        }
1949                        false
1950                    });
1951
1952                    if is_selected {
1953                        if is_temporal_type(sql_type) && matches!(self.driver, Drivers::Postgres) {
1954                            if !self.joins_clauses.is_empty() || self.alias.is_some() {
1955                                args.push(format!(
1956                                    "to_json(\"{}\".\"{}\") #>> '{{}}' AS \"{}\"",
1957                                    table_id,
1958                                    col_snake,
1959                                    col_snake
1960                                ));
1961                            } else {
1962                                args.push(format!("to_json(\"{}\") #>> '{{}}' AS \"{}\"", col_snake, col_snake));
1963                            }
1964                        } else if !self.joins_clauses.is_empty() || self.alias.is_some() {
1965                            args.push(format!("\"{}\".\"{}\"", table_id, col_snake));
1966                        } else {
1967                            args.push(format!("\"{}\"", col_snake));
1968                        }
1969                    }
1970                }
1971                return args;
1972            } else {
1973                // For omitted columns, return 'omited' as placeholder value
1974                return struct_cols
1975                    .iter()
1976                    .map(|c| {
1977                        let col_snake = c.column.to_snake_case();
1978                        let is_omitted = self.omit_columns.contains(&col_snake);
1979                        
1980                        // table_to_alias is used for the result set mapping (AS "table__col")
1981                        // It MUST use the original table name snake_cased for the ORM to map it correctly
1982                        let table_to_alias = if !c.table.is_empty() {
1983                            c.table.to_snake_case()
1984                        } else {
1985                            self.table_name.to_snake_case()
1986                        };
1987
1988                        // table_to_ref is used in the SELECT clause (SELECT "table"."col")
1989                        // It uses the alias if defined, or the original table name
1990                        let table_to_ref = if !c.table.is_empty() {
1991                            let c_table_snake = c.table.to_snake_case();
1992                            if c_table_snake == self.table_name.to_snake_case() {
1993                                table_id.clone()
1994                            } else {
1995                                // Check if we have an alias for this joined table
1996                                self.join_aliases.get(&c_table_snake).cloned().unwrap_or(c_table_snake)
1997                            }
1998                        } else {
1999                            table_id.clone()
2000                        };
2001
2002                        if is_omitted {
2003                            // Return type-appropriate placeholder based on sql_type
2004                            let placeholder = match c.sql_type {
2005                                // String types
2006                                "TEXT" | "VARCHAR" | "CHAR" | "STRING" => "'omited'",
2007                                // Date/Time types - use epoch timestamp
2008                                "TIMESTAMP" | "TIMESTAMPTZ" | "TIMESTAMP WITH TIME ZONE" => "'1970-01-01T00:00:00Z'",
2009                                "DATE" => "'1970-01-01'",
2010                                "TIME" => "'00:00:00'",
2011                                // Numeric types
2012                                "INTEGER" | "INT" | "SMALLINT" | "BIGINT" | "INT4" | "INT8" => "0",
2013                                "REAL" | "FLOAT" | "DOUBLE" | "FLOAT4" | "FLOAT8" | "DECIMAL" | "NUMERIC" => "0.0",
2014                                // Boolean
2015                                "BOOLEAN" | "BOOL" => "false",
2016                                // UUID - nil UUID
2017                                "UUID" => "'00000000-0000-0000-0000-000000000000'",
2018                                // JSON types
2019                                "JSON" | "JSONB" => "'{}'",
2020                                // Default fallback for unknown types
2021                                _ => "'omited'",
2022                            };
2023                            format!("{} AS \"{}__{}\"", placeholder, table_to_alias, col_snake)
2024                        } else if is_temporal_type(c.sql_type) && matches!(self.driver, Drivers::Postgres) {
2025                            format!(
2026                                "to_json(\"{}\".\"{}\") #>> '{{}}' AS \"{}__{}\"",
2027                                table_to_ref, col_snake, table_to_alias, col_snake
2028                            )
2029                        } else {
2030                            format!("\"{}\".\"{}\" AS \"{}__{}\"", table_to_ref, col_snake, table_to_alias, col_snake)
2031                        }
2032                    })
2033                    .collect();
2034            }
2035        }
2036
2037        if !self.select_columns.is_empty() {
2038            return self
2039                .select_columns
2040                .iter()
2041                .map(|c| if c.contains('(') { c.clone() } else { format!("\"{}\"", c) })
2042                .collect();
2043        }
2044
2045        vec!["*".to_string()]
2046    }
2047
2048    /// Executes the query and returns a list of results.
2049    ///
2050    /// This method builds and executes a SELECT query with all accumulated filters,
2051    /// ordering, and pagination settings. It returns all matching rows as a vector.
2052    ///
2053    /// # Type Parameters
2054    ///
2055    /// * `R` - The result type. Must implement `FromRow` for deserialization from database rows.
2056    ///
2057    /// # Returns
2058    ///
2059    /// * `Ok(Vec<R>)` - Vector of results (empty if no matches)
2060    /// * `Err(sqlx::Error)` - Database error during query execution
2061    ///
2062    /// # Example
2063    ///
2064    /// ```rust,ignore
2065    /// // Get all adult users, ordered by age, limited to 10
2066    /// let users: Vec<User> = db.model::<User>()
2067    ///     .filter("age", ">=", 18)
2068    ///     .order("age DESC")
2069    ///     .limit(10)
2070    ///     .scan()
2071    ///     .await?;
2072    ///
2073    /// // Get users by UUID
2074    /// let user_id = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000")?;
2075    /// let users: Vec<User> = db.model::<User>()
2076    ///     .filter("id", "=", user_id)
2077    ///     .scan()
2078    ///     .await?;
2079    ///
2080    /// // Empty result is Ok
2081    /// let results: Vec<User> = db.model::<User>()
2082    ///     .filter("age", ">", 200)
2083    ///     .scan()
2084    ///     .await?;  // Returns empty Vec, not an error
2085    /// ```
2086    pub async fn scan<R>(mut self) -> Result<Vec<R>, sqlx::Error>
2087    where
2088        R: FromAnyRow + AnyImpl + Send + Unpin,
2089    {
2090        // Apply default soft delete filter if not disabled
2091        if !self.with_deleted {
2092            if let Some(soft_delete_col) = self.columns_info.iter().find(|c| c.soft_delete).map(|c| c.name) {
2093                self = self.is_null(soft_delete_col);
2094            }
2095        }
2096
2097        // Build SELECT clause
2098        let mut query = String::from("SELECT ");
2099
2100        if self.is_distinct {
2101            query.push_str("DISTINCT ");
2102        }
2103
2104        query.push_str(&self.select_args_sql::<R>().join(", "));
2105
2106        // Build FROM clause
2107        query.push_str(" FROM \"");
2108        query.push_str(&self.table_name.to_snake_case());
2109        query.push_str("\" ");
2110        if let Some(alias) = &self.alias {
2111            query.push_str(&format!("{} ", alias));
2112        }
2113
2114        let mut args = AnyArguments::default();
2115        let mut arg_counter = 1;
2116
2117        if !self.joins_clauses.is_empty() {
2118            for join_clause in &self.joins_clauses {
2119                query.push(' ');
2120                join_clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2121            }
2122        }
2123
2124        query.push_str(" WHERE 1=1");
2125
2126        // Apply WHERE clauses
2127        for clause in &self.where_clauses {
2128            clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2129        }
2130
2131        // Apply GROUP BY
2132        if !self.group_by_clauses.is_empty() {
2133            query.push_str(&format!(" GROUP BY {}", self.group_by_clauses.join(", ")));
2134        }
2135
2136        // Apply HAVING
2137        if !self.having_clauses.is_empty() {
2138            query.push_str(" HAVING 1=1");
2139            for clause in &self.having_clauses {
2140                clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2141            }
2142        }
2143
2144        // Apply ORDER BY clauses
2145        // We join multiple clauses with commas to form a valid SQL ORDER BY statement
2146        if !self.order_clauses.is_empty() {
2147            query.push_str(&format!(" ORDER BY {}", self.order_clauses.join(", ")));
2148        }
2149
2150        // Apply LIMIT clause
2151        if let Some(limit) = self.limit {
2152            query.push_str(" LIMIT ");
2153            match self.driver {
2154                Drivers::Postgres => {
2155                    query.push_str(&format!("${}", arg_counter));
2156                    arg_counter += 1;
2157                }
2158                _ => query.push('?'),
2159            }
2160            let _ = args.add(limit as i64);
2161        }
2162
2163        // Apply OFFSET clause
2164        if let Some(offset) = self.offset {
2165            query.push_str(" OFFSET ");
2166            match self.driver {
2167                Drivers::Postgres => {
2168                    query.push_str(&format!("${}", arg_counter));
2169                    // arg_counter += 1; // Not needed as this is the last clause
2170                }
2171                _ => query.push('?'),
2172            }
2173            let _ = args.add(offset as i64);
2174        }
2175
2176        // Print SQL query to logs if debug mode is active
2177        if self.debug_mode {
2178            log::debug!("SQL: {}", query);
2179        }
2180
2181        // Execute query and fetch all results
2182        let rows = self.tx.fetch_all(&query, args).await?;
2183
2184        rows.iter().map(|row| R::from_any_row(row)).collect()
2185    }
2186
2187    /// Executes the query and maps the result to a custom DTO.
2188    ///
2189    /// Ideal for JOINs and projections where the return type is not a full Model.
2190    ///
2191    /// # Type Parameters
2192    ///
2193    /// * `R` - The target result type. Must implement `FromAnyRow` and `AnyImpl`.
2194    ///
2195    /// # Returns
2196    ///
2197    /// * `Ok(Vec<R>)` - Vector of results mapped to type `R`.
2198    /// * `Err(sqlx::Error)` - Database error.
2199    ///
2200    /// # Example
2201    ///
2202    /// ```rust,ignore
2203    /// #[derive(FromAnyRow)]
2204    /// struct UserRoleDTO {
2205    ///     username: String,
2206    ///     role_name: String,
2207    /// }
2208    ///
2209    /// let results: Vec<UserRoleDTO> = db.model::<User>()
2210    ///     .inner_join("roles", "users.role_id = roles.id")
2211    ///     .select("users.username, roles.name as role_name")
2212    ///     .scan_as::<UserRoleDTO>()
2213    ///     .await?;
2214    /// ```
2215    pub async fn scan_as<R>(mut self) -> Result<Vec<R>, sqlx::Error>
2216    where
2217        R: FromAnyRow + AnyImpl + Send + Unpin,
2218    {
2219        // Apply default soft delete filter if not disabled
2220        if !self.with_deleted {
2221            if let Some(soft_delete_col) = self.columns_info.iter().find(|c| c.soft_delete).map(|c| c.name) {
2222                self = self.is_null(soft_delete_col);
2223            }
2224        }
2225
2226        let mut query = String::from("SELECT ");
2227        if self.is_distinct {
2228            query.push_str("DISTINCT ");
2229        }
2230
2231        let table_id = self.get_table_identifier();
2232
2233        if self.select_columns.is_empty() {
2234            let mut select_args = Vec::new();
2235            let struct_cols = R::columns();
2236            let main_table_snake = self.table_name.to_snake_case();
2237
2238            for c in struct_cols {
2239                let c_name = c.column.to_snake_case();
2240
2241                // Determine if we should use the table name from AnyInfo
2242                // If it matches a joined table or the main table, we use it.
2243                // Otherwise (like UserDTO), we default to the main table.
2244                let mut table_to_use = table_id.clone();
2245                if !c.table.is_empty() {
2246                    let c_table_snake = c.table.to_snake_case();
2247                    if c_table_snake == main_table_snake
2248                        || self.join_aliases.contains_key(&c_table_snake)
2249                    {
2250                        if c_table_snake == main_table_snake {
2251                            table_to_use = table_id.clone();
2252                        } else {
2253                            // Use join alias if available
2254                            table_to_use = self.join_aliases.get(&c_table_snake).cloned().unwrap_or(c_table_snake);
2255                        }
2256                    }
2257                }
2258
2259                if is_temporal_type(c.sql_type) && matches!(self.driver, Drivers::Postgres) {
2260                    select_args
2261                        .push(format!("to_json(\"{}\".\"{}\") #>> '{{}}' AS \"{}\"", table_to_use, c_name, c_name));
2262                } else {
2263                    select_args.push(format!("\"{}\".\"{}\" AS \"{}\"", table_to_use, c_name, c_name));
2264                }
2265            }
2266
2267            if select_args.is_empty() {
2268                query.push('*');
2269            } else {
2270                query.push_str(&select_args.join(", "));
2271            }
2272        } else {
2273            let mut select_cols = Vec::with_capacity(self.select_columns.capacity());
2274            let struct_cols = R::columns();
2275
2276            // Flatten multi-column strings
2277            let mut flat_selects = Vec::new();
2278            for s in &self.select_columns {
2279                if s.contains(',') {
2280                    for sub in s.split(',') {
2281                        flat_selects.push(sub.trim().to_string());
2282                    }
2283                } else {
2284                    flat_selects.push(s.trim().to_string());
2285                }
2286            }
2287
2288            for col in &flat_selects {
2289                let col_trimmed = col.trim();
2290                if col_trimmed == "*" {
2291                    for c in &self.columns_info {
2292                        let c_name = c.name.strip_prefix("r#").unwrap_or(c.name).to_snake_case();
2293                        let mut is_c_temporal = false;
2294                        if let Some(r_info) = struct_cols.iter().find(|rc| rc.column.to_snake_case() == c_name) {
2295                            if is_temporal_type(r_info.sql_type) {
2296                                is_c_temporal = true;
2297                            }
2298                        }
2299
2300                        if is_c_temporal && matches!(self.driver, Drivers::Postgres) {
2301                            select_cols.push(format!(
2302                                "to_json(\"{}\".\"{}\") #>> '{{}}' AS \"{}\"",
2303                                table_id,
2304                                c_name,
2305                                c_name
2306                            ));
2307                        } else {
2308                            select_cols.push(format!(
2309                                "\"{}\".\"{}\" AS \"{}\"",
2310                                table_id,
2311                                c_name,
2312                                c_name
2313                            ));
2314                        }
2315                    }
2316                    continue;
2317                }
2318
2319                // Check if this column is temporal in the target DTO
2320                let mut is_temporal = false;
2321
2322                // We need to keep the lowercase string alive to use its slice in col_name
2323                let col_lower = col_trimmed.to_lowercase();
2324                let mut col_name = col_trimmed;
2325
2326                // Handle aliases (e.g., "created_at as time" or "user.created_at as time")
2327                if let Some((_, alias)) = col_lower.split_once(" as ") {
2328                    col_name = alias.trim().trim_matches('"').trim_matches('\'');
2329                } else if col_trimmed.contains('.') {
2330                    if let Some((_, actual_col)) = col_trimmed.split_once('.') {
2331                        col_name = actual_col.trim().trim_matches('"').trim_matches('\'');
2332                    }
2333                }
2334
2335                if let Some(info) = struct_cols.iter().find(|c| c.column.to_snake_case() == col_name.to_snake_case()) {
2336                    if is_temporal_type(info.sql_type) {
2337                        is_temporal = true;
2338                    }
2339                }
2340
2341                if col_trimmed.contains('.') {
2342                    if let Some((table, column)) = col_trimmed.split_once('.') {
2343                        let clean_table = table.trim().trim_matches('"');
2344                        let clean_column = column.trim().trim_matches('"').split_whitespace().next().unwrap_or(column);
2345
2346                        if clean_column == "*" {
2347                            let mut expanded = false;
2348                            let table_to_compare = clean_table.to_snake_case();
2349                            if table_to_compare == self.table_name.to_snake_case() || table_to_compare == table_id {
2350                                for c in &self.columns_info {
2351                                    let c_name = c.name.strip_prefix("r#").unwrap_or(c.name).to_snake_case();
2352                                    let mut is_c_temporal = false;
2353                                    if let Some(r_info) =
2354                                        struct_cols.iter().find(|rc| rc.column.to_snake_case() == c_name)
2355                                    {
2356                                        if is_temporal_type(r_info.sql_type) {
2357                                            is_c_temporal = true;
2358                                        }
2359                                    }
2360
2361                                    if is_c_temporal && matches!(self.driver, Drivers::Postgres) {
2362                                        select_cols.push(format!(
2363                                            "to_json(\"{}\".\"{}\") #>> '{{}}' AS \"{}\"",
2364                                            clean_table, c_name, c_name
2365                                        ));
2366                                    } else {
2367                                        select_cols
2368                                            .push(format!("\"{}\".\"{}\" AS \"{}\"", clean_table, c_name, c_name));
2369                                    }
2370                                }
2371                                expanded = true;
2372                            }
2373
2374                            if !expanded {
2375                                select_cols.push(format!("\"{}\".*", clean_table));
2376                            }
2377                        } else if is_temporal && matches!(self.driver, Drivers::Postgres) {
2378                            select_cols.push(format!(
2379                                "to_json(\"{}\".\"{}\") #>> '{{}}' AS \"{}\"",
2380                                clean_table, clean_column, col_name
2381                            ));
2382                        } else {
2383                            select_cols.push(format!("\"{}\".\"{}\" AS \"{}\"", clean_table, clean_column, col_name));
2384                        }
2385                    }
2386                } else if is_temporal && matches!(self.driver, Drivers::Postgres) {
2387                    // Extract column name from potential expression
2388                    let clean_col = col_trimmed.trim_matches('"');
2389                    select_cols.push(format!("to_json(\"{}\".\"{}\") #>> '{{}}' AS \"{}\"", table_id, clean_col, col_name));
2390                } else if col_trimmed != col_name {
2391                    select_cols.push(format!("{} AS \"{}\"", col_trimmed, col_name));
2392                } else {
2393                    let is_main_col = self.columns.contains(&col_trimmed.to_snake_case());
2394                    if is_main_col {
2395                        select_cols.push(format!("\"{}\".\"{}\"", table_id, col_trimmed));
2396                    } else {
2397                        select_cols.push(format!("\"{}\"", col_trimmed));
2398                    }
2399                }
2400            }
2401            query.push_str(&select_cols.join(", "));
2402        }
2403
2404        // Build FROM clause
2405        query.push_str(" FROM \"");
2406        query.push_str(&self.table_name.to_snake_case());
2407        query.push_str("\" ");
2408        if let Some(alias) = &self.alias {
2409            query.push_str(&format!("{} ", alias));
2410        }
2411
2412        let mut args = sqlx::any::AnyArguments::default();
2413        let mut arg_counter = 1;
2414
2415        if !self.joins_clauses.is_empty() {
2416            for join_clause in &self.joins_clauses {
2417                query.push(' ');
2418                join_clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2419            }
2420        }
2421
2422        query.push_str(" WHERE 1=1");
2423
2424        for clause in &self.where_clauses {
2425            clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2426        }
2427
2428        if !self.group_by_clauses.is_empty() {
2429            query.push_str(&format!(" GROUP BY {}", self.group_by_clauses.join(", ")));
2430        }
2431
2432        if !self.having_clauses.is_empty() {
2433            query.push_str(" HAVING 1=1");
2434            for clause in &self.having_clauses {
2435                clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2436            }
2437        }
2438
2439        if !self.order_clauses.is_empty() {
2440            query.push_str(&format!(" ORDER BY {}", self.order_clauses.join(", ")));
2441        }
2442
2443        if let Some(limit) = self.limit {
2444            query.push_str(" LIMIT ");
2445            match self.driver {
2446                Drivers::Postgres => {
2447                    query.push_str(&format!("${}", arg_counter));
2448                    arg_counter += 1;
2449                }
2450                _ => query.push('?'),
2451            }
2452            let _ = args.add(limit as i64);
2453        }
2454
2455        if let Some(offset) = self.offset {
2456            query.push_str(" OFFSET ");
2457            match self.driver {
2458                Drivers::Postgres => {
2459                    query.push_str(&format!("${}", arg_counter));
2460                }
2461                _ => query.push('?'),
2462            }
2463            let _ = args.add(offset as i64);
2464        }
2465
2466        if self.debug_mode {
2467            log::debug!("SQL: {}", query);
2468        }
2469
2470        let rows = self.tx.fetch_all(&query, args).await?;
2471        rows.iter().map(|row| R::from_any_row(row)).collect()
2472    }
2473
2474    /// Executes the query and returns only the first result.
2475    ///
2476    /// This method automatically adds `LIMIT 1` and orders by the Primary Key
2477    /// (if available) to ensure consistent results. It's optimized for fetching
2478    /// a single row and will return an error if no rows match.
2479    ///
2480    /// # Type Parameters
2481    ///
2482    /// * `R` - The result type. Must implement `FromRow` for deserialization.
2483    ///
2484    /// # Returns
2485    ///
2486    /// * `Ok(R)` - The first matching row
2487    /// * `Err(sqlx::Error)` - No rows found or database error
2488    ///
2489    /// # Error Handling
2490    ///
2491    /// Returns `sqlx::Error::RowNotFound` if no rows match the query.
2492    /// Use `scan()` instead if you want an empty Vec rather than an error.
2493    ///
2494    /// # Example
2495    ///
2496    /// ```rust,ignore
2497    /// // Get a specific user by ID
2498    /// let user: User = db.model::<User>()
2499    ///     .filter("id", "=", 1)
2500    ///     .first()
2501    ///     .await?;
2502    ///
2503    /// // Get user by UUID
2504    /// let user_id = Uuid::new_v4();
2505    /// let user: User = db.model::<User>()
2506    ///     .filter("id", "=", user_id)
2507    ///     .first()
2508    ///     .await?;
2509    ///
2510    /// // Get the oldest user
2511    /// let oldest: User = db.model::<User>()
2512    ///     .order("age DESC")
2513    ///     .first()
2514    ///     .await?;
2515    ///
2516    /// // Error handling
2517    /// match db.model::<User>().filter("id", "=", 999).first().await {
2518    ///     Ok(user) => println!("Found: {:?}", user),
2519    ///     Err(sqlx::Error::RowNotFound) => println!("User not found"),
2520    ///     Err(e) => println!("Database error: {}", e),
2521    /// }
2522    /// ```
2523    pub async fn first<R>(mut self) -> Result<R, sqlx::Error>
2524    where
2525        R: FromAnyRow + AnyImpl + Send + Unpin,
2526    {
2527        // Apply default soft delete filter if not disabled
2528        if !self.with_deleted {
2529            if let Some(soft_delete_col) = self.columns_info.iter().find(|c| c.soft_delete).map(|c| c.name) {
2530                self = self.is_null(soft_delete_col);
2531            }
2532        }
2533
2534        // Build SELECT clause
2535        let mut query = String::from("SELECT ");
2536
2537        if self.is_distinct {
2538            query.push_str("DISTINCT ");
2539        }
2540
2541        query.push_str(&self.select_args_sql::<R>().join(", "));
2542
2543        // Build FROM clause
2544        query.push_str(" FROM \"");
2545        query.push_str(&self.table_name.to_snake_case());
2546        query.push_str("\" ");
2547        if let Some(alias) = &self.alias {
2548            query.push_str(&format!("{} ", alias));
2549        }
2550
2551        let mut args = AnyArguments::default();
2552        let mut arg_counter = 1;
2553
2554        if !self.joins_clauses.is_empty() {
2555            for join_clause in &self.joins_clauses {
2556                query.push(' ');
2557                join_clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2558            }
2559        }
2560
2561        query.push_str(" WHERE 1=1");
2562
2563        // Apply WHERE clauses
2564        for clause in &self.where_clauses {
2565            clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2566        }
2567
2568        // Apply GROUP BY
2569        if !self.group_by_clauses.is_empty() {
2570            query.push_str(&format!(" GROUP BY {}", self.group_by_clauses.join(", ")));
2571        }
2572
2573        // Apply HAVING
2574        if !self.having_clauses.is_empty() {
2575            query.push_str(" HAVING 1=1");
2576            for clause in &self.having_clauses {
2577                clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2578            }
2579        }
2580
2581        // Find primary key column for consistent ordering
2582        let pk_column = T::columns()
2583            .iter()
2584            .find(|c| c.is_primary_key)
2585            .map(|c| c.name.strip_prefix("r#").unwrap_or(c.name).to_snake_case());
2586
2587        let table_id = self.get_table_identifier();
2588
2589        // Apply ORDER BY clauses
2590        // We join multiple clauses with commas to form a valid SQL ORDER BY statement
2591        if !self.order_clauses.is_empty() {
2592            query.push_str(&format!(" ORDER BY {}", self.order_clauses.join(", ")));
2593        } else if let Some(pk) = pk_column {
2594            // Fallback to PK ordering if no custom order is specified (ensures deterministic results)
2595            query.push_str(" ORDER BY ");
2596            query.push_str(&format!("\"{}\".\"{}\"", table_id, pk));
2597            query.push_str(" ASC");
2598        }
2599
2600        // Always add LIMIT 1 for first() queries
2601        query.push_str(" LIMIT 1");
2602
2603        // Print SQL query to logs if debug mode is active
2604        log::debug!("SQL: {}", query);
2605
2606        // Execute query and fetch exactly one result
2607        let row = self.tx.fetch_one(&query, args).await?;
2608        R::from_any_row(&row)
2609    }
2610
2611    /// Executes the query and returns a single scalar value.
2612    ///
2613    /// This method is useful for fetching single values like counts, max/min values,
2614    /// or specific columns without mapping to a struct or tuple.
2615    ///
2616    /// # Type Parameters
2617    ///
2618    /// * `O` - The output type. Must implement `Decode` and `Type`.
2619    ///
2620    /// # Example
2621    ///
2622    /// ```rust,ignore
2623    /// // Get count of users
2624    /// let count: i64 = db.model::<User>()
2625    ///     .select("count(*)")
2626    ///     .scalar()
2627    ///     .await?;
2628    ///
2629    /// // Get specific field
2630    /// let username: String = db.model::<User>()
2631    ///     .filter("id", "=", 1)
2632    ///     .select("username")
2633    ///     .scalar()
2634    ///     .await?;
2635    /// ```
2636    pub async fn scalar<O>(mut self) -> Result<O, sqlx::Error>
2637    where
2638        O: FromAnyRow + Send + Unpin,
2639    {
2640        // Apply default soft delete filter if not disabled
2641        if !self.with_deleted {
2642            if let Some(soft_delete_col) = self.columns_info.iter().find(|c| c.soft_delete).map(|c| c.name) {
2643                self = self.is_null(soft_delete_col);
2644            }
2645        }
2646
2647        // Build SELECT clause
2648        let mut query = String::from("SELECT ");
2649
2650        if self.is_distinct {
2651            query.push_str("DISTINCT ");
2652        }
2653
2654        if self.select_columns.is_empty() {
2655            return Err(sqlx::Error::ColumnNotFound("is not possible get data without column".to_string()));
2656        }
2657
2658        let table_id = self.get_table_identifier();
2659
2660        let mut select_cols = Vec::with_capacity(self.select_columns.capacity());
2661        for col in self.select_columns {
2662            let col_snake = col.to_snake_case();
2663            let is_main_col = self.columns.contains(&col_snake);
2664            
2665            // Check if this is a temporal type that needs special handling on Postgres
2666            let mut is_temporal = false;
2667            if matches!(self.driver, Drivers::Postgres) {
2668                if let Some(info) = self.columns_info.iter().find(|c| c.name.to_snake_case() == col_snake) {
2669                    if is_temporal_type(info.sql_type) {
2670                        is_temporal = true;
2671                    }
2672                }
2673            }
2674
2675            if !self.joins_clauses.is_empty() || self.alias.is_some() {
2676                if let Some((table, column)) = col.split_once(".") {
2677                    if is_temporal {
2678                        select_cols.push(format!("to_json(\"{}\".\"{}\") #>> '{{}}' AS \"{}\"", table, column, column));
2679                    } else {
2680                        select_cols.push(format!("\"{}\".\"{}\"", table, column));
2681                    }
2682                } else if col.contains('(') {
2683                    select_cols.push(col);
2684                } else if is_main_col {
2685                    if is_temporal {
2686                        select_cols.push(format!("to_json(\"{}\".\"{}\") #>> '{{}}' AS \"{}\"", table_id, col, col));
2687                    } else {
2688                        select_cols.push(format!("\"{}\".\"{}\"", table_id, col));
2689                    }
2690                } else {
2691                    select_cols.push(format!("\"{}\"", col));
2692                }
2693                continue;
2694            }
2695            
2696            if is_temporal {
2697                select_cols.push(format!("to_json(\"{}\") #>> '{{}}' AS \"{}\"", col, col));
2698            } else {
2699                select_cols.push(col);
2700            }
2701        }
2702
2703        query.push_str(&select_cols.join(", "));
2704
2705        // Build FROM clause
2706        query.push_str(" FROM \"");
2707        query.push_str(&self.table_name.to_snake_case());
2708        query.push_str("\" ");
2709        if let Some(alias) = &self.alias {
2710            query.push_str(&format!("{} ", alias));
2711        }
2712
2713        let mut args = AnyArguments::default();
2714        let mut arg_counter = 1;
2715
2716        if !self.joins_clauses.is_empty() {
2717            for join_clause in &self.joins_clauses {
2718                query.push(' ');
2719                join_clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2720            }
2721        }
2722
2723        query.push_str(" WHERE 1=1");
2724
2725        // Apply WHERE clauses
2726        for clause in &self.where_clauses {
2727            clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2728        }
2729
2730        // Apply GROUP BY
2731        if !self.group_by_clauses.is_empty() {
2732            query.push_str(&format!(" GROUP BY {}", self.group_by_clauses.join(", ")));
2733        }
2734
2735        // Apply HAVING
2736        if !self.having_clauses.is_empty() {
2737            query.push_str(" HAVING 1=1");
2738            for clause in &self.having_clauses {
2739                clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2740            }
2741        }
2742
2743        // Apply ORDER BY
2744        if !self.order_clauses.is_empty() {
2745            query.push_str(&format!(" ORDER BY {}", &self.order_clauses.join(", ")));
2746        }
2747
2748        // Always add LIMIT 1 for scalar queries
2749        query.push_str(" LIMIT 1");
2750
2751        // Print SQL query to logs if debug mode is active
2752        if self.debug_mode {
2753            log::debug!("SQL: {}", query);
2754        }
2755
2756        // Execute query and fetch one row
2757        let row = self.tx.fetch_one(&query, args).await?;
2758
2759        // Map row to the output type using FromAnyRow
2760        O::from_any_row(&row).map_err(|e| sqlx::Error::Decode(Box::new(e)))
2761    }
2762
2763    /// Updates a single column in the database.
2764    ///
2765    /// # Arguments
2766    ///
2767    /// * `col` - The column name to update
2768    /// * `value` - The new value
2769    ///
2770    /// # Returns
2771    ///
2772    /// * `Ok(u64)` - The number of rows affected
2773    pub fn update<'b, V>(&'b mut self, col: &str, value: V) -> BoxFuture<'b, Result<u64, sqlx::Error>>
2774    where
2775        V: ToString + Send + Sync,
2776    {
2777        let mut map = std::collections::HashMap::new();
2778        map.insert(col.to_string(), value.to_string());
2779        self.execute_update(map)
2780    }
2781
2782    /// Updates all columns based on the model instance.
2783    ///
2784    /// This method updates all active columns of the table with values from the provided model.
2785    ///
2786    /// # Arguments
2787    ///
2788    /// * `model` - The model instance containing new values
2789    ///
2790    /// # Returns
2791    ///
2792    /// * `Ok(u64)` - The number of rows affected
2793    pub fn updates<'b>(&'b mut self, model: &T) -> BoxFuture<'b, Result<u64, sqlx::Error>> {
2794        self.execute_update(model.to_map())
2795    }
2796
2797    /// Updates columns based on a partial model (struct implementing AnyImpl).
2798    ///
2799    /// This allows updating a subset of columns using a custom struct.
2800    /// The struct must implement `AnyImpl` (usually via `#[derive(FromAnyRow)]`).
2801    ///
2802    /// # Arguments
2803    ///
2804    /// * `partial` - The partial model containing new values
2805    ///
2806    /// # Returns
2807    ///
2808    /// * `Ok(u64)` - The number of rows affected
2809    pub fn update_partial<'b, P: AnyImpl>(&'b mut self, partial: &P) -> BoxFuture<'b, Result<u64, sqlx::Error>> {
2810        self.execute_update(partial.to_map())
2811    }
2812
2813    /// Internal helper to execute an UPDATE query from a map of values.
2814    fn execute_update<'b>(
2815        &'b mut self,
2816        data_map: std::collections::HashMap<String, String>,
2817    ) -> BoxFuture<'b, Result<u64, sqlx::Error>> {
2818        // Apply default soft delete filter if not disabled
2819        if !self.with_deleted {
2820            if let Some(soft_delete_col) = self.columns_info.iter().find(|c| c.soft_delete).map(|c| c.name) {
2821                let col_owned = soft_delete_col.to_string();
2822                let clause: FilterFn = Box::new(move |query, _args, _driver, _arg_counter| {
2823                    query.push_str(" AND ");
2824                    query.push_str(&format!("\"{}\"", col_owned));
2825                    query.push_str(" IS NULL");
2826                });
2827                self.where_clauses.push(clause);
2828            }
2829        }
2830
2831        Box::pin(async move {
2832            let table_name = self.table_name.to_snake_case();
2833            let mut query = format!("UPDATE \"{}\" ", table_name);
2834            if let Some(alias) = &self.alias {
2835                query.push_str(&format!("{} ", alias));
2836            }
2837            query.push_str("SET ");
2838
2839            let mut bindings: Vec<(String, &str)> = Vec::new();
2840            let mut set_clauses = Vec::new();
2841
2842            // Maintain argument counter for PostgreSQL ($1, $2, ...)
2843            let mut arg_counter = 1;
2844
2845            // Build SET clause
2846            for (col_name, value) in data_map {
2847                // Strip the "r#" prefix if present
2848                let col_name_clean = col_name.strip_prefix("r#").unwrap_or(&col_name).to_snake_case();
2849
2850                // Find the SQL type for this column from the Model metadata
2851                let sql_type = self
2852                    .columns_info
2853                    .iter()
2854                    .find(|c| c.name == col_name || c.name == col_name_clean)
2855                    .map(|c| c.sql_type)
2856                    .unwrap_or("TEXT");
2857
2858                // Generate placeholder
2859                let placeholder = match self.driver {
2860                    Drivers::Postgres => {
2861                        let idx = arg_counter;
2862                        arg_counter += 1;
2863
2864                        if temporal::is_temporal_type(sql_type) {
2865                            format!("${}{}", idx, temporal::get_postgres_type_cast(sql_type))
2866                        } else {
2867                            match sql_type {
2868                                "UUID" => format!("${}::UUID", idx),
2869                                "JSONB" | "jsonb" => format!("${}::JSONB", idx),
2870                                _ => format!("${}", idx),
2871                            }
2872                        }
2873                    }
2874                    _ => "?".to_string(),
2875                };
2876
2877                set_clauses.push(format!("\"{}\" = {}", col_name_clean, placeholder));
2878                bindings.push((value, sql_type));
2879            }
2880
2881            // If no fields to update, return 0
2882            if set_clauses.is_empty() {
2883                return Ok(0);
2884            }
2885
2886            query.push_str(&set_clauses.join(", "));
2887
2888            // Build WHERE clause
2889            query.push_str(" WHERE 1=1");
2890
2891            let mut args = AnyArguments::default();
2892
2893            // Bind SET values
2894            for (val_str, sql_type) in bindings {
2895                if args.bind_value(&val_str, sql_type, &self.driver).is_err() {
2896                    let _ = args.add(val_str);
2897                }
2898            }
2899
2900            // Apply WHERE clauses (appending to args and query)
2901            for clause in &self.where_clauses {
2902                clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2903            }
2904
2905            // Print SQL query to logs if debug mode is active
2906            if self.debug_mode {
2907                log::debug!("SQL: {}", query);
2908            }
2909
2910            // Execute the UPDATE query
2911            let result = self.tx.execute(&query, args).await?;
2912
2913            Ok(result.rows_affected())
2914        })
2915    }
2916
2917    /// Executes a DELETE query based on the current filters.
2918    ///
2919    /// If the model has a `#[orm(soft_delete)]` column, this method performs
2920    /// an UPDATE setting the soft delete column to the current timestamp instead
2921    /// of physically deleting the record.
2922    ///
2923    /// For permanent deletion, use `hard_delete()`.
2924    ///
2925    /// # Returns
2926    ///
2927    /// * `Ok(u64)` - The number of rows deleted (or soft-deleted)
2928    /// * `Err(sqlx::Error)` - Database error
2929    pub async fn delete(self) -> Result<u64, sqlx::Error> {
2930        // Check for soft delete column
2931        let soft_delete_col = self.columns_info.iter().find(|c| c.soft_delete).map(|c| c.name);
2932
2933        if let Some(col) = soft_delete_col {
2934            // Soft Delete: Update the column to current timestamp
2935            let table_name = self.table_name.to_snake_case();
2936            let mut query = format!("UPDATE \"{}\" ", table_name);
2937            if let Some(alias) = &self.alias {
2938                query.push_str(&format!("{} ", alias));
2939            }
2940            query.push_str(&format!("SET \"{}\" = ", col));
2941
2942            match self.driver {
2943                Drivers::Postgres => query.push_str("NOW()"),
2944                Drivers::SQLite => query.push_str("strftime('%Y-%m-%dT%H:%M:%SZ', 'now')"),
2945                Drivers::MySQL => query.push_str("NOW()"),
2946            }
2947
2948            query.push_str(" WHERE 1=1");
2949
2950            let mut args = AnyArguments::default();
2951            let mut arg_counter = 1;
2952
2953            // Apply filters
2954            for clause in &self.where_clauses {
2955                clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2956            }
2957
2958            // Print SQL query to logs if debug mode is active
2959            if self.debug_mode {
2960                log::debug!("SQL: {}", query);
2961            }
2962
2963            let result = self.tx.execute(&query, args).await?;
2964            Ok(result.rows_affected())
2965        } else {
2966            // Standard Delete (no soft delete column)
2967            let mut query = String::from("DELETE FROM \"");
2968            query.push_str(&self.table_name.to_snake_case());
2969            query.push_str("\" WHERE 1=1");
2970
2971            let mut args = AnyArguments::default();
2972            let mut arg_counter = 1;
2973
2974            for clause in &self.where_clauses {
2975                clause(&mut query, &mut args, &self.driver, &mut arg_counter);
2976            }
2977
2978            // Print SQL query to logs if debug mode is active
2979            if self.debug_mode {
2980                log::debug!("SQL: {}", query);
2981            }
2982
2983            let result = self.tx.execute(&query, args).await?;
2984            Ok(result.rows_affected())
2985        }
2986    }
2987
2988    /// Permanently removes records from the database.
2989    ///
2990    /// This method performs a physical DELETE, bypassing any soft delete logic.
2991    /// Use this when you need to permanently remove records.
2992    ///
2993    /// # Returns
2994    ///
2995    /// * `Ok(u64)` - The number of rows deleted
2996    /// * `Err(sqlx::Error)` - Database error
2997    ///
2998    /// # Example
2999    ///
3000    /// ```rust,ignore
3001    /// // Permanently delete soft-deleted records older than 30 days
3002    /// db.model::<User>()
3003    ///     .with_deleted()
3004    ///     .filter("deleted_at", "<", thirty_days_ago)
3005    ///     .hard_delete()
3006    ///     .await?;
3007    /// ```
3008    pub async fn hard_delete(self) -> Result<u64, sqlx::Error> {
3009        let mut query = String::from("DELETE FROM \"");
3010        query.push_str(&self.table_name.to_snake_case());
3011        query.push_str("\" WHERE 1=1");
3012
3013        let mut args = AnyArguments::default();
3014        let mut arg_counter = 1;
3015
3016        for clause in &self.where_clauses {
3017            clause(&mut query, &mut args, &self.driver, &mut arg_counter);
3018        }
3019
3020        // Print SQL query to logs if debug mode is active
3021        if self.debug_mode {
3022            log::debug!("SQL: {}", query);
3023        }
3024
3025        let result = self.tx.execute(&query, args).await?;
3026        Ok(result.rows_affected())
3027    }
3028}