/*ALIAS>l_data*/

/*SQLStart>1022*/
CREATE OR ALTER trigger original_uid for originals
active before insert position 0
as
begin
  if ((new.originals_originalid is null) or (new.originals_originalid = 0)) then
  begin
    new.originals_originalid = gen_id(org_id_gen, 1);
  end
end
/*SQLStop*/

/*SQLStart>1023*/
create or alter procedure GETVERDIRNUM
returns (
  VERSIONNUMBER integer,
  VERSIONDIR integer)
as
  declare variable NNUM integer;
  declare variable DIRNUM integer;
begin
  nnum= gen_id(ver_num_gen, 1);

  if(nnum > 1000) then
  begin
    nnum= -nnum;
    dirnum= 1;
  end
  else
  begin
    nnum= 0;
    dirnum= 0;
  end

  VERSIONNUMBER=gen_id(ver_num_gen, nnum);
  VERSIONDIR=gen_id(ver_dirnum_gen, dirnum);
  suspend;
end
/*SQLStop*/

/*SQLStart>1024*/
GRANT EXECUTE ON PROCEDURE GETVERDIRNUM TO PUBLIC;
/*SQLStop*/

/*SQLStart>1025*/
CREATE OR ALTER trigger version_cnt_event for versions
active before insert position 0
as
  declare variable nnum integer;
  declare variable dirnum integer;
begin
  if ((new.versions_versionnumber is not null) and
      (new.versions_dirnum is not null)) then
    exit;
  select * from getverdirnum
  into :nnum, :dirnum;

  new.versions_versionnumber= :nnum;
  new.versions_dirnum= :dirnum;
end
/*SQLStop*/

/*SQLStart>1026*/
delete from system_strings where SYS_ID = 0;
/*SQLStop*/

/*SQLStart>1027*/
insert into system_strings (SYS_ID,SYS_STR) values (0,'1.5.51.1');
/*SQLStop*/
