mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-26 00:44:12 +00:00
Allow example CLI to read from stdin (#1536)
This commit is contained in:
parent
fad2ddd641
commit
a1150223af
1 changed files with 19 additions and 5 deletions
|
@ -17,9 +17,11 @@
|
||||||
|
|
||||||
#![warn(clippy::all)]
|
#![warn(clippy::all)]
|
||||||
|
|
||||||
/// A small command-line app to run the parser.
|
//! A small command-line app to run the parser.
|
||||||
/// Run with `cargo run --example cli`
|
//! Run with `cargo run --example cli`
|
||||||
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
use std::io::{stdin, Read};
|
||||||
|
|
||||||
use simple_logger::SimpleLogger;
|
use simple_logger::SimpleLogger;
|
||||||
use sqlparser::dialect::*;
|
use sqlparser::dialect::*;
|
||||||
|
@ -38,6 +40,9 @@ $ cargo run --example cli FILENAME.sql [--dialectname]
|
||||||
To print the parse results as JSON:
|
To print the parse results as JSON:
|
||||||
$ cargo run --feature json_example --example cli FILENAME.sql [--dialectname]
|
$ cargo run --feature json_example --example cli FILENAME.sql [--dialectname]
|
||||||
|
|
||||||
|
To read from stdin instead of a file:
|
||||||
|
$ cargo run --example cli - [--dialectname]
|
||||||
|
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -57,9 +62,18 @@ $ cargo run --feature json_example --example cli FILENAME.sql [--dialectname]
|
||||||
s => panic!("Unexpected parameter: {s}"),
|
s => panic!("Unexpected parameter: {s}"),
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("Parsing from file '{}' using {:?}", &filename, dialect);
|
let contents = if filename == "-" {
|
||||||
let contents = fs::read_to_string(&filename)
|
println!("Parsing from stdin using {:?}", dialect);
|
||||||
.unwrap_or_else(|_| panic!("Unable to read the file {}", &filename));
|
let mut buf = Vec::new();
|
||||||
|
stdin()
|
||||||
|
.read_to_end(&mut buf)
|
||||||
|
.expect("failed to read from stdin");
|
||||||
|
String::from_utf8(buf).expect("stdin content wasn't valid utf8")
|
||||||
|
} else {
|
||||||
|
println!("Parsing from file '{}' using {:?}", &filename, dialect);
|
||||||
|
fs::read_to_string(&filename)
|
||||||
|
.unwrap_or_else(|_| panic!("Unable to read the file {}", &filename))
|
||||||
|
};
|
||||||
let without_bom = if contents.chars().next().unwrap() as u64 != 0xfeff {
|
let without_bom = if contents.chars().next().unwrap() as u64 != 0xfeff {
|
||||||
contents.as_str()
|
contents.as_str()
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue