Skip to content

Commit 482a08d

Browse files
committed
Update postgresql commands
1 parent 98c9ee5 commit 482a08d

File tree

1 file changed

+57
-34
lines changed

1 file changed

+57
-34
lines changed

text_2_sql/text_2_sql_core/src/text_2_sql_core/data_dictionary/postgresql_data_dictionary_creator.py

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,64 +24,87 @@ def __init__(self, **kwargs):
2424
def extract_table_entities_sql_query(self) -> str:
2525
"""A property to extract table entities from a PostgreSQL database."""
2626
return """SELECT
27-
t.table_name AS entity,
28-
t.table_schema AS entity_schema,
29-
pg_catalog.col_description(c.oid, 0) AS definition
27+
t.table_name AS "Entity",
28+
t.table_schema AS "EntitySchema",
29+
pg_catalog.obj_description(c.oid, 'pg_class') AS "Definition"
3030
FROM
3131
information_schema.tables t
3232
JOIN
3333
pg_catalog.pg_class c ON c.relname = t.table_name
3434
AND c.relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = t.table_schema)
35+
LEFT JOIN
36+
pg_catalog.pg_description pd ON pd.objoid = c.oid
3537
WHERE
3638
t.table_type = 'BASE TABLE'
37-
ORDER BY entity_schema, entity;"""
39+
AND pd.objsubid = 0 -- 0 indicates the table description, not column descriptions
40+
ORDER BY
41+
"EntitySchema", "Entity";"""
3842

3943
@property
4044
def extract_view_entities_sql_query(self) -> str:
4145
"""A property to extract view entities from a PostgreSQL database."""
4246
return """SELECT
43-
v.view_name AS entity,
44-
v.table_schema AS entity_schema,
45-
pg_catalog.col_description(c.oid, 0) AS definition
47+
v.viewname AS "Entity",
48+
v.schemaname AS "EntitySchema",
49+
pg_catalog.obj_description(c.oid, 'pg_class') AS "Definition"
4650
FROM
47-
information_schema.views v
51+
pg_catalog.pg_views v
4852
JOIN
49-
pg_catalog.pg_class c ON c.relname = v.view_name
50-
AND c.relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = v.table_schema)
51-
ORDER BY entity_schema, entity;"""
53+
pg_catalog.pg_class c ON c.relname = v.viewname
54+
AND c.relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = v.schemaname)
55+
LEFT JOIN
56+
pg_catalog.pg_description pd ON pd.objoid = c.oid
57+
WHERE
58+
pd.objsubid = 0 -- 0 indicates the view description, not a column description
59+
ORDER BY
60+
"EntitySchema", "Entity";"""
5261

5362
def extract_columns_sql_query(self, entity: EntityItem) -> str:
5463
"""A property to extract column information from a PostgreSQL database."""
5564
return f"""SELECT
56-
c.column_name AS name,
57-
c.data_type AS data_type,
58-
pg_catalog.col_description(t.oid, c.ordinal_position) AS definition
65+
c.attname AS "Name",
66+
t.typname AS "DataType",
67+
pgd.description AS "Definition"
5968
FROM
60-
information_schema.columns c
61-
JOIN
62-
pg_catalog.pg_class t ON t.relname = c.table_name
63-
AND t.relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = c.table_schema)
69+
pg_attribute c
70+
INNER JOIN
71+
pg_class tbl ON c.attrelid = tbl.oid
72+
INNER JOIN
73+
pg_namespace ns ON tbl.relnamespace = ns.oid
74+
INNER JOIN
75+
pg_type t ON c.atttypid = t.oid
76+
LEFT JOIN
77+
pg_description pgd ON pgd.objoid = tbl.oid AND pgd.objsubid = c.attnum
6478
WHERE
65-
c.table_schema = '{entity.entity_schema}'
66-
AND c.table_name = '{entity.name}'
67-
ORDER BY c.ordinal_position;"""
79+
ns.nspname = '{entity.entity_schema}'
80+
AND tbl.relname = '{entity.name}'
81+
AND c.attnum > 0 -- Exclude system columns
82+
ORDER BY
83+
c.attnum;"""
6884

6985
@property
7086
def extract_entity_relationships_sql_query(self) -> str:
7187
"""A property to extract entity relationships from a PostgreSQL database."""
7288
return """SELECT
73-
tc.table_schema AS entity_schema,
74-
tc.table_name AS entity,
75-
rc.unique_constraint_schema AS foreign_entity_schema,
76-
rc.unique_constraint_name AS foreign_entity_constraint,
77-
rc.constraint_name AS foreign_key_constraint
89+
fk_schema.nspname AS "EntitySchema",
90+
fk_tab.relname AS "Entity",
91+
pk_schema.nspname AS "ForeignEntitySchema",
92+
pk_tab.relname AS "ForeignEntity",
93+
fk_col.attname AS "Column",
94+
pk_col.attname AS "ForeignColumn"
7895
FROM
79-
information_schema.referential_constraints rc
80-
JOIN
81-
information_schema.table_constraints tc
82-
ON rc.constraint_schema = tc.constraint_schema
83-
AND rc.constraint_name = tc.constraint_name
84-
WHERE
85-
tc.constraint_type = 'FOREIGN KEY'
96+
pg_constraint fk
97+
INNER JOIN
98+
pg_attribute fk_col ON fk.conrelid = fk_col.attrelid AND fk.attnum = fk_col.attnum
99+
INNER JOIN
100+
pg_class fk_tab ON fk.conrelid = fk_tab.oid
101+
INNER JOIN
102+
pg_namespace fk_schema ON fk_tab.relnamespace = fk_schema.oid
103+
INNER JOIN
104+
pg_class pk_tab ON fk.confrelid = pk_tab.oid
105+
INNER JOIN
106+
pg_namespace pk_schema ON pk_tab.relnamespace = pk_schema.oid
107+
INNER JOIN
108+
pg_attribute pk_col ON fk.confrelid = pk_col.attrelid AND fk.confkey[1] = pk_col.attnum
86109
ORDER BY
87-
entity_schema, entity, foreign_entity_schema, foreign_entity_constraint;"""
110+
"EntitySchema", "Entity", "ForeignEntitySchema", "ForeignEntity";"""

0 commit comments

Comments
 (0)