Rapportsystem API - of D03N
Hovedprosjekt FiV Programmering 20-24
Loading...
Searching...
No Matches
dataChanger.py
Go to the documentation of this file.
1from SQLAdminConnections import SQL_AdminConnector as SQLC
2from SQLAdminConnections import SQL_AdminQuerys as SQLQ
3from datetime import datetime,date,time
4from decimal import Decimal
5from flask_jwt_extended import get_jwt_identity
6
8 def __init__(self, table_name, data = None):
9 self.current_user = get_jwt_identity()
10 # Sets the username, key and database name
11 self.username = self.current_user["email"]
12 self.key = self.current_user["password"]
13 self.db_name = self.current_user["db_name"]
14
15 # Sets the table name and data
16 self.table_name = table_name
17 self.data = data
18
19 def changeData(self):
20 try:
21 # Connects to the database server
22 connection = SQLC.SQLConAdmin(None, self.username, self.key, self.db_name)
23 connection.connect()
24
25 # Uses the database
26 query = SQLQ.SQLQueries.use_database(self.db_name)
27 connection.execute_query(query)
28
29 # Checks if the table exists
30 existing_tables = [table[0] for table in connection.execute_query(SQLQ.SQLQueries.show_tables())]
31 if self.table_name not in existing_tables:
32 raise Exception(f"Table {self.table_name} does not exist.")
33
34 # Gets the table description
35 table_description = connection.execute_query(SQLQ.SQLQueries.getTableDescription(self.table_name))
36
37 # Converts strings to the correct datatype based on the table description
38 for column_info in table_description:
39 column_name = column_info[0]
40 column_type = column_info[1]
41 if column_name in self.data:
42 if "int" in column_type:
43 self.data[column_name] = int(self.data[column_name])
44 elif "decimal" in column_type:
45 self.data[column_name] = Decimal(self.data[column_name])
46 elif "date" in column_type:
47 self.data[column_name] = datetime.strptime(self.data[column_name], "%Y-%m-%d").date()
48 elif "time" in column_type:
49 self.data[column_name] = datetime.strptime(self.data[column_name], "%H:%M:%S").time()
50 elif "datetime" in column_type:
51 self.data[column_name] = datetime.strptime(self.data[column_name], "%Y-%m-%d %H:%M:%S")
52
53 # Update the last row with new data based on the latest "id"
54 update_query = SQLQ.SQLQueries.update_last_row_by_id(self.table_name, self.data)
55 connection.execute_query(update_query)
56 connection.cnx.commit()
57
58 except Exception as e:
59 print(e)
60 connection.cnx.rollback() # Rollback changes if an error occurs
61 return {"Error": f"Error when updating data in the table: {self.table_name}"}
62
63 finally:
64 connection.cnx.close()
65 connection.close()
66 return {"Success": f"Data updated in the table: {self.table_name}"}
67
68 def deleteLastRow(self):
69 try:
70 # Connects to the database server
71 connection = SQLC.SQLConAdmin(None, self.username, self.key, self.db_name)
72 connection.connect()
73
74 # Uses the database
75 query = SQLQ.SQLQueries.use_database(self.db_name)
76 connection.execute_query(query)
77
78 # Checks if the table exists
79 existing_tables = [table[0] for table in connection.execute_query(SQLQ.SQLQueries.show_tables())]
80 if self.table_name not in existing_tables:
81 raise Exception(f"Table {self.table_name} does not exist.")
82
83 # Deletes the last row from the table based on the latest "id"
84 delete_query = SQLQ.SQLQueries.delete_last_row_by_id(self.table_name)
85 connection.execute_query(delete_query)
86 connection.cnx.commit()
87
88 except Exception as e:
89 print(e)
90 connection.cnx.rollback() # Rollback changes if an error occurs
91 return {"Error": f"Error when deleting last row from the table: {self.table_name}"}
92
93 finally:
94 connection.cnx.close()
95 connection.close()
96 return {"Success": f"Last row deleted from the table: {self.table_name}"}
97