Rapportsystem API - of D03N
Hovedprosjekt FiV Programmering 20-24
Loading...
Searching...
No Matches
tableInfoExtractor.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, timedelta
4from decimal import Decimal
5
6from flask_jwt_extended import get_jwt_identity
7
8#Defines the class data_extractor
10 def __init__(self):
11 pass
12
14 try:
15 #changes @ and . in email to _
16 current_user = get_jwt_identity()
17 key = current_user["password"]
18 dbName = current_user["db_name"]
19 username = current_user["email"]
20
21 #Dict for storing data
22 result_dict = {}
23
24 #Connects to the database server
25 connection = SQLC.SQLConAdmin(None,username,key,dbName)
26 connection.connect()
27
28 #Uses the database
29 query = SQLQ.SQLQueries.use_database(dbName)
30 connection.execute_query(query)
31
32 #Gets all tablenames from the database
33 show_tables_query = SQLQ.SQLQueries.show_tables()
34 all_tables = connection.execute_query(show_tables_query)
35
36 #Gets tablenames and description from the database
37 for table in all_tables:
38 #Gets the current table
39 current_table = table[0]
40
41 #Adds the data to the dictionary
42 table_description = connection.execute_query(SQLQ.SQLQueries.getTableDescription(current_table))
43
44 #Convert datetime objects to strings in the description
45 table_description = {col[0]: str(col[1]) if isinstance(col[1], (datetime, date, time, timedelta, Decimal)) else col[1] for col in table_description}
46
47 #Adds the data to the dictionary
48 result_dict[current_table] = table_description
49
50 # Error handling
51 except Exception as e:
52 print(e)
53 connection.cnx.close()
54 connection.close()
55 return {"Error": "Error when extracting table description from the database for table: " + current_table}
56
57 # Returns closes the connection and returns the data
58 finally:
59 connection.cnx.close()
60 connection.close()
61 return {"Tables": result_dict}