From 168044458351d2818efadac1fb5779e66a1b19b2 Mon Sep 17 00:00:00 2001 From: campi Date: Wed, 9 Mar 2022 20:16:42 +0100 Subject: [PATCH] database --- database_lib.py | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) 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