From ed2b3f25beac720f14869350baacc5f62b065194 Mon Sep 17 00:00:00 2001 From: Ray N <3433657+raynae@users.noreply.github.com> Date: Fri, 17 Nov 2017 08:29:22 -0500 Subject: [PATCH] add support for ?field__isnull=1 (#107) * add support for ?field__isnull=1 * Add unit test and conditional formatting for ?field__isnull --- datasette/utils.py | 11 ++++++++--- tests/test_utils.py | 9 +++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/datasette/utils.py b/datasette/utils.py index e709cb2b..94093d78 100644 --- a/datasette/utils.py +++ b/datasette/utils.py @@ -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 = { @@ -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 diff --git a/tests/test_utils.py b/tests/test_utils.py index a8b0e379..168b9253 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -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)