Skip to content

Commit

Permalink
Remove 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 4fa6cad commit ad9c98a
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions singlestoredb/mysql/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -980,11 +980,14 @@ 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/collection are being set to utf8mb4, the corresponding global
setting must be also utf8mb4.
Parameters
----------
charset : str
Expand All @@ -997,9 +1000,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 +1106,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 ad9c98a

Please sign in to comment.