mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-25 16:34:04 +00:00
Make the cli example print JSON (#197)
...via the new json_example feature - to make it easier to try out the parser without coding.
This commit is contained in:
parent
f4fbd9b6b3
commit
a0f076acda
3 changed files with 35 additions and 3 deletions
|
@ -18,10 +18,18 @@ edition = "2018"
|
||||||
name = "sqlparser"
|
name = "sqlparser"
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
||||||
|
[features]
|
||||||
|
# Enable JSON output in the `cli` example:
|
||||||
|
json_example = ["serde_json", "serde"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bigdecimal = { version = "0.1.0", features = ["serde"], optional = true }
|
bigdecimal = { version = "0.1.0", features = ["serde"], optional = true }
|
||||||
log = "0.4.5"
|
log = "0.4.5"
|
||||||
serde = { version = "1.0", features = ["derive"], optional = true }
|
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]
|
[dev-dependencies]
|
||||||
simple_logger = "1.0.1"
|
simple_logger = "1.0.1"
|
||||||
|
|
|
@ -40,6 +40,12 @@ 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 })]
|
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 })]
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Command line
|
||||||
|
To parse a file and dump the results as JSON:
|
||||||
|
```
|
||||||
|
$ cargo run --feature json_example --example cli FILENAME.sql [--dialectname]
|
||||||
|
```
|
||||||
|
|
||||||
## SQL compliance
|
## SQL compliance
|
||||||
|
|
||||||
SQL was first standardized in 1987, and revisions of the standard have been
|
SQL was first standardized in 1987, and revisions of the standard have been
|
||||||
|
|
|
@ -23,8 +23,16 @@ fn main() {
|
||||||
simple_logger::init().unwrap();
|
simple_logger::init().unwrap();
|
||||||
|
|
||||||
let filename = std::env::args().nth(1).expect(
|
let filename = std::env::args().nth(1).expect(
|
||||||
"No arguments provided!\n\n\
|
r#"
|
||||||
Usage: cargo run --example cli FILENAME.sql [--dialectname]",
|
No arguments provided!
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
$ cargo run --example cli FILENAME.sql [--dialectname]
|
||||||
|
|
||||||
|
To print the parse results as JSON:
|
||||||
|
$ cargo run --feature json_example --example cli FILENAME.sql [--dialectname]
|
||||||
|
|
||||||
|
"#,
|
||||||
);
|
);
|
||||||
|
|
||||||
let dialect: Box<dyn Dialect> = match std::env::args().nth(2).unwrap_or_default().as_ref() {
|
let dialect: Box<dyn Dialect> = match std::env::args().nth(2).unwrap_or_default().as_ref() {
|
||||||
|
@ -56,7 +64,17 @@ fn main() {
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("\n")
|
.join("\n")
|
||||||
);
|
);
|
||||||
println!("Parse results:\n{:#?}", statements);
|
|
||||||
|
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);
|
std::process::exit(0);
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue