Skip to content

Commit a0f076a

Browse files
authored
Make the cli example print JSON (#197)
...via the new json_example feature - to make it easier to try out the parser without coding.
1 parent f4fbd9b commit a0f076a

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,18 @@ edition = "2018"
1818
name = "sqlparser"
1919
path = "src/lib.rs"
2020

21+
[features]
22+
# Enable JSON output in the `cli` example:
23+
json_example = ["serde_json", "serde"]
24+
2125
[dependencies]
2226
bigdecimal = { version = "0.1.0", features = ["serde"], optional = true }
2327
log = "0.4.5"
2428
serde = { version = "1.0", features = ["derive"], optional = true }
29+
# serde_json is only used in examples/cli, but we have to put it outside
30+
# of dev-dependencies because of
31+
# https://github.com/rust-lang/cargo/issues/1596
32+
serde_json = { version = "1.0", optional = true }
2533

2634
[dev-dependencies]
2735
simple_logger = "1.0.1"

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ This outputs
4040
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 })]
4141
```
4242

43+
## Command line
44+
To parse a file and dump the results as JSON:
45+
```
46+
$ cargo run --feature json_example --example cli FILENAME.sql [--dialectname]
47+
```
48+
4349
## SQL compliance
4450

4551
SQL was first standardized in 1987, and revisions of the standard have been

examples/cli.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ fn main() {
2323
simple_logger::init().unwrap();
2424

2525
let filename = std::env::args().nth(1).expect(
26-
"No arguments provided!\n\n\
27-
Usage: cargo run --example cli FILENAME.sql [--dialectname]",
26+
r#"
27+
No arguments provided!
28+
29+
Usage:
30+
$ cargo run --example cli FILENAME.sql [--dialectname]
31+
32+
To print the parse results as JSON:
33+
$ cargo run --feature json_example --example cli FILENAME.sql [--dialectname]
34+
35+
"#,
2836
);
2937

3038
let dialect: Box<dyn Dialect> = match std::env::args().nth(2).unwrap_or_default().as_ref() {
@@ -56,7 +64,17 @@ fn main() {
5664
.collect::<Vec<_>>()
5765
.join("\n")
5866
);
59-
println!("Parse results:\n{:#?}", statements);
67+
68+
if cfg!(feature = "json_example") {
69+
#[cfg(feature = "json_example")]
70+
{
71+
let serialized = serde_json::to_string_pretty(&statements).unwrap();
72+
println!("Serialized as JSON:\n{}", serialized);
73+
}
74+
} else {
75+
println!("Parse results:\n{:#?}", statements);
76+
}
77+
6078
std::process::exit(0);
6179
}
6280
Err(e) => {

0 commit comments

Comments
 (0)