Skip to content

Commit e2f4bf3

Browse files
Benjamin-Knightaxellpadilla
authored andcommitted
Update the catalog macros to support multiple databases
1 parent 87491ce commit e2f4bf3

1 file changed

Lines changed: 134 additions & 126 deletions

File tree

Lines changed: 134 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% macro sqlserver__get_catalog(information_schemas, schemas) -%}
22
{% set query_label = apply_label() %}
33
{%- call statement('catalog', fetch_result=True) -%}
4-
4+
{{ get_use_database_sql(information_schemas.database) }}
55
with
66
principals as (
77
select
@@ -127,143 +127,151 @@
127127

128128
{% macro sqlserver__get_catalog_relations(information_schema, relations) -%}
129129
{% set query_label = apply_label() %}
130-
{%- call statement('catalog', fetch_result=True) -%}
130+
{%- set distinct_databases = relations | map(attribute='database') | unique | list -%}
131131

132-
with
133-
principals as (
134-
select
135-
name as principal_name,
136-
principal_id as principal_id
137-
from
138-
sys.database_principals {{ information_schema_hints() }}
139-
),
132+
{%- if distinct_databases | length == 1 -%}
133+
{%- call statement('catalog', fetch_result=True) -%}
134+
{{ get_use_database_sql(distinct_databases[0]) }}
135+
with
136+
principals as (
137+
select
138+
name as principal_name,
139+
principal_id as principal_id
140+
from
141+
sys.database_principals {{ information_schema_hints() }}
142+
),
140143

141-
schemas as (
142-
select
143-
name as schema_name,
144-
schema_id as schema_id,
145-
principal_id as principal_id
146-
from
147-
sys.schemas {{ information_schema_hints() }}
148-
),
144+
schemas as (
145+
select
146+
name as schema_name,
147+
schema_id as schema_id,
148+
principal_id as principal_id
149+
from
150+
sys.schemas {{ information_schema_hints() }}
151+
),
149152

150-
tables as (
151-
select
152-
object_id,
153-
name as table_name,
154-
schema_id as schema_id,
155-
principal_id as principal_id,
156-
'BASE TABLE' as table_type
157-
from
158-
sys.tables {{ information_schema_hints() }}
159-
),
153+
tables as (
154+
select
155+
object_id,
156+
name as table_name,
157+
schema_id as schema_id,
158+
principal_id as principal_id,
159+
'BASE TABLE' as table_type
160+
from
161+
sys.tables {{ information_schema_hints() }}
162+
),
160163

161-
tables_with_metadata as (
162-
select
163-
object_id,
164-
table_name,
165-
schema_name,
166-
coalesce(tables.principal_id, schemas.principal_id) as owner_principal_id,
167-
table_type
168-
from
169-
tables
170-
join schemas on tables.schema_id = schemas.schema_id
171-
),
164+
tables_with_metadata as (
165+
select
166+
object_id,
167+
table_name,
168+
schema_name,
169+
coalesce(tables.principal_id, schemas.principal_id) as owner_principal_id,
170+
table_type
171+
from
172+
tables
173+
join schemas on tables.schema_id = schemas.schema_id
174+
),
172175

173-
views as (
174-
select
175-
object_id,
176-
name as table_name,
177-
schema_id as schema_id,
178-
principal_id as principal_id,
179-
'VIEW' as table_type
180-
from
181-
sys.views {{ information_schema_hints() }}
182-
),
176+
views as (
177+
select
178+
object_id,
179+
name as table_name,
180+
schema_id as schema_id,
181+
principal_id as principal_id,
182+
'VIEW' as table_type
183+
from
184+
sys.views {{ information_schema_hints() }}
185+
),
183186

184-
views_with_metadata as (
185-
select
186-
object_id,
187-
table_name,
188-
schema_name,
189-
coalesce(views.principal_id, schemas.principal_id) as owner_principal_id,
190-
table_type
191-
from
192-
views
193-
join schemas on views.schema_id = schemas.schema_id
194-
),
187+
views_with_metadata as (
188+
select
189+
object_id,
190+
table_name,
191+
schema_name,
192+
coalesce(views.principal_id, schemas.principal_id) as owner_principal_id,
193+
table_type
194+
from
195+
views
196+
join schemas on views.schema_id = schemas.schema_id
197+
),
195198

196-
tables_and_views as (
197-
select
198-
object_id,
199-
table_name,
200-
schema_name,
201-
principal_name,
202-
table_type
203-
from
204-
tables_with_metadata
205-
join principals on tables_with_metadata.owner_principal_id = principals.principal_id
206-
union all
207-
select
208-
object_id,
209-
table_name,
210-
schema_name,
211-
principal_name,
212-
table_type
213-
from
214-
views_with_metadata
215-
join principals on views_with_metadata.owner_principal_id = principals.principal_id
216-
),
199+
tables_and_views as (
200+
select
201+
object_id,
202+
table_name,
203+
schema_name,
204+
principal_name,
205+
table_type
206+
from
207+
tables_with_metadata
208+
join principals on tables_with_metadata.owner_principal_id = principals.principal_id
209+
union all
210+
select
211+
object_id,
212+
table_name,
213+
schema_name,
214+
principal_name,
215+
table_type
216+
from
217+
views_with_metadata
218+
join principals on views_with_metadata.owner_principal_id = principals.principal_id
219+
),
217220

218-
cols as (
221+
cols as (
219222

220-
select
221-
c.object_id,
222-
c.name as column_name,
223-
c.column_id as column_index,
224-
t.name as column_type
225-
from sys.columns as c {{ information_schema_hints() }}
226-
left join sys.types as t on c.system_type_id = t.system_type_id
227-
)
223+
select
224+
c.object_id,
225+
c.name as column_name,
226+
c.column_id as column_index,
227+
t.name as column_type
228+
from sys.columns as c {{ information_schema_hints() }}
229+
left join sys.types as t on c.system_type_id = t.system_type_id
230+
)
228231

229-
select
230-
DB_NAME() as table_database,
231-
tv.schema_name as table_schema,
232-
tv.table_name,
233-
tv.table_type,
234-
null as table_comment,
235-
tv.principal_name as table_owner,
236-
cols.column_name,
237-
cols.column_index,
238-
cols.column_type,
239-
null as column_comment
240-
from tables_and_views tv
241-
join cols on tv.object_id = cols.object_id
242-
where (
243-
{%- for relation in relations -%}
244-
{% if relation.schema and relation.identifier %}
245-
(
246-
upper(tv.schema_name) = upper('{{ relation.schema }}')
247-
and upper(tv.table_name) = upper('{{ relation.identifier }}')
248-
)
249-
{% elif relation.schema %}
250-
(
251-
upper(tv.schema_name) = upper('{{ relation.schema }}')
252-
)
253-
{% else %}
254-
{% do exceptions.raise_compiler_error(
255-
'`get_catalog_relations` requires a list of relations, each with a schema'
256-
) %}
257-
{% endif %}
232+
select
233+
DB_NAME() as table_database,
234+
tv.schema_name as table_schema,
235+
tv.table_name,
236+
tv.table_type,
237+
null as table_comment,
238+
tv.principal_name as table_owner,
239+
cols.column_name,
240+
cols.column_index,
241+
cols.column_type,
242+
null as column_comment
243+
from tables_and_views tv
244+
join cols on tv.object_id = cols.object_id
245+
where (
246+
{%- for relation in relations -%}
247+
{% if relation.schema and relation.identifier %}
248+
(
249+
upper(tv.schema_name) = upper('{{ relation.schema }}')
250+
and upper(tv.table_name) = upper('{{ relation.identifier }}')
251+
)
252+
{% elif relation.schema %}
253+
(
254+
upper(tv.schema_name) = upper('{{ relation.schema }}')
255+
)
256+
{% else %}
257+
{% do exceptions.raise_compiler_error(
258+
'`get_catalog_relations` requires a list of relations, each with a schema'
259+
) %}
260+
{% endif %}
258261

259-
{%- if not loop.last %} or {% endif -%}
260-
{%- endfor -%}
261-
)
262+
{%- if not loop.last %} or {% endif -%}
263+
{%- endfor -%}
264+
)
262265

263-
order by column_index
264-
{{ query_label }}
265-
{%- endcall -%}
266+
order by column_index
267+
{{ query_label }}
266268

267-
{{ return(load_result('catalog').table) }}
269+
{%- endcall -%}
270+
{{ return(load_result('catalog').table) }}
271+
{% else %}
272+
{% do exceptions.raise_compiler_error(
273+
'`get_catalog_relations` can catalog one database at a time'
274+
) %}
275+
{% endif %}
268276

269277
{%- endmacro %}

0 commit comments

Comments
 (0)