1414
1515class DBHandler :
1616 """
17- Utility class to safely handle access to the rtbdb
18- This class is designed to ONLY be initialized in the Pipeline base class
19- This class should be initialized as part of a base pipeline initialization procedure AFTER config values have been read and stored
17+ Utility class to safely handle access to the RTB database.
18+
19+ This class encapsulates connection setup and basic pipeline entry
20+ handling against the RTB DB via SQLAlchemy. It is intended to be
21+ constructed by the Pipeline base class, after configuration has been
22+ read and validated.
2023 """
2124
2225 def __init__ (self , ref_type , use_dsn , sql_server_str = None , sql_database_str = None , port = None , dsn_header_str = None ):
26+ """
27+ Initialize a DBHandler and immediately attempt to connect to the RTB database.
28+
29+ Parameters
30+ ----------
31+ ref_type : str
32+ Reference type identifier used by the pipeline.
33+ use_dsn : bool
34+ If True connect using a DSN name else connect using explicit server/database parameters.
35+ sql_server_str : str, optional
36+ SQL Server hostname or address when use_dsn is False.
37+ sql_database_str : str, optional
38+ Database name when use_dsn is False.
39+ port : int, optional
40+ SQL Server port when use_dsn is False.
41+ dsn_header_str : str, optional
42+ DSN name to use when use_dsn is True.
43+
44+ Attributes
45+ ----------
46+ ref_type : str
47+ sql_id : Any or None
48+ SQL identifier.
49+ db_engine : sqlalchemy.engine.Engine or None
50+ SQLAlchemy Engine once connected.
51+ db_entry : DBEntry
52+ Helper object to prepare and insert pipeline entries.
53+ """
54+
55+
2356 if ref_type not in constants .WFI_REF_TYPES :
2457 raise ValueError (
2558 f"ref_type { ref_type } not valid, must be: { constants .WFI_REF_TYPES } "
@@ -32,16 +65,28 @@ def __init__(self, ref_type, use_dsn, sql_server_str=None, sql_database_str=None
3265
3366 self ._connect (use_dsn , sql_server_str , sql_database_str , port , dsn_header_str )
3467
35- def _connect (self , use_dsn , sql_server_str , sql_database_str , port , dsn_header_str ):
36- """Confirm if the user has access to the desired database. If so, establish a SQLalchemy connection.
37- Saves SQLalchemy engine to self.db_engine and saves the db availability boolean to self.use_db.
38-
39- Returns
40- -------
41- self.use_db
42- boolean to trigger if montor should attempt to use the database
4368
69+ def _connect (self , use_dsn , sql_server_str , sql_database_str , port , dsn_header_str ):
4470 """
71+ Establish a connection to the RTB database and store the SQLAlchemy Engine.
72+
73+ Depending on ``use_dsn``, this will either connect via a named DSN or
74+ directly using server parameters.
75+
76+ Parameters
77+ ----------
78+ use_dsn : bool
79+ If True connect using DSN.
80+ sql_server_str : str, optional
81+ SQL Server string (non-DSN mode).
82+ sql_database_str : str, optional
83+ Database name (non-DSN mode).
84+ port : int, optional
85+ SQL Server port (non-DSN mode).
86+ dsn_header_str : str, optional
87+ DSN name (DSN mode).
88+ """
89+
4590 try :
4691 if use_dsn :
4792 engine = connect_server (dsn_name = dsn_header_str )
@@ -54,16 +99,6 @@ def _connect(self, use_dsn, sql_server_str, sql_database_str, port, dsn_header_s
5499 engine = ensure_connection_is_engine (engine )
55100 self .db_engine = engine
56101
57- # TODO - SAPP -> TEMP DEV CODE REMOVE
58- print (f'SQL table names: { table_tools .table_names (engine )} \n ' )
59- print (f'Table class names: { table_tools .table_class_names (engine )} \n ' )
60- print (f'Table name from class: { RFPLogProTable .__tablename__ } ' )
61- print (f"Table class from name: { table_tools .get_table_class_from_tablename ('wfi_rfp_log_pro' )} " )
62- print (table_tools .get_full_table (engine , RFPLogProTable ))
63- #//////////////////////////////////////////////////////////////////
64-
65-
66-
67102 ### TODO: This is not actually catching if the engine exists or not...
68103 ### add raise exception to rtb-db login.connect_sqlalchemy
69104 except Exception as err :
@@ -74,11 +109,18 @@ def _connect(self, use_dsn, sql_server_str, sql_database_str, port, dsn_header_s
74109 )
75110
76111 def new_pipeline_db_entry (self , ref_type , wfi_mode , reef_monitor ):
77- # SAPP TODO - YOU ARE HERE!!!! GET AN INITIAL DATABASE ENTRY AND KEEP THE SQL ID
78- # STORE EVERYTHING YOU HAVE ALREADY PERTAINING TO THIS PIPELINE RUN
79- # CREATE A METHOD IN RTB_DB REPO THAT WILL ALLOW YOU TO UPDATE AN EXISTING ROW
80- # ONCE YOU HAVE INITIAL UPDATE AND EXISTING ROW UPDATE, TRY RUNNING AND POPULATING EVERYTHING NEEDED FROM INITIATION THROUGH PREP PIPELINE
112+ """
113+ Initialize and insert a new logistics processing table row for this pipeline.
114+
115+ Parameters
116+ ----------
117+ ref_type : str
118+ Reference type associated with this pipeline run.
119+ wfi_mode : str
120+ wfi mode associated with this pipeline run.
121+ reef_monitor : bool
122+ Expecting external monitoring for this run.
123+ """
81124
82125 self .db_entry .init_rfp_log_pro (ref_type , wfi_mode , reef_monitor )
83- add_to_tables_from_class_list (self .engine , [self .db_entry .rfp_log_pro ])
84- #self.sql_id = self.rfp_log_pro.sql_id # VERIFY SQL_ID EXISTS
126+ add_to_tables_from_class_list (self .db_engine , [self .db_entry .rfp_log_pro ])
0 commit comments