bank
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
set search_path TO bank;
|
||||
|
||||
create or replace procedure add_person (
|
||||
p_firstname text,
|
||||
p_lastname text,
|
||||
@@ -20,6 +22,60 @@ begin
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace procedure add_person (
|
||||
p_firstname text,
|
||||
p_lastname text,
|
||||
p_birthdate date,
|
||||
p_nb_account int
|
||||
)
|
||||
language plpgsql
|
||||
as $$
|
||||
declare
|
||||
v_holder_id bigint;
|
||||
begin
|
||||
|
||||
insert into holder(type) values ('PERSON')
|
||||
returning id into v_holder_id;
|
||||
|
||||
insert into person(id, firstname, lastname, birthdate)
|
||||
values (v_holder_id, p_firstname, p_lastname, p_birthdate);
|
||||
|
||||
for counter in 1..pb_nb_account loop
|
||||
call add_account(v_holder_id, 'EUR');
|
||||
end loop;
|
||||
|
||||
raise notice 'Titulaire créé : % = % %',
|
||||
v_holder_id, p_firstname, p_lastname;
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace procedure add_person (
|
||||
p_firstname text,
|
||||
p_lastname text,
|
||||
p_birthdate date,
|
||||
currencies text[]
|
||||
)
|
||||
language plpgsql
|
||||
as $$
|
||||
declare
|
||||
v_holder_id bigint;
|
||||
begin
|
||||
|
||||
insert into holder(type) values ('PERSON')
|
||||
returning id into v_holder_id;
|
||||
|
||||
insert into person(id, firstname, lastname, birthdate)
|
||||
values (v_holder_id, p_firstname, p_lastname, p_birthdate);
|
||||
|
||||
for counter in 1..array_length(currencies, 1) loop
|
||||
call add_account(v_holder_id, currencies[counter]);
|
||||
end loop;
|
||||
|
||||
raise notice 'Titulaire créé : % = % %',
|
||||
v_holder_id, p_firstname, p_lastname;
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace procedure add_bank (
|
||||
name text
|
||||
)
|
||||
@@ -33,10 +89,10 @@ begin
|
||||
returning id into v_holder_id;
|
||||
|
||||
insert into bank(id, name)
|
||||
values (v_holder_id, p_name);
|
||||
values (v_holder_id, name);
|
||||
|
||||
raise notice 'Titulaire créé : % = %',
|
||||
v_holder_id, p_name;
|
||||
v_holder_id, name;
|
||||
end;
|
||||
$$;
|
||||
|
||||
@@ -61,3 +117,80 @@ begin
|
||||
v_holder_id, p_name, p_registration_number;
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace procedure add_account(
|
||||
p_holder_id bigint,
|
||||
p_currency text
|
||||
)
|
||||
language plpgsql
|
||||
as $$
|
||||
declare
|
||||
v_account_id int;
|
||||
begin
|
||||
|
||||
insert into account(currency_code)
|
||||
values (p_currency)
|
||||
returning id into v_account_id;
|
||||
|
||||
insert into account_holder (account_id, holder_id, share)
|
||||
values (v_account_id, p_holder_id, 1);
|
||||
|
||||
raise notice 'Compte créé avec succès (ID=%)',
|
||||
v_account_id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
|
||||
create or replace procedure add_account(
|
||||
p_iban text,
|
||||
p_name text,
|
||||
p_holders int[],
|
||||
p_shares numeric[]
|
||||
)
|
||||
language plpgsql
|
||||
as $$
|
||||
declare
|
||||
v_account_id int;
|
||||
v_sum numeric(6,4) := 0;
|
||||
v_count int;
|
||||
begin
|
||||
-- Vérification des tailles
|
||||
if array_length(p_holders, 1) is null or array_length(p_shares, 1) is null then
|
||||
raise exception 'Les tableaux de titulaires et de parts ne peuvent pas être vides.';
|
||||
end if;
|
||||
|
||||
if array_length(p_holders, 1) <> array_length(p_shares, 1) then
|
||||
raise exception 'Les tableaux de titulaires et de parts doivent avoir la même taille.';
|
||||
end if;
|
||||
|
||||
-- Calcul de la somme des parts
|
||||
-- select sum(unnest(p_shares)) into v_sum;
|
||||
for i in 1..array_length(p_shares, 1) loop
|
||||
v_sum := v_sum + p_shares[i];
|
||||
end loop;
|
||||
|
||||
if abs(v_sum - 1.0) > 0.0001 then
|
||||
raise exception 'La somme des parts (%.4f) doit être égale à 1.0000', v_sum;
|
||||
end if;
|
||||
|
||||
-- Vérification des titulaires
|
||||
select count(*) into v_count from holder where id = any(p_holders);
|
||||
if v_count <> array_length(p_holders, 1) then
|
||||
raise exception 'Un ou plusieurs titulaires n''existent pas.';
|
||||
end if;
|
||||
|
||||
-- Création du compte
|
||||
insert into account(iban, name)
|
||||
values (p_iban, p_name)
|
||||
returning id into v_account_id;
|
||||
|
||||
-- Association des titulaires
|
||||
for i in 1..array_length(p_holders, 1) loop
|
||||
insert into account_holder(account_id, holder_id, share)
|
||||
values (v_account_id, p_holders[i], p_shares[i]);
|
||||
end loop;
|
||||
|
||||
raise notice 'Compte créé avec succès (ID=%) avec % titulaires.',
|
||||
v_account_id, array_length(p_holders, 1);
|
||||
end;
|
||||
$$;
|
||||
|
||||
Reference in New Issue
Block a user