aggiunta migrazione per la creazione delle funzioni f_getNumDoc4NumDocForn e getNumDoc4NumDocForn
All checks were successful
IntegryManagementSystem_Multi/pipeline/head This commit looks good

This commit is contained in:
2025-09-19 10:00:34 +02:00
parent 925e08ffc7
commit 1b05d58049

View File

@@ -0,0 +1,197 @@
package it.integry.ems.migration.model;
import it.integry.ems.migration._base.BaseMigration;
import it.integry.ems.migration._base.MigrationModelInterface;
public class Migration_20250919095906 extends BaseMigration implements MigrationModelInterface {
@Override
public void up() throws Exception {
if (isHistoryDB())
return;
createOrUpdateFunction("f_getNumDoc4NumDocForn", "CREATE FUNCTION [dbo].[f_getNumDoc4NumDocForn](@numdocOrig varchar(60), @annoComp varchar(4))\n" +
"RETURNS int\n" +
"AS\n" +
"BEGIN \n" +
" DECLARE @numDoc int\n" +
"\n" +
" select @numDoc = num_doc\n" +
" from(\n" +
"select *, COUNT(*) over (partition by len(num_doc)) as conta_len, ROW_NUMBER() over(order by len(num_doc) desc) as sort\n" +
"from dbo.getNumDoc4NumDocForn(@numDocOrig, @annoComp) )t\n" +
"where sort = 1 AND conta_len = 1\n" +
"\n" +
" return @numDoc\n" +
"END");
createOrUpdateFunction("f_getNumDoc4NumDocFornOld", "CREATE FUNCTION [dbo].[f_getNumDoc4NumDocFornOld](@numdocOrig varchar(60), @annoComp varchar(4))\n" +
"RETURNS int\n" +
"AS\n" +
"BEGIN \n" +
"\n" +
"declare @sep varchar(5), @numDoc varchar(60), @rowc int\n" +
"\n" +
" select @numdocOrig = LTRIM(RTRIM(@numdocOrig))\n" +
"\n" +
" declare @tmpChar table (sep varchar(5));\n" +
" declare @splitString table (numOrig varchar(60), numNew varchar(60), is_numeric int);\n" +
" \n" +
"\n" +
" insert into @tmpChar values ('-')\n" +
" insert into @tmpChar values ('.') \n" +
" insert into @tmpChar values (' ')\n" +
" insert into @tmpChar values ('/')\n" +
" insert into @tmpChar values ('\')\n" +
"\n" +
" declare @string varchar(1), @i int , @parseString varchar(60), @find int\n" +
"\n" +
" set @i = 1\n" +
"\n" +
" select @string = left(@numdocOrig, 1)\n" +
"\n" +
" while @string <> '' and @string is not null\n" +
" begin\n" +
" select @find = count(*)from @tmpChar where sep = @string\n" +
"\n" +
" if @find > 0 \n" +
" begin\n" +
" insert @splitString values (@numdocOrig, @parseString, IsNumeric(@parseString))\n" +
" set @parseString = ''\n" +
" end \n" +
" else\n" +
" begin\n" +
" select @parseString = IsNull(@parseString, '') + @string\n" +
" end \n" +
" select @i = @i + 1\n" +
" select @string = substring(@numdocOrig, @i, 1)\n" +
" end\n" +
"\n" +
" insert @splitString values (@numdocOrig, @parseString, IsNumeric(@parseString))\n" +
"\n" +
" select @rowc = count(*) From @splitString\n" +
" \n" +
" /*select *\n" +
" from @splitString\n" +
" where value <> @annoComp\n" +
" and IsNumeric(value) <> 0 \n" +
" and cast(value as bigint) <> 0 \n" +
" and ((value <> @numdocOrig and ISNUMERIC(@numdocOrig) = 0) or ISNUMERIC(@numdocOrig) = 1)\n" +
" */\n" +
"\n" +
" select @numDoc = numNew from @splitString\n" +
" where ( numNew <> @annoComp and numNew <> RIGHT( @annoComp, 2)) and \n" +
" is_numeric <> 0 and \n" +
" cast(numNew as bigint) <> 0 and\n" +
" numNew <> @numdocOrig\n" +
" \n" +
" Set @rowc = @@ROWCOUNT\n" +
"\n" +
" if @rowc = 0\n" +
" begin\n" +
" --Analizzare la stringa \n" +
" set @i = 0\n" +
" while @i < Len(@numdocOrig)\n" +
" begin\n" +
" select @i = @i+1\n" +
" --select substring(@numdocOrig, @i, 1)\n" +
" if isNumeric(substring(@numdocOrig, @i, 1) ) = 1 AND (select count(*) FROM @tmpChar WHERE sep = substring(@numdocOrig, @i, 1) ) = 0\n" +
" begin\n" +
" select @numdoc = IsNull(@numdoc, '') + substring(@numdocOrig, @i, 1)\n" +
" end\n" +
" else\n" +
" begin \n" +
" if LEN (@numDoc) > 0\n" +
" begin\n" +
" break\n" +
" end \n" +
" \n" +
" end\n" +
"\n" +
" end\n" +
" end\n" +
" else if @rowC > 1\n" +
" begin\n" +
" select @numDoc = 0\n" +
" end\n" +
"\n" +
"\n" +
" select @numDoc = (SELECT numDoc from ( select @numDoc as numDoc except SELECT * from @tmpChar ) t)\n" +
" \n" +
" select @numDoc = right(@numDoc, 6)\n" +
" \n" +
" return cast (@numDoc as int)\n" +
"END");
createOrUpdateFunction("getNumDoc4NumDocForn", "CREATE FUNCTION [dbo].[getNumDoc4NumDocForn]\n" +
"(\t\n" +
"\t@numdocorig varchar(60), @annoComp varchar(4)\n" +
")\n" +
"\n" +
"RETURNS @numTable TABLE \n" +
"(\n" +
"\tnum_doc_orig varchar(max), num_doc int\n" +
")\n" +
"AS\n" +
"BEGIN\n" +
"\t\n" +
"\tdeclare @sep varchar(5), @numDoc varchar(60), @rowc int, @numDocForn varchar(60)\n" +
"\n" +
"\tselect @numDocForn = LTRIM(RTRIM(@numdocOrig))\n" +
"\n" +
"\tdeclare @tmpChar table (sep varchar(5));\n" +
"\tdeclare @splitString table (numOrig varchar(60), numNew varchar(60), is_numeric int);\n" +
"\n" +
"\tSET @numDocForn = REPLACE(@numDocForn, '-', ',');\n" +
"\tSET @numDocForn = REPLACE(@numDocForn, '.', ',');\n" +
"\tSET @numDocForn = REPLACE(@numDocForn, '_', ',');\n" +
"\tSET @numDocForn = REPLACE(@numDocForn, ' ', ',');\n" +
"\tSET @numDocForn = REPLACE(@numDocForn, '/', ',');\n" +
"\tSET @numDocForn = REPLACE(@numDocForn, '', ',');\n" +
"\n" +
"\tselect @numDocForn = IIF(left(@numDocForn, 2)='IT', substring(@numDocForn, 3, len(@numDocForn) - 2),@numDocForn)\n" +
"\n" +
"\t\n" +
"\t;with tab_p as (\n" +
"\tselect value_string as num_new, \n" +
"\tCOUNT(*) over (partition by (select 1)) as tot_row, \n" +
"\tROW_NUMBER() over (order by (select 1)) as id,\n" +
"\tLEN(value_string) as lunghezza\n" +
"\tfrom dbo.parseStringIntoArray(@numDocForn, ',')\n" +
"\twhere ISNUMERIC(value_string) = 1 )\n" +
"\n" +
"\t,\n" +
"\ttab_union as (\t \n" +
"\tselect @numdocOrig as num_doc_forn, try_cast(IIF(cast(num_new as bigint) > 2147483647, right(num_new,6), num_new) as int ) as num_doc\n" +
"\tfrom (\n" +
"\tselect * from tab_p ) t\n" +
"\twhere ((num_new <> @annoComp and\n" +
"\tnum_new <> right(@annoComp,2)) or tot_row = 1 ) \n" +
"\tunion \n" +
"\tselect @numdocOrig, Try_cast(@numDocForn as int )\n" +
"\twhere not exists (select * from tab_p)\n" +
"\tunion\n" +
"\tselect @numdocOrig, \n" +
"\tcoalesce(TRY_CAST(Right(@numDocForn,6) as int), \n" +
"\tIIF(PATINDEX('%[0-9]%', @numDocForn) > 0, try_cast(SUBSTRING(@numDocForn, PATINDEX('%[0-9]%', @numDocForn), len(@numDocForn)) as int), null), \n" +
"\tIIF(PATINDEX('%[a-Z]%', @numDocForn) > 0, try_cast(left(@numDocForn, PATINDEX('%[a-Z]%', @numDocForn) - 1) as int), null))\n" +
"\twhere not exists (select * from tab_p)\n" +
"\t)\n" +
"\n" +
"\tinsert into @numTable\n" +
"\tselect *\n" +
"\tfrom tab_union\n" +
"\twhere num_doc is not null\n" +
"\tand num_doc<> 0\n" +
"\t\n" +
"\t\n" +
"\tRETURN \n" +
"END");
}
@Override
public void down() throws Exception {
}
}