Get column metadata for tables and views :param kinds: kinds: list of postgres relkind filters: 'r' - table 'p' - partitioned table 'f' - foreign table 'v' - view 'm' - materialized view :return: list of
(self, kinds=("r", "p", "f", "v", "m"))
| 571 | yield from self._relations(kinds=["v", "m"]) |
| 572 | |
| 573 | def _columns(self, kinds=("r", "p", "f", "v", "m")): |
| 574 | """Get column metadata for tables and views |
| 575 | |
| 576 | :param kinds: kinds: list of postgres relkind filters: |
| 577 | 'r' - table |
| 578 | 'p' - partitioned table |
| 579 | 'f' - foreign table |
| 580 | 'v' - view |
| 581 | 'm' - materialized view |
| 582 | :return: list of (schema_name, relation_name, column_name, column_type) tuples |
| 583 | """ |
| 584 | |
| 585 | if self.conn.info.server_version >= 80400: |
| 586 | columns_query = """ |
| 587 | SELECT nsp.nspname schema_name, |
| 588 | cls.relname table_name, |
| 589 | att.attname column_name, |
| 590 | att.atttypid::regtype::text type_name, |
| 591 | att.atthasdef AS has_default, |
| 592 | pg_catalog.pg_get_expr(def.adbin, def.adrelid, true) as default |
| 593 | FROM pg_catalog.pg_attribute att |
| 594 | INNER JOIN pg_catalog.pg_class cls |
| 595 | ON att.attrelid = cls.oid |
| 596 | INNER JOIN pg_catalog.pg_namespace nsp |
| 597 | ON cls.relnamespace = nsp.oid |
| 598 | LEFT OUTER JOIN pg_attrdef def |
| 599 | ON def.adrelid = att.attrelid |
| 600 | AND def.adnum = att.attnum |
| 601 | WHERE cls.relkind = ANY(%s) |
| 602 | AND NOT att.attisdropped |
| 603 | AND att.attnum > 0 |
| 604 | ORDER BY 1, 2, att.attnum""" |
| 605 | else: |
| 606 | columns_query = """ |
| 607 | SELECT nsp.nspname schema_name, |
| 608 | cls.relname table_name, |
| 609 | att.attname column_name, |
| 610 | typ.typname type_name, |
| 611 | NULL AS has_default, |
| 612 | NULL AS default |
| 613 | FROM pg_catalog.pg_attribute att |
| 614 | INNER JOIN pg_catalog.pg_class cls |
| 615 | ON att.attrelid = cls.oid |
| 616 | INNER JOIN pg_catalog.pg_namespace nsp |
| 617 | ON cls.relnamespace = nsp.oid |
| 618 | INNER JOIN pg_catalog.pg_type typ |
| 619 | ON typ.oid = att.atttypid |
| 620 | WHERE cls.relkind = ANY(%s) |
| 621 | AND NOT att.attisdropped |
| 622 | AND att.attnum > 0 |
| 623 | ORDER BY 1, 2, att.attnum""" |
| 624 | |
| 625 | with self.conn.cursor() as cur: |
| 626 | # sql = cur.mogrify(columns_query, kinds) |
| 627 | # _logger.debug("Columns Query. sql: %r", sql) |
| 628 | cur.execute(columns_query, [kinds]) |
| 629 | yield from cur |
| 630 |
no test coverage detected