Example #1
0
def get_pcmis_views(engine: Engine, progargs: Any,
                    ddhint: DDHint) -> List[ViewMaker]:  # ddhint modified
    def q(identifier: str) -> str:
        return grammar.quote_identifier(identifier)

    grammar = make_grammar(engine.dialect.name)

    views = []  # type: List[ViewMaker]
    tables = get_table_names(engine, sort=True)
    for tablename in tables:
        need_view = True
        viewname = tablename + CRATE_VIEW_SUFFIX
        viewmaker = ViewMaker(viewname=viewname,
                              engine=engine,
                              basetable=tablename,
                              rename=None,
                              progargs=progargs,
                              enforce_same_n_rows_as_base=True)

        # 1. SELECT all the table's own columns
        # ... done automatically by the viewmaker

        # 2. If the patient ID isn't present, link it in.
        columns = get_column_names(engine, tablename, sort=True)
        if PCMIS_COL_PATIENT_ID not in columns:
            need_view = True

            # Not specifically for this, but we'll need it everywhere:
            viewmaker.record_lookup_table_keyfield(PCMIS_TABLE_MASTER_PATIENT,
                                                   PCMIS_COL_PATIENT_ID)

            if PCMIS_COL_CASE_NUMBER in columns:
                viewmaker.add_select("{referrals}.{pid} AS {pid}".format(
                    referrals=PCMIS_TABLE_REFERRAL_DETAILS,
                    pid=PCMIS_COL_PATIENT_ID))
                viewmaker.add_from(
                    "LEFT JOIN {referrals} ON {t}.{case} = {referrals}.{case}".
                    format(  # noqa
                        referrals=PCMIS_TABLE_REFERRAL_DETAILS,
                        t=q(tablename),
                        case=PCMIS_COL_CASE_NUMBER))
                viewmaker.record_lookup_table_keyfield(
                    PCMIS_TABLE_REFERRAL_DETAILS, PCMIS_COL_CASE_NUMBER)
                viewmaker.request_index(tablename, PCMIS_COL_CASE_NUMBER)

            elif PCMIS_COL_CONTACT_NUMBER in columns:
                # ... and PCMIS_COL_CASE_NUMBER is not...
                viewmaker.add_select("{contacts}.{case} AS {case}".format(
                    contacts=PCMIS_TABLE_CASE_CONTACTS,
                    case=PCMIS_COL_CASE_NUMBER))
Example #2
0
def get_pcmis_views(engine: Engine, configoptions: PcmisConfigOptions,
                    ddhint: DDHint) -> List[ViewMaker]:
    """
    Gets all PCMIS view definitions.

    Args:
        engine: an SQLAlchemy Engine
        configoptions:
            an instance of :class:`PcmisConfigOptions`
        ddhint: a :class:`crate_anon/preprocess/ddhint.DDHint`, which will be
            modified

    Returns:
        a list of :class:`crate_anon.common.sql.ViewMaker` objects

    """
    def q(identifier: str) -> str:
        return grammar.quote_identifier(identifier)

    grammar = make_grammar(engine.dialect.name)

    views = []  # type: List[ViewMaker]
    tables = get_table_names(engine, sort=True)
    for tablename in tables:
        need_view = True
        viewname = tablename + CRATE_VIEW_SUFFIX
        viewmaker = ViewMaker(viewname=viewname,
                              engine=engine,
                              basetable=tablename,
                              rename=None,
                              userobj=None,
                              enforce_same_n_rows_as_base=True)

        # 1. SELECT all the table's own columns
        # ... done automatically by the viewmaker

        # 2. If the patient ID isn't present, link it in.
        columns = get_column_names(engine, tablename, sort=True)
        if PCMIS_COL_PATIENT_ID not in columns:
            need_view = True

            # Not specifically for this, but we'll need it everywhere:
            viewmaker.record_lookup_table_keyfield(PCMIS_TABLE_MASTER_PATIENT,
                                                   PCMIS_COL_PATIENT_ID)

            if PCMIS_COL_CASE_NUMBER in columns:
                viewmaker.add_select("{referrals}.{pid} AS {pid}".format(
                    referrals=PCMIS_TABLE_REFERRAL_DETAILS,
                    pid=PCMIS_COL_PATIENT_ID))
                viewmaker.add_from(
                    "LEFT JOIN {referrals} ON {t}.{case} = {referrals}.{case}".
                    format(  # noqa
                        referrals=PCMIS_TABLE_REFERRAL_DETAILS,
                        t=q(tablename),
                        case=PCMIS_COL_CASE_NUMBER))
                viewmaker.record_lookup_table_keyfield(
                    PCMIS_TABLE_REFERRAL_DETAILS, PCMIS_COL_CASE_NUMBER)
                viewmaker.request_index(tablename, PCMIS_COL_CASE_NUMBER)

            elif PCMIS_COL_CONTACT_NUMBER in columns:
                # ... and PCMIS_COL_CASE_NUMBER is not...
                viewmaker.add_select("{contacts}.{case} AS {case}".format(
                    contacts=PCMIS_TABLE_CASE_CONTACTS,
                    case=PCMIS_COL_CASE_NUMBER))