Skip to content

Commit

Permalink
Support SHOW COLLATION
Browse files Browse the repository at this point in the history
  • Loading branch information
MazterQyou committed Aug 11, 2022
1 parent 54a29e8 commit bb34268
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/ast/mod.rs
Expand Up @@ -1036,6 +1036,10 @@ pub enum Statement {
db_name: Option<Ident>,
filter: Option<ShowStatementFilter>,
},
/// SHOW COLLATION
///
/// Note: this is a MySQL-specific statement.
ShowCollation { filter: Option<ShowStatementFilter> },
/// USE
///
/// Note: This is a MySQL-specific statement.
Expand Down Expand Up @@ -1892,6 +1896,13 @@ impl fmt::Display for Statement {
write!(f, "USE {}", db_name)?;
Ok(())
}
Statement::ShowCollation { filter } => {
write!(f, "SHOW COLLATION")?;
if let Some(filter) = filter {
write!(f, " {}", filter)?;
}
Ok(())
}
Statement::StartTransaction { modes } => {
write!(f, "START TRANSACTION")?;
if !modes.is_empty() {
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Expand Up @@ -131,6 +131,7 @@ define_keywords!(
CLUSTER,
COALESCE,
COLLATE,
COLLATION,
COLLECT,
COLUMN,
COLUMNS,
Expand Down
7 changes: 7 additions & 0 deletions src/parser.rs
Expand Up @@ -3758,6 +3758,8 @@ impl<'a> Parser<'a> {
))
} else if self.parse_one_of_keywords(&[Keyword::CREATE]).is_some() {
Ok(self.parse_show_create()?)
} else if self.parse_keyword(Keyword::COLLATION) {
Ok(self.parse_show_collation()?)
} else if self.parse_keyword(Keyword::VARIABLES)
&& dialect_of!(self is MySqlDialect | GenericDialect)
{
Expand Down Expand Up @@ -3841,6 +3843,11 @@ impl<'a> Parser<'a> {
})
}

pub fn parse_show_collation(&mut self) -> Result<Statement, ParserError> {
let filter = self.parse_show_statement_filter()?;
Ok(Statement::ShowCollation { filter })
}

pub fn parse_show_statement_filter(
&mut self,
) -> Result<Option<ShowStatementFilter>, ParserError> {
Expand Down
28 changes: 28 additions & 0 deletions tests/sqlparser_mysql.rs
Expand Up @@ -189,6 +189,12 @@ fn parse_show_extended_full() {
assert!(mysql_and_generic()
.parse_sql_statements("SHOW EXTENDED FULL CREATE TABLE mytable")
.is_err());
assert!(mysql_and_generic()
.parse_sql_statements("SHOW EXTENDED FULL COLLATION")
.is_err());
assert!(mysql_and_generic()
.parse_sql_statements("SHOW EXTENDED FULL VARIABLES")
.is_err());
}

#[test]
Expand All @@ -213,6 +219,28 @@ fn parse_show_create() {
}
}

#[test]
fn parse_show_collation() {
assert_eq!(
mysql_and_generic().verified_stmt("SHOW COLLATION"),
Statement::ShowCollation { filter: None }
);
assert_eq!(
mysql_and_generic().verified_stmt("SHOW COLLATION LIKE 'pattern'"),
Statement::ShowCollation {
filter: Some(ShowStatementFilter::Like("pattern".into())),
}
);
assert_eq!(
mysql_and_generic().verified_stmt("SHOW COLLATION WHERE 1 = 2"),
Statement::ShowCollation {
filter: Some(ShowStatementFilter::Where(
mysql_and_generic().verified_expr("1 = 2")
)),
}
);
}

#[test]
fn parse_use() {
assert_eq!(
Expand Down

0 comments on commit bb34268

Please sign in to comment.