diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 4bfd95b0e8..b87a32b80d 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -16,7 +16,7 @@ This is a {{ site.pmd.release_type }} release. #### PLSQL Grammar Updates -The grammar has been updated to support Inline Constraints in CREATE TABLE statements. Additionally, the +The grammar has been updated to support inline constraints in CREATE TABLE statements. Additionally, the CREATE TABLE statement may now be followed by physical properties and table properties. However, these properties are skipped over during parsing. @@ -25,6 +25,8 @@ The CREATE VIEW statement now supports subquery views. The EXTRACT function can now be parsed correctly. It is used to extract values from a specified datetime field. Also date time literals are parsed now correctly. +The CASE expression can now be properly used within SELECT statements. + #### New Rules * The Java rule {% rule "java/bestpractices/DoubleBraceInitialization" %} (`java-bestpractices`) @@ -117,6 +119,7 @@ of deprecations. * [#1876](https://github.com/pmd/pmd/pull/1876): \[plsql] Datetime support for queries - [Hugo Araya Nash](https://github.com/kabroxiko) * [#1883](https://github.com/pmd/pmd/pull/1883): \[plsql] Fix #1873 Expression list not working - [Hugo Araya Nash](https://github.com/kabroxiko) * [#1884](https://github.com/pmd/pmd/pull/1884): \[plsql] fix #1878 Support explicit INNER word for INNER JOIN - [Hugo Araya Nash](https://github.com/kabroxiko) +* [#1885](https://github.com/pmd/pmd/pull/1885): \[plsql] Correct case expression - [Hugo Araya Nash](https://github.com/kabroxiko) {% endtocmaker %} diff --git a/pmd-plsql/etc/grammar/PldocAST.jjt b/pmd-plsql/etc/grammar/PldocAST.jjt index 969b400374..1d15c34c64 100644 --- a/pmd-plsql/etc/grammar/PldocAST.jjt +++ b/pmd-plsql/etc/grammar/PldocAST.jjt @@ -27,7 +27,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. /** - * Various fixes for expression lists, join clauses + * Various fixes for expression lists, join clauses, case expression * * Hugo Araya Nash 06/2019 *==================================================================== @@ -2870,24 +2870,14 @@ ASTAssignment Assignment() : } ASTCaseExpression CaseExpression() : -{ Token thisToken; PLSQLNode simpleNode = null; StringBuilder sb = new StringBuilder() ; } +{} { - ( - thisToken = { sb.append(thisToken.image);} - ( simpleNode = Expression() { sb.append(" "); sb.append(simpleNode.getImage()); } )? - ( thisToken = { sb.append(" "); sb.append(thisToken.image); } - simpleNode = Expression() { sb.append(" "); sb.append(simpleNode.getImage()); } - thisToken = { sb.append(" "); sb.append(thisToken.image); } - Expression() { sb.append(" "); sb.append(simpleNode.getImage()); } - )+ - [ thisToken = { sb.append(" "); sb.append(thisToken.image);} - Expression() { sb.append(" "); sb.append(simpleNode.getImage()); } - ] - thisToken = { sb.append(" "); sb.append(thisToken.image);} - ) - { - jjtThis.setImage(sb.toString()); return jjtThis; - } + + ( Expression() ( Expression() Expression() )+ + | ( Condition() Expression() )+ ) + [ Expression() ] + + { return jjtThis; } } /* @@ -3260,7 +3250,7 @@ ASTPrimaryPrefix PrimaryPrefix() : // Note: AnalyticClause and WithinClause are only allowed for specific functions, but this grammar allows it for all functions. LOOKAHEAD(FunctionName() "(") ( simpleNode = FunctionCall() [ AnalyticClause() ] [ WithinClause() ] ) { sb.append(simpleNode.getImage()); } | LOOKAHEAD(MultiSetCondition()) simpleNode = MultiSetCondition() -| LOOKAHEAD(CaseExpression()) ( simpleNode =CaseExpression() ) { sb.append(simpleNode.getImage()) ; } //SRT 20110520 +| LOOKAHEAD() ( simpleNode =CaseExpression() ) { sb.append(simpleNode.getImage()) ; } //SRT 20110520 | LOOKAHEAD(ObjectExpression() ) ( simpleNode = ObjectExpression() ) { sb.append(simpleNode.getImage()) ; } //SRT 20110604 //| LOOKAHEAD(LikeExpression()) ( simpleNode = LikeExpression() ) { sb.append(simpleNode.getImage()) ; } //SRT 20110604 | LOOKAHEAD(Literal()) ( simpleNode = Literal() ) { sb.append(simpleNode.getImage()) ; } diff --git a/pmd-plsql/src/test/resources/net/sourceforge/pmd/lang/plsql/ast/SelectExpressions.pls b/pmd-plsql/src/test/resources/net/sourceforge/pmd/lang/plsql/ast/SelectExpressions.pls index 153e6e0f0b..11da9a6493 100644 --- a/pmd-plsql/src/test/resources/net/sourceforge/pmd/lang/plsql/ast/SelectExpressions.pls +++ b/pmd-plsql/src/test/resources/net/sourceforge/pmd/lang/plsql/ast/SelectExpressions.pls @@ -84,5 +84,28 @@ SELECT CASE INTO my_result FROM DUAL; +SELECT CASE WHEN EXISTS(SELECT * + FROM DUAL + WHERE 1 = 1) + THEN 1 + ELSE 0 + END isExists + INTO VAL + FROM dual; + +SELECT CASE WHEN EXISTS(SELECT * + FROM DUAL) + THEN 1 + ELSE 0 + END isExists + INTO VAL + FROM dual; + +SELECT CASE + WHEN f1(x) IS NULL THEN 1 + ELSE 0 + END isExists + INTO VAL + FROM dual; END; / \ No newline at end of file