sql_cli/ui/utils/enhanced_tui_helpers.rs
1// Helper functions extracted from enhanced_tui.rs
2// These are pure functions with no dependencies on self
3
4/// Sanitize table name by removing special characters and limiting length
5#[must_use]
6pub fn sanitize_table_name(name: &str) -> String {
7 // Replace spaces and other problematic characters with underscores
8 // to create SQL-friendly table names
9 // Examples: "Business Crime Borough Level" -> "Business_Crime_Borough_Level"
10 let sanitized: String = name
11 .trim()
12 .chars()
13 .map(|c| {
14 if c.is_alphanumeric() || c == '_' {
15 c
16 } else {
17 '_'
18 }
19 })
20 .collect();
21
22 // If the sanitized name is too complex (too long or has too many underscores),
23 // fall back to a simple default name
24 const MAX_LENGTH: usize = 30;
25 const MAX_UNDERSCORES: usize = 5;
26
27 let underscore_count = sanitized.chars().filter(|&c| c == '_').count();
28
29 if sanitized.len() > MAX_LENGTH || underscore_count > MAX_UNDERSCORES {
30 // Use a simple fallback name
31 "data".to_string()
32 } else if sanitized.is_empty() || sanitized.chars().all(|c| c == '_') {
33 // If the name is empty or all underscores after sanitization
34 "data".to_string()
35 } else {
36 sanitized
37 }
38}