def get_count_books_pages_by_item_id( item_id: int, column_name: Column): """Get from DB the count of pages containing books.""" result: int = session.query(Books) \ .filter(item_id == column_name) \ .count() count_page = round(result / LIMIT) - 1 return count_page
def get_author_books_by_item_id( item_id: int, current_page: int): """Get a list of DB author's books matching id item.""" offset = LIMIT * current_page result: Books = session.query(Books) \ .filter(Books.author_id == item_id) \ .order_by(Books.name.asc()) \ .offset(offset) \ .limit(LIMIT) \ .all() return result
def get_genre_and_reader_books_by_item_id(table_name: base, item_id: int, current_page: int): """Get a list of DB genre's and reader's books matching id item.""" offset = LIMIT * current_page result: Books = session.query(Books) \ .select_from(table_name) \ .join(table_name.books) \ .group_by(Books) \ .filter(table_name.id == item_id) \ .order_by(Books.name.asc()) \ .offset(offset) \ .limit(LIMIT) \ .all() return result
def save_reader(row: dict): """Reader's control and save in data base.""" try: result: Readers = session.query(Readers) \ .filter(Readers.name == row.get('reader_name')) \ .one() return result except MultipleResultsFound: print('Такой исполнитель уже есть в базе') except NoResultFound: result: Readers = Readers(name=row.get('reader_name')) session.add(result) session.commit() return result
def save_author(row: dict): """Author's control and save in data base.""" try: result: Authors = session.query(Authors) \ .filter(Authors.name == row.get('author_name')) \ .one() return result except MultipleResultsFound: print('Такой автор уже есть в базе') except NoResultFound: result: Authors = Authors(name=row.get('author_name')) session.add(result) session.commit() return result
def save_genre(row: dict): """Genre's control and save in data base.""" try: result: Genres = session.query(Genres) \ .filter(Genres.name == row.get('genre_name')) \ .one() return result except MultipleResultsFound: print('Такой жанр уже есть в базе') except NoResultFound: result: Genres = Genres(name=row.get('genre_name')) session.add(result) session.commit() return result
def save_book(row: dict, author: Authors): """Book's control and save in data base.""" try: result: Books = session.query(Books) \ .filter(Books.name == row.get('name')) \ .one() return result except MultipleResultsFound: print('Такая книга уже есть в базе') except NoResultFound: result: Books = Books(name=row.get('name'), description=row.get('description'), book_url=row.get('book_url'), author=author) session.add(result) session.commit() return result
def get_count_books(): """Get the count of books from DB.""" result: int = session.query(Books) \ .count() count_books = int(result) return count_books
def get_count_page(table_name: base): """Get from DB the count of pages containing authors or genres or readers.""" result: int = session.query(table_name) \ .count() count_page = round(result / LIMIT) - 1 return count_page
def get_book_by_id(book_id: int): """Get the selected books from DB.""" result: int = session.query(Books) \ .filter(book_id == Books.id) return result