This commit is contained in:
2025-11-16 09:10:10 +01:00
parent 1bbbd16e03
commit 4f38a8793a
4 changed files with 104 additions and 16 deletions

25
banque.functions.md Normal file
View File

@@ -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;
$$;
```

View File

@@ -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 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) 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;
$$;
```

View File

@@ -88,3 +88,49 @@ $$;
```sql ```sql
call add_company('Boulangerie de Valorgue', 'FR19803269968', '2014-08-19'); 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;
$$;
```

27
banque.vues.md Normal file
View File

@@ -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;
```