From 907030ddfca2605f96874c1e5b124cdcc64bc8e2 Mon Sep 17 00:00:00 2001 From: John Lyu Date: Thu, 9 Jun 2022 15:53:18 +0800 Subject: [PATCH 1/2] add table comment to generated code --- src/sqlacodegen/generators.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/sqlacodegen/generators.py b/src/sqlacodegen/generators.py index 3f1a401c..96fed71d 100644 --- a/src/sqlacodegen/generators.py +++ b/src/sqlacodegen/generators.py @@ -1032,7 +1032,9 @@ def render_models(self, models: list[Model]) -> str: def render_class(self, model: ModelClass) -> str: sections: list[str] = [] - + comments = self.render_table_comment(model) + if comments: + sections.append(comments) # Render class variables / special declarations class_vars: str = self.render_class_variables(model) if class_vars: @@ -1071,6 +1073,10 @@ def render_class_declaration(self, model: ModelClass) -> str: ) return f"class {model.name}({parent_class_name}):" + def render_table_comment(self, model: ModelClass) -> str: + if model.table.comment: + return f'"""{model.table.comment}"""' + def render_class_variables(self, model: ModelClass) -> str: variables = [f"__tablename__ = {model.table.name!r}"] From b7eb8b3c8014810f25d21c5a81e5e8ef1994d1f4 Mon Sep 17 00:00:00 2001 From: John Lyu Date: Tue, 28 Jun 2022 09:05:11 +0800 Subject: [PATCH 2/2] Change Connectable to Connection | Engine --- src/sqlacodegen/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sqlacodegen/utils.py b/src/sqlacodegen/utils.py index d3844cec..c158bf61 100644 --- a/src/sqlacodegen/utils.py +++ b/src/sqlacodegen/utils.py @@ -4,7 +4,7 @@ from collections.abc import Mapping from sqlalchemy import PrimaryKeyConstraint, UniqueConstraint -from sqlalchemy.engine import Connectable +from sqlalchemy.engine import Connection, Engine from sqlalchemy.sql import ClauseElement from sqlalchemy.sql.elements import TextClause from sqlalchemy.sql.schema import ( @@ -33,7 +33,7 @@ def get_constraint_sort_key(constraint: Constraint) -> str: return str(constraint) -def get_compiled_expression(statement: ClauseElement, bind: Connectable) -> str: +def get_compiled_expression(statement: ClauseElement, bind: Connection | Engine) -> str: """Return the statement in a form where any placeholders have been filled in.""" return str(statement.compile(bind, compile_kwargs={"literal_binds": True}))