diff --git a/database_lib.py b/database_lib.py index 3102d4c..5774de2 100644 --- a/database_lib.py +++ b/database_lib.py @@ -8,15 +8,6 @@ logger = logging.getLogger(__name__) class Database(): - TABLE_BOOKS="books" - COLUMN_TITLE="title" - COLUMN_AUTHOR="author_sort" - COLUMN_PATH="path" - - TABLE_AUTHOR="authors" - COLUMN_NAME="name" - COLUMN_SORT="sort" - def __init__(self, filename): self.filename = filename self.con = None @@ -40,24 +31,31 @@ class Database(): return result_list def select_books_by_title(self, query): + query = self.__format_query(query) sql = f""" - SELECT title, author_sort, path - FROM books WHERE title LIKE '%{query}%'; - """ - return self._execute(sql, res.book_factory) + SELECT title, author_sort, path FROM books + WHERE title LIKE '{query}'; + """ + return self._execute(sql, res.book_factory) def select_books_by_author(self, query): + query = self.__format_query(query) sql = f""" - SELECT title, author_sort, path FROM books - WHERE books.author_sort LIKE - '%' || (SELECT sort FROM authors - WHERE sort = '{query}') || '%'; - """ + SELECT title, author_sort, path FROM books + WHERE books.author_sort LIKE '%' || + (SELECT sort FROM authors + WHERE sort = '{query}') || '%'; + """ return self._execute(sql, res.book_factory) def select_authors(self, query): + query = self.__format_query(query) sql = f""" - SELECT name, sort FROM authors - WHERE name LIKE '%{query}%'; - """ + SELECT name, sort FROM authors + WHERE name LIKE '{query}'; + """ return self._execute(sql, res.author_factory) + + def __format_query(self, query): + formatted_query = "%" + query.replace(" ", "%") + "%" + return formatted_query