Skip to content
Snippets Groups Projects
Commit dbccfea2 authored by Marko Mecina's avatar Marko Mecina
Browse files

get rid of class-owned SQL session instance

parent 5541f35d
No related branches found
No related tags found
No related merge requests found
...@@ -257,8 +257,9 @@ class CBRSearch(Gtk.Box): ...@@ -257,8 +257,9 @@ class CBRSearch(Gtk.Box):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.app_win = app_win self.app_win = app_win
self.logger = logger self.logger = logger
with db_schema.session_scope() as session:
self.session = session # with db_schema.session_scope() as session:
# self.session = session
self._filter_searchstring = '' self._filter_searchstring = ''
self._filter_type_snippet = None self._filter_type_snippet = None
...@@ -769,11 +770,10 @@ class CBRSearch(Gtk.Box): ...@@ -769,11 +770,10 @@ class CBRSearch(Gtk.Box):
Loads the data from the database. The filter properties are used to build the SQL query. Loads the data from the database. The filter properties are used to build the SQL query.
""" """
self.logger.debug('Loading data from the database') self.logger.debug('Loading data from the database')
self.session.commit() # this updates the session instantiated in the class init
if self.filter_searchstring != '': if self.filter_searchstring != '':
self.data = db_interaction.query_using_textsearch(session=self.session, expressions=self.filter_searchstring) self.data = db_interaction.query_using_textsearch(expressions=self.filter_searchstring)
else: else:
self.data = db_interaction.query_get_all_entries(self.session) self.data = db_interaction.query_get_all_entries()
self.load_data_into_liststore(self.data) self.load_data_into_liststore(self.data)
def reload_data(self): def reload_data(self):
......
...@@ -2,8 +2,10 @@ import db_schema ...@@ -2,8 +2,10 @@ import db_schema
from db_schema import CodeBlock from db_schema import CodeBlock
def query_get_all_entries(session): def query_get_all_entries():
with db_schema.session_scope() as session:
data = session.query(CodeBlock).all() data = session.query(CodeBlock).all()
session.expunge_all()
return data return data
...@@ -27,16 +29,19 @@ def delete_db_row(id): ...@@ -27,16 +29,19 @@ def delete_db_row(id):
return return
def query_using_textsearch(session, expressions): def query_using_textsearch(expressions):
with db_schema.session_scope() as session:
data = session.query(CodeBlock).filter(CodeBlock.description.contains(expressions)).all() data = session.query(CodeBlock).filter(CodeBlock.description.contains(expressions)).all()
session.expunge_all()
return data return data
def query_code_types(session): def query_code_types():
with db_schema.session_scope() as session:
data = session.query(CodeBlock.code_type).group_by(CodeBlock.code_type).all() data = session.query(CodeBlock.code_type).group_by(CodeBlock.code_type).all()
session.expunge_all()
return data return data
if __name__ == '__main__': if __name__ == '__main__':
with db_schema.session_scope() as session: query_code_types()
query_code_types(session)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment