diff options
author | Federico Caselli <cfederico87@gmail.com> | 2020-03-14 13:57:42 +0100 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-03-15 19:09:04 -0400 |
commit | 62b7dace0c1d03acf3224085d03a03684a969031 (patch) | |
tree | 29037c928ce0b5b728a5d7ba2eb24ac6f110664c /lib/sqlalchemy/dialects/postgresql/base.py | |
parent | fcc03730c553b3fc0229e446e886d4f61dcb6291 (diff) | |
download | sqlalchemy-62b7dace0c1d03acf3224085d03a03684a969031.tar.gz |
Support inspection of computed column
Added support for reflection of "computed" columns, which are now returned
as part of the structure returned by :meth:`.Inspector.get_columns`.
When reflecting full :class:`.Table` objects, computed columns will
be represented using the :class:`.Computed` construct.
Also improve the documentation in :meth:`Inspector.get_columns`, correctly
listing all the returned keys.
Fixes: #5063
Fixes: #4051
Closes: #5064
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5064
Pull-request-sha: ba00fc321ce468f8885aad23b3dd33c789e50fbe
Change-Id: I789986554fc8ac7f084270474d0b2c12046b1cc2
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/base.py')
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index bfe3812be..8105244fa 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -2826,7 +2826,14 @@ class PGDialect(default.DefaultDialect): table_oid = self.get_table_oid( connection, table_name, schema, info_cache=kw.get("info_cache") ) - SQL_COLS = """ + + generated = ( + "a.attgenerated as generated" + if self.server_version_info >= (12,) + else "NULL as generated" + ) + SQL_COLS = ( + """ SELECT a.attname, pg_catalog.format_type(a.atttypid, a.atttypmod), (SELECT pg_catalog.pg_get_expr(d.adbin, d.adrelid) @@ -2835,7 +2842,8 @@ class PGDialect(default.DefaultDialect): AND a.atthasdef) AS DEFAULT, a.attnotnull, a.attnum, a.attrelid as table_oid, - pgd.description as comment + pgd.description as comment, + %s FROM pg_catalog.pg_attribute a LEFT JOIN pg_catalog.pg_description pgd ON ( pgd.objoid = a.attrelid AND pgd.objsubid = a.attnum) @@ -2843,6 +2851,8 @@ class PGDialect(default.DefaultDialect): AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum """ + % generated + ) s = ( sql.text(SQL_COLS) .bindparams(sql.bindparam("table_oid", type_=sqltypes.Integer)) @@ -2875,6 +2885,7 @@ class PGDialect(default.DefaultDialect): attnum, table_oid, comment, + generated, ) in rows: column_info = self._get_column_info( name, @@ -2885,6 +2896,7 @@ class PGDialect(default.DefaultDialect): enums, schema, comment, + generated, ) columns.append(column_info) return columns @@ -2899,6 +2911,7 @@ class PGDialect(default.DefaultDialect): enums, schema, comment, + generated, ): def _handle_array_type(attype): return ( @@ -3009,6 +3022,15 @@ class PGDialect(default.DefaultDialect): "Did not recognize type '%s' of column '%s'" % (attype, name) ) coltype = sqltypes.NULLTYPE + + # If a zero byte (''), then not a generated column. + # Otherwise, s = stored. (Other values might be added in the future.) + if generated: + computed = dict(sqltext=default, persisted=generated == "s") + default = None + else: + computed = None + # adjust the default value autoincrement = False if default is not None: @@ -3038,6 +3060,8 @@ class PGDialect(default.DefaultDialect): autoincrement=autoincrement, comment=comment, ) + if computed is not None: + column_info["computed"] = computed return column_info @reflection.cache |