Files
sql/banque/banque.2.proc.sql

245 lines
5.7 KiB
MySQL
Raw Normal View History

2025-11-09 10:26:00 +01:00
set search_path TO bank;
2025-11-07 21:29:15 +01:00
create or replace procedure add_person (
p_firstname text,
p_lastname text,
p_birthdate date
)
language plpgsql
as $$
declare
v_holder_id bigint;
begin
2025-11-08 05:51:25 +01:00
2025-11-07 21:29:15 +01:00
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);
raise notice 'Titulaire créé : % = % %',
2025-11-08 05:51:25 +01:00
v_holder_id, p_firstname, p_lastname;
2025-11-07 21:29:15 +01:00
end;
$$;
2025-11-09 10:26:00 +01:00
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;
$$;
2025-11-07 21:29:15 +01:00
create or replace procedure add_bank (
name text
)
language plpgsql
as $$
declare
v_holder_id bigint;
begin
2025-11-08 05:51:25 +01:00
2025-11-07 21:29:15 +01:00
insert into holder(type) values ('BANK')
returning id into v_holder_id;
insert into bank(id, name)
2025-11-09 10:26:00 +01:00
values (v_holder_id, name);
2025-11-07 21:29:15 +01:00
raise notice 'Titulaire créé : % = %',
2025-11-09 10:26:00 +01:00
v_holder_id, name;
2025-11-07 21:29:15 +01:00
end;
$$;
create or replace procedure add_company (
2025-11-08 05:51:25 +01:00
p_name text,
p_registration_number text,
2025-11-07 21:29:15 +01:00
p_created_at date
)
language plpgsql
as $$
declare
v_holder_id bigint;
begin
2025-11-08 05:51:25 +01:00
2025-11-07 21:29:15 +01:00
insert into holder(type) values ('COMPANY')
returning id into v_holder_id;
insert into company(id, name, registration_number, created_at)
values (v_holder_id, p_name, p_registration_number, p_created_at);
raise notice 'Titulaire créé : % = % %',
2025-11-08 05:51:25 +01:00
v_holder_id, p_name, p_registration_number;
2025-11-07 21:29:15 +01:00
end;
$$;
2025-11-09 10:26:00 +01:00
create or replace procedure add_account(
p_holder_id bigint,
p_currency text
)
language plpgsql
as $$
declare
v_account_id int;
begin
2025-11-09 18:26:41 +01:00
2025-11-09 10:26:00 +01:00
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;
$$;
2025-11-09 18:26:41 +01:00
2025-11-14 13:43:59 +01:00
create or replace procedure transfer(
2025-11-09 18:26:41 +01:00
p_source int,
p_destination int,
p_amount numeric,
p_date date
)
language plpgsql
as $$
declare
v_transaction_id int;
2025-11-14 13:43:59 +01:00
rate_source numeric;
rate_destination numeric;
2025-11-09 18:26:41 +01:00
begin
insert into transaction(amount, transaction_date)
values (p_amount, p_date)
returning id into v_transaction_id;
2025-11-14 13:43:59 +01:00
select rate into rate_source
from exchange_rate er
inner join account a
on a.currency_code = er.currency_code
where id = p_source and date < p_date
order by date desc limit 1;
select rate into rate_destination
from exchange_rate er
inner join account a
on a.currency_code = er.currency_code
where id = p_destination and date < p_date
order by date desc limit 1;
2025-11-09 18:26:41 +01:00
insert into operation (transaction_id, account_id, amount, direction)
values (v_transaction_id, p_source, p_amount, 'DEBIT');
insert into operation (transaction_id, account_id, amount, direction)
2025-11-14 13:43:59 +01:00
values (v_transaction_id, p_destination, p_amount / rate_source * rate_destination, 'CREDIT');
2025-11-09 18:26:41 +01:00
update account
set balance = balance - p_amount
where id = p_source;
update account
2025-11-14 13:43:59 +01:00
set balance = balance + round(p_amount / rate_source * rate_destination, 2)
2025-11-09 18:26:41 +01:00
WHERE id = p_destination;
end;
$$;