Skip to content

Commit 93abd1b

Browse files
committed
Fix #79 - Added phrasing and slight reworking of sample code in G-7230 concerning public constants.
1 parent cb2ff5b commit 93abd1b

File tree

1 file changed

+13
-5
lines changed
  • docs/4-language-usage/7-stored-objects/2-packages

1 file changed

+13
-5
lines changed

docs/4-language-usage/7-stored-objects/2-packages/g-7230.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
## Reason
77

8-
You should always declare package-level data inside the package body. You can then define "get and set" methods (functions and procedures, respectively) in the package specification to provide controlled access to that data. By doing so you can guarantee data integrity, you can change your data structure implementation, and also track access to those data structures.
8+
You should always declare package-level data (non-constants) inside the package body. You can then define "get and set" methods (functions and procedures, respectively) in the package specification to provide controlled access to that data. By doing so you can guarantee data integrity, you can change your data structure implementation, and also track access to those data structures.
99

10-
Data structures (scalar variables, collections, cursors) declared in the package specification (not within any specific program) can be referenced directly by any program running in a session with `execute` rights to the package.
10+
Data structures (scalar variables, collections, cursors) declared in the package specification (not within any specific program) can be referenced directly by any program running in a session with `execute` rights to the package.
1111

1212
Instead, declare all package-level data in the package body and provide "get and set" methods - a function to get the value and a procedure to set the value - in the package specification. Developers then can access the data using these methods - and will automatically follow all rules you set upon data modification.
1313

14+
For package-level constants, consider whether the constant should be public and usable from other code, or if only relevant for code within the package. If the latter, declare the constant in the package body. If the former, it is typically good practice to place the constants in a package specification that only holds constants.
15+
1416
## Example (bad)
1517

1618
``` sql
@@ -42,6 +44,12 @@ end employee_api;
4244
## Example (good)
4345

4446
``` sql
47+
create or replace package constants_up as
48+
co_min_increase constant types_up.sal_increase_type := 0.01;
49+
co_max_increase constant types_up.sal_increase_type := 0.5;
50+
end constants_up;
51+
/
52+
4553
create or replace package employee_api as
4654
procedure set_salary_increase (in_increase in types_up.sal_increase_type);
4755
function salary_increase return types_up.sal_increase_type;
@@ -56,8 +64,8 @@ create or replace package body employee_api as
5664
procedure set_salary_increase (in_increase in types_up.sal_increase_type) is
5765
begin
5866
g_salary_increase := greatest(least(in_increase
59-
,constants_up.max_salary_increase())
60-
,constants_up.min_salary_increase());
67+
,constants_up.co_max_increase)
68+
,constants_up.co_min_increase);
6169
end set_salary_increase;
6270

6371
function salary_increase return types_up.sal_increase_type is
@@ -68,7 +76,7 @@ create or replace package body employee_api as
6876
procedure init
6977
is
7078
begin
71-
g_salary_increase := constants_up.min_salary_increase();
79+
g_salary_increase := constants_up.co_min_increase;
7280
end init;
7381
begin
7482
init();

0 commit comments

Comments
 (0)