rspec/rules/S2007/plsql/rule.adoc

74 lines
1.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
When data structures (scalar variables, collections, cursors) are declared in the package specification (not within any specific program), they can be referenced directly by any program running in a session with ``++EXECUTE++`` rights to the package.
2020-06-30 12:48:07 +02:00
2021-02-02 15:02:10 +01:00
2020-06-30 12:48:07 +02:00
Instead, declare all package-level data in the package body and provide getter and setter functions in the package specification. Developers can then access the data using these methods and will automatically follow all rules you set upon data modification.
2021-02-02 15:02:10 +01:00
2020-06-30 12:48:07 +02:00
By doing so you can guarantee data integrity, change your data structure implementation, and also track access to those data structures.
=== Noncompliant code example
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2020-06-30 12:48:07 +02:00
----
-- Package specification
CREATE PACKAGE employee AS
name VARCHAR2(42); -- Non-Compliant
END employee;
/
DROP PACKAGE employee;
----
=== Compliant solution
2020-06-30 12:48:07 +02:00
2022-02-04 17:28:24 +01:00
[source,sql]
2020-06-30 12:48:07 +02:00
----
-- Package specification
CREATE PACKAGE employee AS
PROCEDURE setName (newName VARCHAR2);
FUNCTION getName RETURN VARCHAR2;
END employee;
/
-- Package body
CREATE PACKAGE BODY employee AS
name VARCHAR2(42);
PROCEDURE setName (newName VARCHAR2) IS
BEGIN
name := newName;
END;
FUNCTION getName RETURN VARCHAR2 IS
BEGIN
RETURN name;
END;
END employee;
/
DROP PACKAGE BODY employee;
DROP PACKAGE employee;
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Move this variable declaration into a program.
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]