wiki:WikiStart/DB

Version 6 (modified by 203206, 2 months ago) ( diff )

trigger

Релациона шема

DLL код

create table account(
    id serial primary key,
    username varchar(255) not null,
    password varchar(255) not null,
    balance decimal(15, 2) not null 
);

create table "transaction"(
    id serial primary key,
    amount decimal(15, 2) not null,
    type varchar(50) not null,
    timestamp timestamp default current_timestamp,
    account_id int not null,
    foreign key (account_id) references account(id)
);

create table transfer(
    id serial primary key,
    s_id int not null,
    r_id int not null, 
    foreign key(s_id) references account(id),
    foreign key(r_id) references
    account(id)

);

create table deposit(
    id serial primary key,
    account_id int not null,
    amount decimal(15,2) not null,
    timestamp timestamp default current_timestamp,
    foreign key(account_id) references account(id)
);

create table withdraw(
    id serial primary key,
    account_id int not null,
    amount decimal(15,2) not null,
    timestamp timestamp default current_timestamp,
    foreign key(account_id) references account(id)
);

create table transferred_money(
    id serial primary key, 
    s_account_id int not null, 
    r_account_id int not null, 
    t_id int not null, 
    currency varchar(50) not null, 
    foreign key (s_account_id) references account(id),
    foreign key (r_account_id) references account(id),
    foreign key (t_id) references transaction(id)
);


Triggers

create or replace function after_transaction_trigger()
returns trigger as $$
begin

	if new.type = 'Deposit' then
		insert into Deposit(account_id,amount, timestamp)
		values (new.account_id, new.amount, new.timestamp);
	
	elsif new.type = 'Withdrawal' then
		insert into Withdraw(account_id,amount, timestamp)
		values (new.account_id, new.amount, new.timestamp);
	
	elsif new.type like '(MKD) Transfer Out to%' then
		insert into transferred_money(s_account_id, r_account_id, t_id, currency)
		select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1),
			new.id, 'MKD'; 

	elsif new.type like '(USD) Transfer Out to%' then
		insert into transferred_money(s_account_id, r_account_id, t_id, currency)
		select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1),
			new.id, 'USD';
	
	elsif new.type like '(EUR) Transfer Out to%' then
		insert into transferred_money(s_account_id, r_account_id, t_id, currency)
		select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1),
			new.id, 'EUR';
			
	elsif new.type like '(GBP) Transfer Out to%' then
		insert into transferred_money(s_account_id, r_account_id, t_id, currency)
		select new.account_id, (select id from account where username = trim(substring(new.type from position('Transfer Out to ' in new.type)+15)) limit 1),
			new.id, 'GBP';

	end if;
	return new;
end;
$$ language plpgsql;

create trigger after_transaction
after insert on transaction
for each row
execute function after_transaction_trigger()

Note: See TracWiki for help on using the wiki.