Terarea  2
The automation project
Loading...
Searching...
No Matches
sql_manager.py
Go to the documentation of this file.
1"""
2 File in charge of containing the interfacing between an sql library and the program.
3 This contains functions that simplify the process of interracting with databases as well as check for injection attempts.
4"""
5
6from display_tty import Disp, TOML_CONF, SAVE_TO_FILE, FILE_NAME
7
8from .sql_time_manipulation import SQLTimeManipulation
9from .sql_connections import SQLManageConnections
10from .sql_query_boilerplates import SQLQueryBoilerplates
11
12
13class SQL:
14 """
15 The class in charge of managing a SQL database
16 """
17
18 def __init__(self, url: str, port: int, username: str, password: str, db_name: str, success: int = 0, error: int = 84, debug: bool = False):
19 """
20 The constructor of the SQL class
21 Args:
22 url (str): _description_
23 port (int): _description_
24 username (str): _description_
25 password (str): _description_
26 db_name (str): _description_
27 success (int, optional): _description_. Defaults to 0.
28 error (int, optional): _description_. Defaults to 84.
29 debug (bool, optional): _description_. Defaults to False.
30 """
31 self.debug: bool = debug
32 self.success: int = success
33 self.error: int = error
34 self.url: str = url
35 self.port: int = port
36 self.username: str = username
37 self.password: str = password
38 self.db_name: str = db_name
39 # ----------------- Pre class variable initialisation -----------------
40 self.disp: Disp = None
41 self.sql_manage_connections: SQLManageConnections = None
42 self.sql_time_manipulation: SQLTimeManipulation = None
43 self.sql_query_boilerplates: SQLQueryBoilerplates = None
44 # --------------------------- logger section ---------------------------
45 self.disp: Disp = Disp(
46 TOML_CONF,
47 SAVE_TO_FILE,
48 FILE_NAME,
49 debug=self.debug,
50 logger=self.__class__.__name__
51 )
52 # ------------- The class in charge of the sql connection -------------
53 self.sql_manage_connections: SQLManageConnections = SQLManageConnections(
54 url=self.url,
55 port=self.port,
56 username=self.username,
57 password=self.password,
58 db_name=self.db_name,
59 success=self.success,
60 error=self.error,
61 debug=self.debug
62 )
63
64 # ---------------------------- Time logger ----------------------------
65 self.sql_time_manipulation: SQLTimeManipulation = SQLTimeManipulation(
66 self.debug
67 )
68 self.datetime_to_string: SQLTimeManipulation.datetime_to_string = self.sql_time_manipulation.datetime_to_string
69 self.string_to_datetime: SQLTimeManipulation.string_to_datetime = self.sql_time_manipulation.string_to_datetime
70 self._get_correct_now_value: SQLTimeManipulation.get_correct_now_value = self.sql_time_manipulation.get_correct_now_value
71 self._get_correct_current_date_value: SQLTimeManipulation.get_correct_current_date_value = self.sql_time_manipulation.get_correct_current_date_value
72 # --------------------------- debug section ---------------------------
73 self.sql_manage_connections.show_connection_info("__init__")
74 # --------------------------- initialise pool --------------------------
75 if self.sql_manage_connections.initialise_pool() != self.success:
76 msg = "Failed to initialise the connection pool."
77 self.disp.log_critical(msg, "__init__")
78 raise RuntimeError(f"Error: {msg}")
79 # ----------------------- sql query boilerplates -----------------------
80 self.sql_query_boilerplates: SQLQueryBoilerplates = SQLQueryBoilerplates(
81 sql_pool=self.sql_manage_connections, success=self.success,
82 error=self.error, debug=self.debug
83 )
84 self.get_table_column_names: SQLQueryBoilerplates.get_table_column_names = self.sql_query_boilerplates.get_table_column_names
85 self.get_table_names: SQLQueryBoilerplates.get_table_names = self.sql_query_boilerplates.get_table_names
86 self.describe_table: SQLQueryBoilerplates.describe_table = self.sql_query_boilerplates.describe_table
87 self.insert_data_into_table: SQLQueryBoilerplates.insert_data_into_table = self.sql_query_boilerplates.insert_data_into_table
88 self.get_data_from_table: SQLQueryBoilerplates.get_data_from_table = self.sql_query_boilerplates.get_data_from_table
89 self.get_table_size: SQLQueryBoilerplates.get_table_size = self.sql_query_boilerplates.get_table_size
90 self.update_data_in_table: SQLQueryBoilerplates.update_data_in_table = self.sql_query_boilerplates.update_data_in_table
91 self.insert_or_update_data_into_table: SQLQueryBoilerplates.insert_or_update_data_into_table = self.sql_query_boilerplates.insert_or_update_data_into_table
92 self.remove_data_from_table: SQLQueryBoilerplates.remove_data_from_table = self.sql_query_boilerplates.remove_data_from_table
93 self.drop_data_from_table: SQLQueryBoilerplates.remove_data_from_table = self.sql_query_boilerplates.remove_data_from_table
94
95 def __del__(self) -> None:
96 """
97 Disconnect the database when the class is destroyed
98 """
99 if self.sql_manage_connections is not None:
101 self.sql_manage_connections = None
102 if self.sql_time_manipulation is not None:
103 del self.sql_time_manipulation
104 self.sql_time_manipulation = None
105 if self.sql_query_boilerplates is not None:
107 self.sql_query_boilerplates = None
SQLQueryBoilerplates sql_query_boilerplates
SQLQueryBoilerplates.describe_table describe_table
SQLManageConnections sql_manage_connections
SQLQueryBoilerplates.get_table_column_names get_table_column_names
SQLTimeManipulation.get_correct_now_value _get_correct_now_value
SQLTimeManipulation sql_time_manipulation
SQLQueryBoilerplates.remove_data_from_table drop_data_from_table
SQLQueryBoilerplates.update_data_in_table update_data_in_table
SQLTimeManipulation.get_correct_current_date_value _get_correct_current_date_value
SQLTimeManipulation.datetime_to_string datetime_to_string
SQLQueryBoilerplates.remove_data_from_table remove_data_from_table
SQLQueryBoilerplates.get_data_from_table get_data_from_table
SQLQueryBoilerplates.insert_data_into_table insert_data_into_table
SQLQueryBoilerplates.get_table_size get_table_size
__init__(self, str url, int port, str username, str password, str db_name, int success=0, int error=84, bool debug=False)
SQLTimeManipulation.string_to_datetime string_to_datetime
SQLQueryBoilerplates.get_table_names get_table_names
SQLQueryBoilerplates.insert_or_update_data_into_table insert_or_update_data_into_table