use DATABASE

-- [[sql_server_entity_historical_creator]] --

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

    if object_id(N'dbo._Pascal') is not null drop table dbo._Pascal
    if object_id(N'dbo.Pascal') is not null drop table dbo.Pascal

end
go

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

    if object_id(N'dbo.Pascal', N'U') is null create table dbo.Pascal(
        id integer not null identity(1, 1), 
        [procedure] integer not null, 
        [session] integer not null, -- attributes_definition --
        date_in datetime not null constraint df_snake_date_in default getdate(),
        date_out datetime, 
        constraint pk_snake primary key clustered(id), 
        constraint fk_snake_procedure foreign key([procedure]) references AnPDB.dbo.Procedures(id)
            on update no action 
            on delete no action, 
        constraint fk_snake_session foreign key([session]) references AnPDB.dbo.Sessions(id) 
            on update no action 
            on delete no action -- contrains_definition --
    )

    if object_id(N'dbo._Pascal', N'U') is null create table dbo._Pascal(
        id integer not null identity(1, 1), 
        entity integer not null, 
        history_mode integer not null, 
        [procedure] integer not null, 
        [session] integer not null, -- attributes_history_definition --
        date_in datetime not null,
        date_out datetime, 
        history_date datetime2 not null constraint df__snake_history_date default sysutcdatetime(),
        constraint pk__snake primary key clustered(id), 
        constraint fk__snake_history_mode foreign key(history_mode) references HistoryModes.dbo.HistoryModes(id)
            on update no action 
            on delete no action, 
        constraint fk__snake_procedure foreign key([procedure]) references AnPDB.dbo.Procedures(id)
            on update no action 
            on delete no action, 
        constraint fk__snake_session foreign key([session]) references AnPDB.dbo.Sessions(id) 
            on update no action 
            on delete no action -- contrains_history_definition --
    )

end
go

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

end
go

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

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

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

    -- update.
    insert into dbo._Pascal(history_mode, entity, [procedure], [session], /* attributes */, date_in, date_out) 
    select @update, inserted.id, inserted.[procedure], inserted.[session], /* attributes_insert */, 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 -- attributes_where_diferent --
        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._Pascal(history_mode, entity, [procedure], [session], /* attributes */, date_in, date_out) 
    select @insert, inserted.id, inserted.[procedure], inserted.[session], /* attributes_insert */, 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._Pascal(history_mode, entity, [procedure], [session], /* attributes */, date_in, date_out) 
    select @delete, deleted.id, deleted.[procedure], deleted.[session], /* attributes_delete */, 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

-- [[sql_server_entity_historical_creator]] --