-
-
Notifications
You must be signed in to change notification settings - Fork 700
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
derive_named_parameters() method that works with latest SQLite #2354
Comments
Code to fix, because the opcode trick no longer works: datasette/datasette/utils/__init__.py Lines 1137 to 1150 in 7437d40
|
Got ChatGPT Code Interpreter to have a go at this for me: https://chatgpt.com/share/f2ce4904-184c-4825-847d-30467c3a8236 - it came up with a pattern that first strips all comments, single-quoted and double-quoted strings and then extracts parameters from what's left. |
This new version of the function passes all of the existing tests: @documented
async def derive_named_parameters(db: "Database", sql: str) -> List[str]:
"""
Given a SQL statement, return a list of named parameters that are used in the statement
e.g. for ``select * from foo where id=:id`` this would return ``["id"]``
"""
# Remove single-line comments
sql = re.sub(r"--.*", "", sql)
# Remove multi-line comments
sql = re.sub(r"/\*.*?\*/", "", sql, flags=re.DOTALL)
# Remove single-quoted strings
sql = re.sub(r"'(?:''|[^'])*'", "", sql)
# Remove double-quoted strings
sql = re.sub(r'"(?:\"\"|[^"])*"', "", sql)
# Extract parameters from what is left
return re.findall(r":(\w+)", sql) But... it doesn't need to take the I'm going to add a new, non-async function and switch to that, but I'll leave an |
New implementation: datasette/datasette/utils/__init__.py Lines 1134 to 1165 in d118d5c
|
I don't think those comments are needed with the clear names for the |
Related:
The text was updated successfully, but these errors were encountered: