-
Notifications
You must be signed in to change notification settings - Fork 47
[CHALLENGE24][GCI95][S.T.E.P] : Use every column queried #63
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
Open
max-208
wants to merge
33
commits into
green-code-initiative:main
Choose a base branch
from
max-208:1044-java
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 27 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
6032969
feat: add test files
9419960
feat: :construction: EC1024 skeleton
max-208 f790569
feat: add test class
2facdf2
Fix: fix class and file name
a916018
feat: :construction: WIP : create rule EC10044
max-208 3ad7732
Merge branch '1044-java' of github.com:max-208/ecoCode-java into 1044…
max-208 5a8eb15
fix: improve test
de78977
feat: :construction: WIP finish rule creation for EC1044 rule
max-208 756e40e
Merge branch '1044-java' of github.com:max-208/ecoCode-java into 1044…
max-208 c6eaa43
feat: :construction: WIP : create rule 1044
max-208 75196b8
feat: :sparkles: created rule 1044
max-208 e82572d
fix: :bug: improve false positives detection
max-208 759b8ad
refactor: :recycle: quick refactor
max-208 2b7f600
refactor: :recycle: fix formating
max-208 30b131f
remove executeUpdate
max-208 647df32
add some comments
max-208 50259b0
small refactor and add comments
max-208 3cfaf87
Merge branch 'main' into 1044-java
max-208 1fc5855
Merge branch 'main' into 1044-java
dedece35 d0fe4d2
Add rule to registrar and correct ID
max-208 0e76371
Improve test names and add select * case
max-208 b8a7654
improve ResultSet get method detection
max-208 6b539b7
up to current branch
max-208 6a00a35
Merge branch 'main' into 1044-java
max-208 3d879a6
change package
max-208 8698e41
Merge remote-tracking branch 'origin/1044-java' into 1044-java
max-208 6a1484c
add integration test
max-208 ba71712
update changelog
max-208 482f2f8
fix pr following feedback
max-208 1b8b696
removed disabled tests
max-208 46c9bca
give real number to GCI95
max-208 49ba322
correct changelog
max-208 7fc22e1
Merge branch 'main' into 1044-java
pataluc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
...c/main/java/org/greencodeinitiative/creedengo/java/checks/AttributeQueryNonCompliant.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs | ||
| * Copyright © 2023 Green Code Initiative (https://www.ecocode.io) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.java.checks; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
|
|
||
| /** | ||
| * This is the nominal test case, where the SQL query is an attribute of the class. | ||
| * One field is not accesed, so an issue is raised | ||
| */ | ||
| public class AttributeQueryNonCompliant { | ||
|
|
||
| private static final String DB_URL = "jdbc:mysql://localhost/TEST"; | ||
| private static final String USER = "guest"; | ||
| private static final String PASS = "guest123"; | ||
| private static final String QUERY = "SELECT id, first, last, age FROM Registration"; | ||
|
|
||
| public void callJdbc() { | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); | ||
| Statement stmt = conn.createStatement(); | ||
| ResultSet rs = stmt.executeQuery(QUERY);) { // Noncompliant {{Avoid querying SQL columns that are not used}} | ||
| while (rs.next()) { | ||
| // Display values | ||
| System.out.print("ID: " + rs.getInt("id")); | ||
| System.out.print(", First: " + rs.getString("first")); | ||
| System.out.println(", Last: " + rs.getString("last")); | ||
| } | ||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| } | ||
| } | ||
54 changes: 54 additions & 0 deletions
54
...rc/main/java/org/greencodeinitiative/creedengo/java/checks/LitteralQueryNonCompliant.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs | ||
| * Copyright © 2023 Green Code Initiative (https://www.ecocode.io) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.java.checks; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
|
|
||
| /** | ||
| * In this test case, the query is a litteral string directly inserted in the method | ||
| * One field is not accesed, so an issue is raised | ||
| */ | ||
| public class LitteralQueryNonCompliant { | ||
|
|
||
| private static final String DB_URL = "jdbc:mysql://localhost/TEST"; | ||
| private static final String USER = "guest"; | ||
| private static final String PASS = "guest123"; | ||
|
|
||
| public void callJdbc() { | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); | ||
| Statement stmt = conn.createStatement(); | ||
| ResultSet rs = stmt.executeQuery("SELECT id, first, last, age FROM Registration");) { // Noncompliant {{Avoid querying SQL columns that are not used}} | ||
| while (rs.next()) { | ||
| // Display values | ||
| System.out.print("ID: " + rs.getInt("id")); | ||
| System.out.print(", First: " + rs.getString("first")); | ||
| System.out.println(", Last: " + rs.getString("last")); | ||
| } | ||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } |
68 changes: 68 additions & 0 deletions
68
.../main/java/org/greencodeinitiative/creedengo/java/checks/MultipleQueriesNonCompliant.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs | ||
| * Copyright © 2023 Green Code Initiative (https://www.ecocode.io) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.java.checks; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
|
|
||
| /** | ||
| * In this test case, multiple queries are done using the same Statement Object. | ||
| * One field is not accesed, so an issue is raised | ||
| */ | ||
| public class MultipleQueriesNonCompliant { | ||
|
|
||
| private static final String DB_URL = "jdbc:mysql://localhost/TEST"; | ||
| private static final String USER = "guest"; | ||
| private static final String PASS = "guest123"; | ||
| private static final String QUERY = "SELECT id, first, last, age FROM Registration"; | ||
| private static final String QUERY2 = "SELECT id, first, last, age FROM Registration2"; | ||
|
|
||
|
|
||
| public void callJdbc() { | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); | ||
| Statement stmt = conn.createStatement(); | ||
| ) { | ||
|
|
||
| ResultSet rs = stmt.executeQuery(QUERY); // Noncompliant {{Avoid querying SQL columns that are not used}} | ||
| while (rs.next()) { | ||
| // Display values | ||
| System.out.print("ID: " + rs.getInt("id")); | ||
| System.out.print(", First: " + rs.getString("first")); | ||
| System.out.println(", Last: " + rs.getString("last")); | ||
| } | ||
| rs = stmt.executeQuery(QUERY2); | ||
|
|
||
| while (rs.next()) { | ||
| // Display values | ||
| System.out.print("Age: " + rs.getInt("age")); | ||
| System.out.print("ID: " + rs.getInt("id")); | ||
| System.out.print(", First: " + rs.getString("first")); | ||
| System.out.println(", Last: " + rs.getString("last")); | ||
| } | ||
|
|
||
|
|
||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| } | ||
| } |
54 changes: 54 additions & 0 deletions
54
.../greencodeinitiative/creedengo/java/checks/UseColumnIdsAndNameAttributesNonCompliant.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| /* | ||
| * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs | ||
| * Copyright © 2023 Green Code Initiative (https://www.ecocode.io) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.java.checks; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
|
|
||
| /** | ||
| * In this test case, columns are accesed by IDs and names, some of them being in final variables | ||
| * One field is not accesed, so an issue is raised | ||
| */ | ||
| public class UseColumnIdsAndNameAttributesNonCompliant { | ||
|
|
||
| private static final String DB_URL = "jdbc:mysql://localhost/TEST"; | ||
| private static final String USER = "guest"; | ||
| private static final String PASS = "guest123"; | ||
| private static final String QUERY = "SELECT id, first, last, age FROM Registration"; | ||
| private static final String ID = "id"; | ||
|
|
||
| public void callJdbc() { | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); | ||
| Statement stmt = conn.createStatement(); | ||
| ResultSet rs = stmt.executeQuery(QUERY);) { // Noncompliant {{Avoid querying SQL columns that are not used}} | ||
| while (rs.next()) { | ||
| // Display values | ||
| System.out.print("ID: " + rs.getInt(ID)); | ||
| System.out.print(", First: " + rs.getString(2)); | ||
| System.out.println(", Last: " + rs.getString("last")); | ||
| } | ||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| } | ||
| } |
56 changes: 56 additions & 0 deletions
56
...ct/src/main/java/org/greencodeinitiative/creedengo/java/checks/UseMethodNonCompliant.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs | ||
| * Copyright © 2023 Green Code Initiative (https://www.ecocode.io) | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.java.checks; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
|
|
||
| /** | ||
| * In this test case, the ResultSet is passed through a method | ||
| * One field is not accesed, so an issue is raised | ||
| */ | ||
| public class UseMethodNonCompliant { | ||
|
|
||
| private static final String DB_URL = "jdbc:mysql://localhost/TEST"; | ||
| private static final String USER = "guest"; | ||
| private static final String PASS = "guest123"; | ||
|
|
||
| public void callJdbc() { | ||
|
|
||
| try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS); | ||
| Statement stmt = conn.createStatement(); | ||
| ResultSet rs = stmt.executeQuery("SELECT id, first, last, age FROM Registration");) { // Noncompliant {{Avoid querying SQL columns that are not used}} | ||
| extractGet(rs); | ||
| } catch (SQLException e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private void extractGet(ResultSet rs) throws SQLException { | ||
| while (rs.next()) { | ||
| // Display values | ||
| System.out.print("ID: " + rs.getInt("id")); | ||
| System.out.print(", First: " + rs.getString("first")); | ||
| System.out.println(", Last: " + rs.getString("last")); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.