Skip to content

Add code example to see how to use serde #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -18,10 +18,18 @@ 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]
simple_logger = "1.0.1"
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 })]
```

## 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 was first standardized in 1987, and revisions of the standard have been
24 changes: 21 additions & 3 deletions examples/cli.rs
Original file line number Diff line number Diff line change
@@ -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 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() {
@@ -56,7 +64,17 @@ fn main() {
.collect::<Vec<_>>()
.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);
}
Err(e) => {