1212from awswrangler import _data_types , _utils , exceptions
1313from awswrangler import _databases as _db_utils
1414from awswrangler ._config import apply_configs
15+ from awswrangler ._sql_utils import identifier
1516
1617if TYPE_CHECKING :
1718 try :
@@ -37,15 +38,19 @@ def _validate_connection(con: "Connection[Any]") -> None:
3738
3839
3940def _drop_table (cursor : "Cursor" , schema : Optional [str ], table : str ) -> None :
40- schema_str = f"` { schema } ` ." if schema else ""
41- sql = f"DROP TABLE IF EXISTS { schema_str } ` { table } ` "
41+ schema_str = f"{ identifier ( schema ) } ." if schema else ""
42+ sql = f"DROP TABLE IF EXISTS { schema_str } { identifier ( table ) } "
4243 _logger .debug ("Drop table query:\n %s" , sql )
4344 cursor .execute (sql )
4445
4546
4647def _does_table_exist (cursor : "Cursor" , schema : Optional [str ], table : str ) -> bool :
47- schema_str = f"TABLE_SCHEMA = '{ schema } ' AND" if schema else ""
48- cursor .execute (f"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE { schema_str } TABLE_NAME = %s" , args = [table ])
48+ if schema :
49+ cursor .execute (
50+ "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s" , args = [schema , table ]
51+ )
52+ else :
53+ cursor .execute ("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = %s" , args = [table ])
4954 return len (cursor .fetchall ()) > 0
5055
5156
@@ -71,8 +76,8 @@ def _create_table(
7176 varchar_lengths = varchar_lengths ,
7277 converter_func = _data_types .pyarrow2mysql ,
7378 )
74- cols_str : str = "" .join ([f"` { k } ` { v } ,\n " for k , v in mysql_types .items ()])[:- 2 ]
75- sql = f"CREATE TABLE IF NOT EXISTS ` { schema } `.` { table } ` (\n { cols_str } )"
79+ cols_str : str = "" .join ([f"{ identifier ( k ) } { v } ,\n " for k , v in mysql_types .items ()])[:- 2 ]
80+ sql = f"CREATE TABLE IF NOT EXISTS { identifier ( schema ) } . { identifier ( table ) } (\n { cols_str } )"
7681 _logger .debug ("Create table query:\n %s" , sql )
7782 cursor .execute (sql )
7883
@@ -419,7 +424,11 @@ def read_sql_table(
419424 >>> con.close()
420425
421426 """
422- sql : str = f"SELECT * FROM `{ table } `" if schema is None else f"SELECT * FROM `{ schema } `.`{ table } `"
427+ sql : str = (
428+ f"SELECT * FROM { identifier (table )} "
429+ if schema is None
430+ else f"SELECT * FROM { identifier (schema )} .{ identifier (table )} "
431+ )
423432 return read_sql_query (
424433 sql = sql ,
425434 con = con ,
@@ -551,29 +560,35 @@ def to_sql(
551560 upsert_str = ""
552561 ignore_str = " IGNORE" if mode == "ignore" else ""
553562 if use_column_names :
554- insertion_columns = f"(` { '`, ` ' .join (df .columns ) } ` )"
563+ insertion_columns = f"({ ', ' .join ([ identifier ( col ) for col in df .columns ]) } )"
555564 if mode == "upsert_duplicate_key" :
556- upsert_columns = ", " .join (df .columns .map (lambda column : f"` { column } ` =VALUES(` { column } ` )" ))
565+ upsert_columns = ", " .join (df .columns .map (lambda col : f"{ identifier ( col ) } =VALUES({ identifier ( col ) } )" ))
557566 upsert_str = f" ON DUPLICATE KEY UPDATE { upsert_columns } "
558567 placeholder_parameter_pair_generator = _db_utils .generate_placeholder_parameter_pairs (
559568 df = df , column_placeholders = column_placeholders , chunksize = chunksize
560569 )
561570 sql : str
562571 for placeholders , parameters in placeholder_parameter_pair_generator :
563572 if mode == "upsert_replace_into" :
564- sql = f"REPLACE INTO ` { schema } `.` { table } ` { insertion_columns } VALUES { placeholders } "
573+ sql = f"REPLACE INTO { identifier ( schema ) } . { identifier ( table ) } { insertion_columns } VALUES { placeholders } "
565574 else :
566- sql = f"""INSERT{ ignore_str } INTO ` { schema } `.` { table } ` { insertion_columns }
575+ sql = f"""INSERT{ ignore_str } INTO { identifier ( schema ) } . { identifier ( table ) } { insertion_columns }
567576VALUES { placeholders } { upsert_str } """
568577 _logger .debug ("sql: %s" , sql )
569578 cursor .executemany (sql , (parameters ,))
570579 con .commit ()
571580 if mode == "upsert_distinct" :
572581 temp_table = f"{ table } _{ uuid .uuid4 ().hex } "
573- cursor .execute (f"CREATE TABLE `{ schema } `.`{ temp_table } ` LIKE `{ schema } `.`{ table } `" )
574- cursor .execute (f"INSERT INTO `{ schema } `.`{ temp_table } ` SELECT DISTINCT * FROM `{ schema } `.`{ table } `" )
575- cursor .execute (f"DROP TABLE IF EXISTS `{ schema } `.`{ table } `" )
576- cursor .execute (f"ALTER TABLE `{ schema } `.`{ temp_table } ` RENAME TO `{ table } `" )
582+ cursor .execute (
583+ f"CREATE TABLE { identifier (schema )} .{ identifier (temp_table )} LIKE { identifier (schema )} .{ identifier (table )} "
584+ )
585+ cursor .execute (
586+ f"INSERT INTO { identifier (schema )} .{ identifier (temp_table )} SELECT DISTINCT * FROM { identifier (schema )} .{ identifier (table )} "
587+ )
588+ cursor .execute (f"DROP TABLE IF EXISTS { identifier (schema )} .{ identifier (table )} " )
589+ cursor .execute (
590+ f"ALTER TABLE { identifier (schema )} .{ identifier (temp_table )} RENAME TO { identifier (table )} "
591+ )
577592 con .commit ()
578593
579594 except Exception as ex :
0 commit comments