-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add java rule EC24 : Optimize Database SQL Queries (Clause LIMIT) #20
base: main
Are you sure you want to change the base?
Changes from 3 commits
c0148df
636465a
065db40
1b33736
d2de211
af8350d
a6d1ec8
8efb38e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package fr.greencodeinitiative.java.checks; | ||
|
||
import org.sonar.check.Rule; | ||
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
import org.sonar.plugins.java.api.tree.LiteralTree; | ||
import org.sonar.plugins.java.api.tree.Tree; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static java.util.regex.Pattern.CASE_INSENSITIVE; | ||
import static java.util.regex.Pattern.compile; | ||
|
||
@Rule(key = "EC24") | ||
public class OptimizeSQLQueriesWithLimit extends IssuableSubscriptionVisitor { | ||
|
||
public static final String MESSAGE_RULE = "Optimize Database SQL Queries (Clause LIMIT)"; | ||
private static final Predicate<String> LIMIT_REGEXP = | ||
compile("limit", CASE_INSENSITIVE).asPredicate(); | ||
private static final Predicate<String> SELECT_REGEXP = | ||
compile("select", CASE_INSENSITIVE).asPredicate(); | ||
private static final Predicate<String> FROM_REGEXP = | ||
compile("from", CASE_INSENSITIVE).asPredicate(); | ||
|
||
@Override | ||
public List<Tree.Kind> nodesToVisit() { | ||
return singletonList(Tree.Kind.STRING_LITERAL); | ||
} | ||
|
||
@Override | ||
public void visitNode(Tree tree) { | ||
String value = ((LiteralTree) tree).value(); | ||
if (SELECT_REGEXP.test(value) && FROM_REGEXP.test(value) && !LIMIT_REGEXP.test(value)) { | ||
reportIssue(tree, MESSAGE_RULE); | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class OptimizeSQLQueriesWithLimit { | ||
|
||
public void literalSQLrequest() { | ||
dummyCall("SELECT user FROM myTable"); // Noncompliant {{Optimize Database SQL Queries (Clause LIMIT)}} | ||
dummyCall("SELECT user FROM myTable LIMIT 50"); // Compliant | ||
} | ||
|
||
@Query("select t from Todo t where t.status != 'COMPLETED'") // Noncompliant {{Optimize Database SQL Queries (Clause LIMIT)}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the JavaScript plugin, I added the WHERE keyword in the list of limiting keywords so as not to be too strict either. We could consider that this query already performs a stricter selection than all the data in the table. I don't know what your opinion is on this? maybe it lacks measure too :p There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @utarwyn update done |
||
@Query("select t from Todo t where t.status != 'COMPLETED' LIMIT 25") // Compliant | ||
|
||
private void callQuery() { | ||
String sql1 = "SELECT user FROM myTable"; // Noncompliant {{Optimize Database SQL Queries (Clause LIMIT)}} | ||
String sql2 = "SELECT user FROM myTable LIMIT 50"; // Compliant | ||
} | ||
|
||
private void dummyCall(String request) { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package fr.greencodeinitiative.java.checks; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.sonar.java.checks.verifier.CheckVerifier; | ||
|
||
public class OptimizeSQLQueriesWithLimitTest { | ||
|
||
@Test | ||
void test() { | ||
CheckVerifier.newVerifier() | ||
.onFile("src/test/files/OptimizeSQLQueriesWithLimit.java") | ||
.withCheck(new OptimizeSQLQueriesWithLimit()) | ||
.verifyIssues(); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can wait for the next version of the artifact instead of using a snapshot here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
waiting for approvement of this implementation before releasing ecocode-rules-specifications