Skip to content

Commit efad6b4

Browse files
Replace prefixes parameter with temporary.
1 parent 775a05a commit efad6b4

File tree

3 files changed

+42
-51
lines changed

3 files changed

+42
-51
lines changed

pandas/core/generic.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2788,7 +2788,7 @@ def to_sql(
27882788
chunksize: int | None = None,
27892789
dtype: DtypeArg | None = None,
27902790
method: Literal["multi"] | Callable | None = None,
2791-
prefixes: Sequence[str] | None = None,
2791+
temporary: bool = False,
27922792
) -> int | None:
27932793
"""
27942794
Write records stored in a DataFrame to a SQL database.
@@ -2845,9 +2845,8 @@ def to_sql(
28452845
28462846
Details and a sample callable implementation can be found in the
28472847
section :ref:`insert method <io.sql.method>`.
2848-
prefixes : sequence, optional
2849-
A list of strings to insert after CREATE in the CREATE TABLE statement.
2850-
They will be separated by spaces.
2848+
temporary : bool, default False
2849+
Indicates if the created, replaced or appended table is temporary.
28512850
28522851
Returns
28532852
-------
@@ -3021,7 +3020,7 @@ def to_sql(
30213020
chunksize=chunksize,
30223021
dtype=dtype,
30233022
method=method,
3024-
prefixes=prefixes,
3023+
temporary=temporary,
30253024
)
30263025

30273026
@final

pandas/io/sql.py

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
Generator,
7373
Iterator,
7474
Mapping,
75-
Sequence,
7675
)
7776

7877
from sqlalchemy import Table
@@ -745,7 +744,7 @@ def to_sql(
745744
chunksize: int | None = None,
746745
dtype: DtypeArg | None = None,
747746
method: Literal["multi"] | Callable | None = None,
748-
prefixes: Sequence[str] | None = None,
747+
temporary: bool = False,
749748
engine: str = "auto",
750749
**engine_kwargs,
751750
) -> int | None:
@@ -793,9 +792,8 @@ def to_sql(
793792
794793
Details and a sample callable implementation can be found in the
795794
section :ref:`insert method <io.sql.method>`.
796-
prefixes : sequence, optional
797-
A list of strings to insert after CREATE in the CREATE TABLE statement.
798-
They will be separated by spaces.
795+
temporary : bool, default False
796+
Indicates if the created, replaced or appended table is temporary.
799797
engine : {'auto', 'sqlalchemy'}, default 'auto'
800798
SQL engine library to use. If 'auto', then the option
801799
``io.sql.engine`` is used. The default ``io.sql.engine``
@@ -844,7 +842,7 @@ def to_sql(
844842
chunksize=chunksize,
845843
dtype=dtype,
846844
method=method,
847-
prefixes=prefixes,
845+
temporary=temporary,
848846
engine=engine,
849847
**engine_kwargs,
850848
)
@@ -938,7 +936,7 @@ def __init__(
938936
schema=None,
939937
keys=None,
940938
dtype: DtypeArg | None = None,
941-
prefixes: Sequence[str] | None = None,
939+
temporary: bool = False,
942940
) -> None:
943941
self.name = name
944942
self.pd_sql = pandas_sql_engine
@@ -949,11 +947,7 @@ def __init__(
949947
self.if_exists = if_exists
950948
self.keys = keys
951949
self.dtype = dtype
952-
self.prefixes = prefixes
953-
# check if the table to be created is a temporary table
954-
self.is_temporary = self.prefixes is not None and "TEMPORARY".casefold() in [
955-
prefix.casefold() for prefix in self.prefixes
956-
]
950+
self.temporary = temporary
957951

958952
if frame is not None:
959953
# We want to initialize based on a dataframe
@@ -1000,7 +994,7 @@ def _exists_temporary(self):
1000994
return False
1001995

1002996
def exists(self):
1003-
if self.is_temporary:
997+
if self.temporary:
1004998
return self._exists_temporary()
1005999
else:
10061000
return self.pd_sql.has_table(self.name, self.schema)
@@ -1012,7 +1006,7 @@ def sql_schema(self) -> str:
10121006

10131007
def _execute_create(self) -> None:
10141008
# Inserting table into database, add to MetaData object
1015-
if not self.is_temporary:
1009+
if not self.temporary:
10161010
# only insert into meta data, if table is not temporary
10171011
self.table = self.table.to_metadata(self.pd_sql.meta)
10181012
with self.pd_sql.run_transaction():
@@ -1023,7 +1017,7 @@ def create(self) -> None:
10231017
if self.if_exists == "fail":
10241018
raise ValueError(f"Table '{self.name}' already exists.")
10251019
if self.if_exists == "replace":
1026-
if self.is_temporary:
1020+
if self.temporary:
10271021
self._drop_temporary_table()
10281022
else:
10291023
self.pd_sql.drop_table(self.name, self.schema)
@@ -1317,10 +1311,16 @@ def _create_table_setup(self):
13171311

13181312
schema = self.schema or self.pd_sql.meta.schema
13191313

1314+
# check if table is temporary
1315+
if self.temporary:
1316+
prefixes = ["TEMPORARY"]
1317+
else:
1318+
prefixes = None
1319+
13201320
# At this point, attach to new metadata, only attach to self.meta
13211321
# once table is created.
13221322
meta = MetaData()
1323-
return Table(self.name, meta, *columns, schema=schema, prefixes=self.prefixes)
1323+
return Table(self.name, meta, *columns, schema=schema, prefixes=prefixes)
13241324

13251325
def _harmonize_columns(
13261326
self,
@@ -1538,7 +1538,7 @@ def to_sql(
15381538
chunksize: int | None = None,
15391539
dtype: DtypeArg | None = None,
15401540
method: Literal["multi"] | Callable | None = None,
1541-
prefixes: Sequence[str] | None = None,
1541+
temporary: bool = False,
15421542
engine: str = "auto",
15431543
**engine_kwargs,
15441544
) -> int | None:
@@ -1923,7 +1923,7 @@ def prep_table(
19231923
index_label=None,
19241924
schema=None,
19251925
dtype: DtypeArg | None = None,
1926-
prefixes: Sequence[str] | None = None,
1926+
temporary: bool = False,
19271927
) -> SQLTable:
19281928
"""
19291929
Prepares table in the database for data insertion. Creates it if needed, etc.
@@ -1959,7 +1959,7 @@ def prep_table(
19591959
index_label=index_label,
19601960
schema=schema,
19611961
dtype=dtype,
1962-
prefixes=prefixes,
1962+
temporary=temporary,
19631963
)
19641964
table.create()
19651965
return table
@@ -2004,7 +2004,7 @@ def to_sql(
20042004
chunksize: int | None = None,
20052005
dtype: DtypeArg | None = None,
20062006
method: Literal["multi"] | Callable | None = None,
2007-
prefixes: Sequence[str] | None = None,
2007+
temporary: bool = False,
20082008
engine: str = "auto",
20092009
**engine_kwargs,
20102010
) -> int | None:
@@ -2046,9 +2046,8 @@ def to_sql(
20462046
20472047
Details and a sample callable implementation can be found in the
20482048
section :ref:`insert method <io.sql.method>`.
2049-
prefixes : sequence, optional
2050-
A list of strings to insert after CREATE in the CREATE TABLE statement.
2051-
They will be separated by spaces.
2049+
temporary : bool, default False
2050+
Indicates if the created, replaced or appended table is temporary.
20522051
engine : {'auto', 'sqlalchemy'}, default 'auto'
20532052
SQL engine library to use. If 'auto', then the option
20542053
``io.sql.engine`` is used. The default ``io.sql.engine``
@@ -2069,7 +2068,7 @@ def to_sql(
20692068
index_label=index_label,
20702069
schema=schema,
20712070
dtype=dtype,
2072-
prefixes=prefixes,
2071+
temporary=temporary,
20732072
)
20742073

20752074
total_inserted = sql_engine.insert_records(
@@ -2085,7 +2084,7 @@ def to_sql(
20852084
)
20862085

20872086
# only check case sensitivity for non temporary tables
2088-
if not table.is_temporary:
2087+
if not table.temporary:
20892088
self.check_case_sensitive(name=name, schema=schema)
20902089
return total_inserted
20912090

@@ -2364,7 +2363,7 @@ def to_sql(
23642363
chunksize: int | None = None,
23652364
dtype: DtypeArg | None = None,
23662365
method: Literal["multi"] | Callable | None = None,
2367-
prefixes: Sequence[str] | None = None,
2366+
temporary: bool = False,
23682367
engine: str = "auto",
23692368
**engine_kwargs,
23702369
) -> int | None:
@@ -2394,9 +2393,8 @@ def to_sql(
23942393
Raises NotImplementedError
23952394
method : {None', 'multi', callable}, default None
23962395
Raises NotImplementedError
2397-
prefixes : sequence, optional
2398-
A list of strings to insert after CREATE in the CREATE TABLE statement.
2399-
They will be separated by spaces.
2396+
temporary : bool, default False
2397+
Indicates if the created, replaced or appended table is temporary.
24002398
engine : {'auto', 'sqlalchemy'}, default 'auto'
24012399
Raises NotImplementedError if not set to 'auto'
24022400
"""
@@ -2415,11 +2413,6 @@ def to_sql(
24152413
"engine != 'auto' not implemented for ADBC drivers"
24162414
)
24172415

2418-
# check if the table to be created is a temporary table
2419-
temporary = prefixes is not None and "TEMPORARY".casefold() in [
2420-
prefix.casefold() for prefix in prefixes
2421-
]
2422-
24232416
if schema:
24242417
table_name = f"{schema}.{name}"
24252418
else:
@@ -2864,7 +2857,7 @@ def to_sql(
28642857
chunksize: int | None = None,
28652858
dtype: DtypeArg | None = None,
28662859
method: Literal["multi"] | Callable | None = None,
2867-
prefixes: Sequence[str] | None = None,
2860+
temporary: bool = False,
28682861
engine: str = "auto",
28692862
**engine_kwargs,
28702863
) -> int | None:
@@ -2905,9 +2898,8 @@ def to_sql(
29052898
29062899
Details and a sample callable implementation can be found in the
29072900
section :ref:`insert method <io.sql.method>`.
2908-
prefixes : sequence, optional
2909-
A list of strings to insert after CREATE in the CREATE TABLE statement.
2910-
They will be separated by spaces.
2901+
temporary : bool, default False
2902+
Indicates if the created, replaced or appended table is temporary.
29112903
"""
29122904
if dtype:
29132905
if not is_dict_like(dtype):
@@ -2933,7 +2925,7 @@ def to_sql(
29332925
if_exists=if_exists,
29342926
index_label=index_label,
29352927
dtype=dtype,
2936-
prefixes=prefixes,
2928+
temporary=temporary,
29372929
)
29382930
table.create()
29392931
return table.insert(chunksize, method)

pandas/tests/io/test_sql.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4456,7 +4456,7 @@ def test_exists_temporary_table(conn, test_frame1, request):
44564456
frame=test_frame1,
44574457
index=False,
44584458
if_exists="fail",
4459-
prefixes=["TEMPORARY"],
4459+
temporary=True,
44604460
)
44614461

44624462
table.create()
@@ -4473,15 +4473,15 @@ def test_to_sql_temporary_table_replace(conn, test_frame1, request):
44734473
con=conn,
44744474
if_exists="fail",
44754475
index=False,
4476-
prefixes=["TEMPORARY"],
4476+
temporary=True,
44774477
)
44784478

44794479
test_frame1.to_sql(
44804480
name="test_frame1",
44814481
con=conn,
44824482
if_exists="replace",
44834483
index=False,
4484-
prefixes=["TEMPORARY"],
4484+
temporary=True,
44854485
)
44864486

44874487
df_test = pd.read_sql("SELECT * FROM test_frame1", conn)
@@ -4498,7 +4498,7 @@ def test_to_sql_temporary_table_fail(conn, test_frame1, request):
44984498
con=conn,
44994499
if_exists="fail",
45004500
index=False,
4501-
prefixes=["TEMPORARY"],
4501+
temporary=True,
45024502
)
45034503

45044504
with pytest.raises(ValueError, match=r"Table 'test_frame1' already exists."):
@@ -4507,7 +4507,7 @@ def test_to_sql_temporary_table_fail(conn, test_frame1, request):
45074507
con=conn,
45084508
if_exists="fail",
45094509
index=False,
4510-
prefixes=["TEMPORARY"],
4510+
temporary=True,
45114511
)
45124512

45134513

@@ -4520,15 +4520,15 @@ def test_to_sql_temporary_table_append(conn, test_frame1, request):
45204520
con=conn,
45214521
if_exists="fail",
45224522
index=False,
4523-
prefixes=["TEMPORARY"],
4523+
temporary=True,
45244524
)
45254525

45264526
test_frame1.to_sql(
45274527
name="test_frame1",
45284528
con=conn,
45294529
if_exists="append",
45304530
index=False,
4531-
prefixes=["TEMPORARY"],
4531+
temporary=True,
45324532
)
45334533

45344534
df_test = pd.read_sql("SELECT * FROM test_frame1", conn)

0 commit comments

Comments
 (0)