Skip to content

Commit

Permalink
refactor: Format SQL Query
Browse files Browse the repository at this point in the history
  • Loading branch information
anshg1214 committed Sep 26, 2022
1 parent 65707fa commit 89d3c70
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 183 deletions.
192 changes: 96 additions & 96 deletions critiquebrainz/frontend/external/bookbrainz_db/author.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,104 +42,104 @@ def fetch_multiple_authors(bbids: List[uuid.UUID]) -> dict:
if not results:
with db.bb_engine.connect() as connection:
result = connection.execute(sqlalchemy.text("""
SELECT
bbid::text,
author.name,
sort_name,
author_type,
disambiguation,
identifier_set_id,
relationship_set_id,
area_id,
begin_year,
begin_month,
begin_day,
begin_area_id,
end_year,
end_month,
end_day,
end_area_id,
author.ended,
gender.name as gender,
COALESCE (json_agg(area)
FILTER (WHERE area.name IS NOT NULL),
'[]'
) as area_info,
COALESCE (json_agg(DISTINCT relationships)
FILTER (WHERE relationships IS NOT NULL),
'[]'
) as rels,
COALESCE (json_agg(DISTINCT identifiers)
FILTER (WHERE identifiers IS NOT NULL),
'[]'
) as identifiers
FROM author
LEFT JOIN musicbrainz.area area
ON begin_area_id = area.id
OR end_area_id = area.id
OR area_id = area.id
LEFT JOIN musicbrainz.gender
ON gender_id = gender.id
LEFT JOIN LATERAL (
SELECT rel.id as id,
reltype.id as relationship_type_id,
reltype.label as label,
rel.source_bbid::text as source_bbid,
rel.target_bbid::text as target_bbid,
reltype.target_entity_type as target_entity_type,
reltype.source_entity_type as source_entity_type,
COALESCE(
jsonb_object_agg(relatttype.name, relatttext.text_value)
FILTER (WHERE relatts IS NOT NULL),
'[]'
) as attributes
FROM relationship_set__relationship rels
LEFT JOIN relationship rel ON rels.relationship_id = rel.id
LEFT JOIN relationship_type reltype ON rel.type_id = reltype.id
LEFT JOIN relationship_attribute_set__relationship_attribute relatts ON rel.attribute_set_id = relatts.set_id
LEFT JOIN relationship_attribute relatt ON relatts.attribute_id = relatt.id
LEFT JOIN relationship_attribute_type relatttype ON relatt.attribute_type = relatttype.id
LEFT JOIN relationship_attribute_text_value relatttext ON relatts.attribute_id = relatttext.attribute_id
WHERE rels.set_id = author.relationship_set_id
GROUP BY rel.id,
reltype.id,
reltype.label,
rel.source_bbid,
rel.target_bbid,
reltype.target_entity_type,
reltype.source_entity_type
SELECT
bbid::text,
author.name,
sort_name,
author_type,
disambiguation,
identifier_set_id,
relationship_set_id,
area_id,
begin_year,
begin_month,
begin_day,
begin_area_id,
end_year,
end_month,
end_day,
end_area_id,
author.ended,
gender.name as gender,
COALESCE (json_agg(area)
FILTER (WHERE area.name IS NOT NULL),
'[]'
) as area_info,
COALESCE (json_agg(DISTINCT relationships)
FILTER (WHERE relationships IS NOT NULL),
'[]'
) as rels,
COALESCE (json_agg(DISTINCT identifiers)
FILTER (WHERE identifiers IS NOT NULL),
'[]'
) as identifiers
FROM author
LEFT JOIN musicbrainz.area area
ON begin_area_id = area.id
OR end_area_id = area.id
OR area_id = area.id
LEFT JOIN musicbrainz.gender
ON gender_id = gender.id
LEFT JOIN LATERAL (
SELECT rel.id as id,
reltype.id as relationship_type_id,
reltype.label as label,
rel.source_bbid::text as source_bbid,
rel.target_bbid::text as target_bbid,
reltype.target_entity_type as target_entity_type,
reltype.source_entity_type as source_entity_type,
COALESCE(
jsonb_object_agg(relatttype.name, relatttext.text_value)
FILTER (WHERE relatts IS NOT NULL),
'[]'
) as attributes
FROM relationship_set__relationship rels
LEFT JOIN relationship rel ON rels.relationship_id = rel.id
LEFT JOIN relationship_type reltype ON rel.type_id = reltype.id
LEFT JOIN relationship_attribute_set__relationship_attribute relatts ON rel.attribute_set_id = relatts.set_id
LEFT JOIN relationship_attribute relatt ON relatts.attribute_id = relatt.id
LEFT JOIN relationship_attribute_type relatttype ON relatt.attribute_type = relatttype.id
LEFT JOIN relationship_attribute_text_value relatttext ON relatts.attribute_id = relatttext.attribute_id
WHERE rels.set_id = author.relationship_set_id
GROUP BY rel.id,
reltype.id,
reltype.label,
rel.source_bbid,
rel.target_bbid,
reltype.target_entity_type,
reltype.source_entity_type
) AS relationships ON TRUE
LEFT JOIN LATERAL (
SELECT iden.type_id as type_id,
idtype.label as label,
idtype.display_template as url_template,
iden.value as value
FROM identifier_set__identifier idens
LEFT JOIN identifier iden on idens.identifier_id = iden.id
LEFT JOIN identifier_type idtype on iden.type_id = idtype.id
WHERE idens.set_id = author.identifier_set_id
LEFT JOIN LATERAL (
SELECT iden.type_id as type_id,
idtype.label as label,
idtype.display_template as url_template,
iden.value as value
FROM identifier_set__identifier idens
LEFT JOIN identifier iden on idens.identifier_id = iden.id
LEFT JOIN identifier_type idtype on iden.type_id = idtype.id
WHERE idens.set_id = author.identifier_set_id
) AS identifiers ON TRUE
WHERE bbid IN :bbids
AND master = 't'
GROUP BY bbid,
author.name,
sort_name,
author_type,
disambiguation,
identifier_set_id,
relationship_set_id,
area_id,
begin_year,
begin_month,
begin_day,
begin_area_id,
end_year,
end_month,
end_day,
end_area_id,
author.ended,
gender.name
"""), {'bbids': tuple(bbids)})
WHERE bbid IN :bbids
AND master = 't'
GROUP BY bbid,
author.name,
sort_name,
author_type,
disambiguation,
identifier_set_id,
relationship_set_id,
area_id,
begin_year,
begin_month,
begin_day,
begin_area_id,
end_year,
end_month,
end_day,
end_area_id,
author.ended,
gender.name
"""), {'bbids': tuple(bbids)})

authors = result.fetchall()
results = {}
Expand Down
72 changes: 36 additions & 36 deletions critiquebrainz/frontend/external/bookbrainz_db/edition_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,53 +65,53 @@ def fetch_multiple_edition_groups(bbids: List[uuid.UUID]) -> dict:
'[]'
) as author_credits,
COALESCE (json_agg(DISTINCT relationships)
FILTER (WHERE relationships IS NOT NULL),
'[]'
) as rels,
COALESCE (json_agg(DISTINCT identifiers)
FILTER (WHERE identifiers IS NOT NULL),
'[]'
) as identifiers
FILTER (WHERE relationships IS NOT NULL),
'[]'
) as rels,
COALESCE (json_agg(DISTINCT identifiers)
FILTER (WHERE identifiers IS NOT NULL),
'[]'
) as identifiers
FROM edition_group
LEFT JOIN author_credit_name acn ON acn.author_credit_id = edition_group.author_credit_id
LEFT JOIN LATERAL (
SELECT rel.id as id,
reltype.id as relationship_type_id,
reltype.label as label,
rel.source_bbid::text as source_bbid,
rel.target_bbid::text as target_bbid,
reltype.target_entity_type as target_entity_type,
reltype.source_entity_type as source_entity_type,
COALESCE(
jsonb_object_agg(relatttype.name, relatttext.text_value)
FILTER (WHERE relatts IS NOT NULL),
'[]'
) as attributes
FROM relationship_set__relationship rels
LEFT JOIN relationship rel ON rels.relationship_id = rel.id
LEFT JOIN relationship_type reltype ON rel.type_id = reltype.id
LEFT JOIN relationship_attribute_set__relationship_attribute relatts ON rel.attribute_set_id = relatts.set_id
LEFT JOIN relationship_attribute relatt ON relatts.attribute_id = relatt.id
LEFT JOIN relationship_attribute_type relatttype ON relatt.attribute_type = relatttype.id
LEFT JOIN relationship_attribute_text_value relatttext ON relatts.attribute_id = relatttext.attribute_id
WHERE rels.set_id = edition_group.relationship_set_id
GROUP BY rel.id,
reltype.id,
reltype.label,
rel.source_bbid,
rel.target_bbid,
reltype.target_entity_type,
reltype.source_entity_type
reltype.id as relationship_type_id,
reltype.label as label,
rel.source_bbid::text as source_bbid,
rel.target_bbid::text as target_bbid,
reltype.target_entity_type as target_entity_type,
reltype.source_entity_type as source_entity_type,
COALESCE(
jsonb_object_agg(relatttype.name, relatttext.text_value)
FILTER (WHERE relatts IS NOT NULL),
'[]'
) as attributes
FROM relationship_set__relationship rels
LEFT JOIN relationship rel ON rels.relationship_id = rel.id
LEFT JOIN relationship_type reltype ON rel.type_id = reltype.id
LEFT JOIN relationship_attribute_set__relationship_attribute relatts ON rel.attribute_set_id = relatts.set_id
LEFT JOIN relationship_attribute relatt ON relatts.attribute_id = relatt.id
LEFT JOIN relationship_attribute_type relatttype ON relatt.attribute_type = relatttype.id
LEFT JOIN relationship_attribute_text_value relatttext ON relatts.attribute_id = relatttext.attribute_id
WHERE rels.set_id = edition_group.relationship_set_id
GROUP BY rel.id,
reltype.id,
reltype.label,
rel.source_bbid,
rel.target_bbid,
reltype.target_entity_type,
reltype.source_entity_type
) AS relationships ON TRUE
LEFT JOIN LATERAL (
SELECT iden.type_id as type_id,
idtype.label as label,
idtype.display_template as url_template,
iden.value as value
FROM identifier_set__identifier idens
LEFT JOIN identifier iden on idens.identifier_id = iden.id
LEFT JOIN identifier_type idtype on iden.type_id = idtype.id
WHERE idens.set_id = edition_group.identifier_set_id
LEFT JOIN identifier iden on idens.identifier_id = iden.id
LEFT JOIN identifier_type idtype on iden.type_id = idtype.id
WHERE idens.set_id = edition_group.identifier_set_id
) AS identifiers ON TRUE
WHERE bbid in :bbids
AND master = 't'
Expand Down
44 changes: 22 additions & 22 deletions critiquebrainz/frontend/external/bookbrainz_db/publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,29 @@ def fetch_multiple_publishers(bbids: List[uuid.UUID]) -> dict:
if not results:
with db.bb_engine.connect() as connection:
result = connection.execute(sqlalchemy.text("""
SELECT
bbid::text,
publisher.name,
sort_name,
publisher_type,
disambiguation,
identifier_set_id,
relationship_set_id,
area.gid::text as area_mbid,
SELECT
bbid::text,
publisher.name,
sort_name,
publisher_type,
disambiguation,
identifier_set_id,
relationship_set_id,
area.gid::text as area_mbid,
area.name as area_name,
begin_year,
begin_month,
begin_day,
end_year,
end_month,
end_day,
publisher.ended
FROM publisher
LEFT JOIN musicbrainz.area
ON publisher.area_id = area.id
WHERE bbid IN :bbids
AND master = 't'
"""), {'bbids': tuple(bbids)})
begin_year,
begin_month,
begin_day,
end_year,
end_month,
end_day,
publisher.ended
FROM publisher
LEFT JOIN musicbrainz.area
ON publisher.area_id = area.id
WHERE bbid IN :bbids
AND master = 't'
"""), {'bbids': tuple(bbids)})

publishers = result.fetchall()
results = {}
Expand Down

0 comments on commit 89d3c70

Please sign in to comment.