@@ -5945,6 +5945,85 @@ def test_without_use_unicode_cursor_prepared(self):
5945
5945
)
5946
5946
5947
5947
5948
+ class Bug32496788 (tests .MySQLConnectorTests ):
5949
+ """BUG#32496788: PREPARED STATEMETS ACCEPTS ANY TYPE OF PARAMETERS."""
5950
+
5951
+ table_name = "Bug32496788"
5952
+ test_values = (
5953
+ ("John" , "" , "Doe" , 21 , 77 ),
5954
+ ["Jane" , "" , "Doe" , 19 , 7 ]
5955
+ )
5956
+ exp_values = (
5957
+ [("John" , "" , "Doe" , 21 , 77 )],
5958
+ [("Jane" , "" , "Doe" , 19 , 7 )]
5959
+ )
5960
+
5961
+ def setUp (self ):
5962
+ config = tests .get_mysql_config ()
5963
+ with mysql .connector .connection .MySQLConnection (** config ) as cnx :
5964
+ cnx .cmd_query (f"DROP TABLE IF EXISTS { self .table_name } " )
5965
+ cnx .cmd_query (
5966
+ f"""
5967
+ CREATE TABLE { self .table_name } (
5968
+ column_first_name VARCHAR(30) DEFAULT '' NOT NULL,
5969
+ column_midle_name VARCHAR(30) DEFAULT '' NOT NULL,
5970
+ column_last_name VARCHAR(30) DEFAULT '' NOT NULL,
5971
+ age INT DEFAULT 0 NOT NULL,
5972
+ lucky_number INT DEFAULT 0 NOT NULL
5973
+ ) CHARACTER SET utf8 COLLATE utf8_general_ci
5974
+ """
5975
+ )
5976
+ cnx .commit ()
5977
+
5978
+ def tearDown (self ):
5979
+ config = tests .get_mysql_config ()
5980
+ with mysql .connector .connection .MySQLConnection (** config ) as cnx :
5981
+ cnx .cmd_query (f"DROP TABLE IF EXISTS { self .table_name } " )
5982
+
5983
+ @foreach_cnx ()
5984
+ def test_parameters_type (self ):
5985
+ stmt = f"SELECT * FROM { self .table_name } WHERE age = ? and lucky_number = ?"
5986
+ with self .cnx .cursor (prepared = True ) as cur :
5987
+ # Test incorrect types must raise error
5988
+ for param in ["12" , 12 , 1.3 , lambda : "1" + "2" ]:
5989
+ self .assertRaises (errors .ProgrammingError , cur .execute , stmt , (param ,))
5990
+ with self .assertRaises (errors .ProgrammingError ) as contex :
5991
+ cur .execute (stmt , param )
5992
+ self .assertIn ("Incorrect type of argument, it must be of type tuple or list" ,
5993
+ contex .exception .msg )
5994
+
5995
+ with self .cnx .cursor (prepared = True ) as cur :
5996
+ # Correct form with tuple
5997
+ cur .execute ("SELECT ?, ?" , ("1" , "2" ))
5998
+ self .assertEqual (cur .fetchall (), [('1' , '2' )])
5999
+
6000
+ # Correct form with list
6001
+ cur .execute ("SELECT ?, ?" , ["1" , "2" ])
6002
+ self .assertEqual (cur .fetchall (), [('1' , '2' )])
6003
+
6004
+ with self .cnx .cursor (prepared = True ) as cur :
6005
+ insert_stmt = f"INSERT INTO { self .table_name } VALUES (?, ?, ?, ?, ?)"
6006
+ cur .execute (insert_stmt )
6007
+ with self .assertRaises (errors .ProgrammingError ) as contex :
6008
+ cur .execute (insert_stmt , "JD" )
6009
+ self .assertIn ("Incorrect type of argument, it must be of type tuple or list" ,
6010
+ contex .exception .msg )
6011
+ self .assertRaises (errors .ProgrammingError , cur .execute , insert_stmt , ("JohnDo" ))
6012
+ cur .execute (insert_stmt , self .test_values [0 ])
6013
+ cur .execute (insert_stmt , self .test_values [1 ])
6014
+
6015
+ select_stmt = f"SELECT * FROM { self .table_name } WHERE column_last_name=? and column_first_name=?"
6016
+ self .assertRaises (errors .ProgrammingError , cur .execute , select_stmt , ("JD" ))
6017
+ with self .assertRaises (errors .ProgrammingError ) as contex :
6018
+ cur .execute (select_stmt , "JD" )
6019
+ self .assertIn ("Incorrect type of argument, it must be of type tuple or list" ,
6020
+ contex .exception .msg )
6021
+ cur .execute (select_stmt , ("Doe" , "John" ))
6022
+ self .assertEqual (cur .fetchall (), self .exp_values [0 ])
6023
+ cur .execute (select_stmt , ("Doe" , "Jane" ))
6024
+ self .assertEqual (cur .fetchall (), self .exp_values [1 ])
6025
+
6026
+
5948
6027
class Bug32162928 (tests .MySQLConnectorTests ):
5949
6028
"""BUG#32162928: change user command fails with pure python implementation.
5950
6029
0 commit comments