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 9, 2022
1 parent 231370a commit cc29d03
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/ast/mod.rs
Expand Up @@ -1012,6 +1012,10 @@ pub enum Statement {
table_name: ObjectName,
filter: Option<ShowStatementFilter>,
},
/// SHOW COLLATION
///
/// Note: this is a MySQL-specific statement.
ShowCollation { filter: Option<ShowStatementFilter> },
/// `{ BEGIN [ TRANSACTION | WORK ] | START TRANSACTION } ...`
StartTransaction { modes: Vec<TransactionMode> },
/// `SET TRANSACTION ...`
Expand Down Expand Up @@ -1814,6 +1818,13 @@ impl fmt::Display for Statement {
}
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 @@ -3694,6 +3694,8 @@ 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::COLLATION) {
Ok(self.parse_show_collation()?)
} else {
Ok(Statement::ShowVariable {
variable: self.parse_identifiers()?,
Expand Down Expand Up @@ -3745,6 +3747,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
22 changes: 22 additions & 0 deletions tests/sqlparser_mysql.rs
Expand Up @@ -140,6 +140,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_create_table_auto_increment() {
let sql = "CREATE TABLE foo (bar INT PRIMARY KEY AUTO_INCREMENT)";
Expand Down

0 comments on commit cc29d03

Please sign in to comment.