Delimiter //
/**
 mis à jour du champ SUMMARIES_ALLOWED_FOR_UPDATE_EXP en fonction du champ SUMMARIES_CATEGORIES_CODES_ALLOWED_FOR_UPDATE
la valeur sera une expression regulière qui permetra de vérifier si la mise à jour est authorisé
**/
Drop Procedure If Exists LOADER_SUMMARIES_NORMALIZE_MAP_ALLOWED_UPDATE_CHECK//
Create Procedure LOADER_SUMMARIES_NORMALIZE_MAP_ALLOWED_UPDATE_CHECK(
    In batchnum Int
)
Begin
	lsnm_prepAllowedUpdateCheck: Begin
		Update
			LOADER_STATUS LST
			Inner Join LOADER_SUMMARIES LRD
				On LRD.LOADER_STATUS_ID_LOADER_STATUS = LST.ID_LOADER_STATUS
		Set
			LRD.SUMMARIES_ALLOWED_FOR_UPDATE_EXP = Concat('^('
														, Replace(Replace(LRD.SUMMARIES_CATEGORIES_CODES_ALLOWED_FOR_UPDATE, ',', '|'), ';', '|')
														, ')$'
													)
		Where
			LST.LOADER_STATUS_BATCH_NUM = batchnum
			And LST.LOADER_STATUS_ERRCOUNT = 0
			And LRD.SUMMARIES_CATEGORIES_CODES_ALLOWED_FOR_UPDATE Is Not Null
			And LRD.LOADER_SUMMARIES_UPDATE_ALLOWED Is Null
		;
	End ;
	lsnm_evalAllowedUpdateCheck: Begin
		Update
			LOADER_STATUS LST
			Inner Join LOADER_SUMMARIES LRD
				On LRD.LOADER_STATUS_ID_LOADER_STATUS = LST.ID_LOADER_STATUS
			Inner Join DF_SUMMARIES DMS
				On LRD.DF_SUMMARIES_ID_DF_SUMMR = DMS.ID_DF_SUMMARIES
		Set
			LRD.LOADER_SUMMARIES_UPDATE_ALLOWED = Case When DMS.DF_SUMMARIES_CATEGORY_CODE Regexp LRD.SUMMARIES_ALLOWED_FOR_UPDATE_EXP Then
														1
													Else 
														0
													End
		Where
			LST.LOADER_STATUS_BATCH_NUM = batchnum
			And LRD.SUMMARIES_ALLOWED_FOR_UPDATE_EXP Is Not Null
			And LRD.LOADER_SUMMARIES_UPDATE_ALLOWED Is Null
		;
	End ;
End//
Delimiter ;