elif_orm/query/
performance.rs

1//! Query Builder performance and optimization methods
2
3use super::builder::QueryBuilder;
4use serde_json::Value;
5
6impl<M> QueryBuilder<M> {
7    /// Get parameter bindings (for prepared statements)
8    /// Enhanced to support subqueries and complex conditions
9    pub fn bindings(&self) -> Vec<Value> {
10        let mut bindings = Vec::new();
11
12        for condition in &self.where_conditions {
13            // Skip RAW, EXISTS, NOT EXISTS conditions from parameter binding
14            if matches!(condition.column.as_str(), "RAW" | "EXISTS" | "NOT EXISTS") {
15                continue;
16            }
17
18            if let Some(value) = &condition.value {
19                // Skip subquery values (they're already formatted)
20                if let Value::String(val_str) = value {
21                    if !val_str.starts_with('(') || !val_str.ends_with(')') {
22                        bindings.push(value.clone());
23                    }
24                } else {
25                    bindings.push(value.clone());
26                }
27            }
28            bindings.extend(condition.values.clone());
29        }
30
31        for condition in &self.having_conditions {
32            if let Some(value) = &condition.value {
33                bindings.push(value.clone());
34            }
35            bindings.extend(condition.values.clone());
36        }
37
38        bindings
39    }
40
41    /// Clone this query builder for use in subqueries
42    pub fn clone_for_subquery(&self) -> Self {
43        self.clone()
44    }
45
46    /// Optimize query by analyzing conditions
47    pub fn optimize(self) -> Self {
48        // TODO: Implement query optimization strategies
49        // - Remove redundant conditions
50        // - Optimize join order
51        // - Suggest index usage
52        self
53    }
54
55    /// Get query complexity score for performance monitoring
56    pub fn complexity_score(&self) -> u32 {
57        let mut score = 0;
58
59        score += self.where_conditions.len() as u32;
60        score += self.joins.len() as u32 * 2; // Joins are more expensive
61        score += self.group_by.len() as u32;
62        score += self.having_conditions.len() as u32;
63
64        if self.distinct {
65            score += 1;
66        }
67
68        score
69    }
70}