From 079d72f93fb86564a0c11c860095f04f431d124b Mon Sep 17 00:00:00 2001 From: Jon Tibble Date: Wed, 30 Jun 2010 23:05:51 +0100 Subject: [PATCH] Database refactoring --- openlp/core/lib/db.py | 19 ++- openlp/plugins/bibles/lib/db.py | 7 +- openlp/plugins/custom/customplugin.py | 3 +- openlp/plugins/songs/forms/songimportform.py | 2 +- openlp/plugins/songs/lib/__init__.py | 47 +++++++- openlp/plugins/songs/lib/manager.py | 116 ------------------- openlp/plugins/songs/lib/mediaitem.py | 15 ++- openlp/plugins/songs/lib/songimport.py | 12 +- openlp/plugins/songs/songsplugin.py | 11 +- 9 files changed, 84 insertions(+), 148 deletions(-) delete mode 100644 openlp/plugins/songs/lib/manager.py diff --git a/openlp/core/lib/db.py b/openlp/core/lib/db.py index b000fe918..a60521218 100644 --- a/openlp/core/lib/db.py +++ b/openlp/core/lib/db.py @@ -166,17 +166,17 @@ class Manager(object): else: return self.session.query(object_class).get(key) - def get_object_filtered(self, object_class, filter_string): + def get_object_filtered(self, object_class, filter_clause): """ Returns an object matching specified criteria ``object_class`` The type of object to return - ``filter_string`` + ``filter_clause`` The criteria to select the object by """ - return self.session.query(object_class).filter(filter_string).first() + return self.session.query(object_class).filter(filter_clause).first() def get_all_objects(self, object_class, order_by_ref=None): """ @@ -192,17 +192,24 @@ class Manager(object): return self.session.query(object_class).order_by(order_by_ref).all() return self.session.query(object_class).all() - def get_all_objects_filtered(self, object_class, filter_string): + def get_all_objects_filtered(self, object_class, filter_clause, + order_by_ref=None): """ Returns a selection of objects from the database ``object_class`` The type of objects to return - ``filter_string`` + ``filter_clause`` The filter governing selection of objects to return + + ``order_by_ref`` + Any parameters to order the returned objects by. Defaults to None. """ - return self.session.query(object_class).filter(filter_string).all() + if order_by_ref: + return self.session.query(object_class).filter(filter_clause) \ + .order_by(order_by_ref).all() + return self.session.query(object_class).filter(filter_clause).all() def delete_object(self, object_class, key): """ diff --git a/openlp/plugins/bibles/lib/db.py b/openlp/plugins/bibles/lib/db.py index c799d71dd..a453d5bcc 100644 --- a/openlp/plugins/bibles/lib/db.py +++ b/openlp/plugins/bibles/lib/db.py @@ -299,11 +299,10 @@ class BibleDB(QtCore.QObject, Manager): The name of the book to return """ log.debug(u'BibleDb.get_book("%s")', book) - db_book = self.session.query(Book).filter( - Book.name.like(book + u'%')).first() + db_book = self.get_object_filtered(Book, Book.name.like(book + u'%')) if db_book is None: - db_book = self.session.query(Book).filter( - Book.abbreviation.like(book + u'%')).first() + db_book = self.get_object_filtered(Book, + Book.abbreviation.like(book + u'%')) return db_book def get_verses(self, reference_list): diff --git a/openlp/plugins/custom/customplugin.py b/openlp/plugins/custom/customplugin.py index 76d597f28..e65205351 100644 --- a/openlp/plugins/custom/customplugin.py +++ b/openlp/plugins/custom/customplugin.py @@ -78,8 +78,7 @@ class CustomPlugin(Plugin): return about_text def can_delete_theme(self, theme): - filter_string = u'theme_name=\'%s\'' % theme if not self.custommanager.get_all_objects_filtered(CustomSlide, - filter_string): + CustomSlide.theme_name == theme): return True return False diff --git a/openlp/plugins/songs/forms/songimportform.py b/openlp/plugins/songs/forms/songimportform.py index 8a830d720..09db3dcb8 100644 --- a/openlp/plugins/songs/forms/songimportform.py +++ b/openlp/plugins/songs/forms/songimportform.py @@ -30,7 +30,7 @@ from PyQt4 import QtCore, QtGui from songimportwizard import Ui_SongImportWizard from openlp.core.lib import Receiver, SettingsManager, translate #from openlp.core.utils import AppLocation -from openlp.plugins.songs.lib.manager import SongFormat +from openlp.plugins.songs.lib import SongFormat log = logging.getLogger(__name__) diff --git a/openlp/plugins/songs/lib/__init__.py b/openlp/plugins/songs/lib/__init__.py index dddb23b4e..8c07c31c7 100644 --- a/openlp/plugins/songs/lib/__init__.py +++ b/openlp/plugins/songs/lib/__init__.py @@ -25,6 +25,52 @@ from openlp.core.lib import translate +#from openlp.plugins.songs.lib import OpenLyricsSong, OpenSongSong, CCLISong, \ +# CSVSong + +class SongFormat(object): + """ + This is a special enumeration class that holds the various types of songs, + plus a few helper functions to facilitate generic handling of song types + for importing. + """ + Unknown = -1 + OpenLyrics = 0 + OpenSong = 1 + CCLI = 2 + CSV = 3 + + @staticmethod + def get_class(id): + """ + Return the appropriate imeplementation class. + + ``id`` + The song format. + """ +# if id == SongFormat.OpenLyrics: +# return OpenLyricsSong +# elif id == SongFormat.OpenSong: +# return OpenSongSong +# elif id == SongFormat.CCLI: +# return CCLISong +# elif id == SongFormat.CSV: +# return CSVSong +# else: + return None + + @staticmethod + def list(): + """ + Return a list of the supported song formats. + """ + return [ + SongFormat.OpenLyrics, + SongFormat.OpenSong, + SongFormat.CCLI, + SongFormat.CSV + ] + class VerseType(object): """ VerseType provides an enumeration for the tags that may be associated @@ -91,7 +137,6 @@ class VerseType(object): unicode(VerseType.to_string(VerseType.Other)).lower(): return VerseType.Other -from manager import SongManager from songstab import SongsTab from mediaitem import SongMediaItem from songimport import SongImport diff --git a/openlp/plugins/songs/lib/manager.py b/openlp/plugins/songs/lib/manager.py deleted file mode 100644 index 10ec5aa34..000000000 --- a/openlp/plugins/songs/lib/manager.py +++ /dev/null @@ -1,116 +0,0 @@ -# -*- coding: utf-8 -*- -# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4 - -############################################################################### -# OpenLP - Open Source Lyrics Projection # -# --------------------------------------------------------------------------- # -# Copyright (c) 2008-2010 Raoul Snyman # -# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael # -# Gorven, Scott Guerrieri, Christian Richter, Maikel Stuivenberg, Martin # -# Thompson, Jon Tibble, Carsten Tinggaard # -# --------------------------------------------------------------------------- # -# This program is free software; you can redistribute it and/or modify it # -# under the terms of the GNU General Public License as published by the Free # -# Software Foundation; version 2 of the License. # -# # -# This program is distributed in the hope that it will be useful, but WITHOUT # -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # -# more details. # -# # -# You should have received a copy of the GNU General Public License along # -# with this program; if not, write to the Free Software Foundation, Inc., 59 # -# Temple Place, Suite 330, Boston, MA 02111-1307 USA # -############################################################################### - -import logging - -from openlp.core.lib.db import Manager -from openlp.plugins.songs.lib.db import init_schema, Song, Author -#from openlp.plugins.songs.lib import OpenLyricsSong, OpenSongSong, CCLISong, \ -# CSVSong - -log = logging.getLogger(__name__) - -class SongFormat(object): - """ - This is a special enumeration class that holds the various types of songs, - plus a few helper functions to facilitate generic handling of song types - for importing. - """ - Unknown = -1 - OpenLyrics = 0 - OpenSong = 1 - CCLI = 2 - CSV = 3 - - @staticmethod - def get_class(id): - """ - Return the appropriate imeplementation class. - - ``id`` - The song format. - """ -# if id == SongFormat.OpenLyrics: -# return OpenLyricsSong -# elif id == SongFormat.OpenSong: -# return OpenSongSong -# elif id == SongFormat.CCLI: -# return CCLISong -# elif id == SongFormat.CSV: -# return CSVSong -# else: - return None - - @staticmethod - def list(): - """ - Return a list of the supported song formats. - """ - return [ - SongFormat.OpenLyrics, - SongFormat.OpenSong, - SongFormat.CCLI, - SongFormat.CSV - ] - - -class SongManager(Manager): - """ - The Song Manager provides a central location for all database code. This - class takes care of connecting to the database and running all the queries. - """ - log.info(u'Song manager loaded') - - def __init__(self): - """ - Creates the connection to the database, and creates the tables if they - don't exist. - """ - log.debug(u'Song Initialising') - Manager.__init__(self, u'songs', init_schema) - log.debug(u'Song Initialised') - - def search_song_title(self, keywords): - """ - Searches the song title for keywords. - """ - return self.session.query(Song).filter( - Song.search_title.like(u'%' + keywords + u'%')).order_by( - Song.search_title.asc()).all() - - def search_song_lyrics(self, keywords): - """ - Searches the song lyrics for keywords. - """ - return self.session.query(Song).filter( - Song.search_lyrics.like(u'%' + keywords + u'%')).order_by( - Song.search_lyrics.asc()).all() - - def get_song_from_author(self, keywords): - """ - Searches the song authors for keywords. - """ - return self.session.query(Author).filter(Author.display_name.like( - u'%' + keywords + u'%')).order_by(Author.display_name.asc()).all() diff --git a/openlp/plugins/songs/lib/mediaitem.py b/openlp/plugins/songs/lib/mediaitem.py index 94002b6f4..7ed30e5e0 100644 --- a/openlp/plugins/songs/lib/mediaitem.py +++ b/openlp/plugins/songs/lib/mediaitem.py @@ -165,18 +165,21 @@ class SongMediaItem(MediaManagerItem): search_type = self.SearchTypeComboBox.currentIndex() if search_type == 0: log.debug(u'Titles Search') - search_results = self.parent.manager.search_song_title( - search_keywords) + search_results = self.parent.manager.get_all_objects_filtered(Song, + Song.search_title.like(u'%' + keywords + u'%'), + Song.search_title.asc()) self.displayResultsSong(search_results) elif search_type == 1: log.debug(u'Lyrics Search') - search_results = self.parent.manager.search_song_lyrics( - search_keywords) + search_results = self.parent.manager.get_all_objects_filtered(Song, + Song.search_lyrics.like(u'%' + keywords + u'%'), + Song.search_lyrics.asc()) self.displayResultsSong(search_results) elif search_type == 2: log.debug(u'Authors Search') - search_results = self.parent.manager.get_song_from_author( - search_keywords) + search_results = self.parent.manager.get_all_objects_filtered( + Author, Author.display_name.like(u'%' + search_keywords + u'%'), + Author.display_name.asc()) self.displayResultsAuthor(search_results) #Called to redisplay the song list screen edith from a search #or from the exit of the Song edit dialog. If remote editing is active diff --git a/openlp/plugins/songs/lib/songimport.py b/openlp/plugins/songs/lib/songimport.py index 08855f01a..115f52e58 100644 --- a/openlp/plugins/songs/lib/songimport.py +++ b/openlp/plugins/songs/lib/songimport.py @@ -302,8 +302,8 @@ class SongImport(object): song.theme_name = self.theme_name song.ccli_number = self.ccli_number for authortext in self.authors: - filter_string = u'display_name=%s' % authortext - author = self.manager.get_object_filtered(Author, filter_string) + author = self.manager.get_object_filtered(Author, + Author.display_name == authortext) if author is None: author = Author() author.display_name = authortext @@ -312,8 +312,8 @@ class SongImport(object): self.manager.save_object(author) song.authors.append(author) if self.song_book_name: - filter_string = u'name=%s' % self.song_book_name - song_book = self.manager.get_object_filtered(Book, filter_string) + song_book = self.manager.get_object_filtered(Book, + Book.name == self.song_book_name) if song_book is None: song_book = Book() song_book.name = self.song_book_name @@ -321,8 +321,8 @@ class SongImport(object): self.manager.save_object(song_book) song.song_book_id = song_book.id for topictext in self.topics: - filter_string = u'name=%s' % topictext - topic = self.manager.get_object_filtered(Topic, filter_string) + topic = self.manager.get_object_filtered(Topic, + Topic.name == topictext) if topic is None: topic = Topic() topic.name = topictext diff --git a/openlp/plugins/songs/songsplugin.py b/openlp/plugins/songs/songsplugin.py index 82a780fa9..3567ff3a1 100644 --- a/openlp/plugins/songs/songsplugin.py +++ b/openlp/plugins/songs/songsplugin.py @@ -29,7 +29,8 @@ from PyQt4 import QtCore, QtGui from openlp.core.lib import Plugin, build_icon, PluginStatus, Receiver, \ translate -from openlp.plugins.songs.lib import SongManager, SongMediaItem, SongsTab +from openlp.core.lib.db import Manager +from openlp.plugins.songs.lib import SongMediaItem, SongsTab from openlp.plugins.songs.lib.db import Song try: @@ -56,7 +57,7 @@ class SongsPlugin(Plugin): """ Plugin.__init__(self, u'Songs', u'1.9.2', plugin_helpers) self.weight = -10 - self.manager = SongManager() + self.manager = Manager(u'songs', init_schema) self.icon = build_icon(u':/plugins/plugin_songs.png') self.status = PluginStatus.Active @@ -65,8 +66,6 @@ class SongsPlugin(Plugin): def initialise(self): log.info(u'Songs Initialising') - #if self.songmanager is None: - # self.songmanager = SongManager() Plugin.initialise(self) self.insert_toolbox_item() self.media_item.displayResultsSong( @@ -199,7 +198,7 @@ class SongsPlugin(Plugin): return about_text def can_delete_theme(self, theme): - filter_string = u'theme_name=\'%s\'' % theme - if not self.manager.get_all_objects_filtered(Song, filter_string): + if not self.manager.get_all_objects_filtered(Song, + Song.theme_name == theme): return True return False