From b14ef472da4e9551ac846c5d32c7deed91b5d99a Mon Sep 17 00:00:00 2001 From: Rene Krenn Date: Fri, 26 Apr 2019 12:11:13 +0200 Subject: [PATCH] TT#58101 fix "exact" fee match mode Change-Id: Ibad2c91467bd7a25fec56a685de8c8620168726a --- db_scripts/diff/15526_not_replicated.up | 141 ++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 db_scripts/diff/15526_not_replicated.up diff --git a/db_scripts/diff/15526_not_replicated.up b/db_scripts/diff/15526_not_replicated.up new file mode 100644 index 00000000..8ade69cb --- /dev/null +++ b/db_scripts/diff/15526_not_replicated.up @@ -0,0 +1,141 @@ +SET sql_log_bin=0; +use billing; + +delimiter ;; +drop function get_billing_fee_id;; +create function get_billing_fee_id( + _billing_profile_id int(11), + _type enum('call','sms'), + _direction enum('in','out'), + _source varchar(511), + _destination varchar(511), + _match_mode enum('regex_longest_pattern', 'regex_longest_match', 'prefix', 'exact_destination') +) returns int(11) +deterministic reads sql data +begin + + declare _destination_prefix,_source_prefix varchar(511); + declare _i, _j int(3); + declare _fee_id int(11); + + if _fee_id is null and (_match_mode is null or _match_mode = "exact_destination") + and (select id from billing.billing_fees_history where + billing_profile_id = _billing_profile_id + and type = _type + and match_mode = "exact_destination" + and direction = _direction + and bf_id is not null + limit 1) is not null then + + set _fee_id = (select id from billing.billing_fees_history where + billing_profile_id = _billing_profile_id + and type = _type + and match_mode = "exact_destination" + and direction = _direction + and bf_id is not null + and destination = _destination + limit 1); + + end if; + + if _fee_id is null and (_match_mode is null or _match_mode = "prefix") + and (select id from billing.billing_fees_history where + billing_profile_id = _billing_profile_id + and type = _type + and match_mode = "prefix" + and direction = _direction + and bf_id is not null + limit 1) is not null then + + set _j = length(_destination); + + destination_loop: loop + if _j < 0 or _fee_id is not null then + leave destination_loop; + end if; + set _destination_prefix = substr(coalesce(_destination,""),1,_j); + if (select id from billing.billing_fees_history where + billing_profile_id = _billing_profile_id + and type = _type + and match_mode = "prefix" + and direction = _direction + and bf_id is not null + and destination = _destination_prefix + limit 1) is not null then + + set _i = length(_source); + + source_loop: loop + if _i < 0 or _fee_id is not null then + leave source_loop; + end if; + set _source_prefix = substr(coalesce(_source,""),1,_i); + set _fee_id = (select id from billing.billing_fees_history where + billing_profile_id = _billing_profile_id + and type = _type + and match_mode = "prefix" + and direction = _direction + and bf_id is not null + and source = _source_prefix + and destination = _destination_prefix + limit 1); + set _i = _i - 1; + end loop source_loop; + end if; + set _j = _j - 1; + end loop destination_loop; + end if; + + if _fee_id is null and (_match_mode is null or _match_mode = "regex_longest_match") + and (select id from billing.billing_fees_history where + billing_profile_id = _billing_profile_id + and type = _type + and match_mode = "regex_longest_match" + and direction = _direction + and bf_id is not null + limit 1) is not null then + + set _fee_id = (select id from billing.billing_fees_history where + billing_profile_id = _billing_profile_id + and type = _type + and match_mode = "regex_longest_match" + and direction = _direction + and bf_id is not null + and _source regexp(source) + and _destination regexp(destination) + order by + length(regexp_substr(_destination,destination)) desc, + length(regexp_substr(_source,source)) desc limit 1); + + end if; + + if _fee_id is null and (_match_mode is null or _match_mode = "regex_longest_pattern") + and (select id from billing.billing_fees_history where + billing_profile_id = _billing_profile_id + and type = _type + and match_mode = "regex_longest_pattern" + and direction = _direction + and bf_id is not null + limit 1) is not null then + + set _fee_id = (select id from billing.billing_fees_history where + billing_profile_id = _billing_profile_id + and type = _type + and match_mode = "regex_longest_pattern" + and direction = _direction + and bf_id is not null + and _source regexp(source) + and _destination regexp(destination) + order by + length(destination) desc, + length(source) desc limit 1); + + end if; + + return _fee_id; + +end;; + +delimiter ; + +