-- This file is part of the it-crafts software and is subject to copyright protection.
-- (C) 2022..2023, 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_Nummernkreise')
BEGIN
CREATE TABLE [dbo].[ut_Nummernkreise]
(
  [RecId]  integer NOT NULL
 ,[Nr] integer DEFAULT 0
 ,[LetzteNr] char(20) DEFAULT ''
 ,[Laenge] integer DEFAULT 0
 ,[Numerisch] tinyint DEFAULT 0
 ,[Eindeutig] tinyint DEFAULT 0
 ,[Beschr] char(30) DEFAULT ''
  CONSTRAINT [Pk_Nummernkreise]  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_Nummernkreise_KeyDel')
  DROP TRIGGER [dbo].[tr_Nummernkreise_KeyDel]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'TR'   and [name] = 'tr_Nummernkreise_KeyUpd')
  DROP TRIGGER [dbo].[tr_Nummernkreise_KeyUpd]
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'U' and [name] = 'ut_Nummernkreise_Keys')
  DROP TABLE [dbo].[ut_Nummernkreise_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_Nummernkreise' and sc.[name] = 'Nr')
BEGIN
  ALTER TABLE [dbo].[ut_Nummernkreise] ADD [Nr] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Nummernkreise' and sc.[name] = 'LetzteNr')
BEGIN
  ALTER TABLE [dbo].[ut_Nummernkreise] ADD [LetzteNr] char(20) 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_Nummernkreise' and sc.[name] = 'Laenge')
BEGIN
  ALTER TABLE [dbo].[ut_Nummernkreise] ADD [Laenge] integer NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Nummernkreise' and sc.[name] = 'Numerisch')
BEGIN
  ALTER TABLE [dbo].[ut_Nummernkreise] ADD [Numerisch] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Nummernkreise' and sc.[name] = 'Eindeutig')
BEGIN
  ALTER TABLE [dbo].[ut_Nummernkreise] ADD [Eindeutig] tinyint NOT NULL DEFAULT 0
END
GO
IF 0 = (select count(*) from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Nummernkreise' and sc.[name] = 'Beschr')
BEGIN
  ALTER TABLE [dbo].[ut_Nummernkreise] ADD [Beschr] char(30) NOT NULL DEFAULT ''
END
GO

-- check lengths of fields
IF 20 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Nummernkreise' and sc.[name] = 'LetzteNr')
BEGIN
   ALTER TABLE [dbo].[ut_Nummernkreise] ALTER COLUMN [LetzteNr] char(20)
END
GO
IF 30 > (select length from syscolumns sc inner join sysobjects so on sc.[id] = so.[id] and so.[name] = 'ut_Nummernkreise' and sc.[name] = 'Beschr')
BEGIN
   ALTER TABLE [dbo].[ut_Nummernkreise] ALTER COLUMN [Beschr] char(30)
END
GO

-- create dedicated view
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'V' and [name] = 'Nummernkreise')
  DROP VIEW [dbo].[Nummernkreise]
GO
CREATE VIEW [dbo].[Nummernkreise] AS
  SELECT [RecId]
 ,[Nr]
 ,[LetzteNr]
 ,[Laenge]
 ,[Numerisch]
 ,[Eindeutig]
 ,[Beschr]
  FROM [dbo].[ut_Nummernkreise]
GO


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

-- select with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Sel_Nummernkreise')
  DROP PROCEDURE [dbo].[up_Sel_Nummernkreise]
GO
CREATE PROCEDURE [dbo].[up_Sel_Nummernkreise]  @mRI integer
AS
  SELECT
    [Nr]
   ,[LetzteNr]
   ,[Laenge]
   ,[Numerisch]
   ,[Eindeutig]
   ,[Beschr]
  FROM [dbo].[ut_Nummernkreise] WHERE RecId = @mRI
GO

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

-- insert with classic client
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Append_Nummernkreise')
  DROP PROCEDURE [dbo].[up_Append_Nummernkreise]
GO
CREATE PROCEDURE [dbo].[up_Append_Nummernkreise]
   @Param_Nr integer
  ,@Param_LetzteNr char(20)
  ,@Param_Laenge integer
  ,@Param_Numerisch tinyint
  ,@Param_Eindeutig tinyint
  ,@Param_Beschr 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_Nummernkreise()
      INSERT INTO [dbo].[ut_Nummernkreise] (RecId
        ,[Nr]
        ,[LetzteNr]
        ,[Laenge]
        ,[Numerisch]
        ,[Eindeutig]
        ,[Beschr]
          ) VALUES (@NeueId
        ,@Param_Nr
        ,UPPER(RTRIM(@Param_LetzteNr))
        ,@Param_Laenge
        ,@Param_Numerisch
        ,@Param_Eindeutig
        ,RTRIM(@Param_Beschr)
          )
      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_Nummernkreise')
  DROP PROCEDURE [dbo].[up_Add_Nummernkreise]
GO
CREATE PROCEDURE [dbo].[up_Add_Nummernkreise]
AS
  DECLARE @NeueId integer = dbo.itcfunc_NextRecId_Nummernkreise()
  INSERT INTO [dbo].[ut_Nummernkreise] (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_Nummernkreise')
  DROP PROCEDURE [dbo].[up_Upd_Nummernkreise]
GO
CREATE PROCEDURE [dbo].[up_Upd_Nummernkreise] @mRI integer
  ,@Param_Nr integer
  ,@Param_LetzteNr char(20)
  ,@Param_Laenge integer
  ,@Param_Numerisch tinyint
  ,@Param_Eindeutig tinyint
  ,@Param_Beschr char(30)
AS
  UPDATE [dbo].[ut_Nummernkreise] SET
    [Nr] = @Param_Nr
   ,[LetzteNr] = UPPER(RTRIM(@Param_LetzteNr))
   ,[Laenge] = @Param_Laenge
   ,[Numerisch] = @Param_Numerisch
   ,[Eindeutig] = @Param_Eindeutig
   ,[Beschr] = RTRIM(@Param_Beschr)
  WHERE RecId = @mRI
GO

-- update with modern clients
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Upd2_Nummernkreise')
  DROP PROCEDURE [dbo].[up_Upd2_Nummernkreise]
GO
CREATE PROCEDURE [dbo].[up_Upd2_Nummernkreise] @Param_RecId varchar(20)
  ,@Param_Nr varchar(2)
  ,@Param_LetzteNr varchar(20)
  ,@Param_Laenge varchar(2)
  ,@Param_Numerisch varchar(1)
  ,@Param_Eindeutig varchar(1)
  ,@Param_Beschr varchar(30)
AS
  DECLARE @mRI integer = dbo.itcfunc_integer(@Param_RecId)
  UPDATE [dbo].[ut_Nummernkreise] SET
    [Nr] = dbo.itcfunc_integer(@Param_Nr)
   ,[LetzteNr] = UPPER(RTRIM(@Param_LetzteNr))
   ,[Laenge] = dbo.itcfunc_integer(@Param_Laenge)
   ,[Numerisch] = dbo.itcfunc_integer(@Param_Numerisch)
   ,[Eindeutig] = dbo.itcfunc_integer(@Param_Eindeutig)
   ,[Beschr] = RTRIM(@Param_Beschr)
  WHERE RecId = @mRI

  EXECUTE [dbo].[up_Sel2_Nummernkreise] @mRI
GO

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

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

-- reindex
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Reorg_Nummernkreise')
  DROP PROCEDURE [dbo].[up_Reorg_Nummernkreise]
GO
CREATE PROCEDURE [dbo].[up_Reorg_Nummernkreise] @bDbcc integer,@bRecId integer
AS
  IF (@bRecId = 1)
  BEGIN
    DECLARE  C        CURSOR FOR SELECT RecId FROM [dbo].[ut_Nummernkreise] 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_Nummernkreise] 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_Nummernkreise] )
  END
GO

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

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_First_Nummernkreise_Key00')
  DROP PROCEDURE [dbo].[up_First_Nummernkreise_Key00]
GO
CREATE PROCEDURE [dbo].[up_First_Nummernkreise_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Nummernkreise] WITH(INDEX = Ik_Key00) ORDER BY Nr,RecId
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_First_Nummernkreise_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_First_Nummernkreise_Key00] AS EXECUTE [dbo].[up_First_Nummernkreise_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Last_Nummernkreise_Key00')
  DROP PROCEDURE [dbo].[up_Last_Nummernkreise_Key00]
GO
CREATE PROCEDURE [dbo].[up_Last_Nummernkreise_Key00] AS
  SELECT TOP 1 RecId FROM [dbo].[ut_Nummernkreise] WITH(INDEX = Ik_Key00) ORDER BY Nr desc,RecId desc
GO
IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'qp_Last_Nummernkreise_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Last_Nummernkreise_Key00] AS EXECUTE [dbo].[up_Last_Nummernkreise_Key00]')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Next_Nummernkreise_Key00')
  DROP PROCEDURE [dbo].[up_Next_Nummernkreise_Key00]
GO
CREATE PROCEDURE [dbo].[up_Next_Nummernkreise_Key00] @oldRecId integer AS
  DECLARE @oldNr integer
  SELECT @oldNr = Nr FROM [dbo].[ut_Nummernkreise] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Nummernkreise] WITH(INDEX = Ik_Key00)
    WHERE (Nr > @oldNr)
    ORDER BY Nr,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_Nummernkreise_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Next_Nummernkreise_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Next_Nummernkreise_Key00] @oldRecId')
GO

IF 0 < (SELECT count(*) from [sysobjects] where [xtype] = 'P'   and [name] = 'up_Prior_Nummernkreise_Key00')
  DROP PROCEDURE [dbo].[up_Prior_Nummernkreise_Key00]
GO
CREATE PROCEDURE [dbo].[up_Prior_Nummernkreise_Key00] @oldRecId integer AS
  DECLARE @oldNr integer
  SELECT @oldNr = Nr FROM [dbo].[ut_Nummernkreise] WHERE RecId = @oldRecId

  DECLARE @newRecId integer

  SELECT TOP 1 @newRecId = ISNULL(RecId,0) FROM [dbo].[ut_Nummernkreise] WITH(INDEX = Ik_Key00)
    WHERE (Nr < @oldNr)
    ORDER BY Nr 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_Nummernkreise_Key00')
  EXEC('ALTER PROCEDURE [dbo].[qp_Prior_Nummernkreise_Key00] @oldRecId integer AS EXECUTE [dbo].[up_Prior_Nummernkreise_Key00] @oldRecId')
GO

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

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

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

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

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Nummernkreise] WITH(INDEX = Ik_Key00) WHERE 
      Nr = @mNr
      ORDER BY Nr,RecId
    RETURN
  END

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


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

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

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

  IF @xLen = 2 BEGIN
    SELECT TOP 1 RecId FROM [dbo].[ut_Nummernkreise] WITH(INDEX = Ik_Key00) WHERE 
      Nr = @mNr
      ORDER BY Nr desc,RecId desc
    RETURN
  END

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


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


