5
5
6
6
from __future__ import annotations
7
7
8
+ from abc import (
9
+ ABC ,
10
+ abstractmethod ,
11
+ )
8
12
from contextlib import contextmanager
9
13
from datetime import (
10
14
date ,
@@ -282,9 +286,7 @@ def read_sql_table(
282
286
if not pandas_sql .has_table (table_name ):
283
287
raise ValueError (f"Table { table_name } not found" )
284
288
285
- # error: Item "SQLiteDatabase" of "Union[SQLDatabase, SQLiteDatabase]"
286
- # has no attribute "read_table"
287
- table = pandas_sql .read_table ( # type: ignore[union-attr]
289
+ table = pandas_sql .read_table (
288
290
table_name ,
289
291
index_col = index_col ,
290
292
coerce_float = coerce_float ,
@@ -578,7 +580,6 @@ def read_sql(
578
580
_is_table_name = False
579
581
580
582
if _is_table_name :
581
- pandas_sql .meta .reflect (bind = pandas_sql .connectable , only = [sql ])
582
583
return pandas_sql .read_table (
583
584
sql ,
584
585
index_col = index_col ,
@@ -735,7 +736,7 @@ def has_table(table_name: str, con, schema: str | None = None) -> bool:
735
736
table_exists = has_table
736
737
737
738
738
- def pandasSQL_builder (con , schema : str | None = None ) -> SQLDatabase | SQLiteDatabase :
739
+ def pandasSQL_builder (con , schema : str | None = None ) -> PandasSQL :
739
740
"""
740
741
Convenience function to return the correct PandasSQL subclass based on the
741
742
provided parameters.
@@ -1251,17 +1252,37 @@ def _get_dtype(self, sqltype):
1251
1252
return object
1252
1253
1253
1254
1254
- class PandasSQL (PandasObject ):
1255
+ class PandasSQL (PandasObject , ABC ):
1255
1256
"""
1256
- Subclasses Should define read_sql and to_sql.
1257
+ Subclasses Should define read_query and to_sql.
1257
1258
"""
1258
1259
1259
- def read_sql (self , * args , ** kwargs ):
1260
- raise ValueError (
1261
- "PandasSQL must be created with an SQLAlchemy "
1262
- "connectable or sqlite connection"
1263
- )
1260
+ def read_table (
1261
+ self ,
1262
+ table_name : str ,
1263
+ index_col : str | list [str ] | None = None ,
1264
+ coerce_float : bool = True ,
1265
+ parse_dates = None ,
1266
+ columns = None ,
1267
+ schema : str | None = None ,
1268
+ chunksize : int | None = None ,
1269
+ ) -> DataFrame | Iterator [DataFrame ]:
1270
+ raise NotImplementedError
1264
1271
1272
+ @abstractmethod
1273
+ def read_query (
1274
+ self ,
1275
+ sql : str ,
1276
+ index_col : str | list [str ] | None = None ,
1277
+ coerce_float : bool = True ,
1278
+ parse_dates = None ,
1279
+ params = None ,
1280
+ chunksize : int | None = None ,
1281
+ dtype : DtypeArg | None = None ,
1282
+ ) -> DataFrame | Iterator [DataFrame ]:
1283
+ pass
1284
+
1285
+ @abstractmethod
1265
1286
def to_sql (
1266
1287
self ,
1267
1288
frame ,
@@ -1273,11 +1294,29 @@ def to_sql(
1273
1294
chunksize = None ,
1274
1295
dtype : DtypeArg | None = None ,
1275
1296
method = None ,
1297
+ engine : str = "auto" ,
1298
+ ** engine_kwargs ,
1276
1299
) -> int | None :
1277
- raise ValueError (
1278
- "PandasSQL must be created with an SQLAlchemy "
1279
- "connectable or sqlite connection"
1280
- )
1300
+ pass
1301
+
1302
+ @abstractmethod
1303
+ def execute (self , * args , ** kwargs ):
1304
+ pass
1305
+
1306
+ @abstractmethod
1307
+ def has_table (self , name : str , schema : str | None = None ) -> bool :
1308
+ pass
1309
+
1310
+ @abstractmethod
1311
+ def _create_sql_schema (
1312
+ self ,
1313
+ frame : DataFrame ,
1314
+ table_name : str ,
1315
+ keys : list [str ] | None = None ,
1316
+ dtype : DtypeArg | None = None ,
1317
+ schema : str | None = None ,
1318
+ ):
1319
+ pass
1281
1320
1282
1321
1283
1322
class BaseEngine :
@@ -2065,8 +2104,8 @@ def read_query(
2065
2104
sql ,
2066
2105
index_col = None ,
2067
2106
coerce_float : bool = True ,
2068
- params = None ,
2069
2107
parse_dates = None ,
2108
+ params = None ,
2070
2109
chunksize : int | None = None ,
2071
2110
dtype : DtypeArg | None = None ,
2072
2111
) -> DataFrame | Iterator [DataFrame ]:
@@ -2116,7 +2155,8 @@ def to_sql(
2116
2155
chunksize = None ,
2117
2156
dtype : DtypeArg | None = None ,
2118
2157
method = None ,
2119
- ** kwargs ,
2158
+ engine : str = "auto" ,
2159
+ ** engine_kwargs ,
2120
2160
) -> int | None :
2121
2161
"""
2122
2162
Write records stored in a DataFrame to a SQL database.
0 commit comments