@@ -970,11 +970,13 @@ def create(self) -> None:
970970 if self .exists ():
971971 if self .if_exists == "fail" :
972972 raise ValueError (f"Table '{ self .name } ' already exists." )
973- if self .if_exists == "replace" :
973+ elif self .if_exists == "replace" :
974974 self .pd_sql .drop_table (self .name , self .schema )
975975 self ._execute_create ()
976976 elif self .if_exists == "append" :
977977 pass
978+ elif self .if_exists == "truncate" :
979+ self .pd_sql .truncate_table (self .name , self .schema )
978980 else :
979981 raise ValueError (f"'{ self .if_exists } ' is not valid for if_exists" )
980982 else :
@@ -2047,6 +2049,25 @@ def drop_table(self, table_name: str, schema: str | None = None) -> None:
20472049 self .get_table (table_name , schema ).drop (bind = self .con )
20482050 self .meta .clear ()
20492051
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+
20502071 def _create_sql_schema (
20512072 self ,
20522073 frame : DataFrame ,
@@ -2343,6 +2364,8 @@ def to_sql(
23432364 engine : {'auto', 'sqlalchemy'}, default 'auto'
23442365 Raises NotImplementedError if not set to 'auto'
23452366 """
2367+ from adbc_driver_manager import ProgrammingError
2368+
23462369 if index_label :
23472370 raise NotImplementedError (
23482371 "'index_label' is not implemented for ADBC drivers"
@@ -2376,6 +2399,14 @@ def to_sql(
23762399 cur .execute (f"DROP TABLE { table_name } " )
23772400 elif if_exists == "append" :
23782401 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+
23792410
23802411 import pyarrow as pa
23812412
@@ -2857,6 +2888,9 @@ def drop_table(self, name: str, schema: str | None = None) -> None:
28572888 drop_sql = f"DROP TABLE { _get_valid_sqlite_name (name )} "
28582889 self .execute (drop_sql )
28592890
2891+ def truncate_table (self , name :str , schema : str | None ) -> None :
2892+ raise NotImplementedError ("'TRUNCATE' is not supported by this database." )
2893+
28602894 def _create_sql_schema (
28612895 self ,
28622896 frame ,
0 commit comments