use AnP

if object_id(N'dbo.groups_tables_remove', N'P') is not null drop procedure dbo.groups_tables_remove
go
create procedure dbo.groups_tables_remove as begin set nocount on 

    if object_id(N'dbo._GroupsUsers', N'U') is not null drop table dbo._GroupsUsers
    if object_id(N'dbo._GroupsIps', N'U') is not null drop table dbo._GroupsIps
    if object_id(N'dbo._Groups', N'U') is not null drop table dbo._Groups

    if object_id(N'dbo.GroupsUsers', N'U') is not null drop table dbo.GroupsUsers
    if object_id(N'dbo.GroupsIps', N'U') is not null drop table dbo.GroupsIps
    if object_id(N'dbo.Groups', N'U') is not null drop table dbo.Groups
    
end
go

if object_id(N'dbo.groups_tables_create', N'P') is not null drop procedure dbo.groups_tables_create
go
create procedure dbo.groups_tables_create as begin set nocount on 

    if object_id(N'dbo.Groups', N'U') is null create table dbo.Groups(
        id integer not null identity(1, 1), 
        [procedure] integer not null, 
        [session] integer not null, 
        [name] varchar(64) not null, 
        [owner] bit not null constraint df_groups_owner default 0,
        [description] varchar(512), 
        date_in datetime not null constraint df_groups_date_in default getdate(), 
        date_out datetime, 
        constraint pk_groups primary key clustered(id), 
        constraint fk_groups_procedure foreign key([procedure]) references dbo.Procedures(id) 
            on update no action 
            on delete no action,
        constraint fk_groups_session foreign key([session]) references dbo.Sessions(id) 
            on update no action 
            on delete no action,
        constraint uq_groups_name unique([name]) with(fillfactor = 90), 
        constraint ck_groups_name check([name] != '' and [name] not like '%[^a-zA-Z0-9_:.]%')
    )

    if object_id(N'dbo.GroupsUsers', N'U') is null create table dbo.GroupsUsers(
        id integer not null identity(1, 1), 
        [procedure] integer not null, 
        [session] integer not null, 
        [group] integer not null, 
        [user] integer not null,
        belongs bit not null constraint df_groups_users_belongs default 1,
        date_in datetime not null constraint df_groups_users_date_in default getdate(), 
        date_out datetime, 
        constraint pk_groups_users primary key clustered(id), 
        constraint fk_groups_users_procedure foreign key([procedure]) references dbo.Procedures(id) 
            on update no action 
            on delete no action,
        constraint fk_groups_users_session foreign key([session]) references dbo.Sessions(id) 
            on update no action 
            on delete no action, 
        constraint fk_groups_users_group foreign key([group]) references dbo.Groups(id) 
            on update no action 
            on delete no action,
        constraint fk_groups_users_user foreign key([user]) references dbo.Users(id) 
            on update no action
            on delete no action, 
        constraint uq_groups_users unique([group], [user]) with(fillfactor = 90)
    )

    if object_id(N'dbo.GroupsIps', N'U') is null create table dbo.GroupsIps(
        id integer not null identity(1, 1), 
        [procedure] integer not null, 
        [session] integer not null, 
        [group] integer not null, 
        [ip] integer not null,
        belongs bit not null constraint df_groups_ips_belongs default 1,
        date_in datetime not null constraint df_groups_ips_date_in default getdate(), 
        date_out datetime, 
        constraint pk_groups_ips primary key clustered(id), 
        constraint fk_groups_ips_procedure foreign key([procedure]) references dbo.Procedures(id) 
            on update no action 
            on delete no action,
        constraint fk_groups_ips_session foreign key([session]) references dbo.Sessions(id) 
            on update no action 
            on delete no action, 
        constraint fk_groups_ips_group foreign key([group]) references dbo.Groups(id) 
            on update no action 
            on delete no action,
        constraint fk_groups_ips_ip foreign key([ip]) references dbo.Ips(id) 
            on update no action
            on delete no action, 
        constraint uq_groups_ips unique([group], [ip]) with(fillfactor = 90)
    )

    if object_id(N'dbo._Groups', N'U') is null create table dbo._Groups(
        id integer not null identity(1, 1), 
        history_mode integer not null, 
        entity integer not null, 
        [procedure] integer not null, 
        [session] integer not null, 
        [name] varchar(64) not null, 
        [owner] bit not null constraint df__groups_owner default 0,
        [description] varchar(512), 
        date_in datetime not null, 
        date_out datetime, 
        history_date datetime2 not null constraint df__groups_history_date default sysutcdatetime(), 
        constraint pk__groups primary key clustered(id), 
        constraint fk__groups_history_mode foreign key(history_mode) references dbo.HistoryModes(id) 
            on update no action 
            on delete no action,
        constraint fk__groups_procedure foreign key([procedure]) references dbo.Procedures(id) 
            on update no action 
            on delete no action,
        constraint fk__groups_session foreign key([session]) references dbo.Sessions(id) 
            on update no action 
            on delete no action
    )

    if object_id(N'dbo._GroupsUsers', N'U') is null create table dbo._GroupsUsers(
        id integer not null identity(1, 1), 
        history_mode integer not null,
        entity integer not null, 
        [procedure] integer not null, 
        [session] integer not null, 
        [group] integer not null, 
        [user] integer not null,
        belongs bit not null,
        date_in datetime not null, 
        date_out datetime, 
        history_date datetime2 not null constraint df__groups_users_history_date default sysutcdatetime(),
        constraint pk__groups_users primary key clustered(id), 
        constraint fk__groups_users_history_mode foreign key(history_mode) references dbo.HistoryModes(id) 
            on update no action 
            on delete no action,
        constraint fk__groups_users_procedure foreign key([procedure]) references dbo.Procedures(id) 
            on update no action 
            on delete no action,
        constraint fk__groups_users_session foreign key([session]) references dbo.Sessions(id) 
            on update no action 
            on delete no action, 
        constraint fk__groups_users_group foreign key([group]) references dbo.Groups(id) 
            on update no action 
            on delete no action,
        constraint fk__groups_users_user foreign key([user]) references dbo.Users(id) 
            on update no action
            on delete no action
    )

    if object_id(N'dbo._GroupsIps', N'U') is null create table dbo._GroupsIps(
        id integer not null identity(1, 1), 
        history_mode integer not null, 
        entity integer not null, 
        [procedure] integer not null, 
        [session] integer not null, 
        [group] integer not null, 
        [ip] integer not null,
        belongs bit not null,
        date_in datetime not null, 
        date_out datetime, 
        history_date datetime2 not null constraint df__groups_ips_history_date default sysutcdatetime(),
        constraint pk__groups_ips primary key clustered(id), 
        constraint fk__groups_ips_history_mode foreign key(history_mode) references dbo.HistoryModes(id) 
            on update no action 
            on delete no action,
        constraint fk__groups_ips_procedure foreign key([procedure]) references dbo.Procedures(id) 
            on update no action 
            on delete no action,
        constraint fk__groups_ips_session foreign key([session]) references dbo.Sessions(id) 
            on update no action 
            on delete no action, 
        constraint fk__groups_ips_group foreign key([group]) references dbo.Groups(id) 
            on update no action 
            on delete no action,
        constraint fk__groups_ips_ip foreign key([ip]) references dbo.Ips(id) 
            on update no action
            on delete no action
    )

end
go

execute dbo.groups_tables_create

if object_id(N'dbo.groups_trigger', N'TR') is not null drop trigger dbo.groups_trigger
go
create trigger dbo.groups_trigger on dbo.Groups for insert, update, delete as begin set nocount on 

    declare @insert integer, @update integer, @delete integer, @unknown integer

    execute dbo.history_mode_get @insert output, @update output, @delete output, @unknown output

    -- update.
    insert into dbo._Groups(history_mode, entity, [procedure], [session], [name], [owner], [description], date_in, date_out) 
    select @update, inserted.id, inserted.[procedure], inserted.[session], inserted.[name], inserted.[owner], inserted.[description], inserted.date_in, inserted.date_out 
    from inserted inserted 
    join deleted deleted on inserted.id = deleted.id 
    where 
        inserted.[procedure] != deleted.[procedure] or 
        inserted.[session] != deleted.[session] or 
        inserted.[name] != deleted.[name] or 
        inserted.[owner] != deleted.[owner] or 
        inserted.[description] != deleted.[description] or 
        inserted.date_in != deleted.date_in or 
        isnull(inserted.date_out, '0001-01-01') != isnull(deleted.date_out, '0001-01-01')

    -- Insert.
    insert into dbo._Groups(history_mode, entity, [procedure], [session], [name], [owner], [description], date_in, date_out) 
    select @insert, inserted.id, inserted.[procedure], inserted.[session], inserted.[name], inserted.[owner], inserted.[description], inserted.date_in, inserted.date_out 
    from inserted inserted 
    left join deleted deleted on inserted.id = deleted.id 
    where deleted.id is null

    -- deleted.
    insert into dbo._Groups(history_mode, entity, [procedure], [session], [name], [owner], [description], date_in, date_out) 
    select @delete, deleted.id, deleted.[procedure], deleted.[session], deleted.[name], deleted.[owner], deleted.[description], deleted.date_in, deleted.date_out 
    from deleted deleted 
    left join inserted inserted on deleted.id = inserted.id 
    where inserted.id is null

end
go

if object_id(N'dbo.groups_users_trigger', N'TR') is not null drop trigger dbo.groups_users_trigger
go
create trigger dbo.groups_users_trigger on dbo.GroupsUsers for insert, update, delete as begin set nocount on 

    declare @insert integer, @update integer, @delete integer, @unknown integer

    execute dbo.history_mode_get @insert output, @update output, @delete output, @unknown output

    -- update.
    insert into dbo._GroupsUsers(history_mode, entity, [procedure], [session], [group], [user], belongs, date_in, date_out) 
    select @update, inserted.id, inserted.[procedure], inserted.[session], inserted.[group], inserted.[user], inserted.belongs, inserted.date_in, inserted.date_out 
    from inserted inserted 
    join deleted deleted on inserted.id = deleted.id 
    where 
        inserted.[procedure] != deleted.[procedure] or 
        inserted.[session] != deleted.[session] or 
        inserted.[group] != deleted.[group] or 
        inserted.[user] != deleted.[user] or 
        inserted.belongs != deleted.belongs or 
        inserted.date_in != deleted.date_in or 
        isnull(inserted.date_out, '0001-01-01') != isnull(deleted.date_out, '0001-01-01')

    -- Insert.
    insert into dbo._GroupsUsers(history_mode, entity, [procedure], [session], [group], [user], belongs, date_in, date_out) 
    select @insert, inserted.id, inserted.[procedure], inserted.[session], inserted.[group], inserted.[user], inserted.belongs, inserted.date_in, inserted.date_out 
    from inserted inserted 
    left join deleted deleted on inserted.id = deleted.id 
    where deleted.id is null

    -- deleted.
    insert into dbo._GroupsUsers(history_mode, entity, [procedure], [session], [group], [user], belongs, date_in, date_out) 
    select @delete, deleted.id, deleted.[procedure], deleted.[session], deleted.[group], deleted.[user], deleted.belongs, deleted.date_in, deleted.date_out 
    from deleted deleted 
    left join inserted inserted on deleted.id = inserted.id 
    where inserted.id is null

end
go

if object_id(N'dbo.groups_ips_trigger', N'TR') is not null drop trigger dbo.groups_ips_trigger
go
create trigger dbo.groups_ips_trigger on dbo.GroupsIps for insert, update, delete as begin set nocount on 

    declare @insert integer, @update integer, @delete integer, @unknown integer

    execute dbo.history_mode_get @insert output, @update output, @delete output, @unknown output

    -- update.
    insert into dbo._GroupsIps(history_mode, entity, [procedure], [session], [group], [ip], belongs, date_in, date_out) 
    select @update, inserted.id, inserted.[procedure], inserted.[session], inserted.[group], inserted.[ip], inserted.belongs, inserted.date_in, inserted.date_out 
    from inserted inserted 
    join deleted deleted on inserted.id = deleted.id 
    where 
        inserted.[procedure] != deleted.[procedure] or 
        inserted.[session] != deleted.[session] or 
        inserted.[group] != deleted.[group] or 
        inserted.[ip] != deleted.[ip] or 
        inserted.belongs != deleted.belongs or 
        inserted.date_in != deleted.date_in or 
        isnull(inserted.date_out, '0001-01-01') != isnull(deleted.date_out, '0001-01-01')

    -- Insert.
    insert into dbo._GroupsIps(history_mode, entity, [procedure], [session], [group], [ip], belongs, date_in, date_out) 
    select @insert, inserted.id, inserted.[procedure], inserted.[session], inserted.[group], inserted.[ip], inserted.belongs, inserted.date_in, inserted.date_out 
    from inserted inserted 
    left join deleted deleted on inserted.id = deleted.id 
    where deleted.id is null

    -- deleted.
    insert into dbo._GroupsIps(history_mode, entity, [procedure], [session], [group], [ip], belongs, date_in, date_out) 
    select @delete, deleted.id, deleted.[procedure], deleted.[session], deleted.[group], deleted.[ip], deleted.belongs, deleted.date_in, deleted.date_out 
    from deleted deleted 
    left join inserted inserted on deleted.id = inserted.id 
    where inserted.id is null

end
go