transaction
This commit is contained in:
@@ -127,7 +127,7 @@ as $$
|
||||
declare
|
||||
v_account_id int;
|
||||
begin
|
||||
|
||||
|
||||
insert into account(currency_code)
|
||||
values (p_currency)
|
||||
returning id into v_account_id;
|
||||
@@ -194,3 +194,35 @@ begin
|
||||
v_account_id, array_length(p_holders, 1);
|
||||
end;
|
||||
$$;
|
||||
|
||||
|
||||
create procedure transfer(
|
||||
p_source int,
|
||||
p_destination int,
|
||||
p_amount numeric,
|
||||
p_date date
|
||||
)
|
||||
language plpgsql
|
||||
as $$
|
||||
declare
|
||||
v_transaction_id int;
|
||||
begin
|
||||
insert into transaction(amount, transaction_date)
|
||||
values (p_amount, p_date)
|
||||
returning id into v_transaction_id;
|
||||
|
||||
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, 'CREDIT');
|
||||
|
||||
update account
|
||||
set balance = balance - p_amount
|
||||
where id = p_source;
|
||||
|
||||
update account
|
||||
set balance = balance + p_amount
|
||||
WHERE id = p_destination;
|
||||
end;
|
||||
$$;
|
||||
|
||||
Reference in New Issue
Block a user