-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsql-to-dab-config.sql
158 lines (151 loc) · 3.67 KB
/
sql-to-dab-config.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
Table to publish as API
*/
drop table if exists #t;
select
[name] as entity_name,
[name] as table_name,
object_id,
schema_id
into
#t
from
sys.tables
where
[name] not in ('AdventureWorksDWBuildVersion', 'DatabaseLog', 'NewFactCurrencyRate')
and
schema_id = schema_id('SalesLT')
;
-- select * from #t;
/*
Find many-to-one relationships between tables
*/
drop table if exists #r;
select
t.object_id,
object_name(t.object_id) as entity,
referenced_object_id,
object_name(referenced_object_id) as [target.entity],
cast(iif(referenced_object_id is null, null, 'one') as varchar(10)) as cardinality,
json_array((
select string_agg([name], ',')
from sys.foreign_key_columns fkc
inner join sys.columns c on
fkc.parent_object_id = c.object_id and fkc.parent_column_id = c.column_id
where
fk.object_id = fkc.constraint_object_id
)) as [source.fields],
json_array((
select string_agg([name], ',')
from sys.foreign_key_columns fkc
inner join sys.columns c on
fkc.referenced_object_id = c.object_id and fkc.referenced_column_id = c.column_id
where
fk.object_id = fkc.constraint_object_id
)) as [target.fields]
into
#r
from
#t t
left join
sys.foreign_keys fk on fk.parent_object_id = t.object_id
;
select * from #r;
/*
Add inverse, one-to-many, relationships
*/
insert into
#r
select
referenced_object_id as object_id,
[target.entity] as entity,
object_id as referenced_object_id,
entity as [target.entity],
'many' as cardinality,
[target.fields] as [source.fields],
[source.fields] as [target.fields]
from
#r
where
referenced_object_id is not null
;
--select * from #r;
/*
Add relationship name
*/
drop table if exists #r2;
with cte as
(
select
row_number() over (partition by object_id, referenced_object_id order by referenced_object_id) as rn,
*
from
#r
)
select
relationship_name = [target.entity] + case when rn > 1 then cast(rn as nvarchar(2)) else N'' end,
*
into
#r2
from
cte;
select * from #r2;
/*
Build relationship JSON config for each entity
*/
drop table if exists #jr;
select
object_id,
entity,
json_query(( '{' + (string_agg(
'"' + [relationship_name] + '": ' +
json_object(
'cardinality': cardinality,
'target.entity': [target.entity],
'source.fields': json_query([source.fields]),
'target.fields': json_query([target.fields])
)
,
','
)
) + '}')) as relationships
into
#jr
from
#r2
group by
object_id,
entity
;
select * from #jr;
/*
Build complete JSON config for each entity
*/
drop table if exists #c;
select
[entity_name] as entity,
json_object(
'source': quotename(schema_name(schema_id)) + '.' + quotename(table_name),
'permissions': json_array(
json_object(
'role': 'anonymous',
'actions': json_array(json_object('action':'read'))
)
),
'relationships': json_query(isnull(r.relationships, '{}'))
) as config
into
#c
from
#t t
left outer join
#jr r on t.object_id = r.object_id
;
select * from #c;
/*
Create the final entities config section
*/
select
json_object('entities': json_query('{' + string_agg('"' + entity + '": ' + config, ',') + '}')) as entities_config
from
#c