sql-cli 1.72.0

SQL query tool for CSV/JSON with both interactive TUI and non-interactive CLI modes - perfect for exploration and automation
Documentation
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use crate::api_client::{ApiClient, QueryResponse};
use crate::cursor_aware_parser::CursorAwareParser;
use crate::parser::SqlParser;
use anyhow::Result;
use crossterm::{
    event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{
    backend::{Backend, CrosstermBackend},
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, Paragraph},
    Frame, Terminal,
};
use std::io;
use tui_input::{backend::crossterm::EventHandler, Input};

#[derive(Clone, PartialEq)]
enum AppMode {
    Command,
    Results,
}

#[derive(Clone)]
pub struct TuiApp {
    api_client: ApiClient,
    input: Input,
    mode: AppMode,
    results: Option<QueryResponse>,
    virtual_table_state: crate::virtual_table::VirtualTableState,
    show_help: bool,
    status_message: String,
    sql_parser: SqlParser,
    cursor_parser: CursorAwareParser,
}

impl TuiApp {
    #[must_use]
    pub fn new(api_url: &str) -> Self {
        Self {
            api_client: ApiClient::new(api_url),
            input: Input::default(),
            mode: AppMode::Command,
            results: None,
            virtual_table_state: crate::virtual_table::VirtualTableState::new(),
            show_help: false,
            status_message: "Ready - Type SQL query and press Enter (Enhanced parser)".to_string(),
            sql_parser: SqlParser::new(),
            cursor_parser: CursorAwareParser::new(),
        }
    }

    pub fn run<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> Result<()> {
        // Initial draw
        terminal.draw(|f| self.ui(f))?;

        loop {
            // Only redraw when we get an event
            if let Event::Key(key) = event::read()? {
                let needs_redraw = true; // We'll optimize this later
                match key.code {
                    KeyCode::Esc => {
                        if self.show_help {
                            self.show_help = false;
                        } else if self.mode == AppMode::Results {
                            self.mode = AppMode::Command;
                        } else {
                            break; // Exit app
                        }
                    }
                    KeyCode::F(1) => {
                        self.show_help = !self.show_help;
                    }
                    KeyCode::Enter => {
                        if self.mode == AppMode::Command && !self.input.value().trim().is_empty() {
                            self.execute_query();
                        }
                    }
                    KeyCode::Tab => {
                        if self.mode == AppMode::Command {
                            self.handle_tab_completion();
                        }
                    }
                    KeyCode::Up | KeyCode::Down | KeyCode::Left | KeyCode::Right => {
                        if self.mode == AppMode::Results {
                            self.handle_navigation(key.code);
                        } else if key.code == KeyCode::Up || key.code == KeyCode::Down {
                            // Could add command history here
                        } else {
                            // Handle cursor movement in input
                            self.input.handle_event(&Event::Key(key));
                        }
                    }
                    KeyCode::PageUp | KeyCode::PageDown => {
                        if self.mode == AppMode::Results {
                            self.handle_navigation(key.code);
                        }
                    }
                    KeyCode::Char('g' | 'G') => {
                        if self.mode == AppMode::Results {
                            self.handle_navigation(key.code);
                        } else {
                            self.input.handle_event(&Event::Key(key));
                        }
                    }
                    _ => {
                        if self.mode == AppMode::Command {
                            self.input.handle_event(&Event::Key(key));
                        }
                    }
                }

                // Redraw after handling the event
                if needs_redraw {
                    terminal.draw(|f| self.ui(f))?;
                }
            }
        }
        Ok(())
    }

    fn execute_query(&mut self) {
        let query = self.input.value().trim();
        self.status_message = format!("Executing: {query}");

        match self.api_client.query_trades(query) {
            Ok(response) => {
                self.results = Some(response);
                self.mode = AppMode::Results;
                self.virtual_table_state.select(0);
                self.status_message = format!(
                    "Query executed successfully - {} rows",
                    self.results.as_ref().unwrap().data.len()
                );
            }
            Err(e) => {
                self.status_message = format!("Error: {e}");
            }
        }
    }

    fn handle_tab_completion(&mut self) {
        // Basic completion - could be enhanced with proper parsing
        let input_text = self.input.value().to_string();
        let suggestions = self.get_completions(&input_text);

        if !suggestions.is_empty() {
            // For now, just complete the first suggestion
            // In a full implementation, you'd show a popup with options
            let suggestion = &suggestions[0];
            let words: Vec<&str> = input_text.split_whitespace().collect();
            if let Some(last_word) = words.last() {
                if suggestion
                    .to_lowercase()
                    .starts_with(&last_word.to_lowercase())
                {
                    let new_input =
                        format!("{}{} ", input_text.trim_end_matches(last_word), suggestion);
                    self.input = Input::from(new_input);
                    // Move cursor to end
                    while self.input.cursor() < self.input.value().len() {
                        self.input
                            .handle_event(&Event::Key(crossterm::event::KeyEvent::new(
                                KeyCode::Right,
                                KeyModifiers::NONE,
                            )));
                    }
                }
            }
        }
    }

    fn get_completions(&mut self, input: &str) -> Vec<String> {
        let cursor_pos = self.input.cursor(); // Get actual cursor position
        let result = self.cursor_parser.get_completions(input, cursor_pos);
        result.suggestions
    }

    fn handle_navigation(&mut self, key: KeyCode) {
        if let Some(results) = &self.results {
            let num_rows = results.data.len();
            if num_rows == 0 {
                return;
            }

            match key {
                KeyCode::Up => {
                    self.virtual_table_state.scroll_up(1);
                }
                KeyCode::Down => {
                    self.virtual_table_state.scroll_down(1, num_rows);
                }
                KeyCode::PageUp => {
                    self.virtual_table_state.page_up();
                }
                KeyCode::PageDown => {
                    self.virtual_table_state.page_down(num_rows);
                }
                KeyCode::Char('g') => {
                    self.virtual_table_state.goto_top();
                }
                KeyCode::Char('G') => {
                    self.virtual_table_state.goto_bottom(num_rows);
                }
                _ => {}
            }
        }
    }

    fn ui(&self, f: &mut Frame) {
        let chunks = Layout::default()
            .direction(Direction::Vertical)
            .constraints([
                Constraint::Length(3), // Command input
                Constraint::Min(5),    // Results area
                Constraint::Length(1), // Status bar
            ])
            .split(f.area());

        // Command input area
        let input_block = Block::default().borders(Borders::ALL).title("SQL Command");

        let input_style = if self.mode == AppMode::Command {
            Style::default().fg(Color::Yellow)
        } else {
            Style::default().fg(Color::Gray)
        };

        let input_paragraph = Paragraph::new(self.input.value())
            .block(input_block)
            .style(input_style);
        f.render_widget(input_paragraph, chunks[0]);

        // Set cursor position when in command mode
        if self.mode == AppMode::Command {
            f.set_cursor_position((
                chunks[0].x + self.input.visual_cursor() as u16 + 1,
                chunks[0].y + 1,
            ));
        }

        // Results area
        if let Some(results) = &self.results {
            self.render_results(f, chunks[1], results);
        } else {
            let help_text = if self.mode == AppMode::Command {
                vec![
                    Line::from("Enter your SQL query above and press Enter to execute"),
                    Line::from(""),
                    Line::from("Examples:"),
                    Line::from("  SELECT * FROM trade_deal"),
                    Line::from("  SELECT dealId, price FROM trade_deal WHERE price > 100"),
                    Line::from("  SELECT * FROM trade_deal WHERE ticker = 'AAPL'"),
                    Line::from(""),
                    Line::from("Controls:"),
                    Line::from("  Tab    - Auto-complete"),
                    Line::from("  F1     - Toggle help"),
                    Line::from("  Esc    - Exit"),
                ]
            } else {
                vec![Line::from("No results to display")]
            };

            let help_paragraph = Paragraph::new(help_text)
                .block(Block::default().borders(Borders::ALL).title("Help"))
                .wrap(ratatui::widgets::Wrap { trim: true });
            f.render_widget(help_paragraph, chunks[1]);
        }

        // Status bar
        let status_line = Line::from(vec![
            Span::styled(&self.status_message, Style::default().fg(Color::White)),
            Span::raw(" | "),
            Span::styled(
                match self.mode {
                    AppMode::Command => "CMD",
                    AppMode::Results => "VIEW",
                },
                Style::default()
                    .fg(Color::Cyan)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw(" | F1=Help | Esc=Back/Exit"),
        ]);

        let status = Paragraph::new(status_line).style(Style::default().bg(Color::DarkGray));
        f.render_widget(status, chunks[2]);

        // Help popup if active
        if self.show_help {
            self.render_help_popup(f);
        }
    }

    fn render_results(&self, f: &mut Frame, area: Rect, results: &QueryResponse) {
        let data = &results.data;
        let select_fields = &results.query.select;

        if data.is_empty() {
            let no_data = Paragraph::new("No data returned")
                .block(Block::default().borders(Borders::ALL).title("Results"));
            f.render_widget(no_data, area);
            return;
        }

        // Get headers from first record or from select fields
        let headers: Vec<String> = if select_fields.contains(&"*".to_string()) {
            if let Some(first) = data.first() {
                if let Some(obj) = first.as_object() {
                    obj.keys().cloned().collect()
                } else {
                    vec![]
                }
            } else {
                vec![]
            }
        } else {
            select_fields.clone()
        };

        // Calculate column widths
        let num_cols = headers.len();
        let col_width = if num_cols > 0 {
            (area.width.saturating_sub(2)) / num_cols as u16
        } else {
            10
        };

        let widths: Vec<Constraint> = (0..num_cols)
            .map(|_| Constraint::Length(col_width))
            .collect();

        // Use VirtualTable for efficient rendering
        let header_refs: Vec<&str> = headers.iter().map(std::string::String::as_str).collect();
        let current_row = self.virtual_table_state.selected + 1; // 1-based for display
        let virtual_table = crate::virtual_table::VirtualTable::new(header_refs, data, widths)
            .block(Block::default().borders(Borders::ALL).title(format!(
                "Results (Row {}/{}) - Use ↑↓ to navigate, Esc to return, g=top G=bottom",
                current_row,
                data.len()
            )));

        // Clone is needed here due to Rust's borrowing rules with closures
        // This is a small struct so the performance impact should be minimal
        f.render_stateful_widget(virtual_table, area, &mut self.virtual_table_state.clone());
    }

    fn render_help_popup(&self, f: &mut Frame) {
        let area = centered_rect(80, 60, f.area());
        f.render_widget(Clear, area);

        let help_text = vec![
            Line::from(vec![Span::styled(
                "SQL CLI Help",
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            )]),
            Line::from(""),
            Line::from("Command Mode:"),
            Line::from("  Enter     - Execute query"),
            Line::from("  Tab       - Auto-complete"),
            Line::from("  Esc       - Exit application"),
            Line::from(""),
            Line::from("Results Mode:"),
            Line::from("  ↑↓        - Navigate rows"),
            Line::from("  Page Up/Down - Navigate pages"),
            Line::from("  Esc       - Return to command mode"),
            Line::from(""),
            Line::from("Global:"),
            Line::from("  F1        - Toggle this help"),
            Line::from(""),
            Line::from("Example Queries:"),
            Line::from("  SELECT * FROM trade_deal"),
            Line::from("  SELECT dealId, price FROM trade_deal WHERE price > 100"),
            Line::from("  SELECT * FROM trade_deal WHERE ticker = 'AAPL'"),
            Line::from("  SELECT * FROM trade_deal WHERE counterparty.Contains('Goldman')"),
            Line::from("  SELECT * FROM trade_deal ORDER BY price DESC"),
        ];

        let help_popup = Paragraph::new(help_text)
            .block(Block::default().borders(Borders::ALL).title("Help"))
            .wrap(ratatui::widgets::Wrap { trim: true });

        f.render_widget(help_popup, area);
    }
}

// Helper function to create a centered rect
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
    let popup_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Percentage((100 - percent_y) / 2),
            Constraint::Percentage(percent_y),
            Constraint::Percentage((100 - percent_y) / 2),
        ])
        .split(r);

    Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage((100 - percent_x) / 2),
            Constraint::Percentage(percent_x),
            Constraint::Percentage((100 - percent_x) / 2),
        ])
        .split(popup_layout[1])[1]
}

pub fn run_tui_app() -> Result<()> {
    // Setup terminal
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    // Get API URL from environment or use default
    let api_url =
        std::env::var("TRADE_API_URL").unwrap_or_else(|_| "http://localhost:5000".to_string());

    // Create and run app
    let mut app = TuiApp::new(&api_url);
    let res = app.run(&mut terminal);

    // Restore terminal
    disable_raw_mode()?;
    execute!(
        terminal.backend_mut(),
        LeaveAlternateScreen,
        DisableMouseCapture
    )?;
    terminal.show_cursor()?;

    if let Err(_err) = res {
        // Error handled in TUI status message instead of stdout
    }

    Ok(())
}