Skip to content

Commit

Permalink
#41: Add and use function for distinct fields
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Sep 11, 2019
1 parent eebf29a commit 2f85368
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions common/database_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ def get_rows_by_filter(table, filters):
filter_handler.add_filter(QueryFilterFactory.get_query_filter(query_filter))
filter_handler.apply_filters(query)
results = query.get_all_results()
if query.is_distinct_fields_query:
return _get_distinct_fields_as_dicts(results)
if query.include_related_entities:
return _get_results_with_include(filters, results)
return list(map(lambda x: x.to_dict(), results))
Expand All @@ -366,6 +368,20 @@ def _get_results_with_include(filters, results):
return [x.to_nested_dict(query_filter["include"]) for x in results]


def _get_distinct_fields_as_dicts(results):
"""
Given a list of column results return a list of dictionaries where each column name is the key and the column value
is the dictionary key value
:param results: A list of sql alchemy result objects
:return: A list of dictionary representations of the sqlalchemy result objects
"""
dictionaries = []
for result in results:
dictionary = {k: getattr(result, k) for k in result.keys()}
dictionaries.append(dictionary)
return dictionaries


def get_first_filtered_row(table, filters):
"""
returns the first row that matches a given filter, in a given table
Expand Down

0 comments on commit 2f85368

Please sign in to comment.