refactoring: move sql files
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
create or replace function create_title_in_constitution(title json, constitution_id uuid default null, out resource json)
|
||||
language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_title alias for title;
|
||||
_constitution_id uuid = coalesce(constitution_id, (title#>>'{constitution_id}')::uuid);
|
||||
_author_id uuid = (title#>>'{created_by, id}')::uuid;
|
||||
new_id uuid;
|
||||
begin
|
||||
insert into title (created_by_id, name, rank, constitution_id)
|
||||
select
|
||||
_author_id,
|
||||
ti.name,
|
||||
row_number() OVER (),
|
||||
_constitution_id
|
||||
from json_populate_record(null::title, _title) ti
|
||||
returning id into new_id;
|
||||
|
||||
if (_title->'articles' is not null) then
|
||||
insert into article_in_title (created_by_id, rank, title_id, article_id, constitution_id)
|
||||
select
|
||||
_author_id,
|
||||
row_number() over (),
|
||||
new_id,
|
||||
id,
|
||||
coalesce ((_title->>'constitution_id')::uuid, _constitution_id)
|
||||
from json_populate_recordset(null::article, _title->'articles') ;
|
||||
end if;
|
||||
|
||||
select find_constitution_title_by_id(new_id)
|
||||
into resource;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists create_title_in_constitution(out json);
|
||||
@@ -0,0 +1,19 @@
|
||||
create or replace function find_constitution_by_id(in id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_id alias for id;
|
||||
begin
|
||||
select to_json(t)
|
||||
from (
|
||||
select
|
||||
c.*,
|
||||
find_citizen_by_id(c.created_by_id) as created_by,
|
||||
find_constitution_titles_by_id(c.id) as titles
|
||||
into resource
|
||||
from constitution as c
|
||||
where c.id = _id
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_constitution_by_id(uuid, out json);
|
||||
@@ -0,0 +1,24 @@
|
||||
create or replace function find_constitution_title_by_id(in id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_id alias for id;
|
||||
begin
|
||||
select to_json(t)
|
||||
from (
|
||||
select
|
||||
ti.id,
|
||||
ti.name,
|
||||
ti.rank,
|
||||
array_agg(a order by ait.rank) as articles
|
||||
into resource
|
||||
from title as ti
|
||||
left join article_in_title ait on ti.id = ait.title_id
|
||||
left join article a on ait.article_id = a.id
|
||||
where ti.id = _id
|
||||
group by ti.id
|
||||
order by ti.rank
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_constitution_title_by_id(uuid, out json);
|
||||
@@ -0,0 +1,20 @@
|
||||
create or replace function find_constitution_titles_by_id(in constitution_id uuid, out resource json) language plpgsql as
|
||||
$$
|
||||
declare
|
||||
_constitution_id alias for constitution_id;
|
||||
begin
|
||||
select json_agg(t)
|
||||
from (
|
||||
select
|
||||
ti.id,
|
||||
ti.name,
|
||||
ti.rank
|
||||
into resource
|
||||
from title as ti
|
||||
where ti.constitution_id = _constitution_id
|
||||
order by ti.rank
|
||||
) as t;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop function if exists find_constitution_titles_by_id(uuid, out json);
|
||||
33
resources/sql/functions/constitution/upsert_constitution.sql
Normal file
33
resources/sql/functions/constitution/upsert_constitution.sql
Normal file
@@ -0,0 +1,33 @@
|
||||
create or replace procedure upsert_constitution(inout resource json)
|
||||
language plpgsql as
|
||||
$$
|
||||
declare
|
||||
titles json;
|
||||
_title json;
|
||||
_citizen_id uuid = (resource#>>'{created_by, id}')::uuid;
|
||||
new_id uuid;
|
||||
begin
|
||||
insert into constitution (version_id, created_by_id, title, annonymous)
|
||||
select
|
||||
version_id,
|
||||
_citizen_id,
|
||||
title,
|
||||
annonymous
|
||||
from json_populate_record(null::constitution, resource)
|
||||
returning id into new_id;
|
||||
|
||||
titles := (resource->>'titles');
|
||||
|
||||
for _title in select json_array_elements(titles) loop
|
||||
if _title#>>'{created_by, id}' is null then
|
||||
_title := jsonb_set(_title::jsonb, '{created_by}'::text[], jsonb_build_object('id', _citizen_id::text), true)::json;
|
||||
end if;
|
||||
|
||||
perform create_title_in_constitution(_title, new_id);
|
||||
end loop;
|
||||
|
||||
select find_constitution_by_id(new_id) into resource;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- drop procedure if exists upsert_constitution(inout json);
|
||||
Reference in New Issue
Block a user