Skip to content

Commit

Permalink
Replace SET NAMES usage which is no-op in SingleStore
Browse files Browse the repository at this point in the history
  • Loading branch information
pmishchenko-ua committed Aug 30, 2024
1 parent 315727d commit 756d21c
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions singlestoredb/mysql/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,11 +980,18 @@ def set_charset(self, charset):

def set_character_set(self, charset, collation=None):
"""
Set charaset (and collation) on the server.
Set session charaset (and collation) on the server.
Send "SET NAMES charset [COLLATE collation]" query.
Send "SET [COLLATION|CHARACTER_SET]_SERVER = [collation|charset]" query.
Update Connection.encoding based on charset.
If charset/collation are being set to utf8mb4, the corresponding global
variables (COLLATION_SERVER and CHARACTER_SET_SERVER) must be also set
to utf8mb4. This is true by default for SingleStore 8.7+. For previuous
versions or non-default setting user must manully run the query
`SET global collation_connection = utf8mb4_general_ci`
replacing utf8mb4_general_ci with {collation}.
Parameters
----------
charset : str
Expand All @@ -997,9 +1004,9 @@ def set_character_set(self, charset, collation=None):
encoding = charset_by_name(charset).encoding

if collation:
query = f'SET NAMES {charset} COLLATE {collation}'
query = f'SET COLLATION_SERVER={collation}'
else:
query = f'SET NAMES {charset}'
query = f'SET CHARACTER_SET_SERVER={charset}'
self._execute_command(COMMAND.COM_QUERY, query)
self._read_packet()
self.charset = charset
Expand Down Expand Up @@ -1103,19 +1110,6 @@ def connect(self, sock=None):
self._get_server_information()
self._request_authentication()

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

if self.sql_mode is not None:
Expand Down

0 comments on commit 756d21c

Please sign in to comment.