-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmany_documents.sql
More file actions
111 lines (111 loc) · 2.74 KB
/
many_documents.sql
File metadata and controls
111 lines (111 loc) · 2.74 KB
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
select
d.id,
d.short_name,
d.title,
d.is_reference,
d.written_at,
d.format_id,
d.genre_id,
d.audio_slice_id,
d.include_audio_in_edited_collection,
media_resource.url as "audio_url?",
media_resource.recorded_at as "recorded_at?",
dailp_user.id as "recorded_by?",
dailp_user.display_name as "recorded_by_name?",
media_slice.time_range as "audio_slice?",
ubd.bookmarked_on as "bookmarked_on?",
coalesce(
jsonb_agg(
jsonb_build_object(
'id', contributor.id,
'name', contributor.full_name,
'role', attr.contribution_role
)
) filter (where contributor is not null),
'[]'
) as contributors,
(
select coalesce(
jsonb_agg(
jsonb_build_object(
'id', k.id,
'name', k.name,
'status', k.status
)
),
'[]'
)
from document_keyword dk
join keyword k on k.id = dk.keyword_id
where dk.document_id = d.id
) as keywords,
(
select coalesce(
jsonb_agg(
jsonb_build_object(
'id', l.id,
'name', l.name,
'status', l.status
)
),
'[]'
)
from document_language dl
join language l on l.id = dl.language_id
where dl.document_id = d.id
) as languages,
( -- Subject Headings
select coalesce(
jsonb_agg(
jsonb_build_object(
'id', sh.id,
'name', sh.name,
'status', sh.status
)), '[]')
from document_subject_heading dsh
join subject_heading sh on sh.id = dsh.subject_heading_id
where dsh.document_id = d.id
) as subject_headings,
( -- Spatial Coverage
select coalesce(
jsonb_agg(
jsonb_build_object(
'id', sc.id,
'name', sc.name,
'status', sc.status
)
),
'[]'
)
from document_spatial_coverage dsc
join spatial_coverage sc on sc.id = dsc.spatial_coverage_id
where dsc.document_id = d.id
) as spatial_coverage,
(
select coalesce(
jsonb_agg(
jsonb_build_object(
'id', cr.id,
'name', cr.name
)
),
'[]'
)
from document_creator dcr
join creator cr on cr.id = dcr.creator_id
where dcr.document_id = d.id
) as creators
from document as d
left join contributor_attribution as attr on attr.document_id = d.id
left join contributor on contributor.id = attr.contributor_id
left join media_slice on media_slice.id = d.audio_slice_id
left join media_resource on media_resource.id = media_slice.resource_id
left join dailp_user on dailp_user.id = media_resource.recorded_by
left join user_bookmarked_document as ubd on ubd.document_id = d.id
where d.id = any($1)
group by
d.id,
media_slice.id,
media_resource.id,
dailp_user.id,
ubd.bookmarked_on;