From 55f6197212478480665e3310e744ef536a53aeb1 Mon Sep 17 00:00:00 2001 From: Jon Tibble Date: Sat, 12 Jun 2010 02:52:04 +0100 Subject: [PATCH] RFC: Refactor database setup code A possible way to refactor database code to remove code duplication and, I hope, provide a simpler, more intuitive model to be copied by others --- openlp/core/lib/__init__.py | 1 - openlp/core/lib/{basemodel.py => db.py} | 26 ++- openlp/migration/migratebibles.py | 3 +- openlp/migration/migratesongs.py | 3 +- openlp/plugins/alerts/forms/alertform.py | 2 +- .../plugins/alerts/lib/{classes.py => db.py} | 28 +++- openlp/plugins/alerts/lib/manager.py | 7 +- openlp/plugins/alerts/lib/meta.py | 38 ----- openlp/plugins/alerts/lib/models.py | 39 ----- openlp/plugins/alerts/lib/tables.py | 33 ---- openlp/plugins/bibles/lib/models.py | 2 +- openlp/plugins/custom/lib/classes.py | 2 +- openlp/plugins/songs/forms/authorsdialog.py | 1 + openlp/plugins/songs/forms/authorsform.py | 1 - openlp/plugins/songs/forms/editsongdialog.py | 3 +- openlp/plugins/songs/forms/editsongform.py | 2 +- openlp/plugins/songs/forms/songbookdialog.py | 1 + openlp/plugins/songs/forms/songbookform.py | 1 - .../songs/forms/songmaintenanceform.py | 4 +- openlp/plugins/songs/forms/topicsdialog.py | 1 + openlp/plugins/songs/forms/topicsform.py | 1 - openlp/plugins/songs/lib/classes.py | 52 ------ openlp/plugins/songs/lib/db.py | 150 ++++++++++++++++++ openlp/plugins/songs/lib/manager.py | 7 +- openlp/plugins/songs/lib/meta.py | 38 ----- openlp/plugins/songs/lib/models.py | 48 ------ openlp/plugins/songs/lib/songimport.py | 2 +- openlp/plugins/songs/lib/tables.py | 99 ------------ openlp/plugins/songs/songsplugin.py | 1 - openlp/plugins/songusage/lib/classes.py | 2 +- 30 files changed, 223 insertions(+), 375 deletions(-) rename openlp/core/lib/{basemodel.py => db.py} (74%) rename openlp/plugins/alerts/lib/{classes.py => db.py} (72%) delete mode 100644 openlp/plugins/alerts/lib/meta.py delete mode 100644 openlp/plugins/alerts/lib/models.py delete mode 100644 openlp/plugins/alerts/lib/tables.py delete mode 100644 openlp/plugins/songs/lib/classes.py create mode 100644 openlp/plugins/songs/lib/db.py delete mode 100644 openlp/plugins/songs/lib/meta.py delete mode 100644 openlp/plugins/songs/lib/models.py delete mode 100644 openlp/plugins/songs/lib/tables.py diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index d5b46e6cb..e09b6f1b4 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -184,5 +184,4 @@ from themexmlhandler import ThemeXML from renderer import Renderer from rendermanager import RenderManager from mediamanageritem import MediaManagerItem -from basemodel import BaseModel from baselistwithdnd import BaseListWithDnD diff --git a/openlp/core/lib/basemodel.py b/openlp/core/lib/db.py similarity index 74% rename from openlp/core/lib/basemodel.py rename to openlp/core/lib/db.py index f56336c7b..4762da157 100644 --- a/openlp/core/lib/basemodel.py +++ b/openlp/core/lib/db.py @@ -22,6 +22,31 @@ # with this program; if not, write to the Free Software Foundation, Inc., 59 # # Temple Place, Suite 330, Boston, MA 02111-1307 USA # ############################################################################### +""" +The :mod:`db` module provides the core database functionality for OpenLP +""" + +from sqlalchemy import create_engine, MetaData +from sqlalchemy.orm import scoped_session, sessionmaker + +def init_db(url, auto_flush=True, auto_commit=False): + """ + Initialise and return the session and metadata for a database + + ``url`` + The database to initialise connection with + + ``auto_flush`` + Sets the flushing behaviour of the session + + ``auto_commit`` + Sets the commit behaviour of the session + """ + engine = create_engine(url) + metadata = MetaData(bind=engine) + session = scoped_session(sessionmaker(autoflush=auto_flush, + autocommit=auto_commit, bind=engine)) + return session, metadata class BaseModel(object): """ @@ -37,4 +62,3 @@ class BaseModel(object): for key in kwargs: me.__setattr__(key, kwargs[key]) return me - diff --git a/openlp/migration/migratebibles.py b/openlp/migration/migratebibles.py index 89bb5ffdf..b024343a4 100644 --- a/openlp/migration/migratebibles.py +++ b/openlp/migration/migratebibles.py @@ -30,7 +30,8 @@ import sqlite3 from sqlalchemy.exceptions import InvalidRequestError from sqlalchemy.orm import mapper -from openlp.core.lib import BaseModel, SettingsManager +from openlp.core.lib import SettingsManager +from openlp.core.lib.db import BaseModel from openlp.core.utils import AppLocation from openlp.plugins.bibles.lib.models import * diff --git a/openlp/migration/migratesongs.py b/openlp/migration/migratesongs.py index 87d5de40b..c4cab00bc 100644 --- a/openlp/migration/migratesongs.py +++ b/openlp/migration/migratesongs.py @@ -31,7 +31,8 @@ from sqlalchemy import create_engine from sqlalchemy.exceptions import InvalidRequestError from sqlalchemy.orm import scoped_session, sessionmaker, mapper, relation -from openlp.core.lib import BaseModel, SettingsManager +from openlp.core.lib import SettingsManager +from openlp.core.lib.db import BaseModel from openlp.core.utils import AppLocation from openlp.plugins.songs.lib.models import metadata, songs_table, Song, \ Author, Topic, Book diff --git a/openlp/plugins/alerts/forms/alertform.py b/openlp/plugins/alerts/forms/alertform.py index 04028fc1b..e8fc0d2fe 100644 --- a/openlp/plugins/alerts/forms/alertform.py +++ b/openlp/plugins/alerts/forms/alertform.py @@ -25,8 +25,8 @@ from PyQt4 import QtGui, QtCore -from openlp.plugins.alerts.lib.models import AlertItem from openlp.core.lib import translate +from openlp.plugins.alerts.lib.db import AlertItem from alertdialog import Ui_AlertDialog diff --git a/openlp/plugins/alerts/lib/classes.py b/openlp/plugins/alerts/lib/db.py similarity index 72% rename from openlp/plugins/alerts/lib/classes.py rename to openlp/plugins/alerts/lib/db.py index e58f80829..3656d41b8 100644 --- a/openlp/plugins/alerts/lib/classes.py +++ b/openlp/plugins/alerts/lib/db.py @@ -22,11 +22,35 @@ # with this program; if not, write to the Free Software Foundation, Inc., 59 # # Temple Place, Suite 330, Boston, MA 02111-1307 USA # ############################################################################### +""" +The :mod:`db` module provides the database and schema that is the backend for +the Alerts plugin +""" -from openlp.core.lib import BaseModel +from sqlalchemy import Column, Table, types +from sqlalchemy.orm import mapper + +from openlp.core.lib.db import BaseModel, init_db class AlertItem(BaseModel): """ - Custom Slide model + AlertItem model """ pass + +def init_schema(url): + """ + Setup the alerts database connection and initialise the database schema + + ``url`` + The database to setup + """ + session, metadata = init_db(url) + + alerts_table = Table(u'alerts', metadata, + Column(u'id', types.Integer(), primary_key=True), + Column(u'text', types.UnicodeText, nullable=False)) + + mapper(AlertItem, alerts_table) + + return session, metadata diff --git a/openlp/plugins/alerts/lib/manager.py b/openlp/plugins/alerts/lib/manager.py index 088f0cbae..814e3dca6 100644 --- a/openlp/plugins/alerts/lib/manager.py +++ b/openlp/plugins/alerts/lib/manager.py @@ -29,7 +29,7 @@ from PyQt4 import QtCore from sqlalchemy.exceptions import InvalidRequestError from openlp.core.utils import AppLocation -from openlp.plugins.alerts.lib.models import init_models, metadata, AlertItem +from openlp.plugins.alerts.lib.db import init_schema, AlertItem log = logging.getLogger(__name__) @@ -61,8 +61,8 @@ class DBManager(object): unicode(settings.value(u'db hostname').toString()), unicode(settings.value(u'db database').toString())) settings.endGroup() - self.session = init_models(self.db_url) - metadata.create_all(checkfirst=True) + self.session, self.metadata = init_schema(self.db_url) + self.metadata.create_all(checkfirst=True) log.debug(u'Alerts Initialised') def get_all_alerts(self): @@ -111,4 +111,3 @@ class DBManager(object): return False else: return True - diff --git a/openlp/plugins/alerts/lib/meta.py b/openlp/plugins/alerts/lib/meta.py deleted file mode 100644 index affa31969..000000000 --- a/openlp/plugins/alerts/lib/meta.py +++ /dev/null @@ -1,38 +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 # -############################################################################### - -from sqlalchemy import MetaData - -__all__ = ['session', 'metadata', 'engine'] - -# SQLAlchemy database engine. Updated by model.init_model() -engine = None - -# SQLAlchemy session manager. Updated by model.init_model() -session = None - -# Global metadata. If you have multiple databases with overlapping table -# names, you'll need a metadata for each database -metadata = MetaData() diff --git a/openlp/plugins/alerts/lib/models.py b/openlp/plugins/alerts/lib/models.py deleted file mode 100644 index f222345f1..000000000 --- a/openlp/plugins/alerts/lib/models.py +++ /dev/null @@ -1,39 +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 # -############################################################################### - -from sqlalchemy import create_engine -from sqlalchemy.orm import scoped_session, sessionmaker, mapper - -from openlp.plugins.alerts.lib.meta import metadata -from openlp.plugins.alerts.lib.tables import * -from openlp.plugins.alerts.lib.classes import * - -def init_models(url): - engine = create_engine(url) - metadata.bind = engine - session = scoped_session(sessionmaker(autoflush=True, autocommit=False, - bind=engine)) - mapper(AlertItem, alerts_table) - return session diff --git a/openlp/plugins/alerts/lib/tables.py b/openlp/plugins/alerts/lib/tables.py deleted file mode 100644 index 0e707570d..000000000 --- a/openlp/plugins/alerts/lib/tables.py +++ /dev/null @@ -1,33 +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 # -############################################################################### - -from sqlalchemy import Column, Table, types - -from openlp.plugins.alerts.lib.meta import metadata - -# Definition of the "alerts" table -alerts_table = Table(u'alerts', metadata, - Column(u'id', types.Integer(), primary_key=True), - Column(u'text', types.UnicodeText, nullable=False)) diff --git a/openlp/plugins/bibles/lib/models.py b/openlp/plugins/bibles/lib/models.py index d970bce08..2cd52fb0b 100644 --- a/openlp/plugins/bibles/lib/models.py +++ b/openlp/plugins/bibles/lib/models.py @@ -27,7 +27,7 @@ from sqlalchemy import Column, Table, MetaData, ForeignKey, types, \ create_engine from sqlalchemy.orm import mapper, relation, sessionmaker, scoped_session -from openlp.core.lib import BaseModel +from openlp.core.lib.db import BaseModel class BibleMeta(BaseModel): diff --git a/openlp/plugins/custom/lib/classes.py b/openlp/plugins/custom/lib/classes.py index dc6c5c1b8..6d8c623d1 100644 --- a/openlp/plugins/custom/lib/classes.py +++ b/openlp/plugins/custom/lib/classes.py @@ -23,7 +23,7 @@ # Temple Place, Suite 330, Boston, MA 02111-1307 USA # ############################################################################### -from openlp.core.lib import BaseModel +from openlp.core.lib.db import BaseModel class CustomSlide(BaseModel): """ diff --git a/openlp/plugins/songs/forms/authorsdialog.py b/openlp/plugins/songs/forms/authorsdialog.py index 082b1133d..1a7a4ccb5 100644 --- a/openlp/plugins/songs/forms/authorsdialog.py +++ b/openlp/plugins/songs/forms/authorsdialog.py @@ -24,6 +24,7 @@ ############################################################################### from PyQt4 import QtCore, QtGui + from openlp.core.lib import translate class Ui_AuthorsDialog(object): diff --git a/openlp/plugins/songs/forms/authorsform.py b/openlp/plugins/songs/forms/authorsform.py index 4d7fcd4b6..c5d0b2fa4 100644 --- a/openlp/plugins/songs/forms/authorsform.py +++ b/openlp/plugins/songs/forms/authorsform.py @@ -28,7 +28,6 @@ from PyQt4 import QtGui, QtCore from openlp.core.lib import translate from openlp.plugins.songs.forms.authorsdialog import Ui_AuthorsDialog - class AuthorsForm(QtGui.QDialog, Ui_AuthorsDialog): """ Class to control the Maintenance of Authors Dialog diff --git a/openlp/plugins/songs/forms/editsongdialog.py b/openlp/plugins/songs/forms/editsongdialog.py index d613715dd..9c088ea30 100644 --- a/openlp/plugins/songs/forms/editsongdialog.py +++ b/openlp/plugins/songs/forms/editsongdialog.py @@ -24,9 +24,8 @@ ############################################################################### from PyQt4 import QtCore, QtGui -from openlp.core.lib import translate -from openlp.core.lib import build_icon +from openlp.core.lib import build_icon, translate class Ui_EditSongDialog(object): def setupUi(self, EditSongDialog): diff --git a/openlp/plugins/songs/forms/editsongform.py b/openlp/plugins/songs/forms/editsongform.py index d23bcd298..1ae8796f5 100644 --- a/openlp/plugins/songs/forms/editsongform.py +++ b/openlp/plugins/songs/forms/editsongform.py @@ -30,7 +30,7 @@ from PyQt4 import QtCore, QtGui from openlp.core.lib import SongXMLBuilder, SongXMLParser, Receiver, translate from openlp.plugins.songs.forms import EditVerseForm -from openlp.plugins.songs.lib.models import Song +from openlp.plugins.songs.lib.db import Song from editsongdialog import Ui_EditSongDialog log = logging.getLogger(__name__) diff --git a/openlp/plugins/songs/forms/songbookdialog.py b/openlp/plugins/songs/forms/songbookdialog.py index 06e27d04e..578be03fc 100644 --- a/openlp/plugins/songs/forms/songbookdialog.py +++ b/openlp/plugins/songs/forms/songbookdialog.py @@ -24,6 +24,7 @@ ############################################################################### from PyQt4 import QtCore, QtGui + from openlp.core.lib import translate class Ui_SongBookDialog(object): diff --git a/openlp/plugins/songs/forms/songbookform.py b/openlp/plugins/songs/forms/songbookform.py index ad85990f0..172b74892 100644 --- a/openlp/plugins/songs/forms/songbookform.py +++ b/openlp/plugins/songs/forms/songbookform.py @@ -28,7 +28,6 @@ from PyQt4 import QtGui from openlp.core.lib import translate from openlp.plugins.songs.forms.songbookdialog import Ui_SongBookDialog - class SongBookForm(QtGui.QDialog, Ui_SongBookDialog): """ Class documentation goes here. diff --git a/openlp/plugins/songs/forms/songmaintenanceform.py b/openlp/plugins/songs/forms/songmaintenanceform.py index 34388dd62..d6ac7bf04 100644 --- a/openlp/plugins/songs/forms/songmaintenanceform.py +++ b/openlp/plugins/songs/forms/songmaintenanceform.py @@ -25,12 +25,12 @@ from PyQt4 import QtGui, QtCore -from openlp.plugins.songs.lib.classes import Author, Book, Topic +from openlp.core.lib import translate +from openlp.plugins.songs.lib.db import Author, Book, Topic from songmaintenancedialog import Ui_SongMaintenanceDialog from authorsform import AuthorsForm from topicsform import TopicsForm from songbookform import SongBookForm -from openlp.core.lib import translate class SongMaintenanceForm(QtGui.QDialog, Ui_SongMaintenanceDialog): """ diff --git a/openlp/plugins/songs/forms/topicsdialog.py b/openlp/plugins/songs/forms/topicsdialog.py index 3ed9da3c3..9200285f7 100644 --- a/openlp/plugins/songs/forms/topicsdialog.py +++ b/openlp/plugins/songs/forms/topicsdialog.py @@ -24,6 +24,7 @@ ############################################################################### from PyQt4 import QtCore, QtGui + from openlp.core.lib import translate class Ui_TopicsDialog(object): diff --git a/openlp/plugins/songs/forms/topicsform.py b/openlp/plugins/songs/forms/topicsform.py index fc5efe38d..a78c88c5a 100644 --- a/openlp/plugins/songs/forms/topicsform.py +++ b/openlp/plugins/songs/forms/topicsform.py @@ -28,7 +28,6 @@ from PyQt4 import QtGui from openlp.core.lib import translate from openlp.plugins.songs.forms.topicsdialog import Ui_TopicsDialog - class TopicsForm(QtGui.QDialog, Ui_TopicsDialog): """ Class documentation goes here. diff --git a/openlp/plugins/songs/lib/classes.py b/openlp/plugins/songs/lib/classes.py deleted file mode 100644 index c465588a4..000000000 --- a/openlp/plugins/songs/lib/classes.py +++ /dev/null @@ -1,52 +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 # -############################################################################### - -from openlp.core.lib import BaseModel - -class Author(BaseModel): - """ - Author model - """ - pass - -class Book(BaseModel): - """ - Book model - """ - def __repr__(self): - return u'' % ( - str(self.id), self.name, self.publisher) - -class Song(BaseModel): - """ - Song model - """ - pass - -class Topic(BaseModel): - """ - Topic model - """ - pass diff --git a/openlp/plugins/songs/lib/db.py b/openlp/plugins/songs/lib/db.py new file mode 100644 index 000000000..3c1aae7cb --- /dev/null +++ b/openlp/plugins/songs/lib/db.py @@ -0,0 +1,150 @@ +# -*- 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 # +############################################################################### +""" +The :mod:`db` module provides the database and schema that is the backend for +the Songs plugin +""" + +from sqlalchemy import Column, ForeignKey, Index, Table, types +from sqlalchemy.orm import mapper, relation + +from openlp.core.lib.db import BaseModel, init_db + +class Author(BaseModel): + """ + Author model + """ + pass + +class Book(BaseModel): + """ + Book model + """ + def __repr__(self): + return u'' % ( + str(self.id), self.name, self.publisher) + +class Song(BaseModel): + """ + Song model + """ + pass + +class Topic(BaseModel): + """ + Topic model + """ + pass + +def init_schema(url): + """ + Setup the songs database connection and initialise the database schema + + ``url`` + The database to setup + """ + session, metadata = init_db(url, auto_flush=False) + + # Definition of the "authors" table + authors_table = Table(u'authors', metadata, + Column(u'id', types.Integer, primary_key=True), + Column(u'first_name', types.Unicode(128)), + Column(u'last_name', types.Unicode(128)), + Column(u'display_name', types.Unicode(255), nullable=False) + ) + + # Definition of the "song_books" table + song_books_table = Table(u'song_books', metadata, + Column(u'id', types.Integer, primary_key=True), + Column(u'name', types.Unicode(128), nullable=False), + Column(u'publisher', types.Unicode(128)) + ) + + # Definition of the "songs" table + songs_table = Table(u'songs', metadata, + Column(u'id', types.Integer, primary_key=True), + Column(u'song_book_id', types.Integer, + ForeignKey(u'song_books.id'), default=0), + Column(u'title', types.Unicode(255), nullable=False), + Column(u'lyrics', types.UnicodeText, nullable=False), + Column(u'verse_order', types.Unicode(128)), + Column(u'copyright', types.Unicode(255)), + Column(u'comments', types.UnicodeText), + Column(u'ccli_number', types.Unicode(64)), + Column(u'song_number', types.Unicode(64)), + Column(u'theme_name', types.Unicode(128)), + Column(u'search_title', types.Unicode(255), index=True, nullable=False), + Column(u'search_lyrics', types.UnicodeText, index=True, nullable=False) + ) + + # Definition of the "topics" table + topics_table = Table(u'topics', metadata, + Column(u'id', types.Integer, primary_key=True), + Column(u'name', types.Unicode(128), nullable=False) + ) + + # Definition of the "authors_songs" table + authors_songs_table = Table(u'authors_songs', metadata, + Column(u'author_id', types.Integer, + ForeignKey(u'authors.id'), primary_key=True), + Column(u'song_id', types.Integer, + ForeignKey(u'songs.id'), primary_key=True) + ) + + # Definition of the "songs_topics" table + songs_topics_table = Table(u'songs_topics', metadata, + Column(u'song_id', types.Integer, + ForeignKey(u'songs.id'), primary_key=True), + Column(u'topic_id', types.Integer, + ForeignKey(u'topics.id'), primary_key=True) + ) + + # Define table indexes + Index(u'authors_id', authors_table.c.id) + Index(u'authors_display_name_id', authors_table.c.display_name, + authors_table.c.id) + Index(u'song_books_id', song_books_table.c.id) + Index(u'songs_id', songs_table.c.id) + Index(u'topics_id', topics_table.c.id) + Index(u'authors_songs_author', authors_songs_table.c.author_id, + authors_songs_table.c.song_id) + Index(u'authors_songs_song', authors_songs_table.c.song_id, + authors_songs_table.c.author_id) + Index(u'topics_song_topic', songs_topics_table.c.topic_id, + songs_topics_table.c.song_id) + Index(u'topics_song_song', songs_topics_table.c.song_id, + songs_topics_table.c.topic_id) + + mapper(Author, authors_table) + mapper(Book, song_books_table) + mapper(Song, songs_table, + properties={'authors': relation(Author, backref='songs', + secondary=authors_songs_table), + 'book': relation(Book, backref='songs'), + 'topics': relation(Topic, backref='songs', + secondary=songs_topics_table)}) + mapper(Topic, topics_table) + + return session, metadata diff --git a/openlp/plugins/songs/lib/manager.py b/openlp/plugins/songs/lib/manager.py index 0b833fb0b..0194cea5b 100644 --- a/openlp/plugins/songs/lib/manager.py +++ b/openlp/plugins/songs/lib/manager.py @@ -29,8 +29,7 @@ from PyQt4 import QtCore from sqlalchemy.exceptions import InvalidRequestError from openlp.core.utils import AppLocation -from openlp.plugins.songs.lib.models import init_models, metadata, Song, \ - Author, Topic, Book +from openlp.plugins.songs.lib.db import init_schema, Song, Author, Topic, Book #from openlp.plugins.songs.lib import OpenLyricsSong, OpenSongSong, CCLISong, \ # CSVSong @@ -111,8 +110,8 @@ class SongManager(object): u'db hostname', QtCore.QVariant(u'')).toString()), unicode(settings.value( u'db database', QtCore.QVariant(u'')).toString())) - self.session = init_models(self.db_url) - metadata.create_all(checkfirst=True) + self.session, self.metadata = init_schema(self.db_url) + self.metadata.create_all(checkfirst=True) settings.endGroup() log.debug(u'Song Initialised') diff --git a/openlp/plugins/songs/lib/meta.py b/openlp/plugins/songs/lib/meta.py deleted file mode 100644 index affa31969..000000000 --- a/openlp/plugins/songs/lib/meta.py +++ /dev/null @@ -1,38 +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 # -############################################################################### - -from sqlalchemy import MetaData - -__all__ = ['session', 'metadata', 'engine'] - -# SQLAlchemy database engine. Updated by model.init_model() -engine = None - -# SQLAlchemy session manager. Updated by model.init_model() -session = None - -# Global metadata. If you have multiple databases with overlapping table -# names, you'll need a metadata for each database -metadata = MetaData() diff --git a/openlp/plugins/songs/lib/models.py b/openlp/plugins/songs/lib/models.py deleted file mode 100644 index 272981d20..000000000 --- a/openlp/plugins/songs/lib/models.py +++ /dev/null @@ -1,48 +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 # -############################################################################### - -from sqlalchemy import create_engine -from sqlalchemy.orm import scoped_session, sessionmaker, mapper, relation - -from openlp.plugins.songs.lib.meta import metadata -from openlp.plugins.songs.lib.tables import * -from openlp.plugins.songs.lib.classes import * - -def init_models(url): - engine = create_engine(url) - metadata.bind = engine - session = scoped_session(sessionmaker(autoflush=False, autocommit=False, - bind=engine)) - mapper(Author, authors_table) - mapper(Book, song_books_table) - mapper(Song, songs_table, - properties={'authors': relation(Author, backref='songs', - secondary=authors_songs_table), - 'book': relation(Book, backref='songs'), - 'topics': relation(Topic, backref='songs', - secondary=songs_topics_table)}) - mapper(Topic, topics_table) - return session - diff --git a/openlp/plugins/songs/lib/songimport.py b/openlp/plugins/songs/lib/songimport.py index fa016729d..f2222e09b 100644 --- a/openlp/plugins/songs/lib/songimport.py +++ b/openlp/plugins/songs/lib/songimport.py @@ -28,7 +28,7 @@ import string from PyQt4 import QtGui from openlp.core.lib import SongXMLBuilder -from openlp.plugins.songs.lib.models import Song, Author, Topic, Book +from openlp.plugins.songs.lib.db import Song, Author, Topic, Book from openlp.plugins.songs.forms import VerseType class SongImport(object): diff --git a/openlp/plugins/songs/lib/tables.py b/openlp/plugins/songs/lib/tables.py deleted file mode 100644 index 24137d7b1..000000000 --- a/openlp/plugins/songs/lib/tables.py +++ /dev/null @@ -1,99 +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 # -############################################################################### - -from sqlalchemy import * -from sqlalchemy import Column, Table, ForeignKey, types - -from openlp.plugins.songs.lib.meta import metadata - -# Definition of the "authors" table -authors_table = Table(u'authors', metadata, - Column(u'id', types.Integer, primary_key=True), - Column(u'first_name', types.Unicode(128)), - Column(u'last_name', types.Unicode(128)), - Column(u'display_name', types.Unicode(255), nullable=False) -) - -# Definition of the "song_books" table -song_books_table = Table(u'song_books', metadata, - Column(u'id', types.Integer, primary_key=True), - Column(u'name', types.Unicode(128), nullable=False), - Column(u'publisher', types.Unicode(128)) -) - -# Definition of the "songs" table -songs_table = Table(u'songs', metadata, - Column(u'id', types.Integer, primary_key=True), - Column(u'song_book_id', types.Integer, - ForeignKey(u'song_books.id'), default=0), - Column(u'title', types.Unicode(255), nullable=False), - Column(u'lyrics', types.UnicodeText, nullable=False), - Column(u'verse_order', types.Unicode(128)), - Column(u'copyright', types.Unicode(255)), - Column(u'comments', types.UnicodeText), - Column(u'ccli_number', types.Unicode(64)), - Column(u'song_number', types.Unicode(64)), - Column(u'theme_name', types.Unicode(128)), - Column(u'search_title', types.Unicode(255), index=True, nullable=False), - Column(u'search_lyrics', types.UnicodeText, index=True, nullable=False) -) - -# Definition of the "topics" table -topics_table = Table(u'topics', metadata, - Column(u'id', types.Integer, primary_key=True), - Column(u'name', types.Unicode(128), nullable=False) -) - -# Definition of the "authors_songs" table -authors_songs_table = Table(u'authors_songs', metadata, - Column(u'author_id', types.Integer, - ForeignKey(u'authors.id'), primary_key=True), - Column(u'song_id', types.Integer, - ForeignKey(u'songs.id'), primary_key=True) -) - -# Definition of the "songs_topics" table -songs_topics_table = Table(u'songs_topics', metadata, - Column(u'song_id', types.Integer, - ForeignKey(u'songs.id'), primary_key=True), - Column(u'topic_id', types.Integer, - ForeignKey(u'topics.id'), primary_key=True) -) - -# Define table indexes -Index(u'authors_id', authors_table.c.id) -Index(u'authors_display_name_id', authors_table.c.display_name, - authors_table.c.id) -Index(u'song_books_id', song_books_table.c.id) -Index(u'songs_id', songs_table.c.id) -Index(u'topics_id', topics_table.c.id) -Index(u'authors_songs_author', authors_songs_table.c.author_id, - authors_songs_table.c.song_id) -Index(u'authors_songs_song', authors_songs_table.c.song_id, - authors_songs_table.c.author_id) -Index(u'topics_song_topic', songs_topics_table.c.topic_id, - songs_topics_table.c.song_id) -Index(u'topics_song_song', songs_topics_table.c.song_id, - songs_topics_table.c.topic_id) diff --git a/openlp/plugins/songs/songsplugin.py b/openlp/plugins/songs/songsplugin.py index 993b18bb9..09485082c 100644 --- a/openlp/plugins/songs/songsplugin.py +++ b/openlp/plugins/songs/songsplugin.py @@ -34,7 +34,6 @@ from openlp.plugins.songs.lib import SongManager, SongMediaItem, SongsTab, \ log = logging.getLogger(__name__) - class SongsPlugin(Plugin): """ This is the number 1 plugin, if importance were placed on any diff --git a/openlp/plugins/songusage/lib/classes.py b/openlp/plugins/songusage/lib/classes.py index 298380f58..885d87faf 100644 --- a/openlp/plugins/songusage/lib/classes.py +++ b/openlp/plugins/songusage/lib/classes.py @@ -23,7 +23,7 @@ # Temple Place, Suite 330, Boston, MA 02111-1307 USA # ############################################################################### -from openlp.core.lib import BaseModel +from openlp.core.lib.db import BaseModel class SongUsageItem(BaseModel): """