Skip to content

Commit 15d8bfe

Browse files
etgarperetsiffyio
andauthored
Add support for SHOW CHARSET (#1974)
Co-authored-by: Ifeanyi Ubah <[email protected]>
1 parent bde269b commit 15d8bfe

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

src/ast/mod.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3696,6 +3696,12 @@ pub enum Statement {
36963696
history: bool,
36973697
show_options: ShowStatementOptions,
36983698
},
3699+
// ```sql
3700+
// SHOW {CHARACTER SET | CHARSET}
3701+
// ```
3702+
// [MySQL]:
3703+
// <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>
3704+
ShowCharset(ShowCharset),
36993705
/// ```sql
37003706
/// SHOW OBJECTS LIKE 'line%' IN mydb.public
37013707
/// ```
@@ -5680,6 +5686,7 @@ impl fmt::Display for Statement {
56805686
}
56815687
Ok(())
56825688
}
5689+
Statement::ShowCharset(show_stm) => show_stm.fmt(f),
56835690
Statement::StartTransaction {
56845691
modes,
56855692
begin: syntax_begin,
@@ -9859,6 +9866,32 @@ impl fmt::Display for ShowStatementIn {
98599866
}
98609867
}
98619868

9869+
/// A Show Charset statement
9870+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9871+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9872+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9873+
pub struct ShowCharset {
9874+
/// The statement can be written as `SHOW CHARSET` or `SHOW CHARACTER SET`
9875+
/// true means CHARSET was used and false means CHARACTER SET was used
9876+
pub is_shorthand: bool,
9877+
pub filter: Option<ShowStatementFilter>,
9878+
}
9879+
9880+
impl fmt::Display for ShowCharset {
9881+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9882+
write!(f, "SHOW")?;
9883+
if self.is_shorthand {
9884+
write!(f, " CHARSET")?;
9885+
} else {
9886+
write!(f, " CHARACTER SET")?;
9887+
}
9888+
if self.filter.is_some() {
9889+
write!(f, " {}", self.filter.as_ref().unwrap())?;
9890+
}
9891+
Ok(())
9892+
}
9893+
}
9894+
98629895
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
98639896
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
98649897
#[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
@@ -12595,13 +12595,25 @@ impl<'a> Parser<'a> {
1259512595
self.parse_show_databases(terse)
1259612596
} else if self.parse_keyword(Keyword::SCHEMAS) {
1259712597
self.parse_show_schemas(terse)
12598+
} else if self.parse_keywords(&[Keyword::CHARACTER, Keyword::SET]) {
12599+
self.parse_show_charset(false)
12600+
} else if self.parse_keyword(Keyword::CHARSET) {
12601+
self.parse_show_charset(true)
1259812602
} else {
1259912603
Ok(Statement::ShowVariable {
1260012604
variable: self.parse_identifiers()?,
1260112605
})
1260212606
}
1260312607
}
1260412608

12609+
fn parse_show_charset(&mut self, is_shorthand: bool) -> Result<Statement, ParserError> {
12610+
// parse one of keywords
12611+
Ok(Statement::ShowCharset(ShowCharset {
12612+
is_shorthand,
12613+
filter: self.parse_show_statement_filter()?,
12614+
}))
12615+
}
12616+
1260512617
fn parse_show_databases(&mut self, terse: bool) -> Result<Statement, ParserError> {
1260612618
let history = self.parse_keyword(Keyword::HISTORY);
1260712619
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)