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
5pub fn sanitize_table_name(name: &str) -> String {
6 // Replace spaces and other problematic characters with underscores
7 // to create SQL-friendly table names
8 // Examples: "Business Crime Borough Level" -> "Business_Crime_Borough_Level"
9 let sanitized: String = name
10 .trim()
11 .chars()
12 .map(|c| {
13 if c.is_alphanumeric() || c == '_' {
14 c
15 } else {
16 '_'
17 }
18 })
19 .collect();
20
21 // If the sanitized name is too complex (too long or has too many underscores),
22 // fall back to a simple default name
23 const MAX_LENGTH: usize = 30;
24 const MAX_UNDERSCORES: usize = 5;
25
26 let underscore_count = sanitized.chars().filter(|&c| c == '_').count();
27
28 if sanitized.len() > MAX_LENGTH || underscore_count > MAX_UNDERSCORES {
29 // Use a simple fallback name
30 "data".to_string()
31 } else if sanitized.is_empty() || sanitized.chars().all(|c| c == '_') {
32 // If the name is empty or all underscores after sanitization
33 "data".to_string()
34 } else {
35 sanitized
36 }
37}