File tree 8 files changed +55
-13
lines changed
8 files changed +55
-13
lines changed Original file line number Diff line number Diff line change @@ -2956,10 +2956,8 @@ pub enum Statement {
2956
2956
/// ```sql
2957
2957
/// SET NAMES 'charset_name' [COLLATE 'collation_name']
2958
2958
/// ```
2959
- ///
2960
- /// Note: this is a MySQL-specific statement.
2961
2959
SetNames {
2962
- charset_name : String ,
2960
+ charset_name : Ident ,
2963
2961
collation_name : Option < String > ,
2964
2962
} ,
2965
2963
/// ```sql
@@ -4684,8 +4682,7 @@ impl fmt::Display for Statement {
4684
4682
charset_name,
4685
4683
collation_name,
4686
4684
} => {
4687
- f. write_str ( "SET NAMES " ) ?;
4688
- f. write_str ( charset_name) ?;
4685
+ write ! ( f, "SET NAMES {}" , charset_name) ?;
4689
4686
4690
4687
if let Some ( collation) = collation_name {
4691
4688
f. write_str ( " COLLATE " ) ?;
Original file line number Diff line number Diff line change @@ -155,4 +155,12 @@ impl Dialect for GenericDialect {
155
155
fn supports_match_against ( & self ) -> bool {
156
156
true
157
157
}
158
+
159
+ fn supports_set_names ( & self ) -> bool {
160
+ true
161
+ }
162
+
163
+ fn supports_set_names_collation ( & self ) -> bool {
164
+ true
165
+ }
158
166
}
Original file line number Diff line number Diff line change @@ -953,6 +953,21 @@ pub trait Dialect: Debug + Any {
953
953
fn supports_order_by_all ( & self ) -> bool {
954
954
false
955
955
}
956
+
957
+ /// Returns true if the dialect supports `SET NAMES <charset_name>`.
958
+ ///
959
+ /// - [MySQL](https://dev.mysql.com/doc/refman/8.4/en/set-names.html)
960
+ /// - [Postgres](https://www.postgresql.org/docs/17/sql-set.html)
961
+ fn supports_set_names ( & self ) -> bool {
962
+ false
963
+ }
964
+
965
+ /// Returns true if the dialect supports `SET NAMES <charset_name> [COLLATE <collation_name>]`.
966
+ ///
967
+ /// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/set-names.html)
968
+ fn supports_set_names_collation ( & self ) -> bool {
969
+ false
970
+ }
956
971
}
957
972
958
973
/// This represents the operators for which precedence must be defined
Original file line number Diff line number Diff line change @@ -133,6 +133,14 @@ impl Dialect for MySqlDialect {
133
133
fn supports_match_against ( & self ) -> bool {
134
134
true
135
135
}
136
+
137
+ fn supports_set_names ( & self ) -> bool {
138
+ true
139
+ }
140
+
141
+ fn supports_set_names_collation ( & self ) -> bool {
142
+ true
143
+ }
136
144
}
137
145
138
146
/// `LOCK TABLES`
Original file line number Diff line number Diff line change @@ -254,4 +254,8 @@ impl Dialect for PostgreSqlDialect {
254
254
fn supports_geometric_types ( & self ) -> bool {
255
255
true
256
256
}
257
+
258
+ fn supports_set_names ( & self ) -> bool {
259
+ true
260
+ }
257
261
}
Original file line number Diff line number Diff line change @@ -10962,15 +10962,17 @@ impl<'a> Parser<'a> {
10962
10962
OneOrManyWithParens::One(self.parse_object_name(false)?)
10963
10963
};
10964
10964
10965
- if matches!(&variables, OneOrManyWithParens::One(variable) if variable.to_string().eq_ignore_ascii_case("NAMES")
10966
- && dialect_of!(self is MySqlDialect | GenericDialect))
10967
- {
10965
+ let names = matches!(&variables, OneOrManyWithParens::One(variable) if variable.to_string().eq_ignore_ascii_case("NAMES"));
10966
+
10967
+ if names && self.dialect.supports_set_names() {
10968
10968
if self.parse_keyword(Keyword::DEFAULT) {
10969
10969
return Ok(Statement::SetNamesDefault {});
10970
10970
}
10971
10971
10972
- let charset_name = self.parse_literal_string()?;
10973
- let collation_name = if self.parse_one_of_keywords(&[Keyword::COLLATE]).is_some() {
10972
+ let charset_name = self.parse_identifier()?;
10973
+ let collation_name = if self.dialect.supports_set_names_collation()
10974
+ && self.parse_one_of_keywords(&[Keyword::COLLATE]).is_some()
10975
+ {
10974
10976
Some(self.parse_literal_string()?)
10975
10977
} else {
10976
10978
None
Original file line number Diff line number Diff line change @@ -2685,7 +2685,7 @@ fn parse_set_names() {
2685
2685
assert_eq ! (
2686
2686
stmt,
2687
2687
Statement :: SetNames {
2688
- charset_name: "utf8mb4" . to_string ( ) ,
2688
+ charset_name: "utf8mb4" . into ( ) ,
2689
2689
collation_name: None ,
2690
2690
}
2691
2691
) ;
@@ -2694,7 +2694,7 @@ fn parse_set_names() {
2694
2694
assert_eq ! (
2695
2695
stmt,
2696
2696
Statement :: SetNames {
2697
- charset_name: "utf8mb4" . to_string ( ) ,
2697
+ charset_name: "utf8mb4" . into ( ) ,
2698
2698
collation_name: Some ( "bogus" . to_string( ) ) ,
2699
2699
}
2700
2700
) ;
@@ -2705,7 +2705,7 @@ fn parse_set_names() {
2705
2705
assert_eq ! (
2706
2706
stmt,
2707
2707
vec![ Statement :: SetNames {
2708
- charset_name: "utf8mb4" . to_string ( ) ,
2708
+ charset_name: "utf8mb4" . into ( ) ,
2709
2709
collation_name: Some ( "bogus" . to_string( ) ) ,
2710
2710
} ]
2711
2711
) ;
Original file line number Diff line number Diff line change @@ -5533,3 +5533,11 @@ fn parse_varbit_datatype() {
5533
5533
_ => unreachable ! ( ) ,
5534
5534
}
5535
5535
}
5536
+
5537
+ #[ test]
5538
+ fn parse_set_names ( ) {
5539
+ pg_and_generic ( ) . verified_stmt ( "SET NAMES 'UTF8'" ) ;
5540
+ pg_and_generic ( ) . verified_stmt ( "SET NAMES 'utf8'" ) ;
5541
+ pg ( ) . parse_sql_statements ( "SET NAMES 'UTF8' COLLATE bogus" )
5542
+ . expect_err ( "Postgres does not support COLLATE" ) ;
5543
+ }
You can’t perform that action at this time.
0 commit comments