/* ============================================================================
   SYSTEM_INFO – TRIGGER
   Voraussetzung: Tabelle [_meta].[TBDD_CFG_SYSTEM_INFO] und Prozeduren existieren
   ============================================================================ */

USE [DD_IIM];
GO

CREATE OR ALTER TRIGGER [_meta].[TBDD_CFG_SYSTEM_INFO_IO_INS]
ON [_meta].[TBDD_CFG_SYSTEM_INFO]
INSTEAD OF INSERT
AS
BEGIN
    SET NOCOUNT ON;

    IF TRY_CAST(SESSION_CONTEXT(N'_meta_allow_tbdd_system_info_dml') AS bit) = 1
    BEGIN
        IF TRY_CAST(SESSION_CONTEXT(N'_meta_tbdd_system_info_use_input_pk') AS bit) = 1
        BEGIN
            INSERT INTO [_meta].[TBDD_CFG_SYSTEM_INFO]
            (
                [PK_CFG_SYSTEM_INFO_ID],
                [SYSTEM_VERSION],
                [COMMENT],
                [CREATED_WHO],
                [CREATED_WHEN],
                [CHANGED_WHO],
                [CHANGED_WHEN]
            )
            SELECT
                i.[PK_CFG_SYSTEM_INFO_ID],
                i.[SYSTEM_VERSION],
                i.[COMMENT],
                i.[CREATED_WHO],
                i.[CREATED_WHEN],
                i.[CHANGED_WHO],
                i.[CHANGED_WHEN]
            FROM inserted i;
        END
        ELSE
        BEGIN
            INSERT INTO [_meta].[TBDD_CFG_SYSTEM_INFO]
            (
                [SYSTEM_VERSION],
                [COMMENT],
                [CREATED_WHO],
                [CREATED_WHEN],
                [CHANGED_WHO],
                [CHANGED_WHEN]
            )
            SELECT
                i.[SYSTEM_VERSION],
                i.[COMMENT],
                i.[CREATED_WHO],
                i.[CREATED_WHEN],
                i.[CHANGED_WHO],
                i.[CHANGED_WHEN]
            FROM inserted i;
        END;

        RETURN;
    END;

    THROW 51001, 'Direktes INSERT auf [_meta].[TBDD_CFG_SYSTEM_INFO] ist nicht erlaubt. Bitte Prozedur [_meta].[PRDD_UPSERT_SYSTEM_INFO] verwenden.', 1;
END;
GO

CREATE OR ALTER TRIGGER [_meta].[TBDD_CFG_SYSTEM_INFO_IO_UPD]
ON [_meta].[TBDD_CFG_SYSTEM_INFO]
INSTEAD OF UPDATE
AS
BEGIN
    SET NOCOUNT ON;

    IF TRY_CAST(SESSION_CONTEXT(N'_meta_allow_tbdd_system_info_dml') AS bit) = 1
    BEGIN
        UPDATE t
           SET t.[SYSTEM_VERSION] = i.[SYSTEM_VERSION],
               t.[COMMENT] = i.[COMMENT],
               t.[CREATED_WHO] = i.[CREATED_WHO],
               t.[CREATED_WHEN] = i.[CREATED_WHEN],
               t.[CHANGED_WHO] = i.[CHANGED_WHO],
               t.[CHANGED_WHEN] = i.[CHANGED_WHEN]
        FROM [_meta].[TBDD_CFG_SYSTEM_INFO] t
        JOIN inserted i
          ON i.[PK_CFG_SYSTEM_INFO_ID] = t.[PK_CFG_SYSTEM_INFO_ID];

        RETURN;
    END;

    THROW 51002, 'Direktes UPDATE auf [_meta].[TBDD_CFG_SYSTEM_INFO] ist nicht erlaubt. Bitte Prozedur [_meta].[PRDD_UPSERT_SYSTEM_INFO] verwenden.', 1;
END;
GO

CREATE OR ALTER TRIGGER [_meta].[TBDD_CFG_SYSTEM_INFO_IO_DEL]
ON [_meta].[TBDD_CFG_SYSTEM_INFO]
INSTEAD OF DELETE
AS
BEGIN
    SET NOCOUNT ON;

    IF TRY_CAST(SESSION_CONTEXT(N'_meta_allow_tbdd_system_info_dml') AS bit) = 1
    BEGIN
        DELETE t
        FROM [_meta].[TBDD_CFG_SYSTEM_INFO] t
        JOIN deleted d
          ON d.[PK_CFG_SYSTEM_INFO_ID] = t.[PK_CFG_SYSTEM_INFO_ID];

        RETURN;
    END;

    THROW 51003, 'Direktes DELETE auf [_meta].[TBDD_CFG_SYSTEM_INFO] ist nicht erlaubt. Bitte Prozedur [_meta].[PRDD_DELETE_SYSTEM_INFO] verwenden.', 1;
END;
GO
