bank docker

This commit is contained in:
2025-11-16 09:34:19 +01:00
parent 4f38a8793a
commit e55c60504e
7 changed files with 7029 additions and 270 deletions

View File

@@ -1,14 +1,7 @@
drop schema if exists bank cascade;
create schema bank;
set search_path TO bank;
/************************************************************************
************************************************************************/
create type holder_type as enum ('BANK', 'PERSON', 'COMPANY'); create type holder_type as enum ('BANK', 'PERSON', 'COMPANY');
create table holder ( create table holder (
"id" bigint primary key generated always as identity, "id" bigint primary key generated always as identity,
"type" holder_type not null, "type" holder_type not null,
"created_at" timestamp not null default current_timestamp "created_at" timestamp not null default current_timestamp
); );
@@ -41,7 +34,7 @@ create table person (
/************************************************************************ /************************************************************************
* Company * Company
* *
************************************************************************/ ************************************************************************/
create table company ( create table company (
"id" bigint primary key references holder(id) on delete cascade, "id" bigint primary key references holder(id) on delete cascade,
@@ -52,13 +45,13 @@ create table company (
/************************************************************************ /************************************************************************
* Company * Company
* *
************************************************************************/ ************************************************************************/
create table currency ( create table currency (
"code" text primary key "code" text primary key
); );
insert into currency insert into currency
values ('EUR'), ('YEN'), ('USD'); values ('EUR'), ('YEN'), ('USD');
/************************************************************************ /************************************************************************
@@ -85,9 +78,9 @@ create or replace function latest_exchange_rate (
returns decimal returns decimal
language sql language sql
as $$ as $$
select rate select rate
from exchange_rate from exchange_rate
where currency_code = p_code where currency_code = p_code
and date < p_date and date < p_date
order by date desc order by date desc
limit 1; limit 1;
@@ -184,7 +177,7 @@ call add_person('Justin', 'Hébrard', '1993-03-11');
create or replace procedure add_company ( create or replace procedure add_company (
p_name text, p_name text,
p_registration_number text, p_registration_number text,
p_created_at date p_creation_date date
) )
language plpgsql language plpgsql
as $$ as $$
@@ -195,8 +188,8 @@ begin
insert into holder(type) values ('COMPANY') insert into holder(type) values ('COMPANY')
returning id into v_holder_id; returning id into v_holder_id;
insert into company(id, name, registration_number, created_at) insert into company(id, name, registration_number, creation_date)
values (v_holder_id, p_name, p_registration_number, p_created_at); values (v_holder_id, p_name, p_registration_number, p_creation_date);
raise notice 'Titulaire créé : % = % %', raise notice 'Titulaire créé : % = % %',
v_holder_id, p_name, p_registration_number; v_holder_id, p_name, p_registration_number;
@@ -279,21 +272,21 @@ call add_account('EUR', array[7], array[1]);
call add_account('USD', array[7], array[1]); call add_account('USD', array[7], array[1]);
create or replace view holder_detail as create or replace view holder_detail as
select h.id, h.type, select h.id, h.type,
case case
when type = 'PERSON' then firstname || ' ' || lastname when type = 'PERSON' then firstname || ' ' || lastname
else name else name
end as nom, end as nom,
case case
when type = 'PERSON' then age(birthdate) when type = 'PERSON' then age(birthdate)
when type = 'COMPANY' then age(c.created_at) when type = 'COMPANY' then age(c.creation_date)
end as age end as age
from holder h from holder h
left join person p on p.id = h.id left join person p on p.id = h.id
left join company c on c.id = h.id; left join company c on c.id = h.id;
create or replace view account_detail as create or replace view account_detail as
select a.balance, select a.balance,
a.balance * ah.share as balance_currency, a.balance * ah.share as balance_currency,
a.balance * ah.share / latest_exchange_rate(a.currency_code, current_date), a.balance * ah.share / latest_exchange_rate(a.currency_code, current_date),
a.currency_code, a.currency_code,
@@ -309,16 +302,16 @@ join holder_detail hd on ah.holder_id = hd.id;
insert into transaction (amount) values (100); insert into transaction (amount) values (100);
insert into operation (transaction_id, account_id, amount, direction) insert into operation (transaction_id, account_id, amount, direction)
values (1, 4, 100, 'CREDIT'); values (1, 4, 100, 'CREDIT');
insert into operation (transaction_id, account_id, amount, direction) insert into operation (transaction_id, account_id, amount, direction)
values (1, 1, 100, 'DEBIT'); values (1, 1, 100, 'DEBIT');
insert into transaction (amount) values (10000); insert into transaction (amount) values (10000);
insert into operation (transaction_id, account_id, amount, direction) insert into operation (transaction_id, account_id, amount, direction)
values (2, 5, 10000, 'CREDIT'); values (2, 5, 10000, 'CREDIT');
insert into operation (transaction_id, account_id, amount, direction) insert into operation (transaction_id, account_id, amount, direction)
values (2, 3, 10000, 'DEBIT'); values (2, 3, 10000, 'DEBIT');

132
banque/banque.2.proc.bak Normal file
View File

@@ -0,0 +1,132 @@
set search_path TO bank;
SHOW locale_provider;
SHOW icu_locale;
SHOW lc_collate;
SHOW lc_ctype;
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_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 transfer(
p_source int,
p_destination int,
p_amount numeric,
p_date date
)
language plpgsql
as $$
declare
v_transaction_id int;
rate_source numeric;
rate_destination numeric;
begin
insert into transaction(amount, transaction_date)
values (p_amount, p_date)
returning id into v_transaction_id;
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;
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)
values (v_transaction_id, p_destination, p_amount / rate_source * rate_destination, 'CREDIT');
update account
set balance = balance - p_amount
where id = p_source;
update account
set balance = balance + round(p_amount / rate_source * rate_destination, 2)
WHERE id = p_destination;
end;
$$;

View File

@@ -1,244 +0,0 @@
set search_path TO bank;
create or replace procedure add_person (
p_firstname text,
p_lastname text,
p_birthdate date
)
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);
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,
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
)
language plpgsql
as $$
declare
v_holder_id bigint;
begin
insert into holder(type) values ('BANK')
returning id into v_holder_id;
insert into bank(id, name)
values (v_holder_id, name);
raise notice 'Titulaire créé : % = %',
v_holder_id, name;
end;
$$;
create or replace procedure add_company (
p_name text,
p_registration_number text,
p_created_at date
)
language plpgsql
as $$
declare
v_holder_id bigint;
begin
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éé : % = % %',
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;
$$;
create or replace procedure transfer(
p_source int,
p_destination int,
p_amount numeric,
p_date date
)
language plpgsql
as $$
declare
v_transaction_id int;
rate_source numeric;
rate_destination numeric;
begin
insert into transaction(amount, transaction_date)
values (p_amount, p_date)
returning id into v_transaction_id;
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;
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)
values (v_transaction_id, p_destination, p_amount / rate_source * rate_destination, 'CREDIT');
update account
set balance = balance - p_amount
where id = p_source;
update account
set balance = balance + round(p_amount / rate_source * rate_destination, 2)
WHERE id = p_destination;
end;
$$;

6878
banque/eurofxref-hist.csv Normal file

File diff suppressed because it is too large Load Diff