Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6032969
feat: add test files
May 30, 2024
9419960
feat: :construction: EC1024 skeleton
max-208 May 30, 2024
f790569
feat: add test class
May 30, 2024
2facdf2
Fix: fix class and file name
May 30, 2024
a916018
feat: :construction: WIP : create rule EC10044
max-208 May 30, 2024
3ad7732
Merge branch '1044-java' of github.com:max-208/ecoCode-java into 1044…
max-208 May 30, 2024
5a8eb15
fix: improve test
May 30, 2024
de78977
feat: :construction: WIP finish rule creation for EC1044 rule
max-208 May 30, 2024
756e40e
Merge branch '1044-java' of github.com:max-208/ecoCode-java into 1044…
max-208 May 30, 2024
c6eaa43
feat: :construction: WIP : create rule 1044
max-208 May 30, 2024
75196b8
feat: :sparkles: created rule 1044
max-208 May 30, 2024
e82572d
fix: :bug: improve false positives detection
max-208 Jun 10, 2024
759b8ad
refactor: :recycle: quick refactor
max-208 Jun 11, 2024
2b7f600
refactor: :recycle: fix formating
max-208 Jun 12, 2024
30b131f
remove executeUpdate
max-208 Aug 7, 2024
647df32
add some comments
max-208 Aug 7, 2024
50259b0
small refactor and add comments
max-208 Aug 19, 2024
3cfaf87
Merge branch 'main' into 1044-java
max-208 Aug 19, 2024
1fc5855
Merge branch 'main' into 1044-java
dedece35 Sep 6, 2024
d0fe4d2
Add rule to registrar and correct ID
max-208 Sep 9, 2024
0e76371
Improve test names and add select * case
max-208 Sep 9, 2024
b8a7654
improve ResultSet get method detection
max-208 Sep 9, 2024
6b539b7
up to current branch
max-208 May 20, 2025
6a00a35
Merge branch 'main' into 1044-java
max-208 May 20, 2025
3d879a6
change package
max-208 May 20, 2025
8698e41
Merge remote-tracking branch 'origin/1044-java' into 1044-java
max-208 May 20, 2025
6a1484c
add integration test
max-208 May 20, 2025
ba71712
update changelog
max-208 May 20, 2025
482f2f8
fix pr following feedback
max-208 May 20, 2025
1b8b696
removed disabled tests
max-208 May 21, 2025
46c9bca
give real number to GCI95
max-208 May 21, 2025
49ba322
correct changelog
max-208 May 21, 2025
7fc22e1
Merge branch 'main' into 1044-java
pataluc May 26, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- [#63](https://github.com/green-code-initiative/ecoCode-java/pull/63) [GCI95] [Java] Avoid querying SQL columns that are not used

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,31 @@ void testGCI94() {
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN);
}

@Test
void testGCI95_1() {
String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/UseEveryColumnQueriedAttributeQueryNonCompliant.java";
String ruleId= "creedengo-java:GCI95";
String ruleMsg = "Avoid querying SQL columns that are not used";
int[] startLines = new int[]{41};
int[] endLines = new int[]{41};
}

@Test
void testGCI95_2() {
String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/UseEveryColumnQueriedLitteralQueryNonCompliant.java";
String ruleId= "creedengo-java:GCI95";
String ruleMsg = "Avoid querying SQL columns that are not used";
int[] startLines = new int[]{40};
int[] endLines = new int[]{40};
}

@Test
void testGCI95_3() {
String filePath = "src/main/java/org/greencodeinitiative/creedengo/java/checks/UseEveryColumnQueriedUseColumnIdsAndNameAttributesNonCompliant.java";
String ruleId= "creedengo-java:GCI95";
String ruleMsg = "Avoid querying SQL columns that are not used";
int[] startLines = new int[]{42};
int[] endLines = new int[]{42};
}

}
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 UseEveryColumnQueriedAttributeQueryNonCompliant {

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();
}

}
}
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 UseEveryColumnQueriedLitteralQueryNonCompliant {

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();
}

}


}
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 UseEveryColumnQueriedUseColumnIdsAndNameAttributesNonCompliant {

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();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public class JavaCheckRegistrar implements CheckRegistrar {
FreeResourcesOfAutoCloseableInterface.class,
AvoidMultipleIfElseStatement.class,
UseOptionalOrElseGetVsOrElse.class,
MakeNonReassignedVariablesConstants.class
MakeNonReassignedVariablesConstants.class,
UseEveryColumnQueried.class
);

/**
Expand Down
Loading