@@ -980,11 +980,18 @@ def set_charset(self, charset):
980980
981981 def set_character_set (self , charset , collation = None ):
982982 """
983- Set charaset (and collation) on the server.
983+ Set session charaset (and collation) on the server.
984984
985- Send "SET NAMES charset [COLLATE collation]" query.
985+ Send "SET [COLLATION|CHARACTER_SET]_SERVER = [ collation|charset ]" query.
986986 Update Connection.encoding based on charset.
987987
988+ If charset/collation are being set to utf8mb4, the corresponding global
989+ variables (COLLATION_SERVER and CHARACTER_SET_SERVER) must be also set
990+ to utf8mb4. This is true by default for SingleStore 8.7+. For previuous
991+ versions or non-default setting user must manully run the query
992+ `SET global collation_connection = utf8mb4_general_ci`
993+ replacing utf8mb4_general_ci with {collation}.
994+
988995 Parameters
989996 ----------
990997 charset : str
@@ -997,9 +1004,9 @@ def set_character_set(self, charset, collation=None):
9971004 encoding = charset_by_name (charset ).encoding
9981005
9991006 if collation :
1000- query = f'SET NAMES { charset } COLLATE { collation } '
1007+ query = f'SET COLLATION_SERVER= { collation } '
10011008 else :
1002- query = f'SET NAMES { charset } '
1009+ query = f'SET CHARACTER_SET_SERVER= { charset } '
10031010 self ._execute_command (COMMAND .COM_QUERY , query )
10041011 self ._read_packet ()
10051012 self .charset = charset
@@ -1103,19 +1110,6 @@ def connect(self, sock=None):
11031110 self ._get_server_information ()
11041111 self ._request_authentication ()
11051112
1106- # Send "SET NAMES" query on init for:
1107- # - Ensure charaset (and collation) is set to the server.
1108- # - collation_id in handshake packet may be ignored.
1109- # - If collation is not specified, we don't know what is server's
1110- # default collation for the charset. For example, default collation
1111- # of utf8mb4 is:
1112- # - MySQL 5.7, MariaDB 10.x: utf8mb4_general_ci
1113- # - MySQL 8.0: utf8mb4_0900_ai_ci
1114- #
1115- # Reference:
1116- # - https://github.com/PyMySQL/PyMySQL/issues/1092
1117- # - https://github.com/wagtail/wagtail/issues/9477
1118- # - https://zenn.dev/methane/articles/2023-mysql-collation (Japanese)
11191113 self .set_character_set (self .charset , self .collation )
11201114
11211115 if self .sql_mode is not None :
0 commit comments