diff --git a/docs/4-language-usage/8-patterns/3-validating-input-parameter-size/g-8310.md b/docs/4-language-usage/8-patterns/3-validating-input-parameter-size/g-8310.md index d4dbe67b..c5d671de 100644 --- a/docs/4-language-usage/8-patterns/3-validating-input-parameter-size/g-8310.md +++ b/docs/4-language-usage/8-patterns/3-validating-input-parameter-size/g-8310.md @@ -11,20 +11,30 @@ This technique raises an error (`value_error`) which may not be handled in the c ``` sql create or replace package body department_api is - function dept_by_name(in_dept_name in departments.department_name%type) + function dept_by_name( -- NOSONAR: non-deterministic + in_dept_name in departments.department_name%type + ) return departments%rowtype is - r_return departments%rowtype; + r_return departments%rowtype; + co_max_dept_name_length constant integer := 20; begin - if in_dept_name is null or length(in_dept_name) > 20 then + if in_dept_name is null or length(in_dept_name) > co_max_dept_name_length then raise err.e_param_to_large; end if; -- get the department by name - select * - into r_return - from departments - where department_name = in_dept_name; - - return r_return; + <> + begin + select * + into r_return + from departments + where department_name = in_dept_name; + return r_return; + exception + when no_data_found then + return null; + when too_many_rows then + raise; + end trap; end dept_by_name; end department_api; / @@ -34,18 +44,27 @@ end department_api; ``` sql create or replace package body department_api is - function dept_by_name(in_dept_name in departments.department_name%type) + function dept_by_name( -- NOSONAR: non-deterministic + in_dept_name in departments.department_name%type + ) return departments%rowtype is - l_dept_name departments.department_name%type not null := in_dept_name; - r_return departments%rowtype; + co_dept_name constant departments.department_name%type not null := in_dept_name; + r_return departments%rowtype; begin -- get the department by name - select * - into r_return - from departments - where department_name = l_dept_name; - - return r_return; + <> + begin + select * + into r_return + from departments + where department_name = co_dept_name; + return r_return; + exception + when no_data_found then + return null; + when too_many_rows then + raise; + end trap; end dept_by_name; end department_api; / @@ -54,9 +73,11 @@ end department_api; The exception should be handled where the function is called, like this: ``` sql +declare + co_dept_name constant type_up.text := 'Far to long name of a department'; begin pre_processing; - r_department := department_api.dept_by_name('Far to long name of a department'); + r_department := department_api.dept_by_name(co_dept_name); post_processing; exception when value_error then