[−][src]Function postgres_parser::parse_query
pub fn parse_query(statements: &str) -> Result<Vec<Node>, PgParserError>
Parse a string of delimited SQL statements.
This function should be used to parse single statements, or even multiple statements, that are (hopefully) known to already be syntatically correct.
parse_query()
passes the entire string of statements
to Postgres' raw_parser()
, returning
an Ok(Vec<postgres-parser::Node>)
types.
If one of the statements fails to parse, then it is considered that all the statements failed
to parse. As such, a single Err(PgParserError)
will be returned.
To parse multiple statements and evaluate their parsing success individually, you likely want
to use postgres-parser::SqlStatementScanner
instead.
Examples
An example of parsing a single query:
use postgres_parser::*; let parse_list = parse_query("SELECT 1"); match parse_list { // dump the contents of the Vec of nodes, which will only have one Ok(vec) => println!("{:?}", vec), Err(e) => panic!(e) }
Parsing multiple queries is exactly the same:
use postgres_parser::*; let parse_list = parse_query("SELECT 1; SELECT 2; SELECT 3"); match parse_list { // dump the contents of the Vec of nodes, which will have three elements Ok(vec) => println!("{:?}", vec), Err(e) => panic!(e) }
But if one of those is isn't syntatically correct:
use postgres_parser::*; let parse_list = parse_query("SELECT 1; invalid query; SELECT 3"); match parse_list { Ok(vec) => println!("{:?}", vec), // we received an Err because one of the queries didn't parse Err(e) => assert!(true) }
Parsing nothing (or just a ;
) returns an empty Vec:
use postgres_parser::*; let parse_list = parse_query(";").unwrap(); assert!(parse_list.is_empty())