Skip to content

Commit

Permalink
introspection: use obj_description/col_description in postgres (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
aqrln authored Aug 14, 2023
1 parent 4bac8fc commit aec118a
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
13 changes: 7 additions & 6 deletions schema-engine/sql-schema-describer/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -819,11 +819,10 @@ impl<'a> SqlSchemaDescriber<'a> {
views.viewname AS view_name,
views.definition AS view_sql,
views.schemaname AS namespace,
description.description AS description
obj_description(class.oid, 'pg_class') AS description
FROM pg_catalog.pg_views views
INNER JOIN pg_catalog.pg_namespace ns ON views.schemaname = ns.nspname
INNER JOIN pg_catalog.pg_class class ON class.relnamespace = ns.oid AND class.relname = views.viewname
LEFT JOIN pg_catalog.pg_description description ON description.objoid = class.oid AND description.objsubid = 0
WHERE schemaname = ANY ( $1 )
"#};

Expand Down Expand Up @@ -879,7 +878,7 @@ impl<'a> SqlSchemaDescriber<'a> {
info.is_nullable,
info.is_identity,
info.character_maximum_length,
description.description
col_description(att.attrelid, ordinal_position) AS description
FROM information_schema.columns info
JOIN pg_attribute att ON att.attname = info.column_name
JOIN (
Expand All @@ -891,7 +890,6 @@ impl<'a> SqlSchemaDescriber<'a> {
AND relname = info.table_name
AND namespace = info.table_schema
LEFT OUTER JOIN pg_attrdef attdef ON attdef.adrelid = att.attrelid AND attdef.adnum = att.attnum AND table_schema = namespace
LEFT OUTER JOIN pg_description description ON description.objoid = att.attrelid AND description.objsubid = ordinal_position
WHERE table_schema = ANY ( $1 ) {is_visible_clause}
ORDER BY namespace, table_name, ordinal_position;
"#
Expand Down Expand Up @@ -1404,11 +1402,14 @@ impl<'a> SqlSchemaDescriber<'a> {
let namespaces = &sql_schema.namespaces;

let sql = "
SELECT t.typname as name, e.enumlabel as value, n.nspname as namespace, d.description
SELECT
t.typname AS name,
e.enumlabel AS value,
n.nspname AS namespace,
obj_description(t.oid, 'pg_type') AS description
FROM pg_type t
JOIN pg_enum e ON t.oid = e.enumtypid
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
LEFT OUTER JOIN pg_description d ON d.objoid = t.oid
WHERE n.nspname = ANY ( $1 )
ORDER BY e.enumsortorder";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ SELECT
(tbl.relhassubclass and tbl.relkind = 'r') as has_subclass,
tbl.relrowsecurity as has_row_level_security,
reloptions,
pd.description as description
obj_description(tbl.oid, 'pg_class') as description
FROM pg_class AS tbl
INNER JOIN pg_namespace AS namespace ON namespace.oid = tbl.relnamespace
LEFT JOIN pg_description pd ON pd.objoid = tbl.oid AND pd.objsubid = 0 AND pd.classoid = 'pg_catalog.pg_class'::regclass::oid
WHERE
( -- (relkind = 'r' and relispartition = 't') matches partition table "duplicates"
(tbl.relkind = 'r' AND tbl.relispartition = 'f')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ SELECT
false as has_subclass,
false as has_row_level_security,
reloptions,
pd.description as description
obj_description(tbl.oid, 'pg_class') as description
FROM pg_class AS tbl
INNER JOIN pg_namespace AS namespace ON namespace.oid = tbl.relnamespace
LEFT JOIN pg_description pd ON pd.objoid = tbl.oid AND pd.objsubid = 0 AND pd.classoid = 'pg_catalog.pg_class'::regclass::oid
WHERE
tbl.relkind = 'r' AND namespace.nspname = ANY ( $1 )
ORDER BY namespace, table_name;

0 comments on commit aec118a

Please sign in to comment.