From eb701b9f2c621f8a9d8e21240085125464b94a64 Mon Sep 17 00:00:00 2001 From: Ben Webb Date: Wed, 24 Nov 2021 13:47:56 +0000 Subject: [PATCH] tmp --filter-sql-json-path --- manage.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/manage.py b/manage.py index 15a64ba..c3b1d8f 100755 --- a/manage.py +++ b/manage.py @@ -222,6 +222,11 @@ def construct_where_fragment(cursor, filter_field, filter_value): return where_fragment.decode() +def construct_where_fragment_sql_json_path(cursor, sql_json_path_filter): + where_fragment = cursor.mogrify(' AND jsonb_path_match(d.data, %s)', [sql_json_path_filter]) + return where_fragment.decode() + + @click.group() @click.option('-q', '--quiet', is_flag=True, help='Change the log level to warning') @click.pass_context @@ -260,8 +265,11 @@ def cli(ctx, quiet): help="Any SQL files to skip. Dependent files and final files will be skipped.") @click.option('--filter', "filters", nargs=2, multiple=True, help="A field and value to filter by, like --filter tender.procurementMethod direct") +@click.option('--filter-sql-json-path', "sql_json_path_filters", multiple=True, + help="A SQL/JSON Path Language expression to filter by, e.g. '$.tender.procurementMethod == \"direct\"'") @click.pass_context -def add(ctx, collections, note, name, tables_only, field_counts_option, field_lists_option, skip, filters): +def add(ctx, collections, note, name, tables_only, field_counts_option, field_lists_option, skip, filters, + sql_json_path_filters): """ Create a schema containing summary tables about one or more collections. @@ -278,10 +286,9 @@ def add(ctx, collections, note, name, tables_only, field_counts_option, field_li raise click.UsageError('--name is required for more than 5 collections') name = f"collection_{'_'.join(str(_id) for _id in sorted(collections))}" - if filters: - where_fragment = ''.join(construct_where_fragment(db.cursor, field, value) for field, value in filters) - else: - where_fragment = None + where_fragment = ''.join(construct_where_fragment(db.cursor, field, value) for field, value in filters) + where_fragment += ''.join(construct_where_fragment_sql_json_path(db.cursor, sjp_filter) + for sjp_filter in sql_json_path_filters) schema = f'view_data_{name}'