Skip to content
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

ecoCode Challenge: Adding a new rule to check wether every column queried in a SQL query is indeed used afterwards #332

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
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 RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Some are applicable for different technologies.
| | Resize images browser-side | Do not resize images using the HEIGHT and WIDTH attributes of the HTML code. This approach requires transferring these images to their original size, wasting bandwidth and CPU cycles. | [cnumr best practices (3rd edition) BP_034](https://github.com/cnumr/best-practices/blob/main/chapters/BP_034_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚀 |
| | Modify the DOM when traversing it | Modifying the DOM (Document Object Model) as you traverse it can lead to situations where the loop becomes very resource-intensive, especially CPU cycles. | [cnumr best practices (3rd edition) BP_041](https://github.com/cnumr/best-practices/blob/main/chapters/BP_041_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 |
| | Edit DOM elements to make it invisible | When an element of the Document Object Model (DOM) needs to be modified by several properties, each change in style or content will generate a repaint or reflow. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | 🚫 |
| EC1044 | Avoid querying SQL columns that are not used | When a database is queried without an ORM, every column that is queried should be used afterward somewhere in the function. | [cnumr best practices (3rd edition) BP_075](https://github.com/cnumr/best-practices/blob/main/chapters/BP_075_fr.md) | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | ❓ |

## Rules to be reworked / measured / clarified

Expand Down
18 changes: 18 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC1044/EC1044.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"title": "Avoid querying SQL columns that are not used",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "10min"
},
"tags": [
"performance",
"sql",
"eco-design",
"ecocode",
"memory",
"network"
],
"defaultSeverity": "Minor"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
Querying columns in a SQL command and not using it induce unnecessary RAM usage and network transfer.

## Noncompliant Code Example

```java
private static final String QUERY = "SELECT id, first, last, age FROM Registration"; // Noncompliant {{Avoid querying SQL columns that are not used}}

public void callJdbc() {

try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(QUERY);) {
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();
}

}
```

## Compliant Solution

```java
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);) {
while (rs.next()) {
// Display values
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", Age: " + rs.getInt("age"));
System.out.print(", First: " + rs.getString("first"));
System.out.println(", Last: " + rs.getString("last"));
}
} catch (SQLException e) {
e.printStackTrace();
}

}
```
Loading