Skip to content

Commit 39f2833

Browse files
committed
Update snowflake data dictionary creator
1 parent 3b87dac commit 39f2833

File tree

2 files changed

+77
-56
lines changed

2 files changed

+77
-56
lines changed

text_2_sql/data_dictionary/snowflake_data_dictionary_creator.py

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def __init__(
2424
if excluded_entities is None:
2525
excluded_entities = []
2626

27-
excluded_schemas = ["dbo", "sys"]
27+
excluded_schemas = ["INFORMATION_SCHEMA"]
2828
return super().__init__(
2929
entities, excluded_entities, excluded_schemas, single_file
3030
)
@@ -35,33 +35,54 @@ def __init__(
3535
def extract_table_entities_sql_query(self) -> str:
3636
"""A property to extract table entities from a Snowflake database."""
3737
return """SELECT
38-
t.TABLE_NAME AS Entity,
39-
t.TABLE_SCHEMA AS EntitySchema,
40-
t.COMMENT AS Description
41-
FROM
42-
INFORMATION_SCHEMA.TABLES t"""
38+
t.TABLE_NAME AS Entity,
39+
t.TABLE_SCHEMA AS EntitySchema,
40+
t.COMMENT AS Definition
41+
FROM
42+
INFORMATION_SCHEMA.TABLES t"""
4343

4444
@property
4545
def extract_view_entities_sql_query(self) -> str:
4646
"""A property to extract view entities from a Snowflake database."""
4747
return """SELECT
48-
v.TABLE_NAME AS Entity,
49-
v.TABLE_SCHEMA AS EntitySchema,
50-
v.COMMENT AS Description
51-
FROM
52-
INFORMATION_SCHEMA.VIEWS v"""
48+
v.TABLE_NAME AS Entity,
49+
v.TABLE_SCHEMA AS EntitySchema,
50+
v.COMMENT AS Definition
51+
FROM
52+
INFORMATION_SCHEMA.VIEWS v"""
5353

5454
def extract_columns_sql_query(self, entity: EntityItem) -> str:
5555
"""A property to extract column information from a Snowflake database."""
5656
return f"""SELECT
57-
COLUMN_NAME AS Name,
58-
DATA_TYPE AS Type,
59-
COMMENT AS Definition
60-
FROM
61-
INFORMATION_SCHEMA.COLUMNS
62-
WHERE
63-
TABLE_SCHEMA = '{entity.entity_schema}'
64-
AND TABLE_NAME = '{entity.name}';"""
57+
COLUMN_NAME AS Name,
58+
DATA_TYPE AS Type,
59+
COMMENT AS Definition
60+
FROM
61+
INFORMATION_SCHEMA.COLUMNS
62+
WHERE
63+
TABLE_SCHEMA = '{entity.entity_schema}'
64+
AND TABLE_NAME = '{entity.name}';"""
65+
66+
@property
67+
def extract_entity_relationships_sql_query(self) -> str:
68+
"""A property to extract entity relationships from a SQL Server database."""
69+
return """SELECT
70+
tc.table_schema AS EntitySchema,
71+
tc.table_name AS Entity,
72+
rc.unique_constraint_schema AS ForeignEntitySchema,
73+
rc.unique_constraint_name AS ForeignEntityConstraint,
74+
rc.constraint_name AS ForeignKeyConstraint
75+
FROM
76+
information_schema.referential_constraints rc
77+
JOIN
78+
information_schema.table_constraints tc
79+
ON rc.constraint_schema = tc.constraint_schema
80+
AND rc.constraint_name = tc.constraint_name
81+
WHERE
82+
tc.constraint_type = 'FOREIGN KEY'
83+
ORDER BY
84+
EntitySchema, Entity, ForeignEntitySchema, ForeignEntityConstraint;
85+
"""
6586

6687
async def query_entities(
6788
self, sql_query: str, cast_to: any = None

text_2_sql/data_dictionary/sql_sever_data_dictionary_creator.py

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -32,53 +32,53 @@ def __init__(
3232
def extract_table_entities_sql_query(self) -> str:
3333
"""A property to extract table entities from a SQL Server database."""
3434
return """SELECT
35-
t.TABLE_NAME AS Entity,
36-
t.TABLE_SCHEMA AS EntitySchema,
37-
CAST(ep.value AS NVARCHAR(500)) AS Definition
38-
FROM
39-
INFORMATION_SCHEMA.TABLES t
40-
LEFT JOIN
41-
sys.extended_properties ep
42-
ON ep.major_id = OBJECT_ID(t.TABLE_SCHEMA + '.' + t.TABLE_NAME)
43-
AND ep.minor_id = 0
44-
AND ep.class = 1
45-
AND ep.name = 'MS_Description'
46-
WHERE
47-
t.TABLE_TYPE = 'BASE TABLE';"""
35+
t.TABLE_NAME AS Entity,
36+
t.TABLE_SCHEMA AS EntitySchema,
37+
CAST(ep.value AS NVARCHAR(500)) AS Definition
38+
FROM
39+
INFORMATION_SCHEMA.TABLES t
40+
LEFT JOIN
41+
sys.extended_properties ep
42+
ON ep.major_id = OBJECT_ID(t.TABLE_SCHEMA + '.' + t.TABLE_NAME)
43+
AND ep.minor_id = 0
44+
AND ep.class = 1
45+
AND ep.name = 'MS_Description'
46+
WHERE
47+
t.TABLE_TYPE = 'BASE TABLE';"""
4848

4949
@property
5050
def extract_view_entities_sql_query(self) -> str:
5151
"""A property to extract view entities from a SQL Server database."""
5252
return """SELECT
53-
v.TABLE_NAME AS Entity,
54-
v.TABLE_SCHEMA AS EntitySchema,
55-
CAST(ep.value AS NVARCHAR(500)) AS Definition
56-
FROM
57-
INFORMATION_SCHEMA.VIEWS v
58-
LEFT JOIN
59-
sys.extended_properties ep
60-
ON ep.major_id = OBJECT_ID(v.TABLE_SCHEMA + '.' + v.TABLE_NAME)
61-
AND ep.minor_id = 0
62-
AND ep.class = 1
53+
v.TABLE_NAME AS Entity,
54+
v.TABLE_SCHEMA AS EntitySchema,
55+
CAST(ep.value AS NVARCHAR(500)) AS Definition
56+
FROM
57+
INFORMATION_SCHEMA.VIEWS v
58+
LEFT JOIN
59+
sys.extended_properties ep
60+
ON ep.major_id = OBJECT_ID(v.TABLE_SCHEMA + '.' + v.TABLE_NAME)
61+
AND ep.minor_id = 0
62+
AND ep.class = 1
6363
AND ep.name = 'MS_Description';"""
6464

6565
def extract_columns_sql_query(self, entity: EntityItem) -> str:
6666
"""A property to extract column information from a SQL Server database."""
6767
return f"""SELECT
68-
c.COLUMN_NAME AS Name,
69-
c.DATA_TYPE AS DataType,
70-
CAST(ep.value AS NVARCHAR(500)) AS Definition
71-
FROM
72-
INFORMATION_SCHEMA.COLUMNS c
73-
LEFT JOIN
74-
sys.extended_properties ep
75-
ON ep.major_id = OBJECT_ID(c.TABLE_SCHEMA + '.' + c.TABLE_NAME)
76-
AND ep.minor_id = c.ORDINAL_POSITION
77-
AND ep.class = 1
78-
AND ep.name = 'MS_Description'
79-
WHERE
80-
c.TABLE_SCHEMA = '{entity.entity_schema}'
81-
AND c.TABLE_NAME = '{entity.name}';"""
68+
c.COLUMN_NAME AS Name,
69+
c.DATA_TYPE AS DataType,
70+
CAST(ep.value AS NVARCHAR(500)) AS Definition
71+
FROM
72+
INFORMATION_SCHEMA.COLUMNS c
73+
LEFT JOIN
74+
sys.extended_properties ep
75+
ON ep.major_id = OBJECT_ID(c.TABLE_SCHEMA + '.' + c.TABLE_NAME)
76+
AND ep.minor_id = c.ORDINAL_POSITION
77+
AND ep.class = 1
78+
AND ep.name = 'MS_Description'
79+
WHERE
80+
c.TABLE_SCHEMA = '{entity.entity_schema}'
81+
AND c.TABLE_NAME = '{entity.name}';"""
8282

8383
@property
8484
def extract_entity_relationships_sql_query(self) -> str:

0 commit comments

Comments
 (0)