@@ -14,6 +14,9 @@ def __init__(self):
1414 self .password = os .getenv ("pw" )
1515
1616 def connect_to_db (self ) -> oracledb .Connection :
17+ """
18+ This function is used to connect to the Oracle DB. All the credentials are retrieved from a .env file
19+ """
1720 try :
1821 logging .info ("Attempting DB connection..." )
1922 conn = oracledb .connect (user = self .user , password = self .password , dsn = self .dns )
@@ -22,8 +25,16 @@ def connect_to_db(self) -> oracledb.Connection:
2225 except Exception as queryExecutionError :
2326 logging .error (f"Failed to to extract subject ID with error: { queryExecutionError } " )
2427
28+ def disconnect_from_db (self , conn ):
29+ conn .close ()
30+ logging .info ("Connection Closed" )
2531
2632 def exec_bcss_timed_events (self , nhs_number_df ): # Executes bcss_timed_events when given NHS numbers
33+ """
34+ this function is used to execute bcss_timed_events against NHS Numbers.
35+ 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
36+ Once a subject_screening_id is retrieved, it will then run the command: exec bcss_timed_events [<subject_id>,'Y']
37+ """
2738 conn = self .connect_to_db ()
2839 try :
2940 for index , row in nhs_number_df .iterrows ():
@@ -40,10 +51,12 @@ def exec_bcss_timed_events(self, nhs_number_df): # Executes bcss_timed_events w
4051 logging .error (f"Failed to to extract subject ID with error: { queryExecutionError } " )
4152 finally :
4253 if conn is not None :
43- conn .close ()
44- logging .info ("Connection closed" )
54+ self .disconnect_from_db (conn )
4555
4656 def get_subject_id_from_nhs_number (self , nhs_number ) -> str :
57+ """
58+ This function is used to obtain the subject_screening_id of a subject when given an nhs number
59+ """
4760 conn = self .connect_to_db ()
4861 logging .info (f"Attempting to get subject_id from nhs number: { nhs_number } " )
4962 cursor = conn .cursor ()
@@ -55,6 +68,9 @@ def get_subject_id_from_nhs_number(self, nhs_number) -> str:
5568 return subject_id
5669
5770 def populate_ui_approved_users_table (self , user : str ): # To add users to the UI_APPROVED_USERS table
71+ """
72+ This function is used to add a user to the UI_APPROVED_USERS table
73+ """
5874 conn = self .connect_to_db ()
5975 try :
6076 logging .info ("Attempting to write to the db..." )
@@ -66,41 +82,49 @@ def populate_ui_approved_users_table(self, user: str): # To add users to the UI
6682 logging .error (f"Failed to write to the DB! with write error { dbWriteError } " )
6783 finally :
6884 if conn is not None :
69- conn .close ()
70- logging .info ("Connection closed" )
85+ self .disconnect_from_db (conn )
7186
7287 def delete_all_users_from_approved_users_table (self ): # To remove all users from the UI_APPROVED_USERS table
88+ """
89+ This function is used to remove users from the UI_APPROVED_USERS table where OE_USER_CODE is not null
90+ """
7391 conn = self .connect_to_db ()
7492 try :
7593 logging .info ("Attempting to delete users from DB table..." )
7694 cursor = conn .cursor ()
77- cursor .execute (f "DELETE FROM UI_APPROVED_USERS WHERE OE_USER_CODE is not null" )
95+ cursor .execute ("DELETE FROM UI_APPROVED_USERS WHERE OE_USER_CODE is not null" )
7896 conn .commit ()
7997 logging .info ("DB table values successfully deleted!" )
8098 except Exception as dbValuesDeleteError :
8199 logging .error (
82100 f"Failed to delete values from the DB table! with data deletion error { dbValuesDeleteError } " )
83101 finally :
84102 if conn is not None :
85- conn .close ()
86- logging .info ("Connection Closed" )
103+ self .disconnect_from_db (conn )
87104
88105 def execute_query (self , query : str ) -> pd .DataFrame : # To use when "select xxxx" (stored procedures)
106+ """
107+ This is used to execute any sql queries.
108+ A query is provided and then the result is returned as a pandas dataframe
109+ """
89110 conn = self .connect_to_db ()
90111 engine = create_engine ('oracle+oracledb://' , creator = lambda : conn )
91112 try :
92- logging .info (f "Attempting to execute query" )
113+ logging .info ("Attempting to execute query" )
93114 df = pd .read_sql (query , engine )
94- logging .info ("query execution successful!" )
115+ logging .info ("Query execution successful!" )
95116 except Exception as executionError :
96117 logging .error (f"Failed to execute query with execution error { executionError } " )
97118 finally :
98119 if conn is not None :
99- conn .close ()
100- logging .info ("Connection Closed - Returning results" )
120+ self .disconnect_from_db (conn )
101121 return df
102122
103123 def execute_stored_procedure (self , procedure : str ): # To use when "exec xxxx" (stored procedures)
124+ """
125+ This is to be used whenever we need to execute a stored procedure.
126+ It is provided with the stored procedure name and then executes it
127+ """
104128 conn = self .connect_to_db ()
105129 try :
106130 logging .info (f"Attempting to execute stored procedure: { procedure } " )
@@ -112,10 +136,13 @@ def execute_stored_procedure(self, procedure: str): # To use when "exec xxxx" (
112136 logging .error (f"Failed to execute stored procedure with execution error: { executionError } " )
113137 finally :
114138 if conn is not None :
115- conn .close ()
116- logging .info ("Connection Closed" )
139+ self .disconnect_from_db (conn )
117140
118141 def update_or_insert_data_to_table (self , statement , params ): # To update or insert data into a table
142+ """
143+ This is used to update or insert data into a table.
144+ It is provided with the SQL statement along with the arguments
145+ """
119146 conn = self .connect_to_db ()
120147 try :
121148 logging .info ("Attempting to insert/update table" )
@@ -127,5 +154,4 @@ def update_or_insert_data_to_table(self, statement, params): # To update or ins
127154 logging .error (f"Failed to insert/update values from the DB table! with error { dbUpdateInsertError } " )
128155 finally :
129156 if conn is not None :
130- conn .close ()
131- logging .info ("Connection Closed" )
157+ self .disconnect_from_db (conn )
0 commit comments