Skip to content

Commit 9352477

Browse files
committed
SGA-11783 Address review feedback for SHOW CHARSET
Improves the initial `SHOW CHARSET` implementation by: - Using a named struct for the AST node - Adding AST assertion to the parser test - Improving code comments
1 parent 4a53313 commit 9352477

File tree

3 files changed

+43
-32
lines changed

3 files changed

+43
-32
lines changed

src/ast/mod.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3705,19 +3705,11 @@ pub enum Statement {
37053705
show_options: ShowStatementOptions,
37063706
},
37073707
// ```sql
3708-
// SHOW {CHARACTER SET | CHARSET} [like_or_where]
3708+
// SHOW {CHARACTER SET | CHARSET}
37093709
// ```
3710-
// where:
3711-
// like_or_where: {
3712-
// LIKE 'pattern'
3713-
// | WHERE expr
3714-
// }
3715-
// MySQL specific statement
3710+
// [MySQL]:
37163711
// <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>
3717-
ShowCharset {
3718-
is_shorthand: bool,
3719-
filter: Option<ShowStatementFilter>,
3720-
},
3712+
ShowCharset(ShowCharset),
37213713
/// ```sql
37223714
/// SHOW OBJECTS LIKE 'line%' IN mydb.public
37233715
/// ```
@@ -5688,21 +5680,7 @@ impl fmt::Display for Statement {
56885680
}
56895681
Ok(())
56905682
}
5691-
Statement::ShowCharset {
5692-
is_shorthand,
5693-
filter,
5694-
} => {
5695-
write!(f, "SHOW")?;
5696-
if *is_shorthand {
5697-
write!(f, " CHARSET")?;
5698-
} else {
5699-
write!(f, " CHARACTER SET")?;
5700-
}
5701-
if filter.is_some() {
5702-
write!(f, " {}", filter.as_ref().unwrap())?;
5703-
}
5704-
Ok(())
5705-
}
5683+
Statement::ShowCharset(show_stm) => show_stm.fmt(f),
57065684
Statement::StartTransaction {
57075685
modes,
57085686
begin: syntax_begin,
@@ -9821,6 +9799,32 @@ impl fmt::Display for ShowStatementIn {
98219799
}
98229800
}
98239801

9802+
/// A Show Charset statement
9803+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
9804+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9805+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
9806+
pub struct ShowCharset {
9807+
/// The statement can be written as SHOW CHARSET or SHOW CHARACTER SET
9808+
/// true means CHARSET was used and false means CHARACTER SET was used
9809+
pub is_shorthand: bool,
9810+
pub filter: Option<ShowStatementFilter>,
9811+
}
9812+
9813+
impl fmt::Display for ShowCharset {
9814+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
9815+
write!(f, "SHOW")?;
9816+
if self.is_shorthand {
9817+
write!(f, " CHARSET")?;
9818+
} else {
9819+
write!(f, " CHARACTER SET")?;
9820+
}
9821+
if self.filter.is_some() {
9822+
write!(f, " {}", self.filter.as_ref().unwrap())?;
9823+
}
9824+
Ok(())
9825+
}
9826+
}
9827+
98249828
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
98259829
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
98269830
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]

src/parser/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12518,10 +12518,10 @@ impl<'a> Parser<'a> {
1251812518

1251912519
fn parse_show_charset(&mut self, is_shorthand: bool) -> Result<Statement, ParserError> {
1252012520
// parse one of keywords
12521-
Ok(Statement::ShowCharset {
12521+
Ok(Statement::ShowCharset(ShowCharset {
1252212522
is_shorthand,
1252312523
filter: self.parse_show_statement_filter()?,
12524-
})
12524+
}))
1252512525
}
1252612526

1252712527
fn parse_show_databases(&mut self, terse: bool) -> Result<Statement, ParserError> {

tests/sqlparser_mysql.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4146,8 +4146,15 @@ fn parse_json_member_of() {
41464146

41474147
#[test]
41484148
fn parse_show_charset() {
4149-
let _ = mysql().verified_stmt("SHOW CHARACTER SET");
4150-
let _ = mysql().verified_stmt("SHOW CHARACTER SET LIKE 'utf8mb4%'");
4151-
let _ = mysql().verified_stmt("SHOW CHARSET WHERE charset = 'utf8mb4%'");
4152-
let _ = mysql().verified_stmt("SHOW CHARSET LIKE 'utf8mb4%'");
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%'");
41534160
}

0 commit comments

Comments
 (0)