def set_appointment():
    try:
        patient_id = read_while_not_set("Patient's id: ", on_bad_input="Id must be set")
        doctor_id = read_while_not_set("Doctor's id: ", on_bad_input="Id must be set")
        db_queries.add_session(patient_id, doctor_id)
    except Exception:
        print("Database error: can't add a session")
        raise
def show_table():
    try:
        table_name = read_while_not_set("Type the table's name: ", on_bad_input="Table name can't be empty")
        db_queries.show_table(table_name)
    except Exception:
        print("No such table")
        raise
def get_doctor_ids():
    try:
        doctor_name = read_while_not_set("Doctor's name: ", on_bad_input="Doctor's name can't be empty")
        db_queries.get_ids_by_dname(doctor_name)
    except Exception:
        print("Can't get the ids")
        raise
def get_patient_ids():
    try:
        patient_name = read_while_not_set("Patient's name: ", on_bad_input="Patient's name can't be empty")
        db_queries.get_ids_by_pname(patient_name)
    except Exception:
        print("Can't get the ids")
        raise
def create_doctor_profile():
    conn, cursor = connection.get_conn_cursor()
    # см. предыдущий
    doctor_name = read_while_not_set("Doctor's full name:",
                                     on_bad_input="Name cannot be empty")
    degree = read_and_process('Highest degree obtained: ')
    speciality = read_and_process('Main speciality: ')
    seniority = read_and_process('Seniority, years: ')
    position = read_and_process('Current position: ')
    salary = read_and_process("Salary, $: ")

    logs_query = "INSERT INTO logs.doctors (doctor_name, degree, " \
                 "speciality, seniority, position, salary)" \
                 " VALUES (%s, %s, %s, %s, %s, %s)"

    try:
        cursor.execute(
            logs_query,
            [doctor_name, degree, speciality, seniority, position, salary])
    except:
        conn.rollback()
        raise

    try:
        conn.commit()
    except:
        raise

    print("Doctors's personal data added")
def update_patient_profile():
    try:
        patient_id = read_while_not_set("Patient's id: ", on_bad_input="Id must be set")
    except Exception:
        raise
    if db_queries.check_id_existence(table_name='logs.patients', id_name='patient_id', id_val=patient_id):
        try:
            db_queries.update_patient_profile(patient_id)
        except Exception:
            print("Database error: can't update patient's profile")
            raise
    else:
        print("No data about such a patient's id")
def update_record():
    try:
        record_id = read_while_not_set("Record's id: ", on_bad_input="Id must be set")
    except Exception:
        raise
    if db_queries.check_id_existence(table_name='logs.medical_log', id_name='case_id', id_val=record_id):
        try:
            db_queries.update_record(record_id)
        except Exception:
            print("Database error: can't update patient's record")
            raise
    else:
        print("No data about such a patient's id")
def update_doctor_profile():
    try:
        doctor_id = read_while_not_set("Doctor's id: ", on_bad_input="Id must be set")
    except Exception:
        raise
    if db_queries.check_id_existence(table_name='logs.doctors', id_name='doctor_id', id_val=doctor_id):
        try:
            db_queries.update_doctor_profile(doctor_id)
        except Exception:
            print("Database error: can't update doctor's profile")
            raise
    else:
        print("No data about such a doctor's id")
def get_patient_data():
    try:
        patient_id = read_while_not_set("Patient's id: ", on_bad_input="Id must be set")
    except Exception:
        raise
    if db_queries.check_id_existence(table_name='logs.patients', id_name='patient_id', id_val=patient_id):
        try:
            db_queries.show_patient_personal_data(patient_id)
        except Exception:
            print("Database error: can't show patient's data")
            raise
    else:
        print("No data about such a patient's id")
def create_patient_profile():
    conn, cursor = connection.get_conn_cursor()
    patient_name = read_while_not_set("Patient's full name",
                                      on_bad_input="Name cannot be empty")
    patient_sex = read_while_not_valid(
        "Patient's biological sex: ",
        on_bad_input="Invalid sex: only M and F are allowed",
        validator_function=lambda ch: ch == 'M' or ch == 'F')
    date_of_birth = read_while_not_valid(
        "Date of birth, YYYY-MM-DD: ",
        on_bad_input="Invalid date format",
        validator_function=input_processing.is_valid_date)
    ethnicity = read_and_process('Ethnicity: ')
    relationship_status = read_and_process('Relationship status: ')
    address = read_and_process('Home address: ')
    phone_number = read_and_process('Phone number: ')
    email = read_and_process("Email: ")

    logs_query = "INSERT INTO logs.patients (patient_name, sex, date_of_birth" \
                 "ethnicity, relationship_status, address, phone_number, email)" \
                 " VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"

    try:
        cursor.execute(logs_query, [
            patient_name, patient_sex, date_of_birth, ethnicity,
            relationship_status, address, phone_number, email
        ])
    except:
        conn.rollback()
        raise

    try:
        conn.commit()
    except:
        raise

    print("Patient's personal data added")