ddb-core 2.1.0

DDB v2: A high-performance SQL interface for flat files written in Rust
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
use clap::{Parser, Subcommand};
use ddb_core::{
    config::{Config, Table, TableCatalog},
    engine::QueryExecutor,
    lexer::Tokenizer,
    output::{format_results, OutputFormat},
    parser::Parser as SqlParser,
    parser::Statement,
    VERSION,
};
use std::path::PathBuf;

#[derive(Parser)]
#[command(name = "ddb")]
#[command(author, version, about = "A serviceless SQL interface for flat files", long_about = None)]
struct Cli {
    /// SQL query to execute
    #[arg(short, long)]
    query: Option<String>,

    /// Data file path
    #[arg(short = 'f', long)]
    file: Option<PathBuf>,

    /// Field delimiter (default: comma)
    #[arg(short = 'd', long, default_value = ",")]
    delimiter: String,

    /// Database configuration directory
    #[arg(short = 'c', long)]
    config: Option<PathBuf>,

    /// Output format (json, yaml, csv, table)
    #[arg(short, long, default_value = "table")]
    output: String,

    /// Enable debug mode
    #[arg(long)]
    debug: bool,

    /// Start MCP (Model Context Protocol) server mode
    #[cfg(feature = "mcp")]
    #[arg(long)]
    mcp: bool,

    #[command(subcommand)]
    command: Option<Commands>,
}

#[derive(Subcommand)]
enum Commands {
    /// Start interactive mode
    Interactive,

    /// Show version information
    Version,
}

fn main() {
    let cli = Cli::parse();

    // Handle MCP mode first (if feature enabled)
    #[cfg(feature = "mcp")]
    if cli.mcp {
        start_mcp_server();
        return;
    }

    // Initialize regular env_logger for CLI mode
    env_logger::init();

    match &cli.command {
        Some(Commands::Version) => {
            println!("DDB v{}", VERSION);
            return;
        }
        Some(Commands::Interactive) => {
            println!("Interactive mode not yet implemented");
            std::process::exit(1);
        }
        None => {
            if let Some(query) = &cli.query {
                // Load configuration
                let config = Config::load().unwrap_or_else(|e| {
                    eprintln!("Warning: Failed to load config: {}", e);
                    eprintln!("Using default configuration");
                    Config::default()
                });

                if cli.debug {
                    println!("=== Configuration ===");
                    println!("Database: {}", config.default_database);
                    println!("Schema Dir: {:?}", config.schema_dir);
                    println!("Delimiter: {:?}", config.default_delimiter);
                    println!();
                }

                // Load table catalog
                let catalog = TableCatalog::load_from_config(&config).unwrap_or_else(|e| {
                    eprintln!("Warning: Failed to load table catalog: {}", e);
                    TableCatalog::new()
                });

                if cli.debug && !catalog.list_tables().is_empty() {
                    println!("=== Loaded Tables ===");
                    for table in catalog.list_tables() {
                        println!("  - {}", table);
                    }
                    println!();
                }

                if let Err(e) = execute_query(&cli, query, &config, &catalog) {
                    eprintln!("Error: {}", e);
                    std::process::exit(1);
                }
            } else {
                eprintln!("Error: No query provided. Use --query or run 'ddb version'");
                std::process::exit(1);
            }
        }
    }
}

fn execute_query(cli: &Cli, query: &str, config: &Config, catalog: &TableCatalog) -> Result<(), Box<dyn std::error::Error>> {
    // Tokenize
    let mut tokenizer = Tokenizer::new();
    let tokens = tokenizer.tokenize(query)?;

    if cli.debug {
        println!("=== Tokens ===");
        for token in &tokens {
            println!("  {:?}", token);
        }
        println!();
    }

    // Parse
    let mut parser = SqlParser::new(tokens);
    let statement = parser.parse()?;

    if cli.debug {
        println!("=== AST ===");
        println!("{:#?}", statement);
        println!();
    }

    // Execute
    match statement {
        Statement::Select(select_stmt) => {
            // Get table definition
            let table = if let Some(ref path) = cli.file {
                // Use explicit --file argument
                let delimiter = cli
                    .delimiter
                    .chars()
                    .next()
                    .unwrap_or(config.default_delimiter);

                Table {
                    name: "temp".to_string(),
                    database: "temp".to_string(),
                    data_file: path.to_string_lossy().to_string(),
                    columns: vec![],
                    field_delimiter: delimiter,
                    data_starts_on: config.data_starts_on,
                    comment_char: config.comment_char,
                }
            } else if let Some(ref table_name) = select_stmt.from {
                // Look up table in catalog
                catalog.get_table(table_name)
                    .ok_or_else(|| format!("Table '{}' not found in catalog. Use --file to specify a file, or add table definition to schema directory.", table_name))?
                    .clone()
            } else {
                return Err("SELECT requires a FROM clause or --file argument".into());
            };

            if cli.debug {
                println!("=== Table Info ===");
                println!("Name: {}", table.name);
                println!("File: {}", table.data_file);
                println!("Delimiter: {:?}", table.field_delimiter);
                println!();
            }

            // Execute query
            let executor = QueryExecutor::new();
            let results = executor.execute_select(&select_stmt, &table)?;

            if cli.debug {
                println!("=== Results ===");
                println!("{} rows returned", results.len());
                println!();
            }

            // Format output
            let output_format = if !cli.output.is_empty() {
                OutputFormat::from_str(&cli.output)
            } else {
                OutputFormat::from_str(&config.default_output_format)
            };
            let formatted = format_results(&results, output_format)?;
            println!("{}", formatted);

            Ok(())
        }
        Statement::Insert(insert_stmt) => {
            // Get table definition
            let table = if let Some(ref path) = cli.file {
                // Use explicit --file argument
                let delimiter = cli
                    .delimiter
                    .chars()
                    .next()
                    .unwrap_or(config.default_delimiter);

                Table {
                    name: "temp".to_string(),
                    database: "temp".to_string(),
                    data_file: path.to_string_lossy().to_string(),
                    columns: vec![],
                    field_delimiter: delimiter,
                    data_starts_on: config.data_starts_on,
                    comment_char: config.comment_char,
                }
            } else {
                // Look up table in catalog
                catalog.get_table(&insert_stmt.table)
                    .ok_or_else(|| format!("Table '{}' not found in catalog. Use --file to specify a file, or add table definition to schema directory.", insert_stmt.table))?
                    .clone()
            };

            if cli.debug {
                println!("=== Table Info ===");
                println!("Name: {}", table.name);
                println!("File: {}", table.data_file);
                println!("Delimiter: {:?}", table.field_delimiter);
                println!();
            }

            // Execute INSERT
            let executor = QueryExecutor::new();
            let rows_inserted = executor.execute_insert(&insert_stmt, &table)?;

            if cli.debug {
                println!("=== Results ===");
                println!("{} rows inserted", rows_inserted);
                println!();
            } else {
                println!("{} rows inserted", rows_inserted);
            }

            Ok(())
        }
        Statement::Update(update_stmt) => {
            // Get table definition
            let table = if let Some(ref path) = cli.file {
                // Use explicit --file argument
                let delimiter = cli
                    .delimiter
                    .chars()
                    .next()
                    .unwrap_or(config.default_delimiter);

                Table {
                    name: "temp".to_string(),
                    database: "temp".to_string(),
                    data_file: path.to_string_lossy().to_string(),
                    columns: vec![],
                    field_delimiter: delimiter,
                    data_starts_on: config.data_starts_on,
                    comment_char: config.comment_char,
                }
            } else {
                // Look up table in catalog
                catalog.get_table(&update_stmt.table)
                    .ok_or_else(|| format!("Table '{}' not found in catalog. Use --file to specify a file, or add table definition to schema directory.", update_stmt.table))?
                    .clone()
            };

            if cli.debug {
                println!("=== Table Info ===");
                println!("Name: {}", table.name);
                println!("File: {}", table.data_file);
                println!("Delimiter: {:?}", table.field_delimiter);
                println!();
            }

            // Execute UPDATE
            let executor = QueryExecutor::new();
            let rows_updated = executor.execute_update(&update_stmt, &table)?;

            if cli.debug {
                println!("=== Results ===");
                println!("{} rows updated", rows_updated);
                println!();
            } else {
                println!("{} rows updated", rows_updated);
            }

            Ok(())
        }
        Statement::Upsert(upsert_stmt) => {
            // Get table definition
            let table = if let Some(ref path) = cli.file {
                // Use explicit --file argument
                let delimiter = cli
                    .delimiter
                    .chars()
                    .next()
                    .unwrap_or(config.default_delimiter);

                Table {
                    name: "temp".to_string(),
                    database: "temp".to_string(),
                    data_file: path.to_string_lossy().to_string(),
                    columns: vec![],
                    field_delimiter: delimiter,
                    data_starts_on: config.data_starts_on,
                    comment_char: config.comment_char,
                }
            } else {
                // Look up table in catalog
                catalog.get_table(&upsert_stmt.table)
                    .ok_or_else(|| format!("Table '{}' not found in catalog. Use --file to specify a file, or add table definition to schema directory.", upsert_stmt.table))?
                    .clone()
            };

            if cli.debug {
                println!("=== Table Info ===");
                println!("Name: {}", table.name);
                println!("File: {}", table.data_file);
                println!("Delimiter: {:?}", table.field_delimiter);
                println!();
            }

            // Execute UPSERT
            let executor = QueryExecutor::new();
            let (rows_inserted, rows_updated) = executor.execute_upsert(&upsert_stmt, &table)?;

            if cli.debug {
                println!("=== Results ===");
                println!("{} rows inserted, {} rows updated", rows_inserted, rows_updated);
                println!();
            } else {
                println!("{} rows inserted, {} rows updated", rows_inserted, rows_updated);
            }

            Ok(())
        }
        Statement::Delete(delete_stmt) => {
            // Get table definition
            let table = if let Some(ref path) = cli.file {
                // Use explicit --file argument
                let delimiter = cli
                    .delimiter
                    .chars()
                    .next()
                    .unwrap_or(config.default_delimiter);

                Table {
                    name: "temp".to_string(),
                    database: "temp".to_string(),
                    data_file: path.to_string_lossy().to_string(),
                    columns: vec![],
                    field_delimiter: delimiter,
                    data_starts_on: config.data_starts_on,
                    comment_char: config.comment_char,
                }
            } else {
                // Look up table in catalog
                catalog.get_table(&delete_stmt.table)
                    .ok_or_else(|| format!("Table '{}' not found in catalog. Use --file to specify a file, or add table definition to schema directory.", delete_stmt.table))?
                    .clone()
            };

            if cli.debug {
                println!("=== Table Info ===");
                println!("Name: {}", table.name);
                println!("File: {}", table.data_file);
                println!("Delimiter: {:?}", table.field_delimiter);
                println!();
            }

            // Execute DELETE
            let executor = QueryExecutor::new();
            let rows_deleted = executor.execute_delete(&delete_stmt, &table)?;

            if cli.debug {
                println!("=== Results ===");
                println!("{} rows deleted", rows_deleted);
                println!();
            } else {
                println!("{} rows deleted", rows_deleted);
            }

            Ok(())
        }
        Statement::CreateTable(_) => {
            Err("CREATE TABLE statements are not yet implemented".into())
        }
        Statement::DropTable(_) => {
            Err("DROP TABLE statements are not yet implemented".into())
        }
        Statement::Use(_) => {
            Err("USE statements are not yet implemented".into())
        }
        Statement::Show(_) => {
            Err("SHOW statements are not yet implemented".into())
        }
        Statement::Set(_) => {
            Err("SET statements are not yet implemented".into())
        }
        Statement::Describe(_) => {
            Err("DESCRIBE statements are not yet implemented".into())
        }
        Statement::Begin | Statement::Commit | Statement::Rollback => {
            Err("Transaction statements are not yet implemented".into())
        }
    }
}

#[cfg(feature = "mcp")]
fn start_mcp_server() {
    use ddb_core::mcp::DdbMcpServer;
    use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

    // Initialize tracing for MCP mode
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::EnvFilter::try_from_default_env()
                .unwrap_or_else(|_| "ddb_mcp=info".into()),
        )
        .with(tracing_subscriber::fmt::layer().with_writer(std::io::stderr))
        .init();

    // Create and run the MCP server
    let runtime = tokio::runtime::Runtime::new().expect("Failed to create Tokio runtime");
    runtime.block_on(async {
        let server = DdbMcpServer::new().expect("Failed to create MCP server");
        eprintln!("DDB MCP Server v{} starting...", VERSION);
        eprintln!("Server capabilities: tools, resources, prompts");

        if let Err(e) = server.run().await {
            eprintln!("MCP server error: {}", e);
            std::process::exit(1);
        }
    });
}