-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be13448
commit 40660e8
Showing
6 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" Module for generating condition codeableConcept table""" | ||
import re | ||
from cumulus_library.base_table_builder import BaseTableBuilder | ||
from cumulus_library.helper import get_progress_bar, query_console_output | ||
from cumulus_library.template_sql.templates import ( | ||
get_column_datatype_query, | ||
get_object_denormalize_query, | ||
) | ||
|
||
|
||
class SOEBuilder(BaseTableBuilder): | ||
display_text = "Creating condition code table..." | ||
|
||
def prepare_queries(self, cursor: object, schema: str): | ||
"""Constructs queries related to condition codeableConcept | ||
:param cursor: A database cursor object | ||
:param schema: the schema/db name, matching the cursor | ||
""" | ||
table = "documentreference" | ||
column = "context" | ||
with get_progress_bar(transient=True) as progress: | ||
task = progress.add_task( | ||
"Detecting SOE...", | ||
total=1, | ||
) | ||
|
||
query = get_column_datatype_query(schema, table, column) | ||
cursor.execute(query) | ||
progress.advance(task) | ||
result = str(cursor.fetchone()[0]) | ||
field_config = { | ||
"start": { | ||
"present": False, | ||
"type": "varchar", | ||
}, | ||
"end": {"present": False, "type": "varchar"}, | ||
} | ||
if "period row" in result: | ||
# The following will get all text between parenthesis following | ||
# period row - i.e. the schema of the period object | ||
field_schema_str = re.search(r"period row\(\s*([^\n\r]*)\),", result)[1] | ||
for key in field_config.keys(): | ||
if f"{key} {field_config[key]['type']}" in field_schema_str: | ||
field_config[key]["present"] = True | ||
|
||
self.queries.append( | ||
get_object_denormalize_query( | ||
schema, | ||
table, | ||
"id", | ||
f"{column}.period", | ||
field_config, | ||
"core__soe_doc_period", | ||
) | ||
) | ||
self.write_queries() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
CREATE TABLE "{{schema}}"."{{ target_table }}" AS ( | ||
SELECT | ||
{{ source_id }}, | ||
{%- for key in field_config.keys() %} | ||
{%- if field_config[key]['present'] %} | ||
{{ field }}."{{ key }}" as "{{ key }}" | ||
{%- else %} | ||
cast(NULL AS {{ field_config[key]['type'] }}) as "{{ key }}" | ||
{%- endif %} | ||
{%- if not loop.last -%} | ||
, | ||
{%- endif -%} | ||
{%- endfor %} | ||
FROM {{ source_table }} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters