Skip to content

Commit 7f33dae

Browse files
Replace SET NAMES usage which is no-op in SingleStore
1 parent 900fd22 commit 7f33dae

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

singlestoredb/mysql/connection.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -986,11 +986,18 @@ def set_charset(self, charset):
986986

987987
def set_character_set(self, charset, collation=None):
988988
"""
989-
Set charaset (and collation) on the server.
989+
Set session charaset (and collation) on the server.
990990
991-
Send "SET NAMES charset [COLLATE collation]" query.
991+
Send "SET [COLLATION|CHARACTER_SET]_SERVER = [collation|charset]" query.
992992
Update Connection.encoding based on charset.
993993
994+
If charset/collation are being set to utf8mb4, the corresponding global
995+
variables (COLLATION_SERVER and CHARACTER_SET_SERVER) must be also set
996+
to utf8mb4. This is true by default for SingleStore 8.7+. For previuous
997+
versions or non-default setting user must manully run the query
998+
`SET global collation_connection = utf8mb4_general_ci`
999+
replacing utf8mb4_general_ci with {collation}.
1000+
9941001
Parameters
9951002
----------
9961003
charset : str
@@ -1003,9 +1010,9 @@ def set_character_set(self, charset, collation=None):
10031010
encoding = charset_by_name(charset).encoding
10041011

10051012
if collation:
1006-
query = f'SET NAMES {charset} COLLATE {collation}'
1013+
query = f'SET COLLATION_SERVER={collation}'
10071014
else:
1008-
query = f'SET NAMES {charset}'
1015+
query = f'SET CHARACTER_SET_SERVER={charset}'
10091016
self._execute_command(COMMAND.COM_QUERY, query)
10101017
self._read_packet()
10111018
self.charset = charset
@@ -1109,19 +1116,6 @@ def connect(self, sock=None):
11091116
self._get_server_information()
11101117
self._request_authentication()
11111118

1112-
# Send "SET NAMES" query on init for:
1113-
# - Ensure charaset (and collation) is set to the server.
1114-
# - collation_id in handshake packet may be ignored.
1115-
# - If collation is not specified, we don't know what is server's
1116-
# default collation for the charset. For example, default collation
1117-
# of utf8mb4 is:
1118-
# - MySQL 5.7, MariaDB 10.x: utf8mb4_general_ci
1119-
# - MySQL 8.0: utf8mb4_0900_ai_ci
1120-
#
1121-
# Reference:
1122-
# - https://github.com/PyMySQL/PyMySQL/issues/1092
1123-
# - https://github.com/wagtail/wagtail/issues/9477
1124-
# - https://zenn.dev/methane/articles/2023-mysql-collation (Japanese)
11251119
self.set_character_set(self.charset, self.collation)
11261120

11271121
if self.sql_mode is not None:

0 commit comments

Comments
 (0)