88
99class PostgresEngine :
1010
11- def __init__ (self , databaseName , user , password , host = 'localhost' , port = 5432 ):
11+ def __init__ (self , databaseName : str , user : str , password : str , host : str = 'localhost' , port : int = 5432 ):
12+ """
13+ Class for accessing Postgres databases more easily.
14+
15+ :param databaseName (str): the name of the database to connect to
16+ :param user (str): the user to log in as
17+ :param password (str): password of the user
18+ :param host (str): host address to connect to
19+ :param port (int): port where the database is available
20+ """
1221 self .databaseName = databaseName
1322 self .user = user
1423 self .password = password
@@ -24,7 +33,7 @@ def _get_connection(self):
2433 logging .exception (f'Error connecting to PostgreSQL { ex } ' )
2534 raise ex
2635
27- def _get_cursor (self , isInsertionQuery ):
36+ def _get_cursor (self , isInsertionQuery : bool ):
2837 if isInsertionQuery :
2938 self .cursor = self .connection .cursor ()
3039 else :
@@ -42,7 +51,7 @@ def close(self):
4251 if self .cursor is not None :
4352 self ._close_cursor ()
4453
45- def create_table (self , schema ):
54+ def create_table (self , schema : str ):
4655 self ._get_connection ()
4756 self ._get_cursor (isInsertionQuery = True )
4857 self .cursor .execute (schema )
@@ -54,7 +63,7 @@ def create_table(self, schema):
5463 finally :
5564 self .close ()
5665
57- def create_index (self , tableName , column ):
66+ def create_index (self , tableName : str , column : str ):
5867 self ._get_connection ()
5968 self ._get_cursor (isInsertionQuery = True )
6069 indexQuery = f'CREATE INDEX IF NOT EXISTS { tableName } _{ column } ON { tableName } ({ column } );'
@@ -67,21 +76,8 @@ def create_index(self, tableName, column):
6776 finally :
6877 self .close ()
6978
70- def create_new_foreign_key_constraint (self , tableName , constraintName , foreignKeySQL ):
71- FOREIGN_KEY_QUERY = """
72- ALTER TABLE {tableName}
73- ADD CONSTRAINT {constraintName} {foreignKeySQL};
74- """
75- self .create_table (
76- schema = FOREIGN_KEY_QUERY .format (
77- tableName = tableName ,
78- constraintName = constraintName ,
79- foreignKeySQL = foreignKeySQL
80- )
81- )
82-
83- @retry (numRetries = 5 , retryDelay = 3 , backoffScalingFactor = 2 )
84- def run_select_query (self , query , parameters = None ):
79+ @retry (numRetries = 5 , retryDelaySeconds = 3 , backoffScalingFactor = 2 )
80+ def run_select_query (self , query : str , parameters : dict = None ):
8581 self ._get_connection ()
8682 self ._get_cursor (isInsertionQuery = False )
8783 self .cursor .execute (query , parameters )
@@ -90,8 +86,8 @@ def run_select_query(self, query, parameters=None):
9086 outputDataframe = pd .DataFrame (outputs )
9187 return outputDataframe .where (outputDataframe .notnull (), None ).dropna (axis = 0 , how = 'all' )
9288
93- @retry (numRetries = 5 , retryDelay = 3 , backoffScalingFactor = 2 )
94- def run_update_query (self , query , parameters = None , returnId = True ):
89+ @retry (numRetries = 5 , retryDelaySeconds = 3 , backoffScalingFactor = 2 )
90+ def run_update_query (self , query : str , parameters : dict = None , returnId : bool = True ):
9591 self ._get_connection ()
9692 self ._get_cursor (isInsertionQuery = True )
9793 if returnId :
0 commit comments