-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2026, All rights reserved by Steffen Anders, Elsteraue - Germany
-- Visit also https://www.it-crafts.de.
--

IF 0 = (SELECT count(*) from [sysobjects] where ([xtype] = 'U' or [xtype] = 'V') and [name] = 'ut_Formeln')
BEGIN
CREATE TABLE [dbo].[ut_Formeln]
(
  [RecId]  integer NOT NULL
 ,[NUMMER] char(8) DEFAULT ''
 ,[AUSDRUCK] char(254) DEFAULT ''
 ,[BEZ] char(30) DEFAULT ''
  CONSTRAINT [Pk_Formeln]  PRIMARY KEY NONCLUSTERED ( [RecId] )
)
END
GO

-- drop obsolete triggers and the triggered table with keyvalues
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Formeln_KeyDel')
  DROP TRIGGER [dbo].[tr_Formeln_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Formeln_KeyUpd')
  DROP TRIGGER [dbo].[tr_Formeln_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Formeln_Keys')
  DROP TABLE [dbo].[ut_Formeln_Keys]
GO

-- add new fields
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'NUMMER')
BEGIN
  ALTER TABLE [dbo].[ut_Formeln] ADD [NUMMER] char(8) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'AUSDRUCK')
BEGIN
  ALTER TABLE [dbo].[ut_Formeln] ADD [AUSDRUCK] char(254) NOT NULL DEFAULT ''
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'BEZ')
BEGIN
  ALTER TABLE [dbo].[ut_Formeln] ADD [BEZ] char(30) NOT NULL DEFAULT ''
END
GO

-- check lengths of text fields
IF (8 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'NUMMER'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'NUMMER'))
BEGIN
   ALTER TABLE [dbo].[ut_Formeln] ALTER COLUMN [NUMMER] char(8)
END
GO
IF (8 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'NUMMER'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'NUMMER'))
BEGIN
   ALTER TABLE [dbo].[ut_Formeln] ALTER COLUMN [NUMMER] varchar(8)
END
GO
IF (254 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'AUSDRUCK'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'AUSDRUCK'))
BEGIN
   ALTER TABLE [dbo].[ut_Formeln] ALTER COLUMN [AUSDRUCK] char(254)
END
GO
IF (254 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'AUSDRUCK'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'AUSDRUCK'))
BEGIN
   ALTER TABLE [dbo].[ut_Formeln] ALTER COLUMN [AUSDRUCK] varchar(254)
END
GO
IF (30 > (select sc.length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'BEZ'))
  AND (175 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'BEZ'))
BEGIN
   ALTER TABLE [dbo].[ut_Formeln] ALTER COLUMN [BEZ] char(30)
END
GO
IF (30 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'BEZ'))
  AND (167 = (select sc.xtype from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Formeln' and sc.[name] = 'BEZ'))
BEGIN
   ALTER TABLE [dbo].[ut_Formeln] ALTER COLUMN [BEZ] varchar(30)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Formeln')
  DROP VIEW [dbo].[Formeln]
GO
CREATE VIEW [dbo].[Formeln] AS
  SELECT [RecId]
 ,[NUMMER]
 ,[AUSDRUCK]
 ,[BEZ]
  FROM [dbo].[ut_Formeln]
GO


-- create function to get the next RecId
IF OBJECT_ID (N'itcfunc_NextRecId_Formeln', N'FN') IS NOT NULL
  DROP FUNCTION dbo.itcfunc_NextRecId_Formeln
GO
CREATE FUNCTION dbo.itcfunc_NextRecId_Formeln() RETURNS integer
AS
BEGIN
  RETURN isnull((SELECT MAX(RecId) FROM [dbo].[ut_Formeln] WITH (READUNCOMMITTED)),0) + 1
END
GO

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Formeln')
  DROP PROCEDURE [dbo].[up_Sel_Formeln]
GO
CREATE PROCEDURE [dbo].[up_Sel_Formeln]  @mRI integer
AS
  SELECT
    [NUMMER]
   ,[AUSDRUCK]
   ,[BEZ]
  FROM [dbo].[ut_Formeln] WHERE RecId = @mRI
GO

-- select with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel2_Formeln')
  DROP PROCEDURE [dbo].[up_Sel2_Formeln]
GO
CREATE PROCEDURE [dbo].[up_Sel2_Formeln]  @Param_RecId varchar(20)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  SELECT * FROM [dbo].[Formeln] WHERE RecId = @mRI
GO

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Formeln')
  DROP PROCEDURE [dbo].[up_Append_Formeln]
GO
CREATE PROCEDURE [dbo].[up_Append_Formeln]
   @Param_NUMMER char(8)
  ,@Param_AUSDRUCK char(254)
  ,@Param_BEZ char(30)
AS
  DECLARE @NeueId integer
  DECLARE @m_CountDown integer = 10
  WHILE @m_CountDown > 0 BEGIN
    BEGIN TRANSACTION
    BEGIN TRY
      SET @NeueId = dbo.itcfunc_NextRecId_Formeln()
      INSERT INTO [dbo].[ut_Formeln] (RecId
        ,[NUMMER]
        ,[AUSDRUCK]
        ,[BEZ]
          ) VALUES (@NeueId
        ,UPPER(RTRIM(@Param_NUMMER))
        ,RTRIM(@Param_AUSDRUCK)
        ,RTRIM(@Param_BEZ)
          )
      COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
      ROLLBACK TRANSACTION
      SET @NeueId = 0
    END CATCH

    IF @NeueId <> 0
      SET @m_CountDown = 0
    ELSE
      SET @m_CountDown = @m_CountDown - 1

    IF @m_CountDown > 0
      WAITFOR DELAY '00:00:00.050'
  END -- WHILE
  SELECT @NeueId AS RecId
GO

-- insert with obsolete clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Add_Formeln')
  DROP PROCEDURE [dbo].[up_Add_Formeln]
GO
CREATE PROCEDURE [dbo].[up_Add_Formeln]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Formeln()
  INSERT INTO [dbo].[ut_Formeln] (RecId) VALUES( @NeueId )
  IF @@ERROR = 0
    SELECT @NeueId AS RecId
  ELSE
    SELECT 0 AS RecId
GO

-- update with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd_Formeln')
  DROP PROCEDURE [dbo].[up_Upd_Formeln]
GO
CREATE PROCEDURE [dbo].[up_Upd_Formeln] @mRI integer
  ,@Param_NUMMER char(8)
  ,@Param_AUSDRUCK char(254)
  ,@Param_BEZ char(30)
AS
  UPDATE [dbo].[ut_Formeln] SET
    [NUMMER] = UPPER(RTRIM(@Param_NUMMER))
   ,[AUSDRUCK] = RTRIM(@Param_AUSDRUCK)
   ,[BEZ] = RTRIM(@Param_BEZ)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Formeln')
  DROP PROCEDURE [dbo].[up_Upd2_Formeln]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Formeln] @Param_RecId varchar(20)
  ,@Param_NUMMER varchar(8)
  ,@Param_AUSDRUCK varchar(254)
  ,@Param_BEZ varchar(30)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Formeln] SET
    [NUMMER] = UPPER(RTRIM(@Param_NUMMER))
   ,[AUSDRUCK] = RTRIM(@Param_AUSDRUCK)
   ,[BEZ] = RTRIM(@Param_BEZ)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Formeln] @mRI
GO

-- delete
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Del_Formeln')
  DROP PROCEDURE [dbo].[up_Del_Formeln]
GO
CREATE PROCEDURE [dbo].[up_Del_Formeln] @mRI integer
AS
  DELETE FROM [dbo].[ut_Formeln] WHERE RecId = @mRI
GO

-- text index
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_TrSel_Formeln')
  DROP PROCEDURE [dbo].[up_TrSel_Formeln]
GO
CREATE PROCEDURE [dbo].[up_TrSel_Formeln]
AS
  SELECT RecId,
    LTRIM(RTRIM([NUMMER]))
   +LTRIM(RTRIM([AUSDRUCK]))
   +LTRIM(RTRIM([BEZ]))
  AS Data FROM [dbo].[ut_Formeln] WHERE RecId > 0 ORDER BY RecId
GO

-- reindex
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Formeln')
  DROP PROCEDURE [dbo].[up_Reorg_Formeln]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Formeln] @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Formeln] ORDER BY RecId
    DECLARE  @CurrId  integer
    DECLARE  @NextId  integer
    --BEGIN TRANSACTION
    OPEN C
    SET @NextId = 1
    FETCH NEXT FROM C INTO @CurrId
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
      IF @CurrId <> @NextId
      UPDATE [dbo].[ut_Formeln] SET RecId=@NextId WHERE CURRENT OF C
      SET @NextId = @NextId + 1
      FETCH NEXT FROM C INTO @CurrId
    END
    CLOSE C
    --COMMIT TRANSACTION
    DEALLOCATE C
  END

  IF (@bDbcc = 1)
  BEGIN
    DBCC DBREINDEX ( [ut_Formeln] )
  END
GO

-- logical key  ORDER BY NUMMER,RecId
IF 0 < (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Formeln' and so.[id] = si.[id] and si.[name] = 'Ik_Key0')
  DROP INDEX [Ik_Key0] ON [dbo].[ut_Formeln]
GO
IF 0 = (select count(*) from sysindexes si inner join sysobjects so on so.[xtype] = 'U' and so.[name] = 'ut_Formeln' and so.[id] = si.[id] and si.[name] = 'Ik_Key0')
  CREATE INDEX [Ik_Key0] ON [dbo].[ut_Formeln] (NUMMER,RecId)
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Formeln_Key0')
  DROP PROCEDURE [dbo].[up_First_Formeln_Key0]
GO
CREATE PROCEDURE [dbo].[up_First_Formeln_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Formeln] WITH(INDEX = Ik_Key0) ORDER BY NUMMER,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Formeln_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Formeln_Key0] AS EXECUTE [dbo].[up_First_Formeln_Key0]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Formeln_Key0')
  DROP PROCEDURE [dbo].[up_Last_Formeln_Key0]
GO
CREATE PROCEDURE [dbo].[up_Last_Formeln_Key0] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Formeln] WITH(INDEX = Ik_Key0) ORDER BY NUMMER desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Formeln_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Formeln_Key0] AS EXECUTE [dbo].[up_Last_Formeln_Key0]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Formeln_Key0')
  DROP PROCEDURE [dbo].[up_Next_Formeln_Key0]
GO
CREATE PROCEDURE [dbo].[up_Next_Formeln_Key0] @oldRecId integer AS
  DECLARE @oldNUMMER char(8)
  SELECT @oldNUMMER = NUMMER FROM [dbo].[ut_Formeln] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Formeln] WITH(INDEX = Ik_Key0)
    WHERE (NUMMER = @oldNUMMER) AND (RecId > @oldRecId)
    ORDER BY NUMMER,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Formeln] WITH(INDEX = Ik_Key0)
    WHERE (NUMMER > @oldNUMMER)
    ORDER BY NUMMER,RecId
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next_Formeln_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Formeln_Key0] @oldRecId integer AS EXECUTE [dbo].[up_Next_Formeln_Key0] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Formeln_Key0')
  DROP PROCEDURE [dbo].[up_Prior_Formeln_Key0]
GO
CREATE PROCEDURE [dbo].[up_Prior_Formeln_Key0] @oldRecId integer AS
  DECLARE @oldNUMMER char(8)
  SELECT @oldNUMMER = NUMMER FROM [dbo].[ut_Formeln] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Formeln] WITH(INDEX = Ik_Key0)
    WHERE (NUMMER = @oldNUMMER) AND (RecId < @oldRecId)
    ORDER BY NUMMER desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Formeln] WITH(INDEX = Ik_Key0)
    WHERE (NUMMER < @oldNUMMER)
    ORDER BY NUMMER desc,RecId desc
  IF @newRecId <> 0 BEGIN
    SELECT @newRecId AS RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior_Formeln_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Formeln_Key0] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Formeln_Key0] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next50_Formeln_Key0')
  DROP PROCEDURE [dbo].[up_Next50_Formeln_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Next50_Formeln_Key0')
  DROP PROCEDURE [dbo].[qp_Next50_Formeln_Key0]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior50_Formeln_Key0')
  DROP PROCEDURE [dbo].[up_Prior50_Formeln_Key0]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Prior50_Formeln_Key0')
  DROP PROCEDURE [dbo].[qp_Prior50_Formeln_Key0]
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindF_Formeln_Key0')
  DROP PROCEDURE [dbo].[up_FindF_Formeln_Key0]
GO
CREATE PROCEDURE [dbo].[up_FindF_Formeln_Key0] @xKey char(250), @xLen integer AS
  DECLARE @mNUMMER char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Formeln] WITH(INDEX = Ik_Key0) WHERE 
      NUMMER = @mNUMMER
      ORDER BY NUMMER,RecId
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Formeln] WITH(INDEX = Ik_Key0) WHERE 
      NUMMER >= @mNUMMER AND SUBSTRING(NUMMER,1,LEN(@mNUMMER)) = @mNUMMER
      ORDER BY NUMMER,RecId
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindF_Formeln_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindF_Formeln_Key0] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindF_Formeln_Key0] @xKey, @xLen')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_FindL_Formeln_Key0')
  DROP PROCEDURE [dbo].[up_FindL_Formeln_Key0]
GO
CREATE PROCEDURE [dbo].[up_FindL_Formeln_Key0] @xKey char(250), @xLen integer AS
  DECLARE @mNUMMER char(8) = dbo.itcfunc_KeyToken_varchar(@xKey,@xLen,1,8)

  -- pass 1 -- Abfrage mit vollständigen Feldern (absteigende Vergleichslänge)

  IF @xLen = 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Formeln] WITH(INDEX = Ik_Key0) WHERE 
      NUMMER = @mNUMMER
      ORDER BY NUMMER desc,RecId desc
    RETURN
  END

  -- pass 2 -- Abfrage mit teilweise unkompletten Felder (aufsteigende Vergleichslänge)

  IF @xLen < 8 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Formeln] WITH(INDEX = Ik_Key0) WHERE 
      SUBSTRING(NUMMER,1,LEN(@mNUMMER)) = @mNUMMER
      ORDER BY NUMMER desc,RecId desc
    RETURN
  END

  SELECT 0 AS RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_FindL_Formeln_Key0')
  EXEC('ALTER PROCEDURE [dbo].[qp_FindL_Formeln_Key0] @xKey char(250), @xLen integer AS EXECUTE [dbo].[up_FindL_Formeln_Key0] @xKey, @xLen')
GO


