5
5
6
6
from contextlib import contextmanager
7
7
from datetime import date , datetime , time
8
+ from distutils .version import LooseVersion
8
9
from functools import partial
9
10
import re
10
11
from typing import Iterator , List , Optional , Union , overload
@@ -55,6 +56,16 @@ def _is_sqlalchemy_connectable(con):
55
56
return False
56
57
57
58
59
+ def _gt14 () -> bool :
60
+ """
61
+ Check if sqlalchemy.__version__ is at least 1.4.0, when several
62
+ deprecations were made.
63
+ """
64
+ import sqlalchemy
65
+
66
+ return LooseVersion (sqlalchemy .__version__ ) >= LooseVersion ("1.4.0" )
67
+
68
+
58
69
def _convert_params (sql , params ):
59
70
"""Convert SQL and params args to DBAPI2.0 compliant format."""
60
71
args = [sql ]
@@ -715,7 +726,10 @@ def sql_schema(self):
715
726
716
727
def _execute_create (self ):
717
728
# Inserting table into database, add to MetaData object
718
- self .table = self .table .tometadata (self .pd_sql .meta )
729
+ if _gt14 ():
730
+ self .table = self .table .to_metadata (self .pd_sql .meta )
731
+ else :
732
+ self .table = self .table .tometadata (self .pd_sql .meta )
719
733
self .table .create ()
720
734
721
735
def create (self ):
@@ -1409,9 +1423,17 @@ def to_sql(
1409
1423
# Only check when name is not a number and name is not lower case
1410
1424
engine = self .connectable .engine
1411
1425
with self .connectable .connect () as conn :
1412
- table_names = engine .table_names (
1413
- schema = schema or self .meta .schema , connection = conn
1414
- )
1426
+ if _gt14 ():
1427
+ from sqlalchemy import inspect
1428
+
1429
+ insp = inspect (conn )
1430
+ table_names = insp .get_table_names (
1431
+ schema = schema or self .meta .schema
1432
+ )
1433
+ else :
1434
+ table_names = engine .table_names (
1435
+ schema = schema or self .meta .schema , connection = conn
1436
+ )
1415
1437
if name not in table_names :
1416
1438
msg = (
1417
1439
f"The provided table name '{ name } ' is not found exactly as "
@@ -1426,9 +1448,15 @@ def tables(self):
1426
1448
return self .meta .tables
1427
1449
1428
1450
def has_table (self , name , schema = None ):
1429
- return self .connectable .run_callable (
1430
- self .connectable .dialect .has_table , name , schema or self .meta .schema
1431
- )
1451
+ if _gt14 ():
1452
+ import sqlalchemy as sa
1453
+
1454
+ insp = sa .inspect (self .connectable )
1455
+ return insp .has_table (name , schema or self .meta .schema )
1456
+ else :
1457
+ return self .connectable .run_callable (
1458
+ self .connectable .dialect .has_table , name , schema or self .meta .schema
1459
+ )
1432
1460
1433
1461
def get_table (self , table_name , schema = None ):
1434
1462
schema = schema or self .meta .schema
0 commit comments