From 07342d585321e719e7d729bf96661b3dbb1417ab Mon Sep 17 00:00:00 2001 From: Francis Du Date: Wed, 10 Feb 2021 04:03:49 +0800 Subject: [PATCH] Support parsing multiple show variables. (#290) * feat: support parsing multiple show variables. * fix: fix fmt error --- src/ast/mod.rs | 10 ++++++++-- src/parser.rs | 15 ++++++++++++++- tests/sqlparser_postgres.rs | 8 ++++---- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index d0e32118..9a96e34b 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -637,7 +637,7 @@ pub enum Statement { /// SHOW /// /// Note: this is a PostgreSQL-specific statement. - ShowVariable { variable: Ident }, + ShowVariable { variable: Vec }, /// SHOW COLUMNS /// /// Note: this is a MySQL-specific statement. @@ -1136,7 +1136,13 @@ impl fmt::Display for Statement { value = display_comma_separated(value) ) } - Statement::ShowVariable { variable } => write!(f, "SHOW {}", variable), + Statement::ShowVariable { variable } => { + write!(f, "SHOW")?; + if !variable.is_empty() { + write!(f, " {}", display_separated(variable, " "))?; + } + Ok(()) + } Statement::ShowColumns { extended, full, diff --git a/src/parser.rs b/src/parser.rs index bee671f0..621209fd 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -2043,6 +2043,19 @@ impl<'a> Parser<'a> { Ok(ObjectName(idents)) } + /// Parse identifiers + pub fn parse_identifiers(&mut self) -> Result, ParserError> { + let mut idents = vec![]; + loop { + match self.next_token() { + Token::Word(w) => idents.push(w.to_ident()), + Token::EOF => break, + _ => {} + } + } + Ok(idents) + } + /// Parse a simple one-word identifier (possibly quoted, possibly a keyword) pub fn parse_identifier(&mut self) -> Result { match self.next_token() { @@ -2439,7 +2452,7 @@ impl<'a> Parser<'a> { self.parse_show_columns() } else { Ok(Statement::ShowVariable { - variable: self.parse_identifier()?, + variable: self.parse_identifiers()?, }) } } diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 2abd8ae9..bcfce30f 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -443,19 +443,19 @@ fn parse_set() { #[test] fn parse_show() { - let stmt = pg_and_generic().verified_stmt("SHOW a"); + let stmt = pg_and_generic().verified_stmt("SHOW a a"); assert_eq!( stmt, Statement::ShowVariable { - variable: "a".into() + variable: vec!["a".into(), "a".into()] } ); - let stmt = pg_and_generic().verified_stmt("SHOW ALL"); + let stmt = pg_and_generic().verified_stmt("SHOW ALL ALL"); assert_eq!( stmt, Statement::ShowVariable { - variable: "ALL".into() + variable: vec!["ALL".into(), "ALL".into()] } ) }