25
25
Computed ,
26
26
Constraint ,
27
27
DefaultClause ,
28
+ Dialect ,
28
29
Enum ,
29
30
Float ,
30
31
ForeignKey ,
39
40
UniqueConstraint ,
40
41
)
41
42
from sqlalchemy .dialects .postgresql import JSONB
42
- from sqlalchemy .engine import Connection , Engine
43
43
from sqlalchemy .exc import CompileError
44
44
from sqlalchemy .sql .elements import TextClause
45
45
@@ -95,10 +95,10 @@ class CodeGenerator(metaclass=ABCMeta):
95
95
valid_options : ClassVar [set [str ]] = set ()
96
96
97
97
def __init__ (
98
- self , metadata : MetaData , bind : Connection | Engine , options : Sequence [str ]
98
+ self , metadata : MetaData , dialect : Dialect , options : Sequence [str ]
99
99
):
100
100
self .metadata : MetaData = metadata
101
- self .bind : Connection | Engine = bind
101
+ self .dialect : Dialect = dialect
102
102
self .options : set [str ] = set (options )
103
103
104
104
# Validate options
@@ -124,12 +124,12 @@ class TablesGenerator(CodeGenerator):
124
124
def __init__ (
125
125
self ,
126
126
metadata : MetaData ,
127
- bind : Connection | Engine ,
127
+ dialect : Dialect ,
128
128
options : Sequence [str ],
129
129
* ,
130
130
indentation : str = " " ,
131
131
):
132
- super ().__init__ (metadata , bind , options )
132
+ super ().__init__ (metadata , dialect , options )
133
133
self .indentation : str = indentation
134
134
self .imports : dict [str , set [str ]] = defaultdict (set )
135
135
self .module_imports : set [str ] = set ()
@@ -562,7 +562,7 @@ def add_fk_options(*opts: Any) -> None:
562
562
]
563
563
add_fk_options (local_columns , remote_columns )
564
564
elif isinstance (constraint , CheckConstraint ):
565
- args .append (repr (get_compiled_expression (constraint .sqltext , self .bind )))
565
+ args .append (repr (get_compiled_expression (constraint .sqltext , self .dialect )))
566
566
elif isinstance (constraint , (UniqueConstraint , PrimaryKeyConstraint )):
567
567
args .extend (repr (col .name ) for col in constraint .columns )
568
568
else :
@@ -608,7 +608,7 @@ def fix_column_types(self, table: Table) -> None:
608
608
# Detect check constraints for boolean and enum columns
609
609
for constraint in table .constraints .copy ():
610
610
if isinstance (constraint , CheckConstraint ):
611
- sqltext = get_compiled_expression (constraint .sqltext , self .bind )
611
+ sqltext = get_compiled_expression (constraint .sqltext , self .dialect )
612
612
613
613
# Turn any integer-like column with a CheckConstraint like
614
614
# "column IN (0, 1)" into a Boolean
@@ -646,7 +646,7 @@ def fix_column_types(self, table: Table) -> None:
646
646
pass
647
647
648
648
# PostgreSQL specific fix: detect sequences from server_default
649
- if column .server_default and self .bind . dialect .name == "postgresql" :
649
+ if column .server_default and self .dialect .name == "postgresql" :
650
650
if isinstance (column .server_default , DefaultClause ) and isinstance (
651
651
column .server_default .arg , TextClause
652
652
):
@@ -661,7 +661,7 @@ def fix_column_types(self, table: Table) -> None:
661
661
column .server_default = None
662
662
663
663
def get_adapted_type (self , coltype : Any ) -> Any :
664
- compiled_type = coltype .compile (self .bind . engine . dialect )
664
+ compiled_type = coltype .compile (self .dialect )
665
665
for supercls in coltype .__class__ .__mro__ :
666
666
if not supercls .__name__ .startswith ("_" ) and hasattr (
667
667
supercls , "__visit_name__"
@@ -687,7 +687,7 @@ def get_adapted_type(self, coltype: Any) -> Any:
687
687
try :
688
688
# If the adapted column type does not render the same as the
689
689
# original, don't substitute it
690
- if new_coltype .compile (self .bind . engine . dialect ) != compiled_type :
690
+ if new_coltype .compile (self .dialect ) != compiled_type :
691
691
# Make an exception to the rule for Float and arrays of Float,
692
692
# since at least on PostgreSQL, Float can accurately represent
693
693
# both REAL and DOUBLE_PRECISION
@@ -718,13 +718,13 @@ class DeclarativeGenerator(TablesGenerator):
718
718
def __init__ (
719
719
self ,
720
720
metadata : MetaData ,
721
- bind : Connection | Engine ,
721
+ dialect : Dialect ,
722
722
options : Sequence [str ],
723
723
* ,
724
724
indentation : str = " " ,
725
725
base_class_name : str = "Base" ,
726
726
):
727
- super ().__init__ (metadata , bind , options , indentation = indentation )
727
+ super ().__init__ (metadata , dialect , options , indentation = indentation )
728
728
self .base_class_name : str = base_class_name
729
729
self .inflect_engine = inflect .engine ()
730
730
@@ -1305,7 +1305,7 @@ class DataclassGenerator(DeclarativeGenerator):
1305
1305
def __init__ (
1306
1306
self ,
1307
1307
metadata : MetaData ,
1308
- bind : Connection | Engine ,
1308
+ dialect : Dialect ,
1309
1309
options : Sequence [str ],
1310
1310
* ,
1311
1311
indentation : str = " " ,
@@ -1315,7 +1315,7 @@ def __init__(
1315
1315
):
1316
1316
super ().__init__ (
1317
1317
metadata ,
1318
- bind ,
1318
+ dialect ,
1319
1319
options ,
1320
1320
indentation = indentation ,
1321
1321
base_class_name = base_class_name ,
@@ -1344,15 +1344,15 @@ class SQLModelGenerator(DeclarativeGenerator):
1344
1344
def __init__ (
1345
1345
self ,
1346
1346
metadata : MetaData ,
1347
- bind : Connection | Engine ,
1347
+ dialect : Dialect ,
1348
1348
options : Sequence [str ],
1349
1349
* ,
1350
1350
indentation : str = " " ,
1351
1351
base_class_name : str = "SQLModel" ,
1352
1352
):
1353
1353
super ().__init__ (
1354
1354
metadata ,
1355
- bind ,
1355
+ dialect ,
1356
1356
options ,
1357
1357
indentation = indentation ,
1358
1358
base_class_name = base_class_name ,
0 commit comments