Skip to content

Commit 21da163

Browse files
committed
BUG#35212199: Check for identifier quotes in the database name
Connector/Python raises an error when setting the database name on a connection that includes non-alphanumeric characters and using the pure Python implementation. This patch fixes the issue by using `connection.cmd_init_db()` to set the database name. Change-Id: I7f597238104fd79b324fe67be8f428f5e4c18b6c
1 parent 25e6200 commit 21da163

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ v8.1.0
1212
======
1313

1414
- WL#15591: Improve the network module
15+
- BUG#35212199: Check for identifier quotes in the database name
1516

1617
v8.0.33
1718
=======

lib/mysql/connector/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ def database(self) -> str:
12791279
@database.setter
12801280
def database(self, value: str) -> None:
12811281
"""Set the current database"""
1282-
self.cmd_query(f"USE {value}")
1282+
self.cmd_init_db(value)
12831283

12841284
def is_connected(self) -> bool:
12851285
"""Reports whether the connection to MySQL Server is available

tests/test_bugs.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7484,3 +7484,42 @@ def compressed_packet_incomplete_at_last(self):
74847484
res = cur.fetchone()
74857485
self.assertEqual(data[:-5000], res[0][:-5000])
74867486
self.assertEqual(len(data), len(res[0]))
7487+
7488+
7489+
class BugOra35212199(tests.MySQLConnectorTests):
7490+
"""BUG#35212199: Check for identifier quotes in the database name
7491+
7492+
Connector/Python raises an error when setting the database name on a
7493+
connection that includes non-alphanumeric characters and using the pure
7494+
Python implementation.
7495+
7496+
This patch fixes the issue by using `connection.cmd_init_db()` to set
7497+
the database name.
7498+
"""
7499+
7500+
database = "abc-abc|$%"
7501+
7502+
def setUp(self):
7503+
with mysql.connector.connect(**tests.get_mysql_config()) as cnx:
7504+
try:
7505+
cnx.cmd_query(f"DROP DATABASE `{self.database}`")
7506+
except errors.DatabaseError: # Database already exists
7507+
pass
7508+
cnx.cmd_query(f"CREATE DATABASE `{self.database}`")
7509+
7510+
def tearDown(self):
7511+
with mysql.connector.connect(**tests.get_mysql_config()) as cnx:
7512+
cnx.cmd_query(f"DROP DATABASE `{self.database}`")
7513+
7514+
@foreach_cnx()
7515+
def test_set_database_name(self):
7516+
"""Test setting the database on an established connection."""
7517+
self.cnx.database = self.database
7518+
7519+
@foreach_cnx()
7520+
def test_connect_database_name(self):
7521+
"""Test setting the database when establishing a connection."""
7522+
config = tests.get_mysql_config()
7523+
config["database"] = self.database
7524+
with self.cnx.__class__(**config) as cnx:
7525+
self.assertTrue(cnx.is_connected())

0 commit comments

Comments
 (0)