Skip to content

Commit 087543b

Browse files
authored
Merge pull request #97 from cs50/file
fixes bug whereby comments before query yielded wrong return value
2 parents 72a1706 + 380b5e6 commit 087543b

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
package_dir={"": "src"},
1717
packages=["cs50"],
1818
url="https://github.com/cs50/python-cs50",
19-
version="4.0.3"
19+
version="4.0.4"
2020
)

src/cs50/sql.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ def execute(self, sql, *args, **kwargs):
7575
import termcolor
7676
import warnings
7777

78+
# Parse statement, stripping comments and then leading/trailing whitespace
79+
statements = sqlparse.parse(sqlparse.format(sql, strip_comments=True).strip())
80+
7881
# Allow only one statement at a time, since SQLite doesn't support multiple
7982
# https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.execute
80-
statements = sqlparse.parse(sql)
8183
if len(statements) > 1:
8284
raise RuntimeError("too many statements at once")
8385
elif len(statements) == 0:

tests/sql.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ def test_select_where(self):
6363

6464
self.assertEqual(self.db.execute("SELECT * FROM cs50 WHERE id = :id OR val = :val", id=rows[1]["id"], val=rows[2]["val"]), rows[1:3])
6565

66+
def test_select_with_comments(self):
67+
self.assertEqual(self.db.execute("--comment\nSELECT * FROM cs50;\n--comment"), [])
68+
69+
def test_select_with_semicolon(self):
70+
self.assertEqual(self.db.execute("SELECT * FROM cs50;\n--comment"), [])
71+
6672
def test_update_returns_affected_rows(self):
6773
rows = [
6874
{"id": 1, "val": "foo"},

0 commit comments

Comments
 (0)