""" Util functions for Pandora. Utility functions specific to the Pandora Analytics ingestion workflow. """ def build_cases_by_country(mapping, default_case='NULL'): """Create SQL CASE statement for use in Snowflake queries. Maps between Pandora values and values in the dimension tables in Snowflake based on country. Can be used for countryname, currencyid, currencycode, or royaltycurrency mappings. Args: mapping (dict): Mapping based on country. default_case (str): Default to use when country cannot be mapped. Returns: str: SQL CASE statement. """ case_format = "WHEN r.country = '{key}' THEN {value}" cases = ' '.join( case_format.format(key=key, value=_normalize_value(value)) for key, value in mapping.items()) return ' CASE {cases} ELSE {default_case} END '.format( cases=cases, default_case=default_case) def _normalize_value(value): """Normalize value for use in SQL statement. Ensures that the value is in the appropriate format for insertion into a query statement. Args: value: (str|int): Value to be normalized. Returns: str: Normalized value for insertion into a SQL statement. """ if type(value) is str: return "'{value}'".format(value=value) return str(value)