Skip to content

Commit 70a7f77

Browse files
committedOct 28, 2019
fixed colons in quoted strings
1 parent 89f7279 commit 70a7f77

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-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: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,26 @@ def execute(self, sql, *args, **kwargs):
212212
"value" if len(keys) == 1 else "values",
213213
", ".join(keys)))
214214

215+
# For SQL statements where a colon is required verbatim, as within an inline string, use a backslash to escape
216+
# https://docs.sqlalchemy.org/en/13/core/sqlelement.html?highlight=text#sqlalchemy.sql.expression.text
217+
for index, token in enumerate(tokens):
218+
219+
# In string literal
220+
# https://www.sqlite.org/lang_keywords.html
221+
if token.ttype == sqlparse.tokens.Literal.String.Single:
222+
token.value = re.sub("(^'|\s+):", "\\1\\:", token.value)
223+
224+
# In identifier
225+
# https://www.sqlite.org/lang_keywords.html
226+
elif token.ttype == sqlparse.tokens.Literal.String.Symbol:
227+
token.value = re.sub("(^\"|\s+):", "\\1\\:", token.value)
228+
215229
# Join tokens into statement
216230
statement = "".join([str(token) for token in tokens])
217231

218232
# Catch SQLAlchemy warnings
219233
with warnings.catch_warnings():
220-
234+
221235
# Raise exceptions for warnings
222236
warnings.simplefilter("error")
223237

‎tests/sqlite.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
db.execute("SELECT * FROM Employee WHERE FirstName = :1 AND LastName = :2", ["Andrew", "Adams"])
3232
db.execute("SELECT * FROM Employee WHERE FirstName = :1 AND LastName = :2", ("Andrew", "Adams"))
3333

34+
db.execute("SELECT * FROM Employee WHERE FirstName = ':Andrew :Adams'")
35+
3436
db.execute("SELECT * FROM Employee WHERE FirstName = :first AND LastName = :last", first="Andrew", last="Adams")
3537
db.execute("SELECT * FROM Employee WHERE FirstName = :first AND LastName = :last", {"first": "Andrew", "last": "Adams"})
3638

0 commit comments

Comments
 (0)