Skip to content

Commit

Permalink
add support for ?field__isnull=1 (#107)
Browse files Browse the repository at this point in the history
* add support for ?field__isnull=1

* Add unit test and conditional formatting for ?field__isnull
  • Loading branch information
raynae authored and Simon Willison committed Nov 17, 2017
1 parent b9af49b commit ed2b3f2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions datasette/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def build_where_clauses(args):
'lte': '"{}" <= :{}',
'glob': '"{}" glob :{}',
'like': '"{}" like :{}',
'isnull': '"{}" is null',
}[lookup]
numeric_operators = {'gt', 'gte', 'lt', 'lte'}
value_convert = {
Expand All @@ -56,11 +57,15 @@ def build_where_clauses(args):
converted = value_convert(value)
if lookup in numeric_operators and converted.isdigit():
converted = int(converted)
param_id = 'p{}'.format(i)
if ':{}' in template:
param_id = 'p{}'.format(i)
params[param_id] = converted
tokens = (column, param_id)
else:
tokens = (column,)
sql_bits.append(
template.format(column, param_id)
template.format(*tokens)
)
params[param_id] = converted
return sql_bits, params


Expand Down
9 changes: 9 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ def test_custom_json_encoder(obj, expected):
['"foo" like :p0', '"zax" glob :p1'],
['2%2', '3*']
),
(
{
'foo__isnull': '1',
'baz__isnull': '1',
'bar__gt': '10'
},
['"bar" > :p0', '"baz" is null', '"foo" is null'],
[10]
),
])
def test_build_where(args, expected_where, expected_params):
sql_bits, actual_params = utils.build_where_clauses(args)
Expand Down

0 comments on commit ed2b3f2

Please sign in to comment.