|
2 | 2 | import os |
3 | 3 | from dotenv import load_dotenv |
4 | 4 |
|
5 | | -def database_connection_exec(procedure: str): # To use when "exec xxxx" (stored procedures) |
6 | | - load_dotenv() |
7 | | - un = os.getenv("un") |
8 | | - cs = os.getenv("cs") |
9 | | - pw = os.getenv("pw") |
10 | | - |
11 | | - try: |
12 | | - print("Attempting DB connection...") |
13 | | - conn = oracledb.connect(user=un, password=pw, dsn=cs) |
14 | | - print(conn.version, "DB connection successful!") |
| 5 | +class OracleDB: |
| 6 | + def __init__(self): |
| 7 | + load_dotenv() |
| 8 | + self.user = os.getenv("un") |
| 9 | + self.dns = os.getenv("cs") |
| 10 | + self.password = os.getenv("pw") |
| 11 | + |
| 12 | + def exec_bcss_timed_events(self, nhs_no: str): # Executes bcss_timed_events when given an NHS number |
15 | 13 | try: |
16 | | - print(f"Attempting to execute stored procedure: {procedure}") |
17 | | - cursor = conn.cursor() |
18 | | - cursor.callproc(procedure) |
19 | | - print(conn.version, "stored procedure execution successful!") |
20 | | - except Exception as executionError: |
21 | | - print(f"Failed to execute stored procedure with execution error: {executionError}") |
22 | | - finally: |
23 | | - if conn is not None: |
24 | | - conn.close() |
25 | | - except Exception as connectionError: |
26 | | - print(f"Failed to connect to the DB! with connection error: {connectionError}") |
27 | | - |
28 | | -def exec_bcss_timed_events(nhs_no: str): # To use when "exec xxxx" (stored procedures) |
29 | | - load_dotenv() |
30 | | - un = os.getenv("un") |
31 | | - cs = os.getenv("cs") |
32 | | - pw = os.getenv("pw") |
33 | | - |
34 | | - try: |
35 | | - print("Attempting DB connection...") |
36 | | - conn = oracledb.connect(user=un, password=pw, dsn=cs) |
37 | | - print(conn.version, "DB connection successful!") |
38 | | - try: |
39 | | - print(f"Attempting to get subject_id from nhs number: {nhs_no}") |
40 | | - cursor = conn.cursor() |
41 | | - cursor.execute(f"SELECT SCREENING_SUBJECT_ID FROM SCREENING_SUBJECT_T WHERE SUBJECT_NHS_NUMBER = {int(nhs_no)}") |
42 | | - result = cursor.fetchall() |
43 | | - subject_id = result[0][0] |
44 | | - print(conn.version, "Able to extract subject ID") |
| 14 | + print("Attempting DB connection...") |
| 15 | + conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns) |
| 16 | + print(conn.version, "DB connection successful!") |
45 | 17 | try: |
46 | | - print(f"Attempting to execute stored procedure: {f"'bcss_timed_events', [subject_id,'Y']"}") |
| 18 | + print(f"Attempting to get subject_id from nhs number: {nhs_no}") |
47 | 19 | cursor = conn.cursor() |
48 | | - cursor.callproc('bcss_timed_events', [subject_id,'Y']) |
49 | | - print(conn.version, "stored procedure execution successful!") |
50 | | - except Exception as spExecutionError: |
51 | | - print(f"Failed to execute stored procedure with execution error: {spExecutionError}") |
52 | | - except Exception as queryExecutionError: |
53 | | - print(f"Failed to to extract subject ID with error: {queryExecutionError}") |
54 | | - finally: |
55 | | - if conn is not None: |
56 | | - conn.close() |
57 | | - except Exception as connectionError: |
58 | | - print(f"Failed to connect to the DB! with connection error: {connectionError}") |
59 | | - |
60 | | -def database_connection_query(query: str,): # To use when "exec xxxx" (stored procedures) |
61 | | - load_dotenv() |
62 | | - un = os.getenv("un") |
63 | | - cs = os.getenv("cs") |
64 | | - pw = os.getenv("pw") |
65 | | - |
66 | | - try: |
67 | | - print("Attempting DB connection...") |
68 | | - conn = oracledb.connect(user=un, password=pw, dsn=cs) |
69 | | - print(conn.version, "DB connection successful!") |
| 20 | + cursor.execute(f"SELECT SCREENING_SUBJECT_ID FROM SCREENING_SUBJECT_T WHERE SUBJECT_NHS_NUMBER = {int(nhs_no)}") |
| 21 | + result = cursor.fetchall() |
| 22 | + subject_id = result[0][0] |
| 23 | + print(conn.version, "Able to extract subject ID") |
| 24 | + try: |
| 25 | + print(f"Attempting to execute stored procedure: {f"'bcss_timed_events', [subject_id,'Y']"}") |
| 26 | + cursor = conn.cursor() |
| 27 | + cursor.callproc('bcss_timed_events', [subject_id,'Y']) |
| 28 | + print(conn.version, "stored procedure execution successful!") |
| 29 | + except Exception as spExecutionError: |
| 30 | + print(f"Failed to execute stored procedure with execution error: {spExecutionError}") |
| 31 | + except Exception as queryExecutionError: |
| 32 | + print(f"Failed to to extract subject ID with error: {queryExecutionError}") |
| 33 | + finally: |
| 34 | + if conn is not None: |
| 35 | + conn.close() |
| 36 | + except Exception as connectionError: |
| 37 | + print(f"Failed to connect to the DB! with connection error: {connectionError}") |
| 38 | + |
| 39 | + def populate_ui_approved_users_table(self, user: str): # To add users to the UI_APPROVED_USERS table |
70 | 40 | try: |
71 | | - print(f"Attempting to execute query: {query}") |
72 | | - cursor = conn.cursor() |
73 | | - cursor.execute(query) |
74 | | - result = cursor.fetchall() |
75 | | - print(conn.version, "query execution successful!") |
76 | | - except Exception as executionError: |
77 | | - print(f"Failed to execute query with execution error {executionError}") |
78 | | - finally: |
79 | | - if conn is not None: |
80 | | - conn.close() |
81 | | - return result |
82 | | - except Exception as connectionError: |
83 | | - print(f"Failed to connect to the DB! with connection error {connectionError}") |
84 | | - |
85 | | -def populate_ui_approved_users_table(user: str): # To add users to the UI_APPROVED_USERS table |
86 | | - load_dotenv() |
87 | | - un = os.getenv("un") |
88 | | - cs = os.getenv("cs") |
89 | | - pw = os.getenv("pw") |
90 | | - |
91 | | - try: |
92 | | - print("Attempting DB connection...") |
93 | | - conn = oracledb.connect(user=un, password=pw, dsn=cs) |
94 | | - print(conn.version, "DB connection successful!") |
95 | | - try: |
96 | | - print("Attempting to write to the db...") |
97 | | - cursor = conn.cursor() |
98 | | - cursor.execute(f'INSERT INTO "UI_APPROVED_USERSf" (OE_USER_CODE) VALUES ({user})') |
99 | | - print(conn.version, "DB write successful!") |
100 | | - except Exception as dbWriteError: |
101 | | - print(f"Failed to write to the DB! with write error {dbWriteError}") |
102 | | - finally: |
103 | | - if conn is not None: |
104 | | - conn.close() |
105 | | - except Exception as connectionError: |
106 | | - print(f"Failed to connect to the DB! with connection error {connectionError}") |
107 | | - |
108 | | - |
109 | | - |
110 | | - |
111 | | - |
112 | | - |
113 | | - |
114 | | - |
| 41 | + print("Attempting DB connection...") |
| 42 | + conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns) |
| 43 | + print(conn.version, "DB connection successful!") |
| 44 | + try: |
| 45 | + print("Attempting to write to the db...") |
| 46 | + cursor = conn.cursor() |
| 47 | + cursor.execute(f'INSERT INTO "UI_APPROVED_USERSf" (OE_USER_CODE) VALUES ({user})') |
| 48 | + print(conn.version, "DB write successful!") |
| 49 | + except Exception as dbWriteError: |
| 50 | + print(f"Failed to write to the DB! with write error {dbWriteError}") |
| 51 | + finally: |
| 52 | + if conn is not None: |
| 53 | + conn.close() |
| 54 | + except Exception as connectionError: |
| 55 | + print(f"Failed to connect to the DB! with connection error {connectionError}") |
| 56 | + |
| 57 | + # The following two functions are commented out as they are not used currently, but may be needed in future compartments |
| 58 | + |
| 59 | + # def execute_stored_procedure(self, procedure: str): # To use when "exec xxxx" (stored procedures) |
| 60 | + # try: |
| 61 | + # print("Attempting DB connection...") |
| 62 | + # conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns) |
| 63 | + # print(conn.version, "DB connection successful!") |
| 64 | + # try: |
| 65 | + # print(f"Attempting to execute stored procedure: {procedure}") |
| 66 | + # cursor = conn.cursor() |
| 67 | + # cursor.callproc(procedure) |
| 68 | + # print(conn.version, "stored procedure execution successful!") |
| 69 | + # except Exception as executionError: |
| 70 | + # print(f"Failed to execute stored procedure with execution error: {executionError}") |
| 71 | + # finally: |
| 72 | + # if conn is not None: |
| 73 | + # conn.close() |
| 74 | + # except Exception as connectionError: |
| 75 | + # print(f"Failed to connect to the DB! with connection error: {connectionError}") |
| 76 | + |
| 77 | + # def execute_query(self, query: str,): # To use when "select xxxx" (stored procedures) |
| 78 | + # try: |
| 79 | + # print("Attempting DB connection...") |
| 80 | + # conn = oracledb.connect(user=self.user, password=self.password, dsn=self.dns) |
| 81 | + # print(conn.version, "DB connection successful!") |
| 82 | + # try: |
| 83 | + # print(f"Attempting to execute query: {query}") |
| 84 | + # cursor = conn.cursor() |
| 85 | + # cursor.execute(query) |
| 86 | + # result = cursor.fetchall() |
| 87 | + # print(conn.version, "query execution successful!") |
| 88 | + # except Exception as executionError: |
| 89 | + # print(f"Failed to execute query with execution error {executionError}") |
| 90 | + # finally: |
| 91 | + # if conn is not None: |
| 92 | + # conn.close() |
| 93 | + # return result |
| 94 | + # except Exception as connectionError: |
| 95 | + # print(f"Failed to connect to the DB! with connection error {connectionError}") |
0 commit comments