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