contextdb-cli 0.3.3

Interactive CLI for contextdb — explore relational, graph, and vector queries in a REPL
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
use crate::formatter::format_query_result;
use contextdb_core::{ColumnType, Error, TableMeta};
use contextdb_engine::Database;
use contextdb_engine::sync_types::{ConflictPolicy, SyncDirection};
use contextdb_server::{SyncClient, SyncPlugin};
use rustyline::DefaultEditor;
use rustyline::error::ReadlineError;
use std::collections::HashMap;
use std::io::{BufRead, IsTerminal};
use std::sync::Arc;

/// Run the REPL loop. Returns `true` if all commands succeeded, `false` if any error occurred.
pub fn run(
    db: Arc<Database>,
    sync_client: Option<&SyncClient>,
    rt: Option<&tokio::runtime::Runtime>,
    sync_plugin: Option<&SyncPlugin>,
) -> bool {
    let interactive = std::io::stdin().is_terminal();
    if interactive {
        eprintln!("ContextDB v{}", env!("CARGO_PKG_VERSION"));
        eprintln!("Enter .help for usage hints.");
        return run_interactive(&db, sync_client, rt, sync_plugin);
    }

    run_scripted(&db, sync_client, rt, sync_plugin)
}

fn run_interactive(
    db: &Database,
    sync_client: Option<&SyncClient>,
    rt: Option<&tokio::runtime::Runtime>,
    sync_plugin: Option<&SyncPlugin>,
) -> bool {
    let mut rl = DefaultEditor::new().expect("failed to initialize readline");
    let mut had_error = false;

    loop {
        let readline = rl.readline("contextdb> ");
        match readline {
            Ok(line) => {
                let line = line.trim();
                if line.is_empty() {
                    continue;
                }
                let _ = rl.add_history_entry(line);
                if !process_input_line(db, sync_client, rt, line, true, sync_plugin, &mut had_error)
                {
                    break;
                }
            }
            Err(ReadlineError::Interrupted | ReadlineError::Eof) => break,
            Err(_) => break,
        }
    }

    !had_error
}

fn run_scripted(
    db: &Database,
    sync_client: Option<&SyncClient>,
    rt: Option<&tokio::runtime::Runtime>,
    sync_plugin: Option<&SyncPlugin>,
) -> bool {
    let mut had_error = false;
    let stdin = std::io::stdin();
    for line in stdin.lock().lines() {
        let line = match line {
            Ok(line) => line,
            Err(_) => break,
        };
        let line = line.trim();
        if line.is_empty() {
            continue;
        }
        if !process_input_line(
            db,
            sync_client,
            rt,
            line,
            false,
            sync_plugin,
            &mut had_error,
        ) {
            break;
        }
    }
    !had_error
}

fn process_input_line(
    db: &Database,
    sync_client: Option<&SyncClient>,
    rt: Option<&tokio::runtime::Runtime>,
    line: &str,
    interactive: bool,
    sync_plugin: Option<&SyncPlugin>,
    had_error: &mut bool,
) -> bool {
    if line.starts_with(".sync") || line.starts_with("\\sync") {
        let mut parts = line.splitn(2, ' ');
        let _cmd = parts.next();
        let rest = parts.next().unwrap_or("").trim();
        let outcome = run_sync_command(sync_client, rt, rest, sync_plugin);
        println!("{}", outcome.message);
        if !outcome.ok {
            *had_error = true;
        }
    } else if line.starts_with('.') || line.starts_with('\\') {
        if !handle_meta_command(db, sync_client, rt, line, sync_plugin) {
            return false;
        }
    } else {
        let upper = line.trim_start().to_uppercase();
        if !interactive && upper.starts_with("INSERT") {
            println!("{line}");
        }
        if !execute_sql(db, line) {
            *had_error = true;
        }
    }
    true
}

struct SyncCommandOutcome {
    message: String,
    ok: bool,
}

pub(crate) fn handle_meta_command(
    db: &Database,
    sync_client: Option<&SyncClient>,
    rt: Option<&tokio::runtime::Runtime>,
    line: &str,
    sync_plugin: Option<&SyncPlugin>,
) -> bool {
    let mut parts = line.splitn(2, ' ');
    let cmd = parts.next().unwrap_or("");
    let rest = parts.next().unwrap_or("").trim();

    match cmd {
        ".quit" | ".exit" | "\\q" => return false,
        ".help" | "\\?" => {
            println!(".help / \\?          Show this message");
            println!(".quit/.exit / \\q    Exit REPL");
            println!(".tables / \\dt       List tables");
            println!(".schema / \\d <tbl>  Show table schema and constraints");
            println!(".explain <sql>      Show execution plan");
            println!(".sync status              Show sync connection info");
            println!(".sync push                Push local changes to server");
            println!(".sync pull                Pull remote changes from server");
            println!(".sync reconnect           Reconnect to NATS");
            println!(".sync direction <t> <d>   Set table sync direction (Push|Pull|Both|None)");
            println!(
                ".sync policy <t> <p>      Set table conflict policy (InsertIfNotExists|ServerWins|EdgeWins|LatestWins)"
            );
            println!(".sync policy default <p>  Set default conflict policy");
            println!(".sync auto [on|off]       Toggle auto-sync after DML");
        }
        ".tables" | "\\dt" => {
            for t in db.table_names() {
                println!("{t}");
            }
        }
        ".schema" | "\\d" => {
            if rest.is_empty() {
                eprintln!("Usage: .schema <table> or \\d <table>");
            } else if let Some(meta) = db.table_meta(rest) {
                print_table_meta(rest, &meta);
            } else {
                eprintln!("Table not found: {rest}");
            }
        }
        ".explain" => {
            if rest.is_empty() {
                eprintln!("Usage: .explain <sql>");
            } else {
                match db.explain(rest) {
                    Ok(plan) => println!("{}", plan),
                    Err(e) => {
                        if is_fatal_cli_error(&e) {
                            eprintln!("Error: {}", e);
                        } else {
                            println!("Error: {}", e);
                        }
                    }
                }
            }
        }
        ".sync" | "\\sync" => {
            println!(
                "{}",
                handle_sync_command(sync_client, rt, rest, sync_plugin)
            );
        }
        _ => println!("Unknown command: {}. Type \\? for help.", cmd),
    }

    true
}

fn handle_sync_command(
    sync_client: Option<&SyncClient>,
    rt: Option<&tokio::runtime::Runtime>,
    args: &str,
    sync_plugin: Option<&SyncPlugin>,
) -> String {
    run_sync_command(sync_client, rt, args, sync_plugin).message
}

fn run_sync_command(
    sync_client: Option<&SyncClient>,
    rt: Option<&tokio::runtime::Runtime>,
    args: &str,
    sync_plugin: Option<&SyncPlugin>,
) -> SyncCommandOutcome {
    let (Some(client), Some(rt)) = (sync_client, rt) else {
        return SyncCommandOutcome {
            message: "Sync not configured. Start with --tenant-id to enable.".to_string(),
            ok: true,
        };
    };

    let parts: Vec<&str> = args.split_whitespace().collect();
    let sub = parts.first().copied().unwrap_or("status");

    match sub {
        "status" => {
            let connected = rt.block_on(client.ensure_connected()).is_ok();
            let status = if connected {
                "connected"
            } else {
                "unreachable"
            };
            SyncCommandOutcome {
                message: format!(
                    "Sync: tenant={}, url={}\nNATS: {status}\nDatabase LSN: {}\nPush watermark: LSN {}\nPull watermark: LSN {}",
                    client.tenant_id(),
                    client.nats_url(),
                    client.db().current_lsn(),
                    client.push_watermark(),
                    client.pull_watermark()
                ),
                ok: true,
            }
        }
        "push" => match rt.block_on(client.push()) {
            Ok(result) => {
                let mut msg = format!(
                    "Pushed: {} applied, {} skipped, {} conflicts",
                    result.applied_rows,
                    result.skipped_rows,
                    result.conflicts.len()
                );
                for conflict in &result.conflicts {
                    if let Some(reason) = &conflict.reason {
                        msg.push_str(&format!("\n  conflict: {}", reason));
                    }
                }
                SyncCommandOutcome {
                    message: msg,
                    ok: true,
                }
            }
            Err(e) => SyncCommandOutcome {
                message: format!("Push failed: {e}"),
                ok: false,
            },
        },
        "pull" => match rt.block_on(client.pull_default()) {
            Ok(result) => {
                let mut msg = format!(
                    "Pulled: {} applied, {} skipped, {} conflicts",
                    result.applied_rows,
                    result.skipped_rows,
                    result.conflicts.len()
                );
                for conflict in &result.conflicts {
                    if let Some(reason) = &conflict.reason {
                        msg.push_str(&format!("\n  conflict: {}", reason));
                    }
                }
                SyncCommandOutcome {
                    message: msg,
                    ok: true,
                }
            }
            Err(e) => SyncCommandOutcome {
                message: format!("Pull failed: {e}"),
                ok: false,
            },
        },
        "reconnect" => {
            rt.block_on(client.reconnect());
            let connected = rt.block_on(client.is_connected());
            if connected {
                SyncCommandOutcome {
                    message: "Reconnected to NATS".to_string(),
                    ok: true,
                }
            } else {
                SyncCommandOutcome {
                    message: "Reconnection failed — NATS unreachable".to_string(),
                    ok: false,
                }
            }
        }
        "direction" => {
            if parts.len() != 3 {
                return SyncCommandOutcome {
                    message: "Usage: .sync direction <table> <Push|Pull|Both|None>".to_string(),
                    ok: true,
                };
            }
            let table = parts[1];
            let dir = match parts[2] {
                "Push" | "push" => SyncDirection::Push,
                "Pull" | "pull" => SyncDirection::Pull,
                "Both" | "both" => SyncDirection::Both,
                "None" | "none" => SyncDirection::None,
                other => {
                    return SyncCommandOutcome {
                        message: format!("Unknown direction: {other}. Use: Push, Pull, Both, None"),
                        ok: true,
                    };
                }
            };
            client.set_table_direction(table, dir);
            SyncCommandOutcome {
                message: format!("{table} -> {dir:?}"),
                ok: true,
            }
        }
        "policy" => {
            if parts.len() != 3 {
                return SyncCommandOutcome {
                    message: "Usage: .sync policy <table> <InsertIfNotExists|ServerWins|EdgeWins|LatestWins>\n       .sync policy default <policy>".to_string(),
                    ok: true,
                };
            }
            let policy = match parts[2] {
                "InsertIfNotExists" => ConflictPolicy::InsertIfNotExists,
                "ServerWins" => ConflictPolicy::ServerWins,
                "EdgeWins" => ConflictPolicy::EdgeWins,
                "LatestWins" => ConflictPolicy::LatestWins,
                other => {
                    return SyncCommandOutcome {
                        message: format!(
                            "Unknown policy: {other}. Use: InsertIfNotExists, ServerWins, EdgeWins, LatestWins"
                        ),
                        ok: true,
                    };
                }
            };
            if parts[1] == "default" {
                client.set_default_conflict_policy(policy);
                SyncCommandOutcome {
                    message: format!("Default conflict policy -> {policy:?}"),
                    ok: true,
                }
            } else {
                client.set_conflict_policy(parts[1], policy);
                SyncCommandOutcome {
                    message: format!("{} -> {policy:?}", parts[1]),
                    ok: true,
                }
            }
        }
        "auto" => {
            let Some(plugin) = sync_plugin else {
                return SyncCommandOutcome {
                    message: "Auto-sync not available (no sync plugin)".to_string(),
                    ok: true,
                };
            };
            let toggle = parts.get(1).copied().unwrap_or("");
            match toggle {
                "on" => {
                    plugin.set_auto(true);
                    SyncCommandOutcome {
                        message: "Auto-sync enabled".to_string(),
                        ok: true,
                    }
                }
                "off" => {
                    plugin.set_auto(false);
                    SyncCommandOutcome {
                        message: "Auto-sync disabled".to_string(),
                        ok: true,
                    }
                }
                "" => {
                    let state = if plugin.is_auto() { "on" } else { "off" };
                    SyncCommandOutcome {
                        message: format!("Auto-sync: {state}"),
                        ok: true,
                    }
                }
                other => SyncCommandOutcome {
                    message: format!("Unknown auto-sync option: {other}. Use: on, off"),
                    ok: true,
                },
            }
        }
        _ => SyncCommandOutcome {
            message: format!(
                "Unknown sync command: {sub}. Try: status, push, pull, reconnect, direction, policy, auto"
            ),
            ok: true,
        },
    }
}

fn print_table_meta(table: &str, meta: &TableMeta) {
    print!("{}", render_table_meta(table, meta));
}

fn render_table_meta(table: &str, meta: &TableMeta) -> String {
    let mut out = String::new();
    out.push_str(&format!("CREATE TABLE {table} (\n"));
    for (idx, col) in meta.columns.iter().enumerate() {
        let comma = if idx + 1 == meta.columns.len() {
            ""
        } else {
            ","
        };
        let nullable = if col.nullable { "" } else { " NOT NULL" };
        let pk = if col.primary_key { " PRIMARY KEY" } else { "" };
        out.push_str(&format!(
            "  {} {}{}{}{}",
            col.name,
            render_column_type(&col.column_type),
            nullable,
            pk,
            comma
        ));
        out.push('\n');
    }
    out.push_str(")\n");
    if meta.immutable {
        out.push_str("IMMUTABLE\n");
    }
    if let Some(sm) = &meta.state_machine {
        let mut entries: Vec<_> = sm.transitions.iter().collect();
        entries.sort_by(|a, b| a.0.cmp(b.0));
        let transitions: Vec<String> = entries
            .into_iter()
            .map(|(from, tos)| format!("{from} -> [{}]", tos.join(", ")))
            .collect();
        out.push_str(&format!(
            "STATE MACHINE ({}: {})\n",
            sm.column,
            transitions.join(", ")
        ));
    }
    if !meta.dag_edge_types.is_empty() {
        let edge_types = meta
            .dag_edge_types
            .iter()
            .map(|edge_type| format!("'{edge_type}'"))
            .collect::<Vec<_>>()
            .join(", ");
        out.push_str(&format!("DAG({edge_types})\n"));
    }
    out
}

fn render_column_type(col_type: &ColumnType) -> String {
    match col_type {
        ColumnType::Integer => "INTEGER".to_string(),
        ColumnType::Real => "REAL".to_string(),
        ColumnType::Text => "TEXT".to_string(),
        ColumnType::Boolean => "BOOLEAN".to_string(),
        ColumnType::Json => "JSON".to_string(),
        ColumnType::Uuid => "UUID".to_string(),
        ColumnType::Vector(dim) => format!("VECTOR({dim})"),
        ColumnType::Timestamp => "TIMESTAMP".to_string(),
    }
}

/// Execute a SQL statement and print the result. Returns `true` on success, `false` on error.
fn execute_sql(db: &Database, sql: &str) -> bool {
    match db.execute(sql, &HashMap::new()) {
        Ok(result) => {
            if result.columns.is_empty() {
                println!("ok (rows_affected={})", result.rows_affected);
            } else {
                println!("{}", format_query_result(&result));
            }
            true
        }
        Err(e) => {
            if is_fatal_cli_error(&e) {
                eprintln!("Error: {}", e);
                false
            } else {
                println!("Error: {}", e);
                true
            }
        }
    }
}

fn is_fatal_cli_error(error: &Error) -> bool {
    matches!(
        error,
        Error::ParseError(_)
            | Error::TableNotFound(_)
            | Error::NotFound(_)
            | Error::BfsDepthExceeded(_)
            | Error::RecursiveCteNotSupported
            | Error::WindowFunctionNotSupported
            | Error::FullTextSearchNotSupported
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use contextdb_engine::sync_types::{ConflictPolicy, SyncDirection};
    use contextdb_parser::{Statement, parse};

    #[test]
    fn test_backslash_dt() {
        let db = Database::open_memory();
        db.execute("CREATE TABLE t (id UUID PRIMARY KEY)", &HashMap::new())
            .unwrap();
        assert!(handle_meta_command(&db, None, None, "\\dt", None));
    }

    // B1: Existing \dt works with new handle_meta_command signature
    #[test]
    fn b1_existing_dt_works_with_new_signature() {
        let db = Database::open_memory();
        db.execute("CREATE TABLE t (id UUID PRIMARY KEY)", &HashMap::new())
            .unwrap();
        // Pass None for sync_client and rt — existing commands must work without sync
        assert!(handle_meta_command(&db, None, None, "\\dt", None));
        // Also verify .tables works
        assert!(handle_meta_command(&db, None, None, ".tables", None));
        // .quit returns false
        assert!(!handle_meta_command(&db, None, None, ".quit", None));
    }

    // B2: .sync subcommands handle missing sync configuration
    #[test]
    fn b2_sync_not_configured_message() {
        for subcmd in [
            "status",
            "push",
            "pull",
            "reconnect",
            "direction t Push",
            "policy t ServerWins",
        ] {
            let result = handle_sync_command(None, None, subcmd, None);
            assert!(
                result.contains("Sync not configured"),
                "subcmd '{}' should return 'Sync not configured', got: {}",
                subcmd,
                result
            );
        }
    }

    // B3: .sync direction parses all four direction values
    #[test]
    fn b3_sync_direction_parsing() {
        let db = Arc::new(Database::open_memory());
        let rt = tokio::runtime::Runtime::new().unwrap();
        let client =
            rt.block_on(async { SyncClient::new(db, "nats://localhost:19999", "b3-test") });

        // Suppress unused import warning — SyncDirection is used to verify the API exists
        let _directions = [
            SyncDirection::Push,
            SyncDirection::Pull,
            SyncDirection::Both,
            SyncDirection::None,
        ];

        for (table, dir) in [
            ("observations", "Push"),
            ("patterns", "pull"),
            ("decisions", "Both"),
            ("scratch", "None"),
        ] {
            let args = format!("direction {} {}", table, dir);
            let result = handle_sync_command(Some(&client), Some(&rt), &args, None);
            assert!(
                result.contains(table),
                "direction command for '{}' should contain table name, got: {}",
                table,
                result
            );
        }
    }

    // B4: .sync policy parses all four policies + default
    #[test]
    fn b4_sync_policy_parsing() {
        let db = Arc::new(Database::open_memory());
        let rt = tokio::runtime::Runtime::new().unwrap();
        let client =
            rt.block_on(async { SyncClient::new(db, "nats://localhost:19999", "b4-test") });

        // Suppress unused import warning — ConflictPolicy is used to verify the API exists
        let _policies = [
            ConflictPolicy::InsertIfNotExists,
            ConflictPolicy::ServerWins,
            ConflictPolicy::EdgeWins,
            ConflictPolicy::LatestWins,
        ];

        let result = handle_sync_command(
            Some(&client),
            Some(&rt),
            "policy obs InsertIfNotExists",
            None,
        );
        assert!(
            result.contains("InsertIfNotExists"),
            "policy command should contain 'InsertIfNotExists', got: {}",
            result
        );

        let result =
            handle_sync_command(Some(&client), Some(&rt), "policy default ServerWins", None);
        assert!(
            result.contains("Default") || result.contains("default"),
            "default policy command should reference 'default', got: {}",
            result
        );
    }

    // B5: .sync invalid arguments handled gracefully
    #[test]
    fn b5_sync_invalid_args() {
        let db = Arc::new(Database::open_memory());
        let rt = tokio::runtime::Runtime::new().unwrap();
        let client =
            rt.block_on(async { SyncClient::new(db, "nats://localhost:19999", "b5-test") });

        for bad_input in [
            "bogus",
            "direction",
            "direction table_only",
            "direction t InvalidDir",
            "policy",
            "policy table_only",
            "policy t InvalidPolicy",
        ] {
            let result = handle_sync_command(Some(&client), Some(&rt), bad_input, None);
            assert!(
                !result.contains("not implemented"),
                "bad input '{}' should not return 'not implemented', got: {}",
                bad_input,
                result
            );
        }
    }

    #[test]
    fn rt2_repl_schema_display_round_trip_parse() {
        let db = Database::open_memory();
        db.execute(
            "CREATE TABLE repl_rt_sm (id UUID PRIMARY KEY, status TEXT) STATE MACHINE (status: pending -> [done])",
            &HashMap::new(),
        )
        .unwrap();

        let meta = db.table_meta("repl_rt_sm").expect("table meta");
        let rendered = render_table_meta("repl_rt_sm", &meta);
        assert!(matches!(parse(&rendered), Ok(Statement::CreateTable(_))));
    }
}