mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-07-07 17:04:59 +00:00
[example-cli] Support parsing with different dialects
This commit is contained in:
parent
721c24188a
commit
e6f5ff2926
1 changed files with 15 additions and 6 deletions
|
@ -6,16 +6,26 @@ use simple_logger;
|
|||
/// Run with `cargo run --example cli`
|
||||
use std::fs;
|
||||
|
||||
use sqlparser::dialect::GenericSqlDialect;
|
||||
use sqlparser::dialect::*;
|
||||
use sqlparser::sqlparser::Parser;
|
||||
|
||||
fn main() {
|
||||
simple_logger::init().unwrap();
|
||||
|
||||
let filename = std::env::args()
|
||||
.nth(1)
|
||||
.expect("No arguments provided!\n\nUsage: cargo run --example cli FILENAME.sql");
|
||||
let filename = std::env::args().nth(1).expect(
|
||||
"No arguments provided!\n\n\
|
||||
Usage: cargo run --example cli FILENAME.sql [--dialectname]",
|
||||
);
|
||||
|
||||
let dialect: Box<dyn Dialect> = match std::env::args().nth(2).unwrap_or_default().as_ref() {
|
||||
"--ansi" => Box::new(AnsiSqlDialect {}),
|
||||
"--postgres" => Box::new(PostgreSqlDialect {}),
|
||||
"--ms" => Box::new(MsSqlDialect {}),
|
||||
"--generic" | "" => Box::new(GenericSqlDialect {}),
|
||||
s => panic!("Unexpected parameter: {}", s),
|
||||
};
|
||||
|
||||
println!("Parsing from file '{}' using {:?}", &filename, dialect);
|
||||
let contents = fs::read_to_string(&filename)
|
||||
.unwrap_or_else(|_| panic!("Unable to read the file {}", &filename));
|
||||
let without_bom = if contents.chars().nth(0).unwrap() as u64 != 0xfeff {
|
||||
|
@ -25,8 +35,7 @@ fn main() {
|
|||
chars.next();
|
||||
chars.as_str()
|
||||
};
|
||||
println!("Input:\n'{}'", &without_bom);
|
||||
let parse_result = Parser::parse_sql(&GenericSqlDialect {}, without_bom.to_owned());
|
||||
let parse_result = Parser::parse_sql(&*dialect, without_bom.to_owned());
|
||||
match parse_result {
|
||||
Ok(statements) => {
|
||||
println!(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue