From 4f38a8793a7876afa7f52a3f1f22e8240de5e88c Mon Sep 17 00:00:00 2001 From: medina5 Date: Sun, 16 Nov 2025 09:10:10 +0100 Subject: [PATCH] vues --- banque.functions.md | 25 ++++++++++++++++++++++++ banque.md | 22 ++++++--------------- banque.procedures.md | 46 ++++++++++++++++++++++++++++++++++++++++++++ banque.vues.md | 27 ++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 16 deletions(-) create mode 100644 banque.functions.md create mode 100644 banque.vues.md diff --git a/banque.functions.md b/banque.functions.md new file mode 100644 index 0000000..775e848 --- /dev/null +++ b/banque.functions.md @@ -0,0 +1,25 @@ +# Fonctions + +```sql +insert into exchange_rate values + ('EUR', '1999-01-04', 1), + ('USD', '1999-01-04', 1.1789), + ('YEN', '1999-01-04', 133.73); +``` + +```sql +create or replace function latest_exchange_rate ( + p_code text, + p_date date +) +returns decimal +language sql +as $$ +select rate + from exchange_rate + where currency_code = p_code + and date < p_date + order by date desc + limit 1; +$$; +``` diff --git a/banque.md b/banque.md index 5d71334..09cc7a7 100644 --- a/banque.md +++ b/banque.md @@ -24,9 +24,13 @@ Pour les entités vous utiliserez le singuler et écrirez le tout en minuscule. - Séance 1 : [Le schéma Entités-Relations](banque.erd.md) -- Séance 2 : [Implémentation du modèle](banques.tables.md) +- Séance 2 : + - [Implémentation du modèle](banques.tables.md) + - [Les procédures](banques.procedures.md) -- Séance 3 : [Les procédures](banques.procedures.md) +- Séance 3 : + - [Les vues](banques.vues.md) + - [Les fonctions](banques.functions.md) Voir les adresses des serveurs [postgreSQL](https://sources.neotech.fr/Universite/tp/src/branch/main/geii3_2025.md) @@ -216,21 +220,7 @@ insert into exchange_rate values ``` -#### Dépôts et retraits -```sql -create or replace procedure add_retrait ( - p_account_id bigint, - p_amount decimal -) -language plpgsql -as $$ -declare - v_id bigint -begin -end; -$$; -``` diff --git a/banque.procedures.md b/banque.procedures.md index f849f0e..f64287d 100644 --- a/banque.procedures.md +++ b/banque.procedures.md @@ -88,3 +88,49 @@ $$; ```sql call add_company('Boulangerie de Valorgue', 'FR19803269968', '2014-08-19'); ``` + +## d et r + +```sql +create or replace procedure add_depot ( + p_account_id bigint, + p_amount decimal +) +language plpgsql +as $$ +declare + v_id bigint; +begin + insert into transaction (amount) + values (p_amount) + returning id into v_id; + + insert into operation (transaction_id, account_id, amount, direction) + values (v_id, p_account_id, p_amount, 'CREDIT'); + + raise notice 'Dépôt de % sur le compte %', p_amount, p_account_id; +end; +$$; +``` + +```sql +create or replace procedure add_retrait ( + p_account_id bigint, + p_amount decimal +) +language plpgsql +as $$ +declare + v_id bigint; +begin + insert into transaction (amount) + values (p_amount) + returning id into v_id; + + insert into operation (transaction_id, account_id, amount, direction) + values (v_id, p_account_id, p_amount, 'DEBIT'); + + raise notice 'Retrait de % sur le compte %', p_amount, p_account_id; +end; +$$; +``` diff --git a/banque.vues.md b/banque.vues.md new file mode 100644 index 0000000..a262c98 --- /dev/null +++ b/banque.vues.md @@ -0,0 +1,27 @@ +```sql +create or replace view holder_detail as +select h.id, h.type, + case + when type = 'PERSON' then firstname || ' ' || lastname + else name + end as nom, + case + when type = 'PERSON' then age(birthdate) + when type = 'COMPANY' then age(c.created_at) + end as age +from holder h + left join person p on p.id = h.id + left join company c on c.id = h.id; +``` + +```sql +create or replace view account_detail as +select a.balance, + a.balance * ah.share as balance_currency, + a.balance * ah.share / latest_exchange_rate(a.currency_code, current_date), + a.currency_code, + hd.nom +from account a +join account_holder ah on ah.account_id = a.id +join holder_detail hd on ah.holder_id = hd.id; +```