motedb 0.1.3

AI-native embedded multimodal database for embodied intelligence (robots, AR glasses, industrial arms).
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
#!/usr/bin/env rust
//! MoteDB 命令行工具
//! 
//! 用法:
//!   motedb-cli                    - 启动交互式 SQL shell
//!   motedb-cli <db_path>          - 打开指定数据库
//!   motedb-cli exec "SQL"         - 执行单条 SQL 语句
//!   motedb-cli --version          - 显示版本信息
//!   motedb-cli --help             - 显示帮助信息

use motedb::*;
use motedb::sql::{Lexer, Parser, QueryExecutor};  // ✅ 使用流式 API
use std::env;
use std::io::{self, Write, BufRead};
use std::sync::Arc;
use std::path::PathBuf;

const VERSION: &str = env!("CARGO_PKG_VERSION");

fn main() {
    if let Err(e) = run() {
        eprintln!("❌ Error: {}", e);
        std::process::exit(1);
    }
}

fn run() -> Result<()> {
    let args: Vec<String> = env::args().collect();
    
    match args.len() {
        1 => {
            // 交互式模式 (默认数据库)
            interactive_mode(None)?;
        }
        2 => {
            match args[1].as_str() {
                "--version" | "-v" => {
                    println!("MoteDB v{}", VERSION);
                    println!("高性能嵌入式数据库引擎");
                }
                "--help" | "-h" => {
                    print_help();
                }
                path => {
                    // 打开指定数据库
                    interactive_mode(Some(PathBuf::from(path)))?;
                }
            }
        }
        3 => {
            if args[1] == "exec" {
                // 执行单条 SQL
                execute_single_sql(None, &args[2])?;
            } else {
                print_help();
                return Err(StorageError::InvalidData("Invalid arguments".to_string()));
            }
        }
        4 => {
            if args[1] == "exec" {
                // motedb-cli exec <db_path> "SQL"
                execute_single_sql(Some(PathBuf::from(&args[2])), &args[3])?;
            } else {
                print_help();
                return Err(StorageError::InvalidData("Invalid arguments".to_string()));
            }
        }
        _ => {
            print_help();
            return Err(StorageError::InvalidData("Too many arguments".to_string()));
        }
    }
    
    Ok(())
}

fn print_help() {
    println!(r#"
MoteDB v{} - 高性能嵌入式数据库引擎

用法:
  motedb-cli                    启动交互式 SQL shell (默认数据库: ./motedb_data)
  motedb-cli <db_path>          打开指定数据库
  motedb-cli exec "SQL"         执行单条 SQL 语句 (使用默认数据库)
  motedb-cli exec <db_path> "SQL"  执行单条 SQL 语句 (指定数据库)
  motedb-cli --version          显示版本信息
  motedb-cli --help             显示此帮助信息

示例:
  # 启动交互式 shell
  motedb-cli

  # 打开指定数据库
  motedb-cli /path/to/database

  # 执行单条 SQL
  motedb-cli exec "CREATE TABLE users (id INTEGER, name TEXT)"
  motedb-cli exec mydb "SELECT * FROM users"

支持的索引类型:
  • B-Tree 索引 (主键)
  • LSM-Tree 索引 (高吞吐写入)
  • 全文搜索索引 (FTS)
  • 向量索引 (DiskANN)
  • 空间索引 (R-Tree)
  • 时间序列索引 (Timestamp)
  • 列值索引 (Column Value)

更多信息: https://github.com/yourusername/motedb
"#, VERSION);
}

fn interactive_mode(db_path: Option<PathBuf>) -> Result<()> {
    let path = db_path.unwrap_or_else(|| PathBuf::from("./motedb_data"));
    
    println!("🚀 MoteDB v{}", VERSION);
    println!("📂 Database: {}", path.display());
    println!("💡 Type '.help' for help, '.exit' to quit\n");
    
    // 打开或创建数据库
    let db = Arc::new(if path.exists() {
        MoteDB::open(&path)?
    } else {
        println!("✨ Creating new database...\n");
        MoteDB::create(&path)?
    });
    
    let stdin = io::stdin();
    let mut buffer = String::new();
    let mut multiline_sql = String::new();
    
    loop {
        // 显示提示符
        if multiline_sql.is_empty() {
            print!("motedb> ");
        } else {
            print!("     -> ");
        }
        io::stdout().flush().unwrap();
        
        // 读取输入
        buffer.clear();
        if stdin.lock().read_line(&mut buffer).is_err() {
            break;
        }
        
        let input = buffer.trim();
        
        // 特殊命令
        if input.starts_with('.') {
            if !multiline_sql.is_empty() {
                eprintln!("⚠️  Warning: Incomplete SQL statement discarded");
                multiline_sql.clear();
            }
            
            match input {
                ".exit" | ".quit" => {
                    println!("👋 Goodbye!");
                    break;
                }
                ".help" => {
                    print_interactive_help();
                }
                ".tables" => {
                    list_tables(&db)?;
                }
                ".schema" => {
                    show_all_schemas(&db)?;
                }
                cmd if cmd.starts_with(".schema ") => {
                    let table = &cmd[8..];
                    show_table_schema(&db, table)?;
                }
                _ => {
                    eprintln!("❌ Unknown command: {}", input);
                    println!("💡 Type '.help' for available commands");
                }
            }
            continue;
        }
        
        // 跳过空行
        if input.is_empty() {
            continue;
        }
        
        // 累积多行 SQL
        multiline_sql.push_str(input);
        multiline_sql.push(' ');
        
        // 检查是否是完整的 SQL 语句 (以分号结尾)
        if input.ends_with(';') {
            // 执行 SQL
            let sql = multiline_sql.trim_end_matches(';').trim();
            
            // ✅ 使用流式 API 并物化
            let result = (|| -> Result<_> {
                let mut lexer = Lexer::new(sql);
                let tokens = lexer.tokenize()?;
                let mut parser = Parser::new(tokens);
                let statement = parser.parse()?;
                let executor = QueryExecutor::new(db.clone());
                let streaming_result = executor.execute_streaming(statement)?;
                streaming_result.materialize()
            })();
            
            match result {
                Ok(result) => {
                    display_result(result);
                }
                Err(e) => {
                    eprintln!("❌ Error: {}", e);
                }
            }
            
            multiline_sql.clear();
        }
    }
    
    Ok(())
}

fn execute_single_sql(db_path: Option<PathBuf>, sql: &str) -> Result<()> {
    let path = db_path.unwrap_or_else(|| PathBuf::from("./motedb_data"));
    
    // 打开或创建数据库
    let db = Arc::new(if path.exists() {
        MoteDB::open(&path)?
    } else {
        MoteDB::create(&path)?
    });
    
    // ✅ 使用流式 API 并物化
    let mut lexer = Lexer::new(sql);
    let tokens = lexer.tokenize()?;
    let mut parser = Parser::new(tokens);
    let statement = parser.parse()?;
    let executor = QueryExecutor::new(db);
    let streaming_result = executor.execute_streaming(statement)?;
    let result = streaming_result.materialize()?;
    
    display_result(result);
    
    Ok(())
}

fn display_result(result: sql::QueryResult) {
    use sql::QueryResult;
    
    match result {
        QueryResult::Definition { message } => {
            println!("{}", message);
        }
        QueryResult::Modification { affected_rows } => {
            println!("{} row(s) affected", affected_rows);
        }
        QueryResult::Select { columns, rows } => {
            display_table(&columns, &rows);
        }
    }
}

fn display_table(columns: &[String], rows: &[Vec<types::Value>]) {
    use types::Value;
    
    if rows.is_empty() {
        println!("📊 No results");
        return;
    }
    
    // 计算列宽
    let mut widths: Vec<usize> = columns.iter()
        .map(|col| col.len())
        .collect();
    
    for row in rows {
        for (i, value) in row.iter().enumerate() {
            let len = match value {
                Value::Null => 4,
                Value::Integer(n) => n.to_string().len(),
                Value::Float(f) => format!("{:.2}", f).len(),
                Value::Text(s) => s.len().min(50),
                Value::Bool(b) => b.to_string().len(),
                Value::Vector(_) => 12,
                Value::Tensor(_) => 12,
                Value::Spatial(_) => 12,
                Value::TextDoc(_) => 12,
                Value::Timestamp(_) => 19,  // "YYYY-MM-DD HH:MM:SS" format
            };
            if i < widths.len() {
                widths[i] = widths[i].max(len);
            }
        }
    }
    
    // 打印表头
    print!("");
    for (i, width) in widths.iter().enumerate() {
        print!("{}", "".repeat(width + 2));
        if i < widths.len() - 1 {
            print!("");
        }
    }
    println!("");
    
    print!("");
    for (i, col) in columns.iter().enumerate() {
        let width = widths.get(i).unwrap_or(&10);
        print!(" {:width$} ", col, width = width);
        if i < columns.len() - 1 {
            print!("");
        }
    }
    println!("");
    
    print!("");
    for (i, width) in widths.iter().enumerate() {
        print!("{}", "".repeat(width + 2));
        if i < widths.len() - 1 {
            print!("");
        }
    }
    println!("");
    
    // 打印数据行
    for row in rows {
        print!("");
        for (i, value) in row.iter().enumerate() {
            let width = widths.get(i).unwrap_or(&10);
            let s = match value {
                Value::Null => "NULL".to_string(),
                Value::Integer(n) => n.to_string(),
                Value::Float(f) => format!("{:.2}", f),
                Value::Text(s) => {
                    if s.len() > 50 {
                        format!("{}...", &s[..47])
                    } else {
                        s.clone()
                    }
                }
                Value::Bool(b) => b.to_string(),
                Value::Vector(_) => "<vector>".to_string(),
                Value::Tensor(_) => "<tensor>".to_string(),
                Value::Spatial(_) => "<geometry>".to_string(),
                Value::TextDoc(_) => "<textdoc>".to_string(),
                Value::Timestamp(ts) => {
                    // Format timestamp as microseconds
                    format!("{} μs", ts.as_micros())
                },
            };
            print!(" {:width$} ", s, width = width);
            if i < row.len() - 1 {
                print!("");
            }
        }
        println!("");
    }
    
    print!("");
    for (i, width) in widths.iter().enumerate() {
        print!("{}", "".repeat(width + 2));
        if i < widths.len() - 1 {
            print!("");
        }
    }
    println!("");
    
    println!("\n📊 {} row(s) returned", rows.len());
}

fn print_interactive_help() {
    println!(r#"
特殊命令:
  .help              显示此帮助
  .exit, .quit       退出程序
  .tables            列出所有表
  .schema            显示所有表的结构
  .schema <table>    显示指定表的结构

SQL 示例:
  CREATE TABLE users (id INTEGER, name TEXT, email TEXT);
  CREATE INDEX idx_email ON users(email) USING COLUMN_VALUE;
  INSERT INTO users VALUES (1, 'Alice', 'alice@example.com');
  SELECT * FROM users WHERE email = 'alice@example.com';
  UPDATE users SET name = 'Bob' WHERE id = 1;
  DELETE FROM users WHERE id = 1;

支持的索引类型:
  COLUMN_VALUE    列值索引 (快速等值查询)
  FTS             全文搜索索引
  VECTOR          向量索引 (相似度搜索)
  SPATIAL         空间索引 (地理位置)
  TIMESTAMP       时间序列索引
"#);
}

fn list_tables(db: &MoteDB) -> Result<()> {
    let tables = db.list_tables()?;
    
    if tables.is_empty() {
        println!("📊 No tables found");
    } else {
        println!("📋 Tables:");
        for table in tables {
            println!("{}", table);
        }
    }
    
    Ok(())
}

fn show_all_schemas(db: &MoteDB) -> Result<()> {
    let tables = db.list_tables()?;
    
    if tables.is_empty() {
        println!("📊 No tables found");
        return Ok(());
    }
    
    for table in tables {
        show_table_schema(db, &table)?;
        println!();
    }
    
    Ok(())
}

fn show_table_schema(db: &MoteDB, table_name: &str) -> Result<()> {
    let schema = db.get_table_schema(table_name)?;
    
    println!("📋 Table: {}", table_name);
    println!("┌─────────────────┬──────────────┬──────────┐");
    println!("│ Column          │ Type         │ Nullable │");
    println!("├─────────────────┼──────────────┼──────────┤");
    
    for col in &schema.columns {
        let type_str = format!("{:?}", col.col_type);
        let nullable = if col.nullable { "YES" } else { "NO" };
        println!("│ {:15} │ {:12} │ {:8}", col.name, type_str, nullable);
    }
    
    println!("└─────────────────┴──────────────┴──────────┘");
    
    Ok(())
}