start/sql/ast.rs
1use bson::Bson;
2// use sqlparser::dialect::GenericDialect;
3// use sqlparser::parser::Parser;
4// use sqlparser::ast::{Statement, Expr, Value};
5
6#[derive(Debug)]
7pub enum SQLStatement {
8 Insert {
9 collection: String,
10 document: Bson,
11 },
12 Select {
13 collection: String,
14 filter: Option<(String, String)>,
15 },
16 Commit,
17 Rollback,
18 Begin,
19}
20
21struct FilterExpr {
22 field: String,
23 op: String, // "=", ">", etc.
24 value: bson::Bson,
25}
26
27// fn parse_sql_to_ast(sql: &str) -> Result<SQLStatement, Box<dyn std::error::Error>> {
28// let dialect = GenericDialect {};
29// let statements = Parser::parse_sql(&dialect, sql)?;
30// let stmt = &statements[0];
31
32// match stmt {
33// Statement::Insert { table_name, source, .. } => {
34// let table = table_name.to_string();
35
36// // parse JSON inside VALUES
37// let values = match source.body.as_ref() {
38// sqlparser::ast::SetExpr::Values(v) => &v.0,
39// _ => return Err("Expected VALUES clause".into()),
40// };
41
42// let json_literal = match &values[0][0] {
43// Expr::Value(Value::SingleQuotedString(s)) => s,
44// _ => return Err("Expected JSON as string".into()),
45// };
46
47// let json_doc: serde_json::Value = serde_json::from_str(json_literal)?;
48// let bson_doc = bson::to_bson(&json_doc)?;
49
50// Ok(SQLStatement::Insert {
51// collection: table,
52// document: bson_doc,
53// })
54// }
55
56// Statement::Query(q) => {
57// let table = q.body.get_table_name().unwrap_or("unknown").to_string();
58// let where_clause = q.selection.as_ref().map(|expr| {
59// if let Expr::BinaryOp { left, op: _, right } = expr {
60// let field = left.to_string();
61// let value = match &**right {
62// Expr::Value(Value::SingleQuotedString(s)) => s.clone(),
63// _ => "???".to_string(),
64// };
65// (field, value)
66// } else {
67// ("??".to_string(), "??".to_string())
68// }
69// });
70
71// Ok(SQLStatement::Select {
72// collection: table,
73// filter: where_clause,
74// })
75// }
76
77// Statement::StartTransaction { .. } => Ok(SQLStatement::Begin),
78// Statement::Commit { .. } => Ok(SQLStatement::Commit),
79// Statement::Rollback { .. } => Ok(SQLStatement::Rollback),
80
81// _ => Err("Unsupported SQL statement".into()),
82// }
83// }