Trivadis / plsql-and-sql-coding-guidelines

Trivadis PL/SQL & SQL Coding Guidelines

Home Page:https://trivadis.github.io/plsql-and-sql-coding-guidelines/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

G-8310 value_error good_example

PrzemekFilipiak opened this issue · comments

Hi,
Changing variable declarion to default will raise "value_error" if in_dept_name := null
create or replace package body department_api is
function dept_by_name( -- NOSONAR: non-deterministic
in_dept_name in departments.department_name%type
)
return departments%rowtype is
-- co_dept_name constant departments.department_name%type not null := in_dept_name;
co_dept_name departments.department_name%type not null default 'example department_name';
r_return departments%rowtype;
begin
-- get the department by name
<>
begin
co_dept_name := in_dept_name; -- if in_dept_name := null, will raise "value_error"
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;
when value_error then
handle_error;
end trap;
end dept_by_name;
end department_api;
/

G_8310_good_example

Could you please elaborate how your change of the good example code in G-8310 is related to this guideline?

What kind of change are you suggesting?

Regarding your code change. You changed a constant to a variable with a default value. If you would like to accept a null value you have to get rid of the NOT NULL in the declaration. However, this is not related to the G-8310 IMO.

Hi,
“PL/SQL & SQL Coding Guidelines Reason
This technique raises an error (value_error) which may not be handled in the called program unit. “
In my opinion above sentence is not true, because you could catch “value_error” when:
variable in declaration section will be “not null default”.
Example:
G_8310

In your example, you are assigning the parameter to a variable in the body and not in the declare section. Hence you are not following the guideline G-8310.

G-8310: Always validate input parameter size by assigning the parameter to a size limited variable in the declaration section of program unit.