elif_orm/query/
select.rs

1//! Query Builder SELECT operations
2
3use serde_json::Value;
4use super::builder::QueryBuilder;
5use super::types::*;
6
7impl<M> QueryBuilder<M> {
8    /// Add SELECT fields to the query
9    pub fn select(mut self, fields: &str) -> Self {
10        if fields == "*" {
11            self.select_fields.push("*".to_string());
12        } else {
13            self.select_fields.extend(
14                fields
15                    .split(',')
16                    .map(|f| f.trim().to_string())
17                    .collect::<Vec<String>>()
18            );
19        }
20        self
21    }
22
23    /// Add SELECT DISTINCT to the query
24    pub fn select_distinct(mut self, fields: &str) -> Self {
25        self.distinct = true;
26        self.select(fields)
27    }
28
29    /// Set the FROM table
30    pub fn from(mut self, table: &str) -> Self {
31        self.from_tables = vec![table.to_string()];
32        self
33    }
34
35    /// Add aggregate functions to SELECT
36    pub fn select_count(mut self, column: &str, alias: Option<&str>) -> Self {
37        let select_expr = if let Some(alias) = alias {
38            format!("COUNT({}) AS {}", column, alias)
39        } else {
40            format!("COUNT({})", column)
41        };
42        self.select_fields.push(select_expr);
43        self
44    }
45    
46    /// Add SUM aggregate
47    pub fn select_sum(mut self, column: &str, alias: Option<&str>) -> Self {
48        let select_expr = if let Some(alias) = alias {
49            format!("SUM({}) AS {}", column, alias)
50        } else {
51            format!("SUM({})", column)
52        };
53        self.select_fields.push(select_expr);
54        self
55    }
56    
57    /// Add AVG aggregate
58    pub fn select_avg(mut self, column: &str, alias: Option<&str>) -> Self {
59        let select_expr = if let Some(alias) = alias {
60            format!("AVG({}) AS {}", column, alias)
61        } else {
62            format!("AVG({})", column)
63        };
64        self.select_fields.push(select_expr);
65        self
66    }
67    
68    /// Add MIN aggregate
69    pub fn select_min(mut self, column: &str, alias: Option<&str>) -> Self {
70        let select_expr = if let Some(alias) = alias {
71            format!("MIN({}) AS {}", column, alias)
72        } else {
73            format!("MIN({})", column)
74        };
75        self.select_fields.push(select_expr);
76        self
77    }
78    
79    /// Add MAX aggregate
80    pub fn select_max(mut self, column: &str, alias: Option<&str>) -> Self {
81        let select_expr = if let Some(alias) = alias {
82            format!("MAX({}) AS {}", column, alias)
83        } else {
84            format!("MAX({})", column)
85        };
86        self.select_fields.push(select_expr);
87        self
88    }
89    
90    /// Add custom SELECT expression
91    pub fn select_raw(mut self, expression: &str) -> Self {
92        self.select_fields.push(expression.to_string());
93        self
94    }
95}