From 6ee6ea127a07398b2e492bfc67fd04c1511dba52 Mon Sep 17 00:00:00 2001 From: tempbottle Date: Sun, 31 Dec 2023 21:22:24 +0800 Subject: [PATCH 1/2] just test ast --- Cargo.toml | 4 +++- examples/parse_select.rs | 30 +++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index de553aa18..ccbe04a7d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ name = "sqlparser" path = "src/lib.rs" [features] -default = ["std"] +default = ["std", "json_example"] std = [] # Enable JSON output in the `cli` example: json_example = ["serde_json", "serde"] @@ -40,6 +40,8 @@ sqlparser_derive = { version = "0.2.0", path = "derive", optional = true } simple_logger = "4.0" matches = "0.1" pretty_assertions = "1" +serde = "1" +serde_json = "1" [package.metadata.release] # Instruct `cargo release` to not run `cargo publish` locally: diff --git a/examples/parse_select.rs b/examples/parse_select.rs index 71fe1fa1e..bba76b2ab 100644 --- a/examples/parse_select.rs +++ b/examples/parse_select.rs @@ -12,11 +12,12 @@ #![warn(clippy::all)] +use sqlparser::ast::{Query, SetExpr, Statement}; use sqlparser::dialect::GenericDialect; use sqlparser::parser::*; fn main() { - let sql = "SELECT a, b, 123, myfunc(b) \ + let sql = "SELECT a, b, 123, myfunc(b), (a+b) b AND b < 100 \ ORDER BY a DESC, b"; @@ -26,4 +27,31 @@ fn main() { let ast = Parser::parse_sql(&dialect, sql).unwrap(); println!("AST: {ast:?}"); + println!("ast to json: {}", serde_json::to_string_pretty(&ast).unwrap()); + println!("AST to string: {}", &ast.get(0).unwrap().to_string()); + + let select: Statement= (*ast.get(0).unwrap()).clone().into(); + match select { + + Statement::Query(q) => { + let q: Query = *q.clone(); + let body = *q.body; + match body { + SetExpr::Select(s) => { + let s = *s; + let project = s.projection; + println!("projection is [ {} ]", project.iter().map(|s| s.to_string()).collect::>().join(", ")); + let json = serde_json::to_string_pretty(&project).unwrap(); + println!("project json is {}", &json); + } + _=> { + println!("not a Select SetExpr."); + } + }; + }, + _ => { + println!("not a query sql."); + } + } + } From 12a35410a1ae93c3c2efb176709e1cd09352210f Mon Sep 17 00:00:00 2001 From: tempbottle Date: Sun, 31 Dec 2023 21:45:07 +0800 Subject: [PATCH 2/2] json_path method --- Cargo.toml | 1 + examples/parse_select.rs | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ccbe04a7d..b7add8c27 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,7 @@ matches = "0.1" pretty_assertions = "1" serde = "1" serde_json = "1" +jsonpath-rust = "0.3" [package.metadata.release] # Instruct `cargo release` to not run `cargo publish` locally: diff --git a/examples/parse_select.rs b/examples/parse_select.rs index bba76b2ab..09fbe3496 100644 --- a/examples/parse_select.rs +++ b/examples/parse_select.rs @@ -12,6 +12,9 @@ #![warn(clippy::all)] +use std::str::FromStr; +use jsonpath_rust::JsonPathInst; +use serde_json::Value; use sqlparser::ast::{Query, SetExpr, Statement}; use sqlparser::dialect::GenericDialect; use sqlparser::parser::*; @@ -25,11 +28,19 @@ fn main() { let dialect = GenericDialect {}; let ast = Parser::parse_sql(&dialect, sql).unwrap(); - + let json = serde_json::to_string_pretty(&ast).unwrap(); println!("AST: {ast:?}"); - println!("ast to json: {}", serde_json::to_string_pretty(&ast).unwrap()); + println!("ast to json: {}", &json); + + let jq = "$..ExprWithAlias"; + let query = JsonPathInst::from_str(jq).unwrap(); + let json: Value = serde_json::from_str(&json).expect("to get json"); + let one = query.find_slice(&json).get(0).expect("to get value").to_string(); + println!("outer one is {}", &one); + println!("AST to string: {}", &ast.get(0).unwrap().to_string()); + let select: Statement= (*ast.get(0).unwrap()).clone().into(); match select { @@ -43,6 +54,11 @@ fn main() { println!("projection is [ {} ]", project.iter().map(|s| s.to_string()).collect::>().join(", ")); let json = serde_json::to_string_pretty(&project).unwrap(); println!("project json is {}", &json); + let query = JsonPathInst::from_str(jq).unwrap(); + let json: Value = serde_json::from_str(&json).expect("to get json"); + let one = query.find_slice(&json).get(0).expect("to get value").to_string(); + println!("inner one is {}", &one); + } _=> { println!("not a Select SetExpr."); @@ -54,4 +70,6 @@ fn main() { } } + + }