Skip to content

fixes bug whereby comments before query yielded wrong return value #97

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

Merged
merged 7 commits into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
package_dir={"": "src"},
packages=["cs50"],
url="https://github.com/cs50/python-cs50",
version="4.0.3"
version="4.0.4"
)
4 changes: 3 additions & 1 deletion src/cs50/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ def execute(self, sql, *args, **kwargs):
import termcolor
import warnings

# Parse statement, stripping comments and then leading/trailing whitespace
statements = sqlparse.parse(sqlparse.format(sql, strip_comments=True).strip())

# Allow only one statement at a time, since SQLite doesn't support multiple
# https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.execute
statements = sqlparse.parse(sql)
if len(statements) > 1:
raise RuntimeError("too many statements at once")
elif len(statements) == 0:
Expand Down
6 changes: 6 additions & 0 deletions tests/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ def test_select_where(self):

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])

def test_select_with_comments(self):
self.assertEqual(self.db.execute("--comment\nSELECT * FROM cs50;\n--comment"), [])

def test_select_with_semicolon(self):
self.assertEqual(self.db.execute("SELECT * FROM cs50;\n--comment"), [])

def test_update_returns_affected_rows(self):
rows = [
{"id": 1, "val": "foo"},
Expand Down