Skip to content

Commit

Permalink
SQL view-friendly arraycontains/arraynotcontains implementation, refs #…
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Nov 15, 2021
1 parent 502c02f commit 07044bd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
10 changes: 2 additions & 8 deletions datasette/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,13 @@ class Filters:
TemplatedFilter(
"arraycontains",
"array contains",
"""rowid in (
select {t}.rowid from {t}, json_each([{t}].[{c}]) j
where j.value = :{p}
)""",
""":{p} in (select value from json_each([{t}].[{c}]))""",
'{c} contains "{v}"',
),
TemplatedFilter(
"arraynotcontains",
"array does not contain",
"""rowid not in (
select {t}.rowid from {t}, json_each([{t}].[{c}]) j
where j.value = :{p}
)""",
""":{p} not in (select value from json_each([{t}].[{c}]))""",
'{c} does not contain "{v}"',
),
]
Expand Down
11 changes: 7 additions & 4 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@
# Not in, and JSON array not in
((("foo__notin", "1,2,3"),), ["foo not in (:p0, :p1, :p2)"], ["1", "2", "3"]),
((("foo__notin", "[1,2,3]"),), ["foo not in (:p0, :p1, :p2)"], [1, 2, 3]),
# JSON arraycontains
# JSON arraycontains, arraynotcontains
(
(("Availability+Info__arraycontains", "yes"),),
[
"rowid in (\n select table.rowid from table, json_each([table].[Availability+Info]) j\n where j.value = :p0\n )"
],
[":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]))"],
["yes"],
),
],
Expand Down

0 comments on commit 07044bd

Please sign in to comment.