-
Notifications
You must be signed in to change notification settings - Fork 79
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 #1 from cnumr/main
sync
- Loading branch information
Showing
250 changed files
with
7,226 additions
and
721 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
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
48 changes: 48 additions & 0 deletions
48
...plugin-greenit/python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/64.html
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,48 @@ | ||
<p>Do not execute an SQL request in a loop</p> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
|
||
def foo(): | ||
... | ||
baseQuery= "SELECT name FROM users where id = " | ||
for i in range(0,20): | ||
query=query + str(i) | ||
cursor.execute(query) #Noncompliant | ||
for row in cursor: | ||
print(row) | ||
... | ||
cursor.close() | ||
----------------------------------------------------------- | ||
def foo(): | ||
... | ||
baseQuery= "SELECT name FROM users where id = " | ||
data = [ i for i in range(0,20) ] | ||
cursor.executemany(baseQuery,data) | ||
for row in cursor: | ||
print(row) | ||
... | ||
cursor.close() | ||
|
||
</pre> | ||
|
||
|
||
<h2>Compliant Solution</h2> | ||
<pre> | ||
|
||
def foo() { | ||
... | ||
query = "SELECT name FROM users where id in (0 " | ||
for i in range(0,20): | ||
query = query +","+str(i) | ||
query+=")" | ||
cursor.execute(query) #compliant | ||
|
||
# iterate through the resultset | ||
for row in cursor: | ||
print(row) | ||
|
||
cursor.close(); | ||
... | ||
} | ||
|
||
</pre> |
14 changes: 14 additions & 0 deletions
14
...plugin-greenit/python-plugin/src/main/resources/fr/cnumr/l10n/python/rules/python/64.json
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,14 @@ | ||
{ | ||
"title": "Avoid SQL request in loop", | ||
"type": "CODE_SMELL", | ||
|
||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "10min" | ||
}, | ||
"tags": [ | ||
"eco-conception" | ||
], | ||
"defaultSeverity": "Minor" | ||
} |
12 changes: 12 additions & 0 deletions
12
...it/python-plugin/src/test/java/fr/cnumr/python/checks/AvoidSQLRequestInLoopCheckTest.java
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,12 @@ | ||
package fr.cnumr.python.checks; | ||
|
||
import org.junit.Test; | ||
import org.sonar.python.checks.utils.PythonCheckVerifier; | ||
|
||
public class AvoidSQLRequestInLoopCheckTest { | ||
|
||
@Test | ||
public void test() { | ||
PythonCheckVerifier.verify("src/test/resources/checks/AvoidSQLRequestInLoopCheck.py", new AvoidSQLRequestInLoopCheckTest()); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...qube-plugin-greenit/python-plugin/src/test/resources/checks/AvoidSQLRequestInLoopCheck.py
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,64 @@ | ||
import mysql.connector | ||
|
||
|
||
class AvoidSQLRequestInLoopCheck: | ||
def testWithNoLoop(self): | ||
try : | ||
db = mysql.connector.connect(option_files='my.conf', use_pure=True) | ||
cursor=db.cursor() | ||
query = "SELECT * FROM users" | ||
cursor.execute(query) | ||
with row in cursor: | ||
print(row.id) | ||
cursor.close() | ||
db.close() | ||
except : | ||
print("Got an exception") | ||
db.close() | ||
|
||
def testWithForLoop(): | ||
try: | ||
db = mysql.connector.connect(option_files='my.conf', use_pure=True) | ||
query = "SELECT * FROM users where id = " | ||
for i in range(0,20): | ||
cursor=db.cursor() | ||
query+=str(i) | ||
cursor.execute(query) #Noncompliant | ||
with row in cursor: | ||
print(row.name) | ||
cursor.close() | ||
except : | ||
print("Got an exception") | ||
db.close() | ||
|
||
def testWithWhileLoop(): | ||
try: | ||
db = mysql.connector.connect(option_files='my.conf', use_pure=True) | ||
query = "SELECT * FROM users where id = " | ||
i = 0 | ||
while i<20: | ||
|
||
cursor=db.cursor() | ||
query+=str(i) | ||
cursor.execute(query) #Noncompliant | ||
with row in cursor: | ||
print(row.name) | ||
cursor.close() | ||
i+=1 | ||
except : | ||
print("Got an exception") | ||
db.close() | ||
|
||
def testWithExecuteMany(): | ||
try: | ||
db =db = mysql.connector.connect(option_files='my.conf', use_pure=True) | ||
query = "SELECT * FROM users where id = %d" | ||
cursor=db.cursor() | ||
data = [i for i in range(20)] | ||
cursor.executemany(query,data) | ||
with row in cursor: | ||
print(row.name) | ||
cursor.close() | ||
except: | ||
print("Got an exception") | ||
db.close() |
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
!.gitignore | ||
.* | ||
node_modules | ||
yarn.lock | ||
target/ | ||
node/ | ||
.idea | ||
.DS_Store | ||
*.iml | ||
/lib/*.jar | ||
bin |
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
Oops, something went wrong.