示例#1
0
def code_details_with_site(code, code_system_uri, site):
    # from code details fetching
    # fromCode_details = db.code_details(code, code_system)
    req_fromcode_oid = '''
select c.oid,cs.code_system,cs.uri,cs.version,c.code,c.designation
 from mappings m
 inner join codes c on (c.oid=m.code)
 inner join code_systems cs on (c.code_system=cs.oid)
 inner join sites s on (s.oid=m.site)
 where c.code_system=(select oid from code_systems
                      where uri=:uri)
       and s.site=:site
       and c.code=:code
'''

    with wcursor(True) as cursor:
        fromcodedetails = cursor.execute(req_fromcode_oid, {
            'uri': code_system_uri,
            'code': code,
            'site': site
        }).fetchall()

    if not len(fromcodedetails):
        return "no such code: {}@{}".format(code, code_system_uri), 404

    keys = [
        "id", h.CODE_SYSTEM, h.CODE_SYSTEM_URI, h.CODE_SYSTEM_VERSION, h.CODE,
        h.DESIGNATION
    ]
    code_details = h.dictFromKeyValues(keys, fromcodedetails[0])
    code_details['site'] = site
    return code_details
示例#2
0
def delete_mapping(site=None, concept=None):
    log.warning('XXXXXXXX deleting mapping {}@{}'.format(concept, site))
    assert (site is not None) or (concept is not None)

    d = {'site': site, 'concept': concept}
    where = ' and '.join([
        f"{k}=(select oid from {k}s where {k}=:{k})" for k, v in d.items()
        if v is not None
    ])
    request = f"delete from mappings where {where}"

    with wcursor(False) as cursor:
        try:
            d = h.dictFromKeyValues([h.SITE, h.CONCEPT], [site, concept])
            log_execute(request, d)
            cursor.execute(request, d)
            ans = {'modified_mappings': cursor.rowcount}
            log.warning(ans)

            # delete_code_for_concept(cursor, site=site, concept=concept)
            req = f'''
        delete from codes
          where oid in (select m.code from mappings m where {where})
        '''
            cursor.execute(req, {'site': site, 'concept': concept})
        except Exception as e:
            log.error("!!!! error deleting the mapping")
            log.error(e)
            log.error(request)
            log.error(d)
            db.rollback()
            raise e
    commit_and_rebuild()
    return ans
示例#3
0
def build_all_code_systems():
    with wcursor(readonly=True) as cursor:
        values = cursor.execute(
            "select uri, code_system, version from code_systems").fetchall()
    return [
        h.dictFromKeyValues(
            [h.CODE_SYSTEM_URI, h.CODE_SYSTEM, h.CODE_SYSTEM_VERSION], v)
        for v in values
    ]
示例#4
0
def build_all_codes():
    global codes
    with wcursor(True) as cursor:
        cursor.execute(
            'SELECT code, code_systems.code_system, designation, uri from {}'.
            format(mkjoin('codes', 'code_systems', 'code_system', 'oid')))
        ans = cursor.fetchall()
        codes = [
            h.dictFromKeyValues(
                [h.CODE, h.CODE_SYSTEM, h.DESIGNATION, h.CODE_SYSTEM_URI], e)
            for e in ans
        ]
示例#5
0
def list_all_codes(site=''):

    ans = query_perhaps_condition(condition='where s.site=?',
                                  query='''
select c.code, cs.code_system, cs.uri, c.designation
 from mappings m
 inner join codes c on (m.code=c.oid)
 inner join code_systems cs on (c.code_system=cs.oid)
 inner join sites s on (m.site=s.oid)
 {}
''')(site)
    return [
        h.dictFromKeyValues(
            [h.CODE, h.CODE_SYSTEM, h.CODE_SYSTEM_URI, h.DESIGNATION], e)
        for e in ans
    ]
示例#6
0
def build_all_mappings():
    global mappings
    query = '''
select concepts.concept,cs.code_system,cs.uri,c.code,c.designation,s.site
 from mappings m
 inner join codes c on (m.code = c.oid)
 inner join concepts on (m.concept = concepts.oid)
 inner join sites s on (m.site = s.oid)
 inner join code_systems cs on (c.code_system = cs.oid);
'''
    columns = [
        h.CONCEPT, h.CODE_SYSTEM, h.CODE_SYSTEM_URI, h.CODE, h.DESIGNATION,
        h.SITE
    ]
    with wcursor(True) as cursor:
        mappings = [
            h.dictFromKeyValues(columns, e)
            for e in cursor.execute(query).fetchall()
        ]
示例#7
0
def list_all_code_systems():
    keys = [h.CODE_SYSTEM_URI, h.CODE_SYSTEM, h.CODE_SYSTEM_VERSION]
    with wcursor(True) as cursor:
        values = cursor.execute(
            "select uri, code_system, version from code_systems").fetchall()
    return [h.dictFromKeyValues(keys, v) for v in values]
示例#8
0
def build_all_equivalences():
    global equivalences
    keys = list_column('equivalences', 'equivalence')
    values = list_column('equivalences', 'definition')
    equivalences = h.dictFromKeyValues(keys, values)