Skip to content

Commit 2e9da53

Browse files
committed
Small CLI app that can be used to test parsing an external SQL file
1 parent 577e634 commit 2e9da53

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ path = "src/lib.rs"
2121
log = "0.4.5"
2222
chrono = "0.4.6"
2323
uuid = "0.7.1"
24+
25+
[dev-dependencies]
26+
simple_logger = "1.0.1"

examples/cli.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
extern crate simple_logger;
2+
extern crate sqlparser;
3+
///! A small command-line app to run the parser.
4+
/// Run with `cargo run --example cli`
5+
use std::fs;
6+
7+
use sqlparser::dialect::GenericSqlDialect;
8+
use sqlparser::sqlparser::Parser;
9+
10+
fn main() {
11+
simple_logger::init().unwrap();
12+
13+
let filename = std::env::args()
14+
.nth(1)
15+
.expect("No arguments provided!\n\nUsage: cargo run --example cli FILENAME.sql");
16+
17+
let contents =
18+
fs::read_to_string(&filename).expect(&format!("Unable to read the file {}", &filename));
19+
let without_bom = if contents.chars().nth(0).unwrap() as u64 != 0xfeff {
20+
contents.as_str()
21+
} else {
22+
let mut chars = contents.chars();
23+
chars.next();
24+
chars.as_str()
25+
};
26+
println!("Input:\n'{}'", &without_bom);
27+
let parse_result = Parser::parse_sql(&GenericSqlDialect {}, without_bom.to_owned());
28+
match parse_result {
29+
Ok(statements) => {
30+
println!(
31+
"Round-trip:\n'{}'",
32+
statements
33+
.iter()
34+
.map(|s| s.to_string())
35+
.collect::<Vec<_>>()
36+
.join("\n")
37+
);
38+
println!("Parse results:\n{:#?}", statements);
39+
std::process::exit(0);
40+
}
41+
Err(e) => {
42+
println!("Error during parsing: {:?}", e);
43+
std::process::exit(1);
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)