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