camper/deploy/add_campsite.sql

37 lines
1.0 KiB
PL/PgSQL

-- Deploy camper:add_campsite to pg
-- requires: roles
-- requires: schema_camper
-- requires: campsite
-- requires: campsite_type
begin;
set search_path to camper, public;
drop function if exists add_campsite(integer, text);
create or replace function add_campsite(campsite_type integer, label text, info1 text, info2 text) returns integer as
$$
declare
cid integer;
begin
insert into campsite (company_id, campsite_type_id, label, info1, info2)
select company_id, campsite_type_id, label, xmlparse(content info1), xmlparse(content info2)
from campsite_type
where campsite_type_id = add_campsite.campsite_type
returning campsite_id into cid
;
if cid is null then
raise foreign_key_violation using message = 'insert or update on table "campsite" violates foreign key constraint "campsite_campsite_type_id_fkey"';
end if;
return cid;
end
$$
language plpgsql
;
revoke execute on function add_campsite(integer, text, text, text) from public;
grant execute on function add_campsite(integer, text, text, text) to admin;
commit;