|
1 | 1 | from urllib.parse import quote |
2 | 2 |
|
3 | 3 | from settings import config |
4 | | -from sqlalchemy import create_engine, event |
| 4 | +from sqlalchemy import MetaData, Table, create_engine, event |
5 | 5 | from sqlalchemy.engine.interfaces import DBAPIConnection |
6 | 6 | from sqlalchemy.ext.declarative import declarative_base |
7 | 7 | from sqlalchemy.orm import sessionmaker |
@@ -46,3 +46,78 @@ def set_timezone( |
46 | 46 | SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) |
47 | 47 |
|
48 | 48 | Base = declarative_base() |
| 49 | + |
| 50 | +metadata = MetaData() |
| 51 | + |
| 52 | + |
| 53 | +def reflect_tables() -> dict[str, Table]: |
| 54 | + """ |
| 55 | + Reflect tables and views from the specified schemas. |
| 56 | + Returns dynamically reflected tables. |
| 57 | + """ |
| 58 | + result_schema = config.omop.result_schema # Schema for result views |
| 59 | + data_schema = config.omop.data_schema # Schema for data tables |
| 60 | + |
| 61 | + interval_result = Table( |
| 62 | + "interval_result", metadata, autoload_with=engine, schema=result_schema |
| 63 | + ) |
| 64 | + full_day_coverage = Table( |
| 65 | + "full_day_coverage", metadata, autoload_with=engine, schema=result_schema |
| 66 | + ) |
| 67 | + partial_day_coverage = Table( |
| 68 | + "partial_day_coverage", metadata, autoload_with=engine, schema=result_schema |
| 69 | + ) |
| 70 | + criterion = Table("criterion", metadata, autoload_with=engine, schema=result_schema) |
| 71 | + |
| 72 | + concept = Table("concept", metadata, autoload_with=engine, schema=data_schema) |
| 73 | + person = Table("person", metadata, autoload_with=engine, schema=data_schema) |
| 74 | + measurement = Table( |
| 75 | + "measurement", metadata, autoload_with=engine, schema=data_schema |
| 76 | + ) |
| 77 | + visit_occurrence = Table( |
| 78 | + "visit_occurrence", metadata, autoload_with=engine, schema=data_schema |
| 79 | + ) |
| 80 | + drug_exposure = Table( |
| 81 | + "drug_exposure", metadata, autoload_with=engine, schema=data_schema |
| 82 | + ) |
| 83 | + observation = Table( |
| 84 | + "observation", metadata, autoload_with=engine, schema=data_schema |
| 85 | + ) |
| 86 | + condition_occurrence = Table( |
| 87 | + "condition_occurrence", metadata, autoload_with=engine, schema=data_schema |
| 88 | + ) |
| 89 | + procedure_occurrence = Table( |
| 90 | + "procedure_occurrence", metadata, autoload_with=engine, schema=data_schema |
| 91 | + ) |
| 92 | + |
| 93 | + return { |
| 94 | + "interval_result": interval_result, |
| 95 | + "full_day_coverage": full_day_coverage, |
| 96 | + "partial_day_coverage": partial_day_coverage, |
| 97 | + "criterion": criterion, |
| 98 | + "concept": concept, |
| 99 | + "person": person, |
| 100 | + "measurement": measurement, |
| 101 | + "visit_occurrence": visit_occurrence, |
| 102 | + "drug_exposure": drug_exposure, |
| 103 | + "observation": observation, |
| 104 | + "condition_occurrence": condition_occurrence, |
| 105 | + "procedure_occurrence": procedure_occurrence, |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | +# Reflect tables and expose them |
| 110 | +tables = reflect_tables() |
| 111 | +interval_result = tables["interval_result"] |
| 112 | +full_day_coverage = tables["full_day_coverage"] |
| 113 | +partial_day_coverage = tables["partial_day_coverage"] |
| 114 | +criterion = tables["criterion"] |
| 115 | + |
| 116 | +concept = tables["concept"] |
| 117 | +person = tables["person"] |
| 118 | +measurement = tables["measurement"] |
| 119 | +visit_occurrence = tables["visit_occurrence"] |
| 120 | +drug_exposure = tables["drug_exposure"] |
| 121 | +observation = tables["observation"] |
| 122 | +condition_occurrence = tables["condition_occurrence"] |
| 123 | +procedure_occurrence = tables["procedure_occurrence"] |
0 commit comments