Skip to content
Open
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
15 changes: 12 additions & 3 deletions datasette/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,16 @@ def where_clause(self, table, column, value, param_counter):
kwargs = {"c": column}
converted = None
else:
kwargs = {"c": column, "p": f"p{param_counter}", "t": table}
kwargs = {
"c": column,
"p": f"p{param_counter}",
"t": table,
# Properly quoted identifiers for templates that reference the
# table/column directly (e.g. json_each()). Bracket quoting
# cannot escape a "]" in a name, so use escape_sqlite() instead.
"c_escaped": escape_sqlite(column),
"t_escaped": escape_sqlite(table) if table is not None else "",
}
return self.sql_template.format(**kwargs), converted

def human_clause(self, column, value):
Expand Down Expand Up @@ -322,13 +331,13 @@ class Filters:
TemplatedFilter(
"arraycontains",
"array contains",
""":{p} in (select value from json_each([{t}].[{c}]))""",
""":{p} in (select value from json_each({t_escaped}.{c_escaped}))""",
'{c} contains "{v}"',
),
TemplatedFilter(
"arraynotcontains",
"array does not contain",
""":{p} not in (select value from json_each([{t}].[{c}]))""",
""":{p} not in (select value from json_each({t_escaped}.{c_escaped}))""",
'{c} does not contain "{v}"',
),
]
Expand Down
16 changes: 14 additions & 2 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,24 @@
# JSON arraycontains, arraynotcontains
(
(("Availability+Info__arraycontains", "yes"),),
[":p0 in (select value from json_each([table].[Availability+Info]))"],
[':p0 in (select value from json_each("table"."Availability+Info"))'],
["yes"],
),
(
(("Availability+Info__arraynotcontains", "yes"),),
[":p0 not in (select value from json_each([table].[Availability+Info]))"],
[':p0 not in (select value from json_each("table"."Availability+Info"))'],
["yes"],
),
# A column name containing "]" must be escaped with escape_sqlite() -
# bracket quoting would produce invalid SQL, see refs #2431
(
(("ta]gs__arraycontains", "yes"),),
[':p0 in (select value from json_each("table"."ta]gs"))'],
["yes"],
),
(
(("ta]gs__arraynotcontains", "yes"),),
[':p0 not in (select value from json_each("table"."ta]gs"))'],
["yes"],
),
],
Expand Down