From 84974cf09220105bc7a92e5bcde31f52827c1de7 Mon Sep 17 00:00:00 2001 From: Frazer McLean Date: Mon, 13 Jun 2016 10:31:18 -0400 Subject: Comment support Implementation of [#1546 (BitBucket)](https://bitbucket.org/zzzeek/sqlalchemy/issues/1546). Very unfinished but I have reflection and setting after table creation working for PostgreSQL. If you're not opposed, perhaps best to continue discussion on Gerrit? Change-Id: Ib90683850805a2b4ee198e420dc294f32f15d35d Pull-request: https://github.com/zzzeek/sqlalchemy/pull/284 --- lib/sqlalchemy/dialects/postgresql/base.py | 34 +++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) (limited to 'lib/sqlalchemy/dialects/postgresql') diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 16b22129a..3c111e3cc 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -2057,8 +2057,11 @@ class PGDialect(default.DefaultDialect): WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) AS DEFAULT, - a.attnotnull, a.attnum, a.attrelid as table_oid + a.attnotnull, a.attnum, a.attrelid as table_oid, + pgd.description as comment FROM pg_catalog.pg_attribute a + LEFT JOIN pg_catalog.pg_description pgd ON ( + pgd.objoid = a.attrelid AND pgd.objsubid = a.attnum) WHERE a.attrelid = :table_oid AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum @@ -2082,14 +2085,16 @@ class PGDialect(default.DefaultDialect): # format columns columns = [] - for name, format_type, default, notnull, attnum, table_oid in rows: + for name, format_type, default, notnull, attnum, table_oid, \ + comment in rows: column_info = self._get_column_info( - name, format_type, default, notnull, domains, enums, schema) + name, format_type, default, notnull, domains, enums, + schema, comment) columns.append(column_info) return columns def _get_column_info(self, name, format_type, default, - notnull, domains, enums, schema): + notnull, domains, enums, schema, comment): # strip (*) from character varying(5), timestamp(5) # with time zone, geometry(POLYGON), etc. attype = re.sub(r'\(.*\)', '', format_type) @@ -2196,7 +2201,8 @@ class PGDialect(default.DefaultDialect): match.group(2) + match.group(3) column_info = dict(name=name, type=coltype, nullable=nullable, - default=default, autoincrement=autoincrement) + default=default, autoincrement=autoincrement, + comment=comment) return column_info @reflection.cache @@ -2527,6 +2533,24 @@ class PGDialect(default.DefaultDialect): for name, uc in uniques.items() ] + @reflection.cache + def get_table_comment(self, connection, table_name, schema=None, **kw): + table_oid = self.get_table_oid(connection, table_name, schema, + info_cache=kw.get('info_cache')) + + COMMENT_SQL = """ + SELECT + pgd.description as table_comment + FROM + pg_catalog.pg_description pgd + WHERE + pgd.objsubid = 0 AND + pgd.objoid = :table_oid + """ + + c = connection.execute(sql.text(COMMENT_SQL), table_oid=table_oid) + return c.scalar() + @reflection.cache def get_check_constraints( self, connection, table_name, schema=None, **kw): -- cgit v1.2.1