plsql: add new rule "MisplacedPragma"

#1539 [plsql] Create new rule for strict syntax checking: MisplacedPragma
This commit is contained in:
Andreas Dangel
2016-11-04 11:19:52 +01:00
parent 176eda21fe
commit cec3e1a2ad
6 changed files with 141 additions and 1 deletions

View File

@ -2,4 +2,4 @@
# BSD-style license; for more info see http://pmd.sourceforge.net/license.html
#
rulesets.filenames=rulesets/plsql/codesize.xml,rulesets/plsql/dates.xml,rulesets/plsql/TomKytesDespair.xml
rulesets.filenames=rulesets/plsql/codesize.xml,rulesets/plsql/dates.xml,rulesets/plsql/TomKytesDespair.xml,rulesets/plsql/strictsyntax.xml

View File

@ -0,0 +1,51 @@
<?xml version="1.0"?>
<ruleset name="Strict Syntax"
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>
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}/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>
</ruleset>

View File

@ -0,0 +1,16 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.plsql.rule.strictsyntax;
import net.sourceforge.pmd.testframework.SimpleAggregatorTst;
public class StrictsyntaxRulesTest extends SimpleAggregatorTst {
private static final String RULESET = "plsql-strictsyntax";
@Override
public void setUp() {
addRule(RULESET, "MisplacedPragma");
}
}

View File

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<test-data>
<test-code>
<description>Correct Syntax</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
create or replace package inline_pragma_ok is
end;
/
create or replace package body inline_pragma_ok is
procedure do_transaction(p_input_token in varchar(200)) is
PRAGMA AUTONOMOUS_TRANSACTION;
begin
bno74.log_hentglass_request(p_hentglass_request
,v_logging_req_seq_no);
COMMIT;
end do_transaction;
end inline_pragma_ok;
/
]]></code>
<source-type>plsql</source-type>
</test-code>
<test-code>
<description>Wrong Syntax</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>10</expected-linenumbers>
<code><![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
begin
PRAGMA AUTONOMOUS_TRANSACTION;
bno74.log_hentglass_request(p_hentglass_request
,v_logging_req_seq_no);
COMMIT;
end do_transaction;
end inline_pragma_error;
/
]]></code>
<source-type>plsql</source-type>
</test-code>
</test-data>