From 0af9a13e0337ce4782a461ac33261ec37ffa3a90 Mon Sep 17 00:00:00 2001 From: Taehoon Moon Date: Wed, 10 Jun 2020 23:51:41 +0900 Subject: [PATCH 1/4] Add json serialize option to examples/cli.rs --- Cargo.toml | 1 + examples/cli.rs | 25 ++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3232e6a05..e4bd3cf65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,5 +24,6 @@ log = "0.4.5" serde = { version = "1.0", features = ["derive"], optional = true } [dev-dependencies] +serde_json = "1.0" simple_logger = "1.0.1" matches = "0.1" diff --git a/examples/cli.rs b/examples/cli.rs index 2b0822584..057865576 100644 --- a/examples/cli.rs +++ b/examples/cli.rs @@ -23,8 +23,16 @@ fn main() { simple_logger::init().unwrap(); let filename = std::env::args().nth(1).expect( - "No arguments provided!\n\n\ - Usage: cargo run --example cli FILENAME.sql [--dialectname]", + r#" +No arguments provided! + +Usage: +$ cargo run --example cli FILENAME.sql [--dialectname] + +To serialize as JSON: +$ cargo run --feature serde --example cli FILENAME.sql [--dialectname] + +"#, ); let dialect: Box = match std::env::args().nth(2).unwrap_or_default().as_ref() { @@ -56,7 +64,18 @@ fn main() { .collect::>() .join("\n") ); - println!("Parse results:\n{:#?}", statements); + + if cfg!(not(feature = "serde")) { + println!("Parse results:\n{:#?}", statements); + } else { + #[cfg(feature = "serde")] + { + let serialized = serde_json::to_string_pretty(&statements).unwrap(); + + println!("Serialized as JSON:\n{}", serialized); + } + } + std::process::exit(0); } Err(e) => { From 6fb26dbbcdc5384e3a43a42e74018450bc588682 Mon Sep 17 00:00:00 2001 From: Taehoon Moon Date: Thu, 11 Jun 2020 00:37:52 +0900 Subject: [PATCH 2/4] Update README.md, add CLI usage guide --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 24674d911..f3e3bf048 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,22 @@ This outputs AST: [Query(Query { ctes: [], body: Select(Select { distinct: false, projection: [UnnamedExpr(Identifier("a")), UnnamedExpr(Identifier("b")), UnnamedExpr(Value(Long(123))), UnnamedExpr(Function(Function { name: ObjectName(["myfunc"]), args: [Identifier("b")], over: None, distinct: false }))], from: [TableWithJoins { relation: Table { name: ObjectName(["table_1"]), alias: None, args: [], with_hints: [] }, joins: [] }], selection: Some(BinaryOp { left: BinaryOp { left: Identifier("a"), op: Gt, right: Identifier("b") }, op: And, right: BinaryOp { left: Identifier("b"), op: Lt, right: Value(Long(100)) } }), group_by: [], having: None }), order_by: [OrderByExpr { expr: Identifier("a"), asc: Some(false) }, OrderByExpr { expr: Identifier("b"), asc: None }], limit: None, offset: None, fetch: None })] ``` + +To simply try in CLI: +``` +$ cargo run --example cli FILENAME.sql [--dialectname] + +[--dialectname] +--ansi => AnsiDialect {} +--postgres => PostgreSqlDialect {} +--ms => MsSqlDialect {} +--generic | "" => GenericDialect {} +``` +To serialize as JSON, just add `--feature serde` +``` +$ cargo run --features serde --example cli FILENAME.sql [--dialectname] +``` + ## SQL compliance SQL was first standardized in 1987, and revisions of the standard have been From 3a05b32e4d80e6a483fcb797fcebae0575b1933c Mon Sep 17 00:00:00 2001 From: Nickolay Ponomarev Date: Thu, 11 Jun 2020 18:24:12 +0300 Subject: [PATCH 3/4] Add json_example feature --- Cargo.toml | 9 ++++++++- README.md | 16 +++------------- examples/cli.rs | 14 ++++++-------- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e4bd3cf65..804fac0e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,12 +18,19 @@ edition = "2018" name = "sqlparser" path = "src/lib.rs" +[features] +# Enable JSON output in the `cli` example: +json_example = ["serde_json", "serde"] + [dependencies] bigdecimal = { version = "0.1.0", features = ["serde"], optional = true } log = "0.4.5" serde = { version = "1.0", features = ["derive"], optional = true } +# serde_json is only used in examples/cli, but we have to put it outside +# of dev-dependencies because of +# https://github.com/rust-lang/cargo/issues/1596 +serde_json = { version = "1.0", optional = true } [dev-dependencies] -serde_json = "1.0" simple_logger = "1.0.1" matches = "0.1" diff --git a/README.md b/README.md index f3e3bf048..8bf1d4617 100644 --- a/README.md +++ b/README.md @@ -40,20 +40,10 @@ This outputs AST: [Query(Query { ctes: [], body: Select(Select { distinct: false, projection: [UnnamedExpr(Identifier("a")), UnnamedExpr(Identifier("b")), UnnamedExpr(Value(Long(123))), UnnamedExpr(Function(Function { name: ObjectName(["myfunc"]), args: [Identifier("b")], over: None, distinct: false }))], from: [TableWithJoins { relation: Table { name: ObjectName(["table_1"]), alias: None, args: [], with_hints: [] }, joins: [] }], selection: Some(BinaryOp { left: BinaryOp { left: Identifier("a"), op: Gt, right: Identifier("b") }, op: And, right: BinaryOp { left: Identifier("b"), op: Lt, right: Value(Long(100)) } }), group_by: [], having: None }), order_by: [OrderByExpr { expr: Identifier("a"), asc: Some(false) }, OrderByExpr { expr: Identifier("b"), asc: None }], limit: None, offset: None, fetch: None })] ``` - -To simply try in CLI: -``` -$ cargo run --example cli FILENAME.sql [--dialectname] - -[--dialectname] ---ansi => AnsiDialect {} ---postgres => PostgreSqlDialect {} ---ms => MsSqlDialect {} ---generic | "" => GenericDialect {} -``` -To serialize as JSON, just add `--feature serde` +## Command line +To parse a file and dump the results as JSON: ``` -$ cargo run --features serde --example cli FILENAME.sql [--dialectname] +$ cargo run --feature json_example --example cli FILENAME.sql [--dialectname] ``` ## SQL compliance diff --git a/examples/cli.rs b/examples/cli.rs index 057865576..ecccbc9ce 100644 --- a/examples/cli.rs +++ b/examples/cli.rs @@ -29,8 +29,8 @@ No arguments provided! Usage: $ cargo run --example cli FILENAME.sql [--dialectname] -To serialize as JSON: -$ cargo run --feature serde --example cli FILENAME.sql [--dialectname] +To print the parse results as JSON: +$ cargo run --feature json_example --example cli FILENAME.sql [--dialectname] "#, ); @@ -65,15 +65,13 @@ $ cargo run --feature serde --example cli FILENAME.sql [--dialectname] .join("\n") ); - if cfg!(not(feature = "serde")) { - println!("Parse results:\n{:#?}", statements); - } else { - #[cfg(feature = "serde")] - { + if cfg!(feature = "json_example") { + #[cfg(feature = "json_example")] { let serialized = serde_json::to_string_pretty(&statements).unwrap(); - println!("Serialized as JSON:\n{}", serialized); } + } else { + println!("Parse results:\n{:#?}", statements); } std::process::exit(0); From eccc501e96fe1ea9da2c70886961028d8d6b3910 Mon Sep 17 00:00:00 2001 From: Nickolay Ponomarev Date: Thu, 11 Jun 2020 18:29:47 +0300 Subject: [PATCH 4/4] run cargo fmt --- examples/cli.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/cli.rs b/examples/cli.rs index ecccbc9ce..f019c520b 100644 --- a/examples/cli.rs +++ b/examples/cli.rs @@ -66,7 +66,8 @@ $ cargo run --feature json_example --example cli FILENAME.sql [--dialectname] ); if cfg!(feature = "json_example") { - #[cfg(feature = "json_example")] { + #[cfg(feature = "json_example")] + { let serialized = serde_json::to_string_pretty(&statements).unwrap(); println!("Serialized as JSON:\n{}", serialized); }