@@ -970,11 +970,13 @@ def create(self) -> None:
970
970
if self .exists ():
971
971
if self .if_exists == "fail" :
972
972
raise ValueError (f"Table '{ self .name } ' already exists." )
973
- if self .if_exists == "replace" :
973
+ elif self .if_exists == "replace" :
974
974
self .pd_sql .drop_table (self .name , self .schema )
975
975
self ._execute_create ()
976
976
elif self .if_exists == "append" :
977
977
pass
978
+ elif self .if_exists == "truncate" :
979
+ self .pd_sql .truncate_table (self .name , self .schema )
978
980
else :
979
981
raise ValueError (f"'{ self .if_exists } ' is not valid for if_exists" )
980
982
else :
@@ -2047,6 +2049,25 @@ def drop_table(self, table_name: str, schema: str | None = None) -> None:
2047
2049
self .get_table (table_name , schema ).drop (bind = self .con )
2048
2050
self .meta .clear ()
2049
2051
2052
+ def truncate_table (self , table_name : str , schema : str | None = None ) -> None :
2053
+ from sqlalchemy .exc import OperationalError
2054
+
2055
+ schema = schema or self .meta .schema
2056
+
2057
+ if self .has_table (table_name , schema ):
2058
+ self .meta .reflect (
2059
+ bind = self .con , only = [table_name ], schema = schema , views = True
2060
+ )
2061
+ with self .run_transaction ():
2062
+ table = self .get_table (table_name , schema )
2063
+ try :
2064
+ self .execute (f"TRUNCATE TABLE { table .name } " )
2065
+ except OperationalError :
2066
+ raise NotImplementedError ("'TRUNCATE' is not supported by this database." )
2067
+
2068
+ self .meta .clear ()
2069
+
2070
+
2050
2071
def _create_sql_schema (
2051
2072
self ,
2052
2073
frame : DataFrame ,
@@ -2343,6 +2364,8 @@ def to_sql(
2343
2364
engine : {'auto', 'sqlalchemy'}, default 'auto'
2344
2365
Raises NotImplementedError if not set to 'auto'
2345
2366
"""
2367
+ from adbc_driver_manager import ProgrammingError
2368
+
2346
2369
if index_label :
2347
2370
raise NotImplementedError (
2348
2371
"'index_label' is not implemented for ADBC drivers"
@@ -2376,6 +2399,14 @@ def to_sql(
2376
2399
cur .execute (f"DROP TABLE { table_name } " )
2377
2400
elif if_exists == "append" :
2378
2401
mode = "append"
2402
+ elif if_exists == "truncate" :
2403
+ mode = "append"
2404
+ with self .con .cursor () as cur :
2405
+ try :
2406
+ cur .execute (f"TRUNCATE TABLE { table_name } " )
2407
+ except ProgrammingError :
2408
+ raise NotImplementedError ("'TRUNCATE' is not supported by this database." )
2409
+
2379
2410
2380
2411
import pyarrow as pa
2381
2412
@@ -2857,6 +2888,9 @@ def drop_table(self, name: str, schema: str | None = None) -> None:
2857
2888
drop_sql = f"DROP TABLE { _get_valid_sqlite_name (name )} "
2858
2889
self .execute (drop_sql )
2859
2890
2891
+ def truncate_table (self , name :str , schema : str | None ) -> None :
2892
+ raise NotImplementedError ("'TRUNCATE' is not supported by this database." )
2893
+
2860
2894
def _create_sql_schema (
2861
2895
self ,
2862
2896
frame ,
0 commit comments