sql_cli/services/
application_orchestrator.rs1use crate::services::{DataLoaderService, QueryOrchestrator};
2use crate::ui::enhanced_tui::EnhancedTuiApp;
3use anyhow::Result;
4use tracing::info;
5
6pub struct ApplicationOrchestrator {
9 data_loader: DataLoaderService,
10 query_orchestrator: QueryOrchestrator,
11}
12
13impl ApplicationOrchestrator {
14 #[must_use]
15 pub fn new(case_insensitive: bool, auto_hide_empty: bool) -> Self {
16 Self {
17 data_loader: DataLoaderService::new(case_insensitive),
18 query_orchestrator: QueryOrchestrator::new(case_insensitive, auto_hide_empty),
19 }
20 }
21
22 pub fn create_tui_with_file(&self, file_path: &str) -> Result<EnhancedTuiApp> {
25 info!("Creating TUI with file: {}", file_path);
26
27 let load_result = self.data_loader.load_file(file_path)?;
29
30 let status_message = load_result.status_message();
32 let source_path = load_result.source_path.clone();
33 let table_name = load_result.table_name.clone();
34 let raw_table_name = load_result.raw_table_name.clone();
35
36 let mut app = EnhancedTuiApp::new_with_dataview(load_result.dataview, &source_path)?;
38
39 app.set_status_message(status_message);
41
42 app.set_sql_query(&table_name, &raw_table_name);
44
45 Ok(app)
46 }
47
48 pub fn load_additional_file(&self, app: &mut EnhancedTuiApp, file_path: &str) -> Result<()> {
50 info!("Loading additional file: {}", file_path);
51
52 let load_result = self.data_loader.load_file(file_path)?;
54
55 let status_message = load_result.status_message();
57 let source_path = load_result.source_path.clone();
58 let table_name = load_result.table_name.clone();
59 let raw_table_name = load_result.raw_table_name.clone();
60
61 app.add_dataview(load_result.dataview, &source_path)?;
63
64 app.set_status_message(status_message);
66
67 app.set_sql_query(&table_name, &raw_table_name);
69
70 Ok(())
71 }
72
73 pub fn execute_query(&mut self, app: &mut EnhancedTuiApp, query: &str) -> Result<()> {
77 app.execute_query_v2(query)
80 }
81}
82
83pub struct ApplicationOrchestratorBuilder {
85 case_insensitive: bool,
86 auto_hide_empty: bool,
87}
88
89impl Default for ApplicationOrchestratorBuilder {
90 fn default() -> Self {
91 Self::new()
92 }
93}
94
95impl ApplicationOrchestratorBuilder {
96 #[must_use]
97 pub fn new() -> Self {
98 Self {
99 case_insensitive: false,
100 auto_hide_empty: false,
101 }
102 }
103
104 #[must_use]
105 pub fn with_case_insensitive(mut self, value: bool) -> Self {
106 self.case_insensitive = value;
107 self
108 }
109
110 #[must_use]
111 pub fn with_auto_hide_empty(mut self, value: bool) -> Self {
112 self.auto_hide_empty = value;
113 self
114 }
115
116 #[must_use]
117 pub fn build(self) -> ApplicationOrchestrator {
118 ApplicationOrchestrator::new(self.case_insensitive, self.auto_hide_empty)
119 }
120}