Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SHOW COLLATION #564

Merged
merged 1 commit into from Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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