Skip to content

Commit 5011169

Browse files
etgarperetsiffyio
authored andcommitted
Add support for SHOW CHARSET (apache#1974)
Co-authored-by: Ifeanyi Ubah <ify1992@yahoo.com>
1 parent 31d62c8 commit 5011169

4 files changed

Lines changed: 61 additions & 0 deletions

File tree

src/ast/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3725,6 +3725,12 @@ pub enum Statement {
37253725
history: bool,
37263726
show_options: ShowStatementOptions,
37273727
},
3728+
// ```sql
3729+
// SHOW {CHARACTER SET | CHARSET}
3730+
// ```
3731+
// [MySQL]:
3732+
// <https://dev.mysql.com/doc/refman/8.4/en/show.html#:~:text=SHOW%20%7BCHARACTER%20SET%20%7C%20CHARSET%7D%20%5Blike_or_where%5D>
3733+
ShowCharset(ShowCharset),
37283734
/// ```sql
37293735
/// SHOW OBJECTS LIKE 'line%' IN mydb.public
37303736
/// ```
@@ -5709,6 +5715,7 @@ impl fmt::Display for Statement {
57095715
}
57105716
Ok(())
57115717
}
5718+
Statement::ShowCharset(show_stm) => show_stm.fmt(f),
57125719
Statement::StartTransaction {
57135720
modes,
57145721
begin: syntax_begin,
@@ -9888,6 +9895,32 @@ impl fmt::Display for ShowStatementIn {
98889895
}
98899896
}
98909897

9898+
/// A Show Charset statement
9899+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9900+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9901+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9902+
pub struct ShowCharset {
9903+
/// The statement can be written as `SHOW CHARSET` or `SHOW CHARACTER SET`
9904+
/// true means CHARSET was used and false means CHARACTER SET was used
9905+
pub is_shorthand: bool,
9906+
pub filter: Option<ShowStatementFilter>,
9907+
}
9908+
9909+
impl fmt::Display for ShowCharset {
9910+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9911+
write!(f, "SHOW")?;
9912+
if self.is_shorthand {
9913+
write!(f, " CHARSET")?;
9914+
} else {
9915+
write!(f, " CHARACTER SET")?;
9916+
}
9917+
if self.filter.is_some() {
9918+
write!(f, " {}", self.filter.as_ref().unwrap())?;
9919+
}
9920+
Ok(())
9921+
}
9922+
}
9923+
98919924
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
98929925
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
98939926
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ impl Spanned for Statement {
477477
Statement::ShowColumns { .. } => Span::empty(),
478478
Statement::ShowTables { .. } => Span::empty(),
479479
Statement::ShowCollation { .. } => Span::empty(),
480+
Statement::ShowCharset { .. } => Span::empty(),
480481
Statement::Use(u) => u.span(),
481482
Statement::StartTransaction { .. } => Span::empty(),
482483
Statement::Comment { .. } => Span::empty(),

src/parser/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12626,13 +12626,25 @@ impl<'a> Parser<'a> {
1262612626
self.parse_show_databases(terse)
1262712627
} else if self.parse_keyword(Keyword::SCHEMAS) {
1262812628
self.parse_show_schemas(terse)
12629+
} else if self.parse_keywords(&[Keyword::CHARACTER, Keyword::SET]) {
12630+
self.parse_show_charset(false)
12631+
} else if self.parse_keyword(Keyword::CHARSET) {
12632+
self.parse_show_charset(true)
1262912633
} else {
1263012634
Ok(Statement::ShowVariable {
1263112635
variable: self.parse_identifiers()?,
1263212636
})
1263312637
}
1263412638
}
1263512639

12640+
fn parse_show_charset(&mut self, is_shorthand: bool) -> Result<Statement, ParserError> {
12641+
// parse one of keywords
12642+
Ok(Statement::ShowCharset(ShowCharset {
12643+
is_shorthand,
12644+
filter: self.parse_show_statement_filter()?,
12645+
}))
12646+
}
12647+
1263612648
fn parse_show_databases(&mut self, terse: bool) -> Result<Statement, ParserError> {
1263712649
let history = self.parse_keyword(Keyword::HISTORY);
1263812650
let show_options = self.parse_show_stmt_options()?;

tests/sqlparser_mysql.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4143,3 +4143,18 @@ fn parse_json_member_of() {
41434143
_ => panic!("Unexpected statement {stmt}"),
41444144
}
41454145
}
4146+
4147+
#[test]
4148+
fn parse_show_charset() {
4149+
let res = mysql().verified_stmt("SHOW CHARACTER SET");
4150+
assert_eq!(
4151+
res,
4152+
Statement::ShowCharset(ShowCharset {
4153+
is_shorthand: false,
4154+
filter: None
4155+
})
4156+
);
4157+
mysql().verified_stmt("SHOW CHARACTER SET LIKE 'utf8mb4%'");
4158+
mysql().verified_stmt("SHOW CHARSET WHERE charset = 'utf8mb4%'");
4159+
mysql().verified_stmt("SHOW CHARSET LIKE 'utf8mb4%'");
4160+
}

0 commit comments

Comments
 (0)