Skip to content

Commit

Permalink
Add rules to avoid commit and rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasPohl committed Oct 17, 2023
1 parent 2021b19 commit d997aa2
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static List<Class<?>> getChecks() {
CommentRegularExpressionCheck.class,
CommentedCodeCheck.class,
CommentsCheck.class,
CommitCheck.class,
ConditionParenthesisCheck.class,
CyclomaticComplexityCheck.class,
DeclareCombineCheck.class,
Expand Down Expand Up @@ -91,6 +92,7 @@ public static List<Class<?>> getChecks() {
PropagateConsistencyCheck.class,
PropagateToLabelCheck.class,
RecursionCheck.class,
RollbackCheck.class,
RoutineCommentsCheck.class,
RoutineWithExcessiveReturnsCheck.class,
SelectAllCheck.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Sonar ESQL Plugin
* Copyright (C) 2013-2023 Thomas Pohl and EXXETA AG
* http://www.exxeta.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.exxeta.iss.sonar.esql.check;

import com.exxeta.iss.sonar.esql.api.visitors.DoubleDispatchVisitorCheck;
import com.exxeta.iss.sonar.esql.tree.impl.statement.CommitStatementTreeImpl;
import org.sonar.check.Rule;

@Rule(key="Commit")
public class CommitCheck extends DoubleDispatchVisitorCheck {

public static final String MESSAGE = "COMMIT should not be called explicitly. Otherwise the messageflow can't handle the transaction.";
@Override
public void visitCommitStatement(CommitStatementTreeImpl tree) {
addIssue(tree.commitKeyword(), MESSAGE);
super.visitCommitStatement(tree);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Sonar ESQL Plugin
* Copyright (C) 2013-2023 Thomas Pohl and EXXETA AG
* http://www.exxeta.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.exxeta.iss.sonar.esql.check;

import com.exxeta.iss.sonar.esql.api.visitors.DoubleDispatchVisitorCheck;
import com.exxeta.iss.sonar.esql.tree.impl.statement.RollbackStatementTreeImpl;
import org.sonar.check.Rule;

@Rule(key="Rollback")
public class RollbackCheck extends DoubleDispatchVisitorCheck {

public static final String MESSAGE = "ROLLBACK should not be called explicitly. Otherwise the messageflow can't handle the transaction.";
@Override
public void visitRollbackStatement(RollbackStatementTreeImpl tree) {
addIssue(tree.rollbackKeyword(), MESSAGE);
super.visitRollbackStatement(tree);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<p>Avoid using the COMMIT statement in ESQL code, as it can undermine the transaction
handling mechanism of the message flow. Instead, consider restructuring the flow
to handle smaller transactions or committing all changes at once. This approach
helps ensure that the transaction handling mechanism operates correctly and reliably,
without risking unexpected behavior or data loss. By avoiding the use of COMMIT statements,
you can help maintain the integrity and consistency of the message flow's data.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"title": "COMMIT should not be used.",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "20min"
},
"tags": [
"bad-practice"
],
"defaultSeverity": "Major"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p>Avoid using the ROLLBACK statement in ESQL code, as it can undermine the transaction
handling mechanism of the message flow. Instead, consider using the THROW statement to
raise an exception and trigger a rollback of the entire message flow. This approach helps
ensure that the transaction handling mechanism operates correctly and reliably, without
risking unexpected behavior or data loss.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"title": "ROLLBACK should not be used.",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "20min"
},
"tags": [
"bad-practice"
],
"defaultSeverity": "Major"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"CaseWithoutElse",
"CommentedCode",
"Comments",
"Commit",
"CyclomaticComplexity",
"DeleteFromWithoutWhere",
"DeprecatedMethod",
Expand Down Expand Up @@ -40,6 +41,7 @@
"OneStatementPerLine",
"ParameterWithDirection",
"PassThruStatement",
"Rollback",
"PropagateConsistency",
"PropagateToLabel",
"RoutineWithExcessiveReturns",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.exxeta.iss.sonar.esql.check;

import com.exxeta.iss.sonar.esql.checks.verifier.EsqlCheckVerifier;
import java.io.File;
import org.junit.jupiter.api.Test;

class CommitCheckTest {

@Test
void test() {

EsqlCheckVerifier.issues(new RollbackCheck(), new File("src/test/resources/commitRollback.esql"))
.next().atLine(5).withMessage("ROLLBACK should not be called explicitly. Otherwise the messageflow can't handle the transaction.")
.noMore();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.exxeta.iss.sonar.esql.check;

import com.exxeta.iss.sonar.esql.checks.verifier.EsqlCheckVerifier;
import java.io.File;
import org.junit.jupiter.api.Test;

class RollbackCheckTest {

@Test
void test() {

EsqlCheckVerifier.issues(new CommitCheck(), new File("src/test/resources/commitRollback.esql"))
.next().atLine(6).withMessage("COMMIT should not be called explicitly. Otherwise the messageflow can't handle the transaction.")
.noMore();
}
}
9 changes: 9 additions & 0 deletions esql-checks/src/test/resources/commitRollback.esql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE COMPUTE MODULE CommitRollback

CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN
ROLLBACK;
COMMIT;
END;

END MODULE;
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void esqlReservedKeyword() {

@Test
void esqlNonReservedKeyword() {
assertThat(EsqlNonReservedKeyword.values().length).isEqualTo(214);
assertThat(EsqlNonReservedKeyword.values().length).isEqualTo(216);
assertThat(EsqlNonReservedKeyword.keywordValues().length).isEqualTo(EsqlNonReservedKeyword.values().length);

for (EsqlNonReservedKeyword keyword : EsqlNonReservedKeyword.values()) {
Expand Down

0 comments on commit d997aa2

Please sign in to comment.