From cbc609d7a5c0b60090af83b51c62bfffa724be52 Mon Sep 17 00:00:00 2001 From: "k.siwek@kainos.com" Date: Wed, 22 Jun 2016 00:19:21 +0200 Subject: [PATCH] bugfixes and nested sorting on location --- src/fhir/search.litcoffee | 63 ++++++++++++++++++++++++-------- src/fhir/search_nested.litcoffee | 9 +++-- 2 files changed, 52 insertions(+), 20 deletions(-) diff --git a/src/fhir/search.litcoffee b/src/fhir/search.litcoffee index 31e3e60..f632b11 100644 --- a/src/fhir/search.litcoffee +++ b/src/fhir/search.litcoffee @@ -55,6 +55,10 @@ appropriate elements from resource by path DEFAULT_RESOURCES_PER_PAGE = 10 + special_resource_types = { + "practitioner" : "'participant'", + "location" : "'location'->0->'location'" + } This is main function: @param query [Object] @@ -293,23 +297,50 @@ implementation based on searchType if name.split(".").length > 1 return name.split(".")[0] + resource_type = (table_name)-> + if special_resource_types[table_name] + return special_resource_types[table_name] + return "'"+table_name+"'" + + build_join_part = (table, nested_table, alias)-> + switch table + when "appointment" + return [[['$raw', "#{nested_table} AS #{alias}"],['$raw', "#{alias}.id = subelem_id"]]] + when "encounter" + resource = resource_type(nested_table) + if resource == "'participant'" + return [[['$raw', "#{nested_table} AS #{alias}"],['$raw', "#{alias}.id = subelem_id"]]] + else + return [[['$raw', "#{table}"], ['$raw', "#{alias}.id=split_part((\"#{table}\".resource->#{resource}->>'reference'), '/', 2)"]]] + + build_from_part = (table, nested_table, alias)-> + switch table + when "appointment" + return ['$raw', "(SELECT split_part((json_array_elements((\"#{table}\".resource->'participant')::json)->'actor'->>'reference')::text, '/', 2)::text as subelem_id, \"#{table}\".* FROM \"#{table}\") AS #{table}"] + when "encounter" + resource = resource_type(nested_table) + if resource == "'participant'" + return ['$raw', "(SELECT split_part((json_array_elements((\"#{table}\".resource->'participant')::json)->'individual'->>'reference')::text, '/', 2)::text as subelem_id, \"#{table}\".* FROM \"#{table}\") AS #{table}"] + else + return ['$alias', ['$q', "#{nested_table}"], alias] + get_nested_order_query = (expr, alias)-> - nested_table_name = nested_table(expr.sort) - table_name = namings.table_name(plv8, expr.query) - if table_name == "appointment" - { - select: ":#{table_name}.*", - from: ['$raw', "(SELECT split_part((json_array_elements((\"#{table_name}\".resource->'participant')::json)->'actor'->>'reference')::text, '/', 2)::text as subelem_id, \"#{table_name}\".* FROM \"#{table_name}\") AS #{table_name}"], - join: [[["$raw", "#{nested_table_name} AS #{alias}"],["$raw", "#{alias}.id = subelem_id"]]], - where: expr.where - } - else if table_name == "encounter" - { - select: ":#{table_name}.*", - from: ['$alias', ['$q', "#{nested_table_name}"], alias], - join: [["#{table_name}", "#{alias}.id=split_part((\"#{table_name}\".resource->'#{nested_table_name}'->>'reference'), '/', 2)"]], - where: expr.where - } + nested_table_name = nested_table(expr.sort) + table_name = namings.table_name(plv8, expr.query) + if table_name == "appointment" + { + select: ":#{table_name}.*", + from: build_from_part(table_name, nested_table_name, alias), + join: build_join_part(table_name, nested_table_name, alias), + where: expr.where + } + else if table_name == "encounter" + { + select: ":#{table_name}.*", + from: build_from_part(table_name, nested_table_name, alias), + join: build_join_part(table_name, nested_table_name, alias), + where: expr.where + } ### Handling chained parameters diff --git a/src/fhir/search_nested.litcoffee b/src/fhir/search_nested.litcoffee index 2bdb6b6..f8ce365 100644 --- a/src/fhir/search_nested.litcoffee +++ b/src/fhir/search_nested.litcoffee @@ -8,9 +8,10 @@ paths = { "patient.given" : "\"tbl1\".resource->'name'->0->'given'->0", "patient.family" : "\"tbl1\".resource->'name'->0->'family'->0", - "patient.birthDate" : "\"tbl1\".resource->'birthDate'", - "practitioner.given" : "\"tbl1\".resource->'name'->0->'given'->0", - "practitioner.family" : "\"tbl1\".resource->'name'->0->'family'->0", + "patient.birthdate" : "\"tbl1\".resource->'birthDate'", + "practitioner.given" : "\"tbl1\".resource->'name'->'given'->0", + "practitioner.family" : "\"tbl1\".resource->'name'->'family'->0", + "location.name" : "\"tbl1\".resource->>'name'" } exports.order_expression = (tbl, meta)-> @@ -18,7 +19,7 @@ expression = paths[elem] if meta.operator == "desc" expression += " DESC" - expression + ['$raw', expression] exports.order_expression.plv8_signature = arguments: ['text', 'json']