|
| 1 | +//! Minimal CLI to query the Cmdr index SQLite database. |
| 2 | +//! |
| 3 | +//! The index DB uses a custom `platform_case` collation on the `name` column, |
| 4 | +//! so the standard `sqlite3` CLI can't query it. This tool registers the |
| 5 | +//! collation and runs an arbitrary SQL query. |
| 6 | +//! |
| 7 | +//! Usage: cargo run --bin index_query -- <db_path> <sql> |
| 8 | +
|
| 9 | +use rusqlite::{Connection, OpenFlags}; |
| 10 | + |
| 11 | +fn main() { |
| 12 | + let args: Vec<String> = std::env::args().collect(); |
| 13 | + if args.len() != 3 { |
| 14 | + eprintln!("Usage: {} <db_path> <sql>", args[0]); |
| 15 | + std::process::exit(1); |
| 16 | + } |
| 17 | + let db_path = &args[1]; |
| 18 | + let sql = &args[2]; |
| 19 | + |
| 20 | + let conn = |
| 21 | + Connection::open_with_flags(db_path, OpenFlags::SQLITE_OPEN_READ_ONLY).expect("Couldn't open database"); |
| 22 | + cmdr_lib::indexing::store::register_platform_case_collation(&conn).expect("Couldn't register collation"); |
| 23 | + |
| 24 | + let mut stmt = conn.prepare(sql).expect("Couldn't prepare statement"); |
| 25 | + let column_count = stmt.column_count(); |
| 26 | + |
| 27 | + // Print header row |
| 28 | + let headers: Vec<&str> = (0..column_count).map(|i| stmt.column_name(i).unwrap_or("?")).collect(); |
| 29 | + println!("{}", headers.join("\t")); |
| 30 | + |
| 31 | + // Print data rows |
| 32 | + let mut rows = stmt.query([]).expect("Couldn't execute query"); |
| 33 | + while let Some(row) = rows.next().expect("Couldn't read row") { |
| 34 | + let values: Vec<String> = (0..column_count) |
| 35 | + .map(|i| { |
| 36 | + row.get::<_, rusqlite::types::Value>(i) |
| 37 | + .map(|v| match v { |
| 38 | + rusqlite::types::Value::Null => "NULL".to_string(), |
| 39 | + rusqlite::types::Value::Integer(n) => n.to_string(), |
| 40 | + rusqlite::types::Value::Real(f) => f.to_string(), |
| 41 | + rusqlite::types::Value::Text(s) => s, |
| 42 | + rusqlite::types::Value::Blob(b) => format!("<blob {} bytes>", b.len()), |
| 43 | + }) |
| 44 | + .unwrap_or_else(|_| "ERROR".to_string()) |
| 45 | + }) |
| 46 | + .collect(); |
| 47 | + println!("{}", values.join("\t")); |
| 48 | + } |
| 49 | +} |
0 commit comments