create or replace function words__words__trg()
returns trigger
language plpgsql
as
$$
begin
   delete from words__words where words__words.parent__id = new.recid;
   insert into words__words select * from words(new.recid, new.words, true);
   return new;
end;
$$;

drop trigger if exists words__words__upd ON words;

create trigger words__words__upd after
update of words
   on
   words for each row execute procedure words__words__trg();

drop trigger if exists words__words__ins ON words;

create trigger words__words__ins after
insert
   on
   words for each row execute procedure words__words__trg();

create or replace function ttw__f1_1__trg()
returns trigger
language plpgsql
as
$$
begin
   delete from ttw__f1_1 where ttw__f1_1.parent__id = new.recid and ttw__f1_1.list__index = 1;
   insert into ttw__f1_1 select * from words(new.recid, 1, new.f1_1, false);
   return new;
end;
$$;

drop trigger if exists ttw__f1_1__upd ON ttw;

create trigger ttw__f1_1__upd after
update of f1_1
   on
   ttw for each row execute procedure ttw__f1_1__trg();

create or replace function ttw__f1_2__trg()
returns trigger
language plpgsql
as
$$
begin
   delete from ttw__f1_1 where ttw__f1_1.parent__id = new.recid and ttw__f1_1.list__index = 2;
   insert into ttw__f1_1 select * from words(new.recid, 2, new.f1_2, false);
   return new;
end;
$$;

drop trigger if exists ttw__f1_2__upd ON ttw;

create trigger ttw__f1_2__upd after
update of f1_2
   on
   ttw for each row execute procedure ttw__f1_2__trg();

create or replace function ttw__f1_3__trg()
returns trigger
language plpgsql
as
$$
begin
   delete from ttw__f1_1 where ttw__f1_1.parent__id = new.recid and ttw__f1_1.list__index = 3;
   insert into ttw__f1_1 select * from words(new.recid, 3, new.f1_3, false);
   return new;
end;
$$;

drop trigger if exists ttw__f1_3__upd ON ttw;

create trigger ttw__f1_3__upd after
update of f1_3
   on
   ttw for each row execute procedure ttw__f1_3__trg();

create or replace function ttw__f1_4__trg()
returns trigger
language plpgsql
as
$$
begin
   delete from ttw__f1_1 where ttw__f1_1.parent__id = new.recid and ttw__f1_1.list__index = 4;
   insert into ttw__f1_1 select * from words(new.recid, 4, new.f1_4, false);
   return new;
end;
$$;

drop trigger if exists ttw__f1_4__upd ON ttw;

create trigger ttw__f1_4__upd after
update of f1_4
   on
   ttw for each row execute procedure ttw__f1_4__trg();

create or replace function ttw__f1_5__trg()
returns trigger
language plpgsql
as
$$
begin
   delete from ttw__f1_1 where ttw__f1_1.parent__id = new.recid and ttw__f1_1.list__index = 5;
   insert into ttw__f1_1 select * from words(new.recid, 5, new.f1_5, false);
   return new;
end;
$$;

drop trigger if exists ttw__f1_5__upd ON ttw;

create trigger ttw__f1_5__upd after
update of f1_5
   on
   ttw for each row execute procedure ttw__f1_5__trg();

drop trigger if exists ttw__f1_1__ins ON ttw;

create or replace function ttw__f1_1__trg()
returns trigger
language plpgsql
as
$$
begin
   delete from ttw__f1_1 where ttw__f1_1.parent__id = new.recid;
   insert into ttw__f1_1 select * from words(new.recid, 1, new.f1_1, false);
   insert into ttw__f1_1 select * from words(new.recid, 2, new.f1_2, false);
   insert into ttw__f1_1 select * from words(new.recid, 3, new.f1_3, false);
   insert into ttw__f1_1 select * from words(new.recid, 4, new.f1_4, false);
   insert into ttw__f1_1 select * from words(new.recid, 5, new.f1_5, false);
   return new;
end;
$$;

create trigger ttw__f1_1__ins after
insert
   on
   ttw for each row execute procedure ttw__f1_1__trg();

alter table words__words   drop constraint if exists pk__words__words;
alter table words__words   add constraint pk__words__words   primary key (parent__id, word);
drop index if exists idx__words__words;
create index idx__words__words on words__words (word);
alter table words__words   drop constraint if exists fk__words__words;
drop index if exists fkidx__words__words;
create index fkidx__words__words on words__words (parent__id);
alter table words__words
   add constraint fk__words__words
   foreign key (parent__id)
   references words(recid)
   on delete cascade;

alter table ttw__f1_1   drop constraint if exists pk__ttw__f1_1;
alter table ttw__f1_1   add constraint pk__ttw__f1_1   primary key (parent__id, list__index, word);
drop index if exists idx__ttw__f1_1;
create index idx__ttw__f1_1 on ttw__f1_1 (word);
alter table ttw__f1_1   drop constraint if exists fk__ttw__f1_1;
drop index if exists fkidx__ttw__f1_1;
create index fkidx__ttw__f1_1 on ttw__f1_1 (parent__id);
alter table ttw__f1_1
   add constraint fk__ttw__f1_1
   foreign key (parent__id)
   references ttw(recid)
   on delete cascade;

