forked from openlp/openlp
Changed layout of AlternativeBookNamesDB from sqlalchemy to sqlite3
This commit is contained in:
parent
a1c5ece681
commit
df0d9244eb
@ -70,13 +70,6 @@ class Verse(BaseModel):
|
|||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class AlternativeBookNames(BaseModel):
|
|
||||||
"""
|
|
||||||
Alternative Book Names model
|
|
||||||
"""
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def init_schema(url):
|
def init_schema(url):
|
||||||
"""
|
"""
|
||||||
Setup a bible database connection and initialise the database schema.
|
Setup a bible database connection and initialise the database schema.
|
||||||
@ -136,30 +129,6 @@ def init_schema(url):
|
|||||||
metadata.create_all(checkfirst=True)
|
metadata.create_all(checkfirst=True)
|
||||||
return session
|
return session
|
||||||
|
|
||||||
def init_schema_alternative_book_names(url):
|
|
||||||
"""
|
|
||||||
Setup a alternative book names database connection and initialise the
|
|
||||||
database schema.
|
|
||||||
|
|
||||||
``url``
|
|
||||||
The database to setup.
|
|
||||||
"""
|
|
||||||
session, metadata = init_db(url)
|
|
||||||
|
|
||||||
alternative_book_names_table = Table(u'alternative_book_names', metadata,
|
|
||||||
Column(u'id', types.Integer, primary_key=True),
|
|
||||||
Column(u'book_reference_id', types.Integer),
|
|
||||||
Column(u'language_id', types.Integer),
|
|
||||||
Column(u'name', types.Unicode(50), index=True),
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
class_mapper(AlternativeBookNames)
|
|
||||||
except UnmappedClassError:
|
|
||||||
mapper(AlternativeBookNames, alternative_book_names_table)
|
|
||||||
|
|
||||||
metadata.create_all(checkfirst=True)
|
|
||||||
return session
|
|
||||||
|
|
||||||
class BibleDB(QtCore.QObject, Manager):
|
class BibleDB(QtCore.QObject, Manager):
|
||||||
"""
|
"""
|
||||||
@ -388,17 +357,15 @@ class BibleDB(QtCore.QObject, Manager):
|
|||||||
def get_book_ref_id_by_name(self, book, language_id=None):
|
def get_book_ref_id_by_name(self, book, language_id=None):
|
||||||
log.debug(u'BibleDB.get_book_ref_id_by_name:("%s", "%s")', book,
|
log.debug(u'BibleDB.get_book_ref_id_by_name:("%s", "%s")', book,
|
||||||
language_id)
|
language_id)
|
||||||
self.alternative_book_names_cache = AlternativeBookNamesDB(
|
|
||||||
self.bible_plugin, path=self.path)
|
|
||||||
if BiblesResourcesDB.get_book(book):
|
if BiblesResourcesDB.get_book(book):
|
||||||
book_temp = BiblesResourcesDB.get_book(book)
|
book_temp = BiblesResourcesDB.get_book(book)
|
||||||
book_id = book_temp[u'id']
|
book_id = book_temp[u'id']
|
||||||
elif BiblesResourcesDB.get_alternative_book_name(book, language_id):
|
elif BiblesResourcesDB.get_alternative_book_name(book, language_id):
|
||||||
book_id = BiblesResourcesDB.get_alternative_book_name(book,
|
book_id = BiblesResourcesDB.get_alternative_book_name(book,
|
||||||
language_id)
|
language_id)
|
||||||
elif self.alternative_book_names_cache.get_book_reference_id(book,
|
elif AlternativeBookNamesDB.get_book_reference_id(book,
|
||||||
language_id):
|
language_id):
|
||||||
book_id = self.alternative_book_names_cache.get_book_reference_id(
|
book_id = AlternativeBookNamesDB.get_book_reference_id(
|
||||||
book, language_id)
|
book, language_id)
|
||||||
else:
|
else:
|
||||||
from openlp.plugins.bibles.forms import BookNameForm
|
from openlp.plugins.bibles.forms import BookNameForm
|
||||||
@ -415,7 +382,7 @@ class BibleDB(QtCore.QObject, Manager):
|
|||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
if book_id:
|
if book_id:
|
||||||
self.alternative_book_names_cache.create_alternative_book_name(
|
AlternativeBookNamesDB.create_alternative_book_name(
|
||||||
book, book_id, language_id)
|
book, book_id, language_id)
|
||||||
if book_id:
|
if book_id:
|
||||||
return book_id
|
return book_id
|
||||||
@ -822,6 +789,12 @@ class BiblesResourcesDB(QtCore.QObject, Manager):
|
|||||||
def get_alternative_book_name(name, language_id=None):
|
def get_alternative_book_name(name, language_id=None):
|
||||||
"""
|
"""
|
||||||
Return a book_reference_id if the name matches.
|
Return a book_reference_id if the name matches.
|
||||||
|
|
||||||
|
``name``
|
||||||
|
The name to search the id.
|
||||||
|
|
||||||
|
``language_id``
|
||||||
|
The language_id for which language should be searched
|
||||||
"""
|
"""
|
||||||
log.debug(u'BiblesResourcesDB.get_alternative_book_name("%s", "%s")',
|
log.debug(u'BiblesResourcesDB.get_alternative_book_name("%s", "%s")',
|
||||||
name, language_id)
|
name, language_id)
|
||||||
@ -898,73 +871,85 @@ class BiblesResourcesDB(QtCore.QObject, Manager):
|
|||||||
})
|
})
|
||||||
return testament_list
|
return testament_list
|
||||||
|
|
||||||
|
|
||||||
class AlternativeBookNamesDB(QtCore.QObject, Manager):
|
class AlternativeBookNamesDB(QtCore.QObject, Manager):
|
||||||
"""
|
"""
|
||||||
This class represents a database-bound alternative book names system.
|
This class represents a database-bound alternative book names system.
|
||||||
"""
|
"""
|
||||||
|
cursor = None
|
||||||
|
conn = None
|
||||||
|
|
||||||
def __init__(self, parent, **kwargs):
|
@staticmethod
|
||||||
|
def get_cursor():
|
||||||
"""
|
"""
|
||||||
The constructor loads up the database and creates and initialises the
|
Return the cursor object. Instantiate one if it doesn't exist yet.
|
||||||
tables if the database doesn't exist.
|
If necessary loads up the database and creates the tables if the
|
||||||
|
database doesn't exist.
|
||||||
**Required keyword arguments:**
|
|
||||||
|
|
||||||
``path``
|
|
||||||
The path to the bible database file.
|
|
||||||
|
|
||||||
``name``
|
|
||||||
The name of the database. This is also used as the file name for
|
|
||||||
SQLite databases.
|
|
||||||
"""
|
"""
|
||||||
log.info(u'AlternativeBookNamesDB loaded')
|
if AlternativeBookNamesDB.cursor is None:
|
||||||
QtCore.QObject.__init__(self)
|
filepath = os.path.join(
|
||||||
self.bible_plugin = parent
|
AppLocation.get_directory(AppLocation.DataDir), u'bibles',
|
||||||
if u'path' not in kwargs:
|
u'resources', u'alternative_book_names.sqlite')
|
||||||
raise KeyError(u'Missing keyword argument "path".')
|
log.debug(u'Filepath: %s' % filepath)
|
||||||
self.stop_import_flag = False
|
if not os.path.exists(filepath):
|
||||||
self.name = u'alternative_book_names.sqlite'
|
#create new DB, create table alternative_book_names
|
||||||
if not isinstance(self.name, unicode):
|
AlternativeBookNamesDB.conn = sqlite3.connect(filepath)
|
||||||
self.name = unicode(self.name, u'utf-8')
|
AlternativeBookNamesDB.conn.execute(u'CREATE TABLE '
|
||||||
self.file = self.name
|
u'alternative_book_names(id INTEGER NOT NULL, '
|
||||||
Manager.__init__(self, u'bibles/resources',
|
u'book_reference_id INTEGER, language_id INTEGER, name '
|
||||||
init_schema_alternative_book_names, self.file)
|
u'VARCHAR(50), PRIMARY KEY (id))')
|
||||||
self.wizard = None
|
else:
|
||||||
QtCore.QObject.connect(Receiver.get_receiver(),
|
#use existing DB
|
||||||
QtCore.SIGNAL(u'openlp_stop_wizard'), self.stop_import)
|
AlternativeBookNamesDB.conn = sqlite3.connect(filepath)
|
||||||
|
AlternativeBookNamesDB.cursor = AlternativeBookNamesDB.conn.cursor()
|
||||||
|
return AlternativeBookNamesDB.cursor
|
||||||
|
|
||||||
def stop_import(self):
|
@staticmethod
|
||||||
|
def run_sql(query, parameters=(), commit=None):
|
||||||
"""
|
"""
|
||||||
Stops the import of the Bible.
|
Run an SQL query on the database, returning the results.
|
||||||
"""
|
|
||||||
log.debug(u'Stopping import')
|
|
||||||
self.stop_import_flag = True
|
|
||||||
|
|
||||||
def get_book_reference_id(self, name, language=None):
|
``query``
|
||||||
|
The actual SQL query to run.
|
||||||
|
|
||||||
|
``parameters``
|
||||||
|
Any variable parameters to add to the query
|
||||||
|
|
||||||
|
``commit``
|
||||||
|
If a commit statement is necessary this should be True.
|
||||||
"""
|
"""
|
||||||
Return the book_reference_id of a book by name.
|
cursor = AlternativeBookNamesDB.get_cursor()
|
||||||
|
cursor.execute(query, parameters)
|
||||||
|
if commit:
|
||||||
|
AlternativeBookNamesDB.conn.commit()
|
||||||
|
return cursor.fetchall()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_book_reference_id(name, language_id=None):
|
||||||
|
"""
|
||||||
|
Return a book_reference_id if the name matches.
|
||||||
|
|
||||||
``name``
|
``name``
|
||||||
The name to search the id.
|
The name to search the id.
|
||||||
|
|
||||||
``language``
|
``language_id``
|
||||||
The language for which should be searched
|
The language_id for which language should be searched
|
||||||
"""
|
"""
|
||||||
log.debug(u'AlternativeBookNamesDB.get_book_reference_id("%s")', name)
|
log.debug(u'AlternativeBookNamesDB.get_book_reference_id("%s", "%s")',
|
||||||
if language:
|
name, language_id)
|
||||||
id = self.session.query(AlternativeBookNames.book_reference_id)\
|
if language_id:
|
||||||
.filter(AlternativeBookNames.name.like(name))\
|
id = AlternativeBookNamesDB.run_sql(u'SELECT book_reference_id FROM '
|
||||||
.filter(AlternativeBookNames.language_id.like(language)).first()
|
u'alternative_book_names WHERE name = ? AND language_id = ?', (name, language_id))
|
||||||
else:
|
else:
|
||||||
id = self.get_object_filtered(AlternativeBookNames.book_reference_id,
|
id = AlternativeBookNamesDB.run_sql(u'SELECT book_reference_id FROM '
|
||||||
AlternativeBookNames.name.like(name))
|
u'alternative_book_names WHERE name = ?', name)
|
||||||
if not id:
|
if not id:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
return id[0]
|
return id[0][0]
|
||||||
|
|
||||||
def create_alternative_book_name(self, name, book_reference_id,
|
@staticmethod
|
||||||
language_id):
|
def create_alternative_book_name(name, book_reference_id, language_id):
|
||||||
"""
|
"""
|
||||||
Add an alternative book name to the database.
|
Add an alternative book name to the database.
|
||||||
|
|
||||||
@ -979,7 +964,7 @@ class AlternativeBookNamesDB(QtCore.QObject, Manager):
|
|||||||
"""
|
"""
|
||||||
log.debug(u'AlternativeBookNamesDB.create_alternative_book_name("%s", '
|
log.debug(u'AlternativeBookNamesDB.create_alternative_book_name("%s", '
|
||||||
'"%s", "%s"', name, book_reference_id, language_id)
|
'"%s", "%s"', name, book_reference_id, language_id)
|
||||||
alternative_book_name = AlternativeBookNames.populate(name=name,
|
alternative_book_name = AlternativeBookNamesDB.run_sql(u'INSERT INTO '
|
||||||
book_reference_id=book_reference_id, language_id=language_id)
|
u'alternative_book_names(book_reference_id, language_id, name) '
|
||||||
self.save_object(alternative_book_name)
|
u'VALUES (?, ?, ?)', (book_reference_id, language_id, name), True)
|
||||||
return alternative_book_name
|
return alternative_book_name
|
||||||
|
Loading…
Reference in New Issue
Block a user