def read_all(m: Type[M], **kwargs) -> List[M]: """ Queries the database for all Patients and Reaedings :param m: Type of the model to query for :param kwargs: Keyword arguments mapping column names to values to parameterize the query (e.g., ``patientId="abc"``) :return: A list of models from the database """ # relates to api/android/patients if m.schema() == Patient.schema(): if not kwargs: # get all the patients patient_list = read_all_patients_db() # get all reading + referral + followup reading_list = read_all_readings_db(True, None) # O(n+m) loop. *Requires* patients and readings to be sorted by patientId readingIdx = 0 for p in patient_list: while (readingIdx < len(reading_list) and reading_list[readingIdx]["patientId"] == p["patientId"]): p["readings"].append(reading_list[readingIdx]) readingIdx += 1 return patient_list return m.query.filter_by(**kwargs).all() else: if not kwargs: return m.query.all() return m.query.filter_by(**kwargs).all()
def read_all_admin_view(m: Type[M], **kwargs) -> List[M]: """ Queries the database for all Patients or Referrals :param m: Type of the model to query for :param kwargs: limit, page, search, sortBy, sortDir :return: A list of models from the database """ search_param = (None if kwargs.get("search", None) == "" else kwargs.get( "search", None)) sql_str = SQL.get_sql_string(search_param, **kwargs) sql_str_table = SQL.get_sql_table_operations(m) if m.schema() == Patient.schema(): if search_param is not None: return db_session.execute(sql_str_table + sql_str) else: return db_session.execute(sql_str_table + sql_str) if m.schema() == Referral.schema(): if search_param is not None: return db_session.execute(sql_str_table + sql_str) else: return db_session.execute(sql_str_table + sql_str)
def get_sql_table_operations(m: Type[M]) -> str: if m.schema() == Patient.schema(): return ( "SELECT p.patientName, " "p.patientId, " "p.villageNumber, " "r.trafficLightStatus, " "r.dateTimeTaken" " FROM patient p LEFT JOIN reading r ON r.readingId = " "(SELECT r2.readingId FROM reading r2 WHERE r2.patientId=p.patientId" " ORDER BY r2.dateTimeTaken DESC LIMIT 1) ") elif m.schema() == Referral.schema(): return ("SELECT p.patientId," " p.patientName," " p.villageNumber," " rd.trafficLightStatus," " rf.dateReferred," " rf.isAssessed," " rf.id" " FROM referral rf" " JOIN patient p ON rf.patientId=p.patientId" " JOIN reading rd ON rd.readingId=rf.readingId")