Skip to content

Commit

Permalink
Create Read Query
Browse files Browse the repository at this point in the history
  • Loading branch information
keiranjprice101 committed Jul 29, 2019
1 parent b6b92b5 commit d8f5ab8
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions common/database_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from sqlalchemy import create_engine, asc, desc
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.collections import InstrumentedList

from common.constants import Constants
from common.exceptions import MissingRecordError, BadFilterError, BadRequestError
Expand Down Expand Up @@ -44,7 +43,29 @@ def commit_changes(self):
self.session.commit()
log.info(f" Closing DB session")
self.session.close()



class ReadQuery(Query):

def __init__(self, table):
super.__init__(table)
self.include_related_entities = False

def execute_query(self):
self.commit_changes()

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

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


def insert_row_into_table(row):
"""
Expand Down Expand Up @@ -201,7 +222,6 @@ def get_rows_by_filter(table, filters):
if list(query_filter)[0] == "include":
return list(map(lambda x: x.to_nested_dict(query_filter["include"]), results))


log.info(" Closing DB session")
session.close()
return list(map(lambda x: x.to_dict(), results))
Expand Down

0 comments on commit d8f5ab8

Please sign in to comment.