From 54597f22fafb1744718b98f3018da595374f8e7e Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 7 Jul 2026 14:26:34 -0700 Subject: [PATCH] A few more SQLite string fixes, refs #2783 --- datasette/utils/__init__.py | 4 +++- tests/test_utils.py | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/datasette/utils/__init__.py b/datasette/utils/__init__.py index 7b9a90b9..42574d3b 100644 --- a/datasette/utils/__init__.py +++ b/datasette/utils/__init__.py @@ -1294,9 +1294,11 @@ class StartupError(Exception): _comments_and_strings_re = re.compile( r""" --[^\n]* # single line comment - | /\*.*?\*/ # multi line comment + | /\*.*?(?:\*/|\Z) # multi line comment, possibly to end-of-input | '(?:''|[^'])*' # single quoted string ('' escapes a quote) | "(?:""|[^"])*" # double quoted identifier ("" escapes a quote) + | \[(?:[^\]])*\] # square-bracket quoted identifier + | `(?:``|[^`])*` # backtick quoted identifier """, re.DOTALL | re.VERBOSE, ) diff --git a/tests/test_utils.py b/tests/test_utils.py index 0b2fcd2b..a535ca93 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -764,6 +764,11 @@ def test_parse_metadata(content, expected): # Parameters that live inside a comment should be ignored ("select :x -- and :ignored", ["x"]), ("select :x /* and :ignored */ from t", ["x"]), + ("select :x /* and :ignored", ["x"]), + # Parameters inside quoted identifiers should be ignored + ("select [a:b] from t where id = :id", ["id"]), + ("select `a:b` from t where id = :id", ["id"]), + ("select `a``:b` from t where id = :id", ["id"]), # Parameters inside a string literal should be ignored ("select ':ignored' || :real", ["real"]), ),