Skip to content

Commit

Permalink
Make sure session is closed correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Aug 2, 2019
1 parent beb9df8 commit 075f476
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions common/database_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ def commit_changes(self):
self.session.close()



class CountQuery(Query):

def __init__(self, table):
Expand All @@ -43,8 +42,10 @@ def execute_query(self):
self.commit_changes()

def get_count(self):
self.execute_query()
try:
return self.base_query.count()
finally:
self.execute_query()


class ReadQuery(Query):
Expand All @@ -57,18 +58,22 @@ def execute_query(self):
self.commit_changes()

def get_single_result(self):
self.execute_query()
try:
result = self.base_query.first()
if result is not None:
return result
raise MissingRecordError(" No result found")
finally:
self.session.close()

def get_all_results(self):
self.execute_query()
try:
results = self.base_query.all()
if results is not None:
return results
raise MissingRecordError(" No results found")
finally:
self.session.close()


class CreateQuery(Query):
Expand Down Expand Up @@ -275,8 +280,6 @@ def get_rows_by_filter(table, filters):
return list(map(lambda x: x.to_dict(), results))




def get_first_filtered_row(table, filters):
"""
returns the first row that matches a given filter, in a given table
Expand Down

0 comments on commit 075f476

Please sign in to comment.