From effcac6496bbc9f9b2d7ee32acb05b9fb96d5802 Mon Sep 17 00:00:00 2001 From: Alex Vasilev Date: Sun, 5 Dec 2021 18:08:45 +0300 Subject: [PATCH] feat: support parsing MySQL show variables (#6) --- src/ast/mod.rs | 11 +++++++++++ src/keywords.rs | 1 + src/parser.rs | 4 ++++ tests/sqlparser_mysql.rs | 7 +++++++ 4 files changed, 23 insertions(+) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 312a86813..01724c80b 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -996,6 +996,10 @@ pub enum Statement { /// /// Note: this is a PostgreSQL-specific statement. ShowVariable { variable: Vec }, + /// SHOW VARIABLES + /// + /// Note: this is a MySQL-specific statement. + ShowVariables { filter: Option }, /// SHOW CREATE TABLE /// /// Note: this is a MySQL-specific statement. @@ -1787,6 +1791,13 @@ impl fmt::Display for Statement { } Ok(()) } + Statement::ShowVariables { filter } => { + write!(f, "SHOW VARIABLES")?; + if filter.is_some() { + write!(f, " {}", filter.as_ref().unwrap().to_string())?; + } + Ok(()) + } Statement::ShowCreate { obj_type, obj_name } => { write!( f, diff --git a/src/keywords.rs b/src/keywords.rs index 7643298e4..af8bf2f5e 100644 --- a/src/keywords.rs +++ b/src/keywords.rs @@ -543,6 +543,7 @@ define_keywords!( VALUE_OF, VARBINARY, VARCHAR, + VARIABLES, VARYING, VAR_POP, VAR_SAMP, diff --git a/src/parser.rs b/src/parser.rs index 76190feb9..0363e1376 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -3694,6 +3694,10 @@ impl<'a> Parser<'a> { Ok(self.parse_show_columns()?) } else if self.parse_one_of_keywords(&[Keyword::CREATE]).is_some() { Ok(self.parse_show_create()?) + } else if self.parse_keyword(Keyword::VARIABLES) && dialect_of!(self is MySqlDialect | GenericDialect) { + Ok(Statement::ShowVariables { + filter: self.parse_show_statement_filter()?, + }) } else { Ok(Statement::ShowVariable { variable: self.parse_identifiers()?, diff --git a/tests/sqlparser_mysql.rs b/tests/sqlparser_mysql.rs index b3c02de30..004b2c876 100644 --- a/tests/sqlparser_mysql.rs +++ b/tests/sqlparser_mysql.rs @@ -820,6 +820,13 @@ fn parse_substring_in_select() { } } +#[test] +fn parse_show_variables() { + mysql_and_generic().verified_stmt("SHOW VARIABLES"); + mysql_and_generic().verified_stmt("SHOW VARIABLES LIKE 'admin%'"); + mysql_and_generic().verified_stmt("SHOW VARIABLES WHERE value = '3306'"); +} + #[test] fn parse_kill() { let stmt = mysql_and_generic().verified_stmt("KILL CONNECTION 5");