refactor: use context manager for database connection
This commit is contained in:
		
							parent
							
								
									84fe1a0480
								
							
						
					
					
						commit
						6df4c5be07
					
				
					 2 changed files with 16 additions and 10 deletions
				
			
		|  | @ -1,6 +1,6 @@ | ||||||
| import argparse | import argparse | ||||||
| 
 | 
 | ||||||
| from services.database_service import DatabaseService | from database_connection import DatabaseConnection | ||||||
| from services.parse_file_service import ParseFileService | from services.parse_file_service import ParseFileService | ||||||
| from services.table_service import TableService | from services.table_service import TableService | ||||||
| 
 | 
 | ||||||
|  | @ -29,14 +29,11 @@ def main(): | ||||||
|     SOURCE_DIRECTORY = args.source |     SOURCE_DIRECTORY = args.source | ||||||
|     TARGET_DIRECTORY = args.target |     TARGET_DIRECTORY = args.target | ||||||
| 
 | 
 | ||||||
|     database_service = DatabaseService("eolas", TARGET_DIRECTORY) |     with DatabaseConnection("eolas", TARGET_DIRECTORY) as conn: | ||||||
|     database_connection = database_service.connect() |         table_service = TableService(conn) | ||||||
|     table_service = TableService(database_connection) |  | ||||||
|         parse_file_service = ParseFileService(SOURCE_DIRECTORY) |         parse_file_service = ParseFileService(SOURCE_DIRECTORY) | ||||||
| 
 |  | ||||||
|         entries = parse_file_service.parse_source_directory() |         entries = parse_file_service.parse_source_directory() | ||||||
|         table_service.populate_tables(entries) |         table_service.populate_tables(entries) | ||||||
|     database_service.disconnect() |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| if __name__ == "__main__": | if __name__ == "__main__": | ||||||
|  |  | ||||||
|  | @ -3,7 +3,7 @@ import sqlite3 | ||||||
| from typing import Optional | from typing import Optional | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class DatabaseService: | class DatabaseConnection: | ||||||
|     def __init__(self, db_name, db_path): |     def __init__(self, db_name, db_path): | ||||||
|         self.db_name = db_name |         self.db_name = db_name | ||||||
|         self.db_path = db_path |         self.db_path = db_path | ||||||
|  | @ -31,3 +31,12 @@ class DatabaseService: | ||||||
|                 self.connection = None |                 self.connection = None | ||||||
|         except Exception as e: |         except Exception as e: | ||||||
|             raise Exception(f"ERROR Problem disconnecting from database: {e}") |             raise Exception(f"ERROR Problem disconnecting from database: {e}") | ||||||
|  | 
 | ||||||
|  |     def __enter__(self) -> sqlite3.Connection: | ||||||
|  |         connection = self.connect() | ||||||
|  |         if connection is None: | ||||||
|  |             raise RuntimeError("Failed to establish database connection") | ||||||
|  |         return connection | ||||||
|  | 
 | ||||||
|  |     def __exit__(self, exc_type, exc_val, exc_tb) -> None: | ||||||
|  |         self.disconnect() | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue