Skip to content

Commit

Permalink
tmp --filter-sql-json-path
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjwebb committed Nov 24, 2021
1 parent 351b5fc commit eb701b9
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions manage.py
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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}'

Expand Down

0 comments on commit eb701b9

Please sign in to comment.