[plsql] Reorganize rules into categories

This commit is contained in:
Andreas Dangel
2017-11-03 16:17:14 +01:00
parent c466179c2a
commit d05ca8ade4
13 changed files with 906 additions and 787 deletions

View File

@@ -0,0 +1,78 @@
<?xml version="1.0"?>
<ruleset name="Best Practices"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
Rules which enforce generally accepted best practices.
</description>
<rule name="TomKytesDespair"
language="plsql"
since="5.1"
message="WHEN OTHERS THEN NULL - when you do this, Tom Kyte cries"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_plsql_bestpractices.html#tomkytesdespair">
<description>
"WHEN OTHERS THEN NULL" hides all errors - (Re)RAISE an exception or call RAISE_APPLICATION_ERROR
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ExceptionHandler[QualifiedName/@Image='OTHERS' and upper-case(Statement/UnlabelledStatement/Expression/@Image)='NULL']
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
CREATE OR REPLACE PACKAGE BODY update_planned_hrs
IS
PROCEDURE set_new_planned (p_emp_id IN NUMBER, p_project_id IN NUMBER, p_hours IN NUMBER)
IS
BEGIN
UPDATE employee_on_activity ea
SET ea.ea_planned_hours = p_hours
WHERE
ea.ea_emp_id = p_emp_id
AND ea.ea_proj_id = p_project_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR (-20100, 'No such employee or project');
END set_new_planned;
FUNCTION existing_planned (p_emp_id IN NUMBER, p_project_id IN NUMBER) RETURN NUMBER
IS
existing_hours NUMBER(4);
BEGIN
SELECT ea.ea_planned_hours INTO existing_hours
FROM employee_on_activity ea
WHERE
ea.ea_emp_id = p_emp_id
AND ea.ea_proj_id = p_project_id;
RETURN (existing_hours);
EXCEPTION
WHEN OTHERS THEN NULL;
END existing_planned;
END update_planned_hrs;
/
]]>
</example>
</rule>
</ruleset>

View File

@@ -0,0 +1,55 @@
<?xml version="1.0"?>
<ruleset name="Codestyle"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
Rules which enforce a specific coding style.
</description>
<rule name="MisplacedPragma"
language="plsql"
since="5.5.2"
message="Pragma should be used only inside the declaration block before 'BEGIN'."
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_plsql_codestyle.html#misplacedpragma">
<description>
Oracle states that the PRAQMA AUTONOMOUS_TRANSACTION must be in the declaration block,
but the code does not complain, when being compiled on the 11g DB.
https://docs.oracle.com/cd/B28359_01/appdev.111/b28370/static.htm#BABIIHBJ
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ProgramUnit/Pragma
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
create or replace package inline_pragma_error is
end;
/
create or replace package body inline_pragma_error is
procedure do_transaction(p_input_token in varchar(200)) is
PRAGMA AUTONOMOUS_TRANSACTION; /* this is correct place for PRAGMA */
begin
PRAGMA AUTONOMOUS_TRANSACTION; /* this is the wrong place for PRAGMA -> violation */
/* do something */
COMMIT;
end do_transaction;
end inline_pragma_error;
/
]]>
</example>
</rule>
</ruleset>

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<ruleset name="Documentation"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
Rules that are related to code documentation.
</description>
</ruleset>

View File

@@ -0,0 +1,158 @@
<?xml version="1.0"?>
<ruleset name="Error Prone"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
Rules to detect constructs that are either broken, extremely confusing or prone to runtime errors.
</description>
<rule name="TO_DATE_TO_CHAR"
language="plsql"
since="5.1"
message="TO_DATE(TO_CHAR(variable)) instead of TRUNC(variable)"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_plsql_errorprone.html#to_date_to_char">
<description>
TO_DATE(TO_CHAR(date-variable)) used to remove time component - use TRUNC(date-variable)
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryExpression
[PrimaryPrefix/Name/@Image='TO_DATE']
[count(PrimarySuffix/Arguments/ArgumentList/Argument) = 1]
[.//PrimaryExpression
[PrimaryPrefix/Name/@Image='TO_CHAR']
[count(PrimarySuffix/Arguments/ArgumentList/Argument) = 1]
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
CREATE OR REPLACE PACKAGE BODY date_utilities
IS
-- Take single parameter, relying on current default NLS date format
FUNCTION strip_time (p_date IN DATE) RETURN DATE
IS
BEGIN
RETURN TO_DATE(TO_CHAR(p_date));
END strip_time;
END date_utilities;
/
]]>
</example>
</rule>
<rule name="TO_DATEWithoutDateFormat"
language="plsql"
since="5.1"
message="TO_DATE without date format"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_plsql_errorprone.html#to_datewithoutdateformat">
<description>
TO_DATE without date format- use TO_DATE(expression, date-format)
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryExpression[PrimaryPrefix/Name/@Image='TO_DATE' and count(PrimarySuffix/Arguments/ArgumentList/Argument) = 1 ]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
CREATE OR REPLACE PACKAGE BODY date_utilities
IS
-- Take single parameter, relying on current default NLS date format
FUNCTION to_date_single_parameter (p_date_string IN VARCHAR2) RETURN DATE
IS
BEGIN
RETURN TO_DATE(p_date_string);
END to_date_single_parameter ;
-- Take 2 parameters, using an explicit date format string
FUNCTION to_date_two_parameters (p_date_string IN VARCHAR2, p_format_mask IN VARCHAR2) RETURN DATE
IS
BEGIN
TO_DATE(p_date_string, p_date_format);
END to_date_two_parameters;
-- Take 3 parameters, using an explicit date format string and an explicit language
FUNCTION to_date_three_parameters (p_date_string IN VARCHAR2, p_format_mask IN VARCHAR2, p_nls_language VARCHAR2 ) RETURN DATE
IS
BEGIN
TO_DATE(p_date_string, p_format_mask, p_nls_language);
END to_date_three_parameters;
END date_utilities;
/
]]>
</example>
</rule>
<rule name="TO_TIMESTAMPWithoutDateFormat"
language="plsql"
message="TO_TIMESTAMP without date format"
class="net.sourceforge.pmd.lang.rule.XPathRule"
since="5.1"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_plsql_errorprone.html#to_timestampwithoutdateformat">
<description>
TO_TIMESTAMP without date format- use TO_TIMESTAMP(expression, date-format)
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryExpression[PrimaryPrefix/Name/@Image='TO_TIMESTAMP' and count(PrimarySuffix/Arguments/ArgumentList/Argument) = 1 ]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
CREATE OR REPLACE PACKAGE BODY date_utilities
IS
-- Take single parameter, relying on current default NLS date format
FUNCTION to_timestamp_single_parameter (p_date_string IN VARCHAR2) RETURN DATE
IS
BEGIN
RETURN TO_TIMESTAMP(p_date_string);
END to_timestamp_single_parameter;
-- Take 2 parameters, using an explicit date format string
FUNCTION to_timestamp_two_parameters (p_date_string IN VARCHAR2, p_format_mask IN VARCHAR2) RETURN DATE
IS
BEGIN
TO_TIMESTAMP(p_date_string, p_date_format);
END to_timestamp_two_parameters;
-- Take 3 parameters, using an explicit date format string and an explicit language
FUNCTION to_timestamp_three_parameters (p_date_string IN VARCHAR2, p_format_mask IN VARCHAR2, p_nls_language VARCHAR2 ) RETURN DATE
IS
BEGIN
TO_TIMESTAMP(p_date_string, p_format_mask, p_nls_language);
END to_timestamp_three_parameters;
END date_utilities;
/
]]>
</example>
</rule>
</ruleset>

View File

@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<ruleset name="Multithreading"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
Rules that flag issues when dealing with multiple threads of execution.
</description>
</ruleset>

View File

@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<ruleset name="Performance"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
Rules that flag suboptimal code.
</description>
</ruleset>

View File

@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<ruleset name="Security"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
Rules that flag potential security flaws.
</description>
</ruleset>

View File

@@ -8,69 +8,6 @@
Rules based on Thomas Kyte's recommendations on http://asktom.oracle.com/ and http://tkyte.blogspot.com/.
</description>
<rule name="TomKytesDespair"
language="plsql"
since="5.1"
message="WHEN OTHERS THEN NULL - when you do this, Tom Kyte cries"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_plsql_TomKytesDespair.html#tomkytesdespair">
<description>
"WHEN OTHERS THEN NULL" hides all errors - (Re)RAISE an exception or call RAISE_APPLICATION_ERROR
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ExceptionHandler[QualifiedName/@Image='OTHERS' and upper-case(Statement/UnlabelledStatement/Expression/@Image)='NULL']
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
CREATE OR REPLACE PACKAGE BODY update_planned_hrs
IS
PROCEDURE set_new_planned (p_emp_id IN NUMBER, p_project_id IN NUMBER, p_hours IN NUMBER)
IS
BEGIN
UPDATE employee_on_activity ea
SET ea.ea_planned_hours = p_hours
WHERE
ea.ea_emp_id = p_emp_id
AND ea.ea_proj_id = p_project_id;
EXCEPTION
WHEN NO_DATA_FOUND THEN
RAISE_APPLICATION_ERROR (-20100, 'No such employee or project');
END set_new_planned;
FUNCTION existing_planned (p_emp_id IN NUMBER, p_project_id IN NUMBER) RETURN NUMBER
IS
existing_hours NUMBER(4);
BEGIN
SELECT ea.ea_planned_hours INTO existing_hours
FROM employee_on_activity ea
WHERE
ea.ea_emp_id = p_emp_id
AND ea.ea_proj_id = p_project_id;
RETURN (existing_hours);
EXCEPTION
WHEN OTHERS THEN NULL;
END existing_planned;
END update_planned_hrs;
/
]]>
</example>
</rule>
<rule ref="category/plsql/bestpractices.xml/TomKytesDespair" deprecated="true" />
</ruleset>

File diff suppressed because it is too large Load Diff

View File

@@ -8,150 +8,8 @@
The Dates ruleset deals with PLSQL DATETIME usages.
</description>
<rule name="TO_DATEWithoutDateFormat"
language="plsql"
since="5.1"
message="TO_DATE without date format"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_plsql_dates.html#to_datewithoutdateformat">
<description>
TO_DATE without date format- use TO_DATE(expression, date-format)
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryExpression[PrimaryPrefix/Name/@Image='TO_DATE' and count(PrimarySuffix/Arguments/ArgumentList/Argument) = 1 ]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
CREATE OR REPLACE PACKAGE BODY date_utilities
IS
-- Take single parameter, relyimg on current default NLS date format
FUNCTION to_date_single_parameter (p_date_string IN VARCHAR2) RETURN DATE
IS
BEGIN
RETURN TO_DATE(p_date_string);
END to_date_single_parameter ;
-- Take 2 parameters, using an explicit date format string
FUNCTION to_date_two_parameters (p_date_string IN VARCHAR2, p_format_mask IN VARCHAR2) RETURN DATE
IS
BEGIN
TO_DATE(p_date_string, p_date_format);
END to_date_two_parameters;
-- Take 3 parameters, using an explicit date format string and an explicit language
FUNCTION to_date_three_parameters (p_date_string IN VARCHAR2, p_format_mask IN VARCHAR2, p_nls_language VARCHAR2 ) RETURN DATE
IS
BEGIN
TO_DATE(p_date_string, p_format_mask, p_nls_language);
END to_date_three_parameters;
END date_utilities;
/
]]>
</example>
</rule>
<rule name="TO_DATE_TO_CHAR"
language="plsql"
since="5.1"
message="TO_DATE(TO_CHAR(variable)) instead of TRUNC(variable)"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_plsql_dates.html#to_date_to_char">
<description>
TO_DATE(TO_CHAR(date-variable)) used to remove time component - use TRUNC(date-veriable)
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryExpression
[PrimaryPrefix/Name/@Image='TO_DATE']
[count(PrimarySuffix/Arguments/ArgumentList/Argument) = 1]
[.//PrimaryExpression
[PrimaryPrefix/Name/@Image='TO_CHAR']
[count(PrimarySuffix/Arguments/ArgumentList/Argument) = 1]
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
CREATE OR REPLACE PACKAGE BODY date_utilities
IS
-- Take single parameter, relyimg on current default NLS date format
FUNCTION strip_time (p_date IN DATE) RETURN DATE
IS
BEGIN
RETURN TO_DATE(TO_CHAR(p_date));
END strip_time;
END date_utilities;
/
]]>
</example>
</rule>
<rule name="TO_TIMESTAMPWithoutDateFormat"
language="plsql"
message="TO_TIMESTAMP without date format"
class="net.sourceforge.pmd.lang.rule.XPathRule"
since="5.1"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_plsql_dates.html#to_timestampwithoutdateformat">
<description>
TO_TIMESTAMP without date format- use TO_TIMESTAMP(expression, date-format)
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryExpression[PrimaryPrefix/Name/@Image='TO_TIMESTAMP' and count(PrimarySuffix/Arguments/ArgumentList/Argument) = 1 ]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
CREATE OR REPLACE PACKAGE BODY date_utilities
IS
-- Take single parameter, relyimg on current default NLS date format
FUNCTION to_timestamp_single_parameter (p_date_string IN VARCHAR2) RETURN DATE
IS
BEGIN
RETURN TO_TIMESTAMP(p_date_string);
END to_timestamp_single_parameter;
-- Take 2 parameters, using an explicit date format string
FUNCTION to_timestamp_two_parameters (p_date_string IN VARCHAR2, p_format_mask IN VARCHAR2) RETURN DATE
IS
BEGIN
TO_TIMESTAMP(p_date_string, p_date_format);
END to_timestamp_two_parameters;
-- Take 3 parameters, using an explicit date format string and an explicit language
FUNCTION to_timestamp_three_parameters (p_date_string IN VARCHAR2, p_format_mask IN VARCHAR2, p_nls_language VARCHAR2 ) RETURN DATE
IS
BEGIN
TO_TIMESTAMP(p_date_string, p_format_mask, p_nls_language);
END to_timestamp_three_parameters;
END date_utilities;
/
]]>
</example>
</rule>
<rule ref="category/plsql/errorprone.xml/TO_DATE_TO_CHAR" deprecated="true" />
<rule ref="category/plsql/errorprone.xml/TO_DATEWithoutDateFormat" deprecated="true" />
<rule ref="category/plsql/errorprone.xml/TO_TIMESTAMPWithoutDateFormat" deprecated="true" />
</ruleset>

View File

@@ -3,7 +3,15 @@
#
rulesets.filenames=\
rulesets/plsql/codesize.xml,\
rulesets/plsql/dates.xml,\
rulesets/plsql/TomKytesDespair.xml,\
rulesets/plsql/strictsyntax.xml
category/plsql/bestpractices.xml,\
category/plsql/codestyle.xml,\
category/plsql/design.xml,\
category/plsql/errorprone.xml
#
# categories without rules
#
# category/plsql/documentation.xml
# category/plsql/multithreading.xml
# category/plsql/performance.xml
# category/plsql/security.xml

View File

@@ -8,47 +8,6 @@
The Strict Syntax ruleset contains rules that highlight invalid plsql syntax, which works, but should be avoided.
</description>
<rule name="MisplacedPragma"
language="plsql"
since="5.5.2"
message="Pragma should be used only inside the declaration block before 'BEGIN'."
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_plsql_strictsyntax.html#misplacedpragma">
<description>
Oracle states that the PRAQMA AUTONOMOUS_TRANSACTION must be in the declaration block,
but the code does not complain, when being compiled on the 11g DB.
https://docs.oracle.com/cd/B28359_01/appdev.111/b28370/static.htm#BABIIHBJ
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ProgramUnit/Pragma
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
create or replace package inline_pragma_error is
end;
/
create or replace package body inline_pragma_error is
procedure do_transaction(p_input_token in varchar(200)) is
PRAGMA AUTONOMOUS_TRANSACTION; /* this is correct place for PRAGMA */
begin
PRAGMA AUTONOMOUS_TRANSACTION; /* this is the wrong place for PRAGMA -> violation */
/* do something */
COMMIT;
end do_transaction;
end inline_pragma_error;
/
]]>
</example>
</rule>
<rule ref="category/plsql/codestyle.xml/MisplacedPragma" deprecated="true" />
</ruleset>