@@ -44,25 +44,43 @@ def disconnect_from_db(self, conn: oracledb.Connection) -> None:
4444 logging .debug ("Connection Closed" )
4545
4646 def exec_bcss_timed_events (
47- self , nhs_number_df : pd .DataFrame
48- ) -> None : # Executes bcss_timed_events when given NHS numbers
47+ self ,
48+ nhs_number_df : Optional [pd .DataFrame ] = None ,
49+ nhs_number : Optional [str ] = None ,
50+ ) -> None :
4951 """
50- This function is used to execute bcss_timed_events against NHS Numbers.
51- It expects the nhs_numbers to be in a dataframe, and runs a for loop to get the subject_screening_id for each nhs number
52- Once a subject_screening_id is retrieved, it will then run the command: exec bcss_timed_events [<subject_id>,'Y']
52+ Executes bcss_timed_events for either a DataFrame of NHS numbers or a single NHS number.
5353
5454 Args:
55- nhs_number_df (pd.DataFrame): A dataframe containing all of the NHS numbers as separate rows
55+ nhs_number_df (Optional[pd.DataFrame]): DataFrame with NHS numbers under 'subject_nhs_number'.
56+ nhs_number (Optional[str]): A single NHS number.
57+
58+ Raises:
59+ ValueError: If neither nhs_number_df nor nhs_number is provided.
5660 """
5761 conn = self .connect_to_db ()
5862 try :
59- for index , row in nhs_number_df .iterrows ():
60- subject_id = self .get_subject_id_from_nhs_number (
61- row ["subject_nhs_number" ]
62- )
63+ if nhs_number_df is not None :
64+ for _ , row in nhs_number_df .iterrows ():
65+ subject_id = self .get_subject_id_from_nhs_number (
66+ row ["subject_nhs_number" ]
67+ )
68+ try :
69+ logging .info (
70+ f"[ORACLE] Attempting to execute stored procedure: 'bcss_timed_events', [{ subject_id } , 'Y']"
71+ )
72+ cursor = conn .cursor ()
73+ cursor .callproc ("bcss_timed_events" , [subject_id , "Y" ])
74+ logging .info ("Stored procedure execution successful!" )
75+ except Exception as spExecutionError :
76+ logging .error (
77+ f"[ORACLE] Failed to execute stored procedure with execution error: { spExecutionError } "
78+ )
79+ elif nhs_number is not None :
80+ subject_id = self .get_subject_id_from_nhs_number (nhs_number )
6381 try :
6482 logging .info (
65- f"[ORACLE] Attempting to execute stored procedure: { f" 'bcss_timed_events', [{ subject_id } ,'Y']" } "
83+ f"[ORACLE] Attempting to execute stored procedure: 'bcss_timed_events', [{ subject_id } , 'Y']"
6684 )
6785 cursor = conn .cursor ()
6886 cursor .callproc ("bcss_timed_events" , [subject_id , "Y" ])
@@ -71,9 +89,11 @@ def exec_bcss_timed_events(
7189 logging .error (
7290 f"[ORACLE] Failed to execute stored procedure with execution error: { spExecutionError } "
7391 )
92+ else :
93+ raise ValueError ("Must provide either nhs_number_df or nhs_number" )
7494 except Exception as queryExecutionError :
7595 logging .error (
76- f"[ORACLE] Failed to to extract subject ID with error: { queryExecutionError } "
96+ f"[ORACLE] Failed to extract subject ID with error: { queryExecutionError } "
7797 )
7898 finally :
7999 if conn is not None :
0 commit comments