heliosdb-nano 3.30.0

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! REPL shell implementation

use crate::{EmbeddedDatabase, Result, Error};
use super::{ReplConfig, MetaCommand, commands::MetaCommandResult};
use super::completer::SqlCompleter;
use super::formatter;
use rustyline::error::ReadlineError;
use rustyline::Editor;
use rustyline::Helper;
use rustyline::validate::Validator;
use rustyline::highlight::Highlighter;
use rustyline::hint::Hinter;
use rustyline::history::FileHistory;
use colored::Colorize;
use std::time::Instant;

/// REPL shell
pub struct ReplShell {
    db: EmbeddedDatabase,
    config: ReplConfig,
    editor: Editor<ReplHelper, FileHistory>,
    show_timing: bool,
    current_branch: String,
    show_lsn: bool,
}

/// Helper for rustyline
struct ReplHelper {
    completer: SqlCompleter,
}

impl Helper for ReplHelper {}
impl Validator for ReplHelper {}
impl Highlighter for ReplHelper {}
impl Hinter for ReplHelper {
    type Hint = String;
}

impl rustyline::completion::Completer for ReplHelper {
    type Candidate = rustyline::completion::Pair;

    fn complete(
        &self,
        line: &str,
        pos: usize,
        ctx: &rustyline::Context<'_>,
    ) -> rustyline::Result<(usize, Vec<Self::Candidate>)> {
        self.completer.complete(line, pos, ctx)
    }
}

impl ReplShell {
    /// Get a reference to the database
    ///
    /// Useful for dump on shutdown functionality
    pub fn db(&self) -> &EmbeddedDatabase {
        &self.db
    }

    /// Strip SQL comments from input
    /// Handles both line comments (-- ...) and block comments (/* ... */)
    #[allow(clippy::indexing_slicing)]
    // SAFETY: All `chars[i]` and `chars[i+1]` accesses are guarded by `i < chars.len()` and `i+1 < chars.len()`
    fn strip_sql_comments(sql: &str) -> String {
        let mut result = String::with_capacity(sql.len());
        let chars: Vec<char> = sql.chars().collect();
        let mut i = 0;
        let mut in_single_quote = false;
        let mut in_double_quote = false;

        while i < chars.len() {
            // Handle string literals (don't strip comments inside strings)
            if chars[i] == '\'' && !in_double_quote {
                in_single_quote = !in_single_quote;
                result.push(chars[i]);
                i += 1;
                continue;
            }
            if chars[i] == '"' && !in_single_quote {
                in_double_quote = !in_double_quote;
                result.push(chars[i]);
                i += 1;
                continue;
            }

            // Skip comments only when not inside a string
            if !in_single_quote && !in_double_quote {
                // Line comment: -- until end of line
                if i + 1 < chars.len() && chars[i] == '-' && chars[i + 1] == '-' {
                    // Skip to end of line
                    while i < chars.len() && chars[i] != '\n' {
                        i += 1;
                    }
                    // Keep the newline if it exists
                    if i < chars.len() {
                        result.push('\n');
                        i += 1;
                    }
                    continue;
                }
                // Block comment: /* ... */
                if i + 1 < chars.len() && chars[i] == '/' && chars[i + 1] == '*' {
                    i += 2; // Skip /*
                    // Find closing */
                    while i + 1 < chars.len() && !(chars[i] == '*' && chars[i + 1] == '/') {
                        i += 1;
                    }
                    if i + 1 < chars.len() {
                        i += 2; // Skip */
                    }
                    // Add a space to prevent tokens from merging
                    result.push(' ');
                    continue;
                }
            }

            result.push(chars[i]);
            i += 1;
        }

        result
    }

    /// Create a new REPL shell
    pub fn new(db: EmbeddedDatabase, config: ReplConfig) -> Result<Self> {
        let mut completer = SqlCompleter::new();

        // Load table names for completion
        if let Ok(tables) = db.storage.catalog().list_tables() {
            completer.set_table_names(tables);
        }

        let helper = ReplHelper { completer };

        let mut editor = Editor::new()
            .map_err(|e| Error::Generic(format!("Failed to create editor: {}", e)))?;
        editor.set_helper(Some(helper));

        // Load history
        if let Some(history_path) = &config.history_path {
            let _ = editor.load_history(history_path);
        }

        let show_timing = config.show_timing;

        Ok(Self {
            db,
            config,
            editor,
            show_timing,
            current_branch: "main".to_string(),
            show_lsn: false,
        })
    }

    /// Run the REPL
    pub fn run(&mut self) -> Result<()> {
        self.print_banner();

        let mut multi_line_buffer = String::new();

        loop {
            // Determine prompt based on whether we're in multi-line mode
            let prompt = if multi_line_buffer.is_empty() {
                format!(
                    "{} {} ",
                    "heliosdb".green().bold(),
                    format!("[{}]", &self.current_branch).cyan()
                ) + ">"
            } else {
                "       -> ".yellow().to_string()
            };

            // Read line
            match self.editor.readline(&prompt) {
                Ok(line) => {
                    let trimmed = line.trim();

                    // Skip empty lines
                    if trimmed.is_empty() {
                        continue;
                    }

                    // Add to history
                    let _ = self.editor.add_history_entry(&line);

                    // Check for meta commands (only when not in multi-line mode)
                    if multi_line_buffer.is_empty() && trimmed.starts_with('\\') {
                        if let Some(meta_cmd) = MetaCommand::parse(trimmed) {
                            match meta_cmd.execute(&self.db, self.show_timing, Some(&self.config)) {
                                Ok(MetaCommandResult::Quit) => {
                                    println!("Goodbye!");
                                    break;
                                }
                                Ok(MetaCommandResult::ToggleTiming(new_state)) => {
                                    self.show_timing = new_state;
                                }
                                Ok(MetaCommandResult::SwitchBranch(branch_name)) => {
                                    self.current_branch = branch_name.clone();
                                    // Sync branch context to storage layer
                                    self.db.storage.set_current_branch(Some(branch_name.clone()));

                                    // Branch data isolation is now active
                                    if branch_name != "main" {
                                        println!("{}", format!("Switched to branch '{}' with data isolation enabled.", branch_name).green());
                                        println!("{}", "  Data changes on this branch are isolated from main.".dimmed());
                                    }
                                }
                                Ok(MetaCommandResult::ToggleLsn(_)) => {
                                    self.show_lsn = !self.show_lsn;
                                }
                                Ok(MetaCommandResult::ConfigReloaded(new_config)) => {
                                    // Apply the new configuration
                                    self.show_timing = new_config.show_timing;
                                    self.config = new_config;
                                }
                                Ok(MetaCommandResult::Continue) => {}
                                Err(e) => {
                                    eprintln!("{}", formatter::format_error(&e.to_string()));
                                }
                            }
                            continue;
                        } else {
                            eprintln!("{}", formatter::format_error(&format!(
                                "Unknown meta command: {}. Type \\h for help.",
                                trimmed
                            )));
                            continue;
                        }
                    }

                    // Strip comments from the line for proper semicolon detection
                    let stripped_line = Self::strip_sql_comments(trimmed);
                    let stripped_trimmed = stripped_line.trim();

                    // Skip comment-only lines entirely - don't add to buffer
                    if stripped_trimmed.is_empty() {
                        continue;
                    }

                    // Add stripped content to multi-line buffer
                    if !multi_line_buffer.is_empty() {
                        multi_line_buffer.push('\n');
                    }
                    multi_line_buffer.push_str(stripped_trimmed);

                    // Check if statement is complete (ends with semicolon after stripping comments)
                    if stripped_trimmed.ends_with(';') {
                        // Execute the statement
                        self.execute_sql(&multi_line_buffer);

                        // Clear buffer
                        multi_line_buffer.clear();

                        // Update table names for completion
                        if let Ok(tables) = self.db.storage.catalog().list_tables() {
                            if let Some(helper) = self.editor.helper_mut() {
                                helper.completer.set_table_names(tables);
                            }
                        }
                    }
                }
                Err(ReadlineError::Interrupted) => {
                    // Ctrl-C - clear multi-line buffer
                    if !multi_line_buffer.is_empty() {
                        println!("^C");
                        multi_line_buffer.clear();
                    } else {
                        println!("Use \\q or Ctrl-D to exit");
                    }
                }
                Err(ReadlineError::Eof) => {
                    // Ctrl-D - exit
                    println!("Goodbye!");
                    break;
                }
                Err(err) => {
                    eprintln!("{}", formatter::format_error(&format!("Error: {}", err)));
                    break;
                }
            }
        }

        // Save history
        if let Some(history_path) = &self.config.history_path {
            let _ = self.editor.save_history(history_path);
        }

        Ok(())
    }

    /// Execute a SQL statement (expects comments already stripped)
    fn execute_sql(&mut self, sql: &str) {
        // Skip whitespace-only input (comments already stripped before calling this)
        if sql.trim().is_empty() {
            return;
        }

        let start = Instant::now();

        // Check for USE BRANCH statement
        if crate::sql::Parser::is_use_branch(sql) {
            match crate::sql::Parser::parse_use_branch_sql(sql) {
                Ok(branch_name) => {
                    self.current_branch = branch_name.clone();
                    // Sync branch context to storage layer
                    self.db.storage.set_current_branch(Some(branch_name.clone()));
                    println!("{}", format!("Switched to branch: {}", branch_name).green());

                    // Show isolation status
                    if branch_name != "main" {
                        println!("{}", "  Data isolation enabled for this branch.".dimmed());
                    }

                    if self.show_timing {
                        println!("{}", formatter::format_timing(start.elapsed().as_secs_f64()));
                    }
                    return;
                }
                Err(e) => {
                    eprintln!("{}", formatter::format_error(&e.to_string()));
                    return;
                }
            }
        }

        // Try to determine if this is a query (SELECT) or a command (INSERT, UPDATE, etc.)
        // Also treat UPDATE/DELETE/INSERT with RETURNING as queries.
        // EXPLAIN also returns results, so treat it as a query.
        // PRAGMA table_info(...) returns rows; route it through the query path.
        let sql_upper = sql.trim().to_uppercase();
        let has_returning = sql_upper.contains(" RETURNING ");
        let is_query = sql_upper.starts_with("SELECT")
            || sql_upper.starts_with("WITH")
            || sql_upper.starts_with("TABLE")
            || sql_upper.starts_with("VALUES")
            || sql_upper.starts_with("EXPLAIN")
            || sql_upper.starts_with("PRAGMA")
            // SHOW BRANCHES / SHOW DATABASE BRANCHES / SHOW <var> all
            // produce result rows; without this the executor still
            // returns the tuples but `db.execute()` discards them and
            // only the row count surfaces ("Query OK, N row(s) affected").
            || sql_upper.starts_with("SHOW")
            || has_returning;

        if is_query {
            // PRAGMA short-circuit: sqlparser doesn't recognise PRAGMA, so
            // we synthesise a fixed schema for `table_info(t)` and an empty
            // result schema for connection-tunable PRAGMAs (foreign_keys,
            // journal_mode, …) — everything still goes through `db.query`.
            if sql_upper.starts_with("PRAGMA") {
                if let Some((name, _arg)) = crate::sql::sqlite_compat::parse_pragma(sql) {
                    let schema = if name.eq_ignore_ascii_case("table_info") {
                        crate::Schema::new(vec![
                            crate::Column::new("cid", crate::DataType::Int4),
                            crate::Column::new("name", crate::DataType::Text),
                            crate::Column::new("type", crate::DataType::Text),
                            crate::Column::new("notnull", crate::DataType::Int4),
                            crate::Column::new("dflt_value", crate::DataType::Text),
                            crate::Column::new("pk", crate::DataType::Int4),
                        ])
                    } else {
                        crate::Schema::new(vec![])
                    };
                    match self.db.query(sql, &[]) {
                        Ok(results) => {
                            let duration = start.elapsed();
                            println!("\n{}", formatter::format_results(&results, &schema));
                            if self.show_timing {
                                println!("{}", formatter::format_timing(duration.as_secs_f64()));
                            }
                        }
                        Err(e) => eprintln!("{}", formatter::format_error(&e.to_string())),
                    }
                    return;
                }
            }

            // Execute query
            match self.db.query(sql, &[]) {
                Ok(results) => {
                    let duration = start.elapsed();

                    // Get schema from the first tuple if available
                    if let Ok(parser) = crate::sql::Parser::new().parse_one(sql) {
                        let catalog = self.db.storage.catalog();
                        let planner = crate::sql::Planner::with_catalog(&catalog);
                        if let Ok(plan) = planner.statement_to_plan(parser) {
                            let schema = plan.schema();
                            println!("\n{}", formatter::format_results(&results, &schema));
                            if self.show_timing {
                                println!("{}", formatter::format_timing(duration.as_secs_f64()));
                            }
                            if self.show_lsn {
                                if let Some(lsn) = self.db.current_lsn() {
                                    println!("{}", format!("LSN: {}", lsn).dimmed());
                                }
                            }
                            return;
                        }
                    }

                    // Fallback: just show tuple count
                    println!("{}", format!("Query returned {} row(s)", results.len()).dimmed());
                    if self.show_timing {
                        println!("{}", formatter::format_timing(duration.as_secs_f64()));
                    }
                    if self.show_lsn {
                        if let Some(lsn) = self.db.current_lsn() {
                            println!("{}", format!("LSN: {}", lsn).dimmed());
                        }
                    }
                }
                Err(e) => {
                    eprintln!("{}", formatter::format_error(&e.to_string()));
                }
            }
        } else {
            // Execute command
            match self.db.execute(sql) {
                Ok(affected) => {
                    let duration = start.elapsed();

                    let message = if sql_upper.starts_with("CREATE") {
                        "Query OK".to_string()
                    } else if sql_upper.starts_with("DROP") {
                        "Query OK".to_string()
                    } else {
                        format!("Query OK, {} row(s) affected", affected)
                    };

                    println!("{}", message.green());
                    if self.show_timing {
                        println!("{}", formatter::format_timing(duration.as_secs_f64()));
                    }
                    if self.show_lsn {
                        if let Some(lsn) = self.db.current_lsn() {
                            println!("{}", format!("LSN: {}", lsn).dimmed());
                        }
                    }
                }
                Err(e) => {
                    eprintln!("{}", formatter::format_error(&e.to_string()));
                }
            }
        }
    }

    /// Print welcome banner
    fn print_banner(&self) {
        println!();
        println!("╔═══════════════════════════════════════════════════════════════╗");
        println!("{} {}{}",
            "HeliosDB Nano".bold().cyan(),
            format!("v{}", env!("CARGO_PKG_VERSION")).bold().green(),
            ""
        );
        println!("{}", "PostgreSQL-compatible database with enterprise features".dimmed());
        println!("╚═══════════════════════════════════════════════════════════════╝");
        println!();

        // Current version features
        println!("{}", "Key Features:".bold());
        println!("  {} PostgreSQL wire protocol compatible", "".cyan());
        println!("  {} Multi-tenancy with Row-Level Security (RLS)", "".cyan());
        println!("  {} Change Data Capture (CDC) for migrations", "".cyan());
        println!("  {} Database branching & time-travel queries", "".cyan());
        println!("  {} Vector search with Product Quantization", "".cyan());
        println!("  {} Encryption at rest (AES-256-GCM)", "".cyan());
        println!();

        println!("Mode: {} (single-user, direct access)", "REPL".bold().yellow());
        println!("      For multi-user access, use: {} {}", "heliosdb-nano start".cyan(), "--help".dimmed());
        println!();

        println!("Commands: {} help | {} list tables | {} system views | {} quit",
            "\\h".cyan(), "\\d".cyan(), "\\dS".cyan(), "\\q".cyan());
        println!();
    }
}