-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from elisecodedestrucs/limit-db-query-results
add rule Limit db query results
- Loading branch information
Showing
5 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,23 @@ | ||
# Should limit the number of returns for a SQL query (`@ecocode/limit-db-query-results`) | ||
|
||
⚠️ This rule _warns_ in the ✅ `recommended` config. | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
## Rule Details | ||
|
||
This rule aims at reducing CPU consumption by limiting the number of returns for a single SQL query. | ||
|
||
## Examples | ||
|
||
Examples of **non compliant** code for this rule: | ||
|
||
```js | ||
const query = "SELECT * FROM clients"; | ||
``` | ||
|
||
Examples of **compliant** code for this rule: | ||
|
||
```js | ||
const query = "SELECT columns FROM table_name FETCH FIRST number ROWS ONLY"; | ||
``` |
This file contains 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,55 @@ | ||
/** | ||
* Copyright (C) 2023 Green Code Initiative | ||
* | ||
* 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/>. | ||
*/ | ||
"use strict"; | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: "suggestion", | ||
docs: { | ||
description: "Should limit the number of returns for a SQL query", | ||
category: "eco-design", | ||
recommended: "warn", | ||
}, | ||
messages: { | ||
LimitTheNumberOfReturns: | ||
"Try and limit the number of data returned for a single query (by using the LIMIT keyword for example)", | ||
}, | ||
schema: [], | ||
}, | ||
create(context) { | ||
//list of limiting clauses to check against | ||
const limitingClauses = ["LIMIT", "TOP", "ROW_NUMBER", "FETCH FIRST"]; | ||
return { | ||
Literal: function (node) { | ||
if (typeof node.value == "string") { | ||
const query = node.value.toUpperCase(); | ||
if ( | ||
query.includes("SELECT") && | ||
query.includes("FROM") && | ||
!limitingClauses.some((clause) => query.includes(clause)) | ||
) { | ||
context.report({ | ||
node: node, | ||
messageId: "LimitTheNumberOfReturns", | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains 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,62 @@ | ||
/** | ||
* Copyright (C) 2023 Green Code Initiative | ||
* | ||
* 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/>. | ||
*/ | ||
"use strict"; | ||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require("../../../lib/rules/limit-db-query-results"), | ||
RuleTester = require("eslint").RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: "module", | ||
}, | ||
}); | ||
const expectedError = { | ||
messageId: "LimitTheNumberOfReturns", | ||
type: "Literal", | ||
}; | ||
|
||
ruleTester.run("limit-db-query-results", rule, { | ||
valid: [ | ||
` | ||
const query = "SELECT * FROM customers LIMIT 10;"; | ||
`, | ||
` | ||
const query = "SELECT TOP 5 * FROM products;"; | ||
`, | ||
` | ||
const query = "SELECT * FROM orders FETCH FIRST 20 ROWS ONLY;"; | ||
`, | ||
` | ||
const query = "WITH numbered_customers AS (SELECT *, ROW_NUMBER() OVER (ORDER BY customer_id) AS row_num FROM customers) SELECT * FROM numbered_customers WHERE row_num <= 50;"; | ||
`, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: `const query = "SELECT * FROM bikes;";`, | ||
errors: [expectedError], | ||
}, | ||
], | ||
}); |