mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Modify database driver to workaround problems with OS X 10.5.2
This commit is contained in:
parent
46d3bae2c9
commit
d411a0d3fc
@ -156,7 +156,7 @@ class MetadataSingleDialog(QDialog, Ui_MetadataSingleDialog):
|
|||||||
self.remove_unused_series)
|
self.remove_unused_series)
|
||||||
self.timeout = float(QSettings().value('network timeout', QVariant(5)).toInt()[0])
|
self.timeout = float(QSettings().value('network timeout', QVariant(5)).toInt()[0])
|
||||||
self.title.setText(db.title(row))
|
self.title.setText(db.title(row))
|
||||||
isbn = db.isbn(self.id)
|
isbn = db.isbn(self.id, index_is_id=True)
|
||||||
if not isbn:
|
if not isbn:
|
||||||
isbn = ''
|
isbn = ''
|
||||||
self.isbn.setText(isbn)
|
self.isbn.setText(isbn)
|
||||||
|
@ -32,7 +32,8 @@ class Concatenate(object):
|
|||||||
self.ans = ''
|
self.ans = ''
|
||||||
|
|
||||||
def step(self, value):
|
def step(self, value):
|
||||||
self.ans += value + self.sep
|
if value is not None:
|
||||||
|
self.ans += value + self.sep
|
||||||
|
|
||||||
def finalize(self):
|
def finalize(self):
|
||||||
if not self.ans:
|
if not self.ans:
|
||||||
@ -890,8 +891,10 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
|
|||||||
def id(self, index):
|
def id(self, index):
|
||||||
return self.data[index][0]
|
return self.data[index][0]
|
||||||
|
|
||||||
def title(self, index):
|
def title(self, index, index_is_id=False):
|
||||||
return self.data[index][1]
|
if not index_is_id:
|
||||||
|
return self.data[index][1]
|
||||||
|
return self.conn.execute('SELECT title FROM meta WHERE id=?',(index,)).fetchone()[0]
|
||||||
|
|
||||||
def authors(self, index, index_is_id=False):
|
def authors(self, index, index_is_id=False):
|
||||||
''' Authors as a comman separated list or None'''
|
''' Authors as a comman separated list or None'''
|
||||||
@ -899,50 +902,59 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
|
|||||||
return self.data[index][2]
|
return self.data[index][2]
|
||||||
return self.conn.execute('SELECT authors FROM meta WHERE id=?',(index,)).fetchone()[0]
|
return self.conn.execute('SELECT authors FROM meta WHERE id=?',(index,)).fetchone()[0]
|
||||||
|
|
||||||
def isbn(self, id):
|
def isbn(self, idx, index_is_id=False):
|
||||||
|
id = idx if index_is_id else self.id(idx)
|
||||||
return self.conn.execute('SELECT isbn FROM books WHERE id=?',(id,)).fetchone()[0]
|
return self.conn.execute('SELECT isbn FROM books WHERE id=?',(id,)).fetchone()[0]
|
||||||
|
|
||||||
def author_sort(self, index):
|
def author_sort(self, index, index_is_id=False):
|
||||||
id = self.id(index)
|
id = index if index_is_id else self.id(index)
|
||||||
return self.conn.execute('SELECT author_sort FROM books WHERE id=?', (id,)).fetchone()[0]
|
return self.conn.execute('SELECT author_sort FROM books WHERE id=?', (id,)).fetchone()[0]
|
||||||
|
|
||||||
def publisher(self, index):
|
def publisher(self, index, index_is_id=False):
|
||||||
|
if index_is_id:
|
||||||
|
return self.conn.execute('SELECT publisher FROM meta WHERE id=?', (id,)).fetchone()[0]
|
||||||
return self.data[index][3]
|
return self.data[index][3]
|
||||||
|
|
||||||
def rating(self, index):
|
def rating(self, index, index_is_id=False):
|
||||||
|
if index_is_id:
|
||||||
|
return self.conn.execute('SELECT rating FROM meta WHERE id=?', (id,)).fetchone()[0]
|
||||||
return self.data[index][4]
|
return self.data[index][4]
|
||||||
|
|
||||||
def timestamp(self, index):
|
def timestamp(self, index, index_is_id=False):
|
||||||
|
if index_is_id:
|
||||||
|
return self.conn.execute('SELECT timestamp FROM meta WHERE id=?', (id,)).fetchone()[0]
|
||||||
return self.data[index][5]
|
return self.data[index][5]
|
||||||
|
|
||||||
def max_size(self, index):
|
def max_size(self, index, index_is_id=False):
|
||||||
|
if index_is_id:
|
||||||
|
return self.conn.execute('SELECT size FROM meta WHERE id=?', (id,)).fetchone()[0]
|
||||||
return self.data[index][6]
|
return self.data[index][6]
|
||||||
|
|
||||||
def cover(self, index):
|
def cover(self, index, index_is_id=False):
|
||||||
'''Cover as a data string or None'''
|
'''Cover as a data string or None'''
|
||||||
id = self.id(index)
|
id = index if index_is_id else self.id(index)
|
||||||
data = self.conn.execute('SELECT data FROM covers WHERE book=?', (id,)).fetchone()
|
data = self.conn.execute('SELECT data FROM covers WHERE book=?', (id,)).fetchone()
|
||||||
if not data or not data[0]:
|
if not data or not data[0]:
|
||||||
return None
|
return None
|
||||||
return(decompress(data[0]))
|
return(decompress(data[0]))
|
||||||
|
|
||||||
def tags(self, index):
|
def tags(self, index, index_is_id=False):
|
||||||
'''tags as a comma separated list or None'''
|
'''tags as a comma separated list or None'''
|
||||||
id = self.id(index)
|
id = index if index_is_id else self.id(index)
|
||||||
matches = self.conn.execute('SELECT concat(name) FROM tags WHERE tags.id IN (SELECT tag from books_tags_link WHERE book=?)', (id,)).fetchall()
|
matches = self.conn.execute('SELECT concat(name) FROM tags WHERE tags.id IN (SELECT tag from books_tags_link WHERE book=?)', (id,)).fetchall()
|
||||||
if not matches or not matches[0][0]:
|
if not matches or not matches[0][0]:
|
||||||
return None
|
return None
|
||||||
matches = [t.lower().strip() for t in matches[0][0].split(',')]
|
matches = [t.lower().strip() for t in matches[0][0].split(',')]
|
||||||
return ','.join(matches)
|
return ','.join(matches)
|
||||||
|
|
||||||
def series_id(self, index):
|
def series_id(self, index, index_is_id=False):
|
||||||
id = self.id(index)
|
id = index if index_is_id else self.id(index)
|
||||||
ans= self.conn.execute('SELECT series from books_series_link WHERE book=?', (id,)).fetchone()
|
ans= self.conn.execute('SELECT series from books_series_link WHERE book=?', (id,)).fetchone()
|
||||||
if ans:
|
if ans:
|
||||||
return ans[0]
|
return ans[0]
|
||||||
|
|
||||||
def series(self, index):
|
def series(self, index, index_is_id=False):
|
||||||
id = self.series_id(index)
|
id = self.series_id(index, index_is_id)
|
||||||
ans = self.conn.execute('SELECT name from series WHERE id=?', (id,)).fetchone()
|
ans = self.conn.execute('SELECT name from series WHERE id=?', (id,)).fetchone()
|
||||||
if ans:
|
if ans:
|
||||||
return ans[0]
|
return ans[0]
|
||||||
@ -965,18 +977,18 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
|
|||||||
ans.sort(cmp = lambda x, y: cmp(self.series_index(x, True), self.series_index(y, True)))
|
ans.sort(cmp = lambda x, y: cmp(self.series_index(x, True), self.series_index(y, True)))
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
def books_in_series_of(self, index):
|
def books_in_series_of(self, index, index_is_id=False):
|
||||||
'''
|
'''
|
||||||
Return an ordered list of all books in the series that the book indetified by index belongs to.
|
Return an ordered list of all books in the series that the book indetified by index belongs to.
|
||||||
If the book does not belong to a series return an empty list. The list contains book ids.
|
If the book does not belong to a series return an empty list. The list contains book ids.
|
||||||
'''
|
'''
|
||||||
series_id = self.series_id(index)
|
series_id = self.series_id(index, index_is_id=index_is_id)
|
||||||
return self.books_in_series(series_id)
|
return self.books_in_series(series_id)
|
||||||
|
|
||||||
|
|
||||||
def comments(self, index):
|
def comments(self, index, index_is_id=False):
|
||||||
'''Comments as string or None'''
|
'''Comments as string or None'''
|
||||||
id = self.id(index)
|
id = index if index_is_id else self.id(index)
|
||||||
matches = self.conn.execute('SELECT text FROM comments WHERE book=?', (id,)).fetchall()
|
matches = self.conn.execute('SELECT text FROM comments WHERE book=?', (id,)).fetchall()
|
||||||
if not matches:
|
if not matches:
|
||||||
return None
|
return None
|
||||||
@ -997,8 +1009,8 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
|
|||||||
return self.conn.execute('SELECT uncompressed_size FROM data WHERE data.book=? AND data.format=?', (id, format)).fetchone()[0]
|
return self.conn.execute('SELECT uncompressed_size FROM data WHERE data.book=? AND data.format=?', (id, format)).fetchone()[0]
|
||||||
|
|
||||||
|
|
||||||
def format(self, index, format):
|
def format(self, index, format, index_is_id=False):
|
||||||
id = self.id(index)
|
id = index if index_is_id else self.id(index)
|
||||||
return decompress(self.conn.execute('SELECT data FROM data WHERE book=? AND format=?', (id, format)).fetchone()[0])
|
return decompress(self.conn.execute('SELECT data FROM data WHERE book=? AND format=?', (id, format)).fetchone()[0])
|
||||||
|
|
||||||
def all_series(self):
|
def all_series(self):
|
||||||
@ -1040,8 +1052,8 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
|
|||||||
(id, ext, usize, data))
|
(id, ext, usize, data))
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
def remove_format(self, index, ext):
|
def remove_format(self, index, ext, index_is_id=False):
|
||||||
id = self.id(index)
|
id = index if index_is_id else self.id(index)
|
||||||
self.conn.execute('DELETE FROM data WHERE book=? AND format=?', (id, ext.lower()))
|
self.conn.execute('DELETE FROM data WHERE book=? AND format=?', (id, ext.lower()))
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
@ -1309,22 +1321,25 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
|
|||||||
self.conn.execute('DELETE FROM books WHERE id=?', (id,))
|
self.conn.execute('DELETE FROM books WHERE id=?', (id,))
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
def get_metadata(self, idx):
|
def get_metadata(self, idx, index_is_id=False):
|
||||||
aum = self.authors(idx)
|
'''
|
||||||
|
Convenience method to return metadata as a L{MetaInformation} object.
|
||||||
|
'''
|
||||||
|
aum = self.authors(idx, index_is_id=index_is_id)
|
||||||
if aum: aum = aum.split(',')
|
if aum: aum = aum.split(',')
|
||||||
mi = MetaInformation(self.title(idx), aum)
|
mi = MetaInformation(self.title(idx, index_is_id=index_is_id), aum)
|
||||||
mi.author_sort = self.author_sort(idx)
|
mi.author_sort = self.author_sort(idx, index_is_id=index_is_id)
|
||||||
mi.comments = self.comments(idx)
|
mi.comments = self.comments(idx, index_is_id=index_is_id)
|
||||||
mi.publisher = self.publisher(idx)
|
mi.publisher = self.publisher(idx, index_is_id=index_is_id)
|
||||||
tags = self.tags(idx)
|
tags = self.tags(idx, index_is_id=index_is_id)
|
||||||
if tags:
|
if tags:
|
||||||
mi.tags = [i.strip() for i in tags.split(',')]
|
mi.tags = [i.strip() for i in tags.split(',')]
|
||||||
mi.series = self.series(idx)
|
mi.series = self.series(idx, index_is_id=index_is_id)
|
||||||
if mi.series:
|
if mi.series:
|
||||||
mi.series_index = self.series_index(idx)
|
mi.series_index = self.series_index(idx, index_is_id=index_is_id)
|
||||||
mi.rating = self.rating(idx)
|
mi.rating = self.rating(idx, index_is_id=index_is_id)
|
||||||
id = self.id(idx)
|
mi.isbn = self.isbn(idx, index_is_id=index_is_id)
|
||||||
mi.isbn = self.isbn(id)
|
id = idx if index_is_id else self.id(idx)
|
||||||
mi.libprs_id = id
|
mi.libprs_id = id
|
||||||
return mi
|
return mi
|
||||||
|
|
||||||
@ -1332,16 +1347,17 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
|
|||||||
self.conn.execute('VACUUM;')
|
self.conn.execute('VACUUM;')
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
def export_to_dir(self, dir, indices, byauthor=False, single_dir=False):
|
def export_to_dir(self, dir, indices, byauthor=False, single_dir=False,
|
||||||
|
index_is_id=False):
|
||||||
if not os.path.exists(dir):
|
if not os.path.exists(dir):
|
||||||
raise IOError('Target directory does not exist: '+dir)
|
raise IOError('Target directory does not exist: '+dir)
|
||||||
by_author = {}
|
by_author = {}
|
||||||
for index in indices:
|
for index in indices:
|
||||||
id = self.id(index)
|
id = index if index_is_id else self.id(index)
|
||||||
au = self.conn.execute('SELECT author_sort FROM books WHERE id=?',
|
au = self.conn.execute('SELECT author_sort FROM books WHERE id=?',
|
||||||
(id,)).fetchone()[0]
|
(id,)).fetchone()[0]
|
||||||
if not au:
|
if not au:
|
||||||
au = self.authors(index)
|
au = self.authors(index, index_is_id=index_is_id)
|
||||||
if not au:
|
if not au:
|
||||||
au = 'Unknown'
|
au = 'Unknown'
|
||||||
au = au.split(',')[0]
|
au = au.split(',')[0]
|
||||||
@ -1355,9 +1371,10 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
|
|||||||
if not single_dir and not os.path.exists(apath):
|
if not single_dir and not os.path.exists(apath):
|
||||||
os.mkdir(apath)
|
os.mkdir(apath)
|
||||||
for idx in by_author[au]:
|
for idx in by_author[au]:
|
||||||
title = re.sub(r'\s', ' ', self.title(idx))
|
title = re.sub(r'\s', ' ', self.title(idx, index_is_id=index_is_id))
|
||||||
tpath = os.path.join(apath, sanitize_file_name(title))
|
tpath = os.path.join(apath, sanitize_file_name(title))
|
||||||
id = str(self.id(idx))
|
id = idx if index_is_id else self.id(idx)
|
||||||
|
id = str(id)
|
||||||
if not single_dir and not os.path.exists(tpath):
|
if not single_dir and not os.path.exists(tpath):
|
||||||
os.mkdir(tpath)
|
os.mkdir(tpath)
|
||||||
|
|
||||||
@ -1365,8 +1382,8 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
|
|||||||
name += '_'+id
|
name += '_'+id
|
||||||
base = dir if single_dir else tpath
|
base = dir if single_dir else tpath
|
||||||
|
|
||||||
mi = OPFCreator(self.get_metadata(idx))
|
mi = OPFCreator(self.get_metadata(idx, index_is_id=index_is_id))
|
||||||
cover = self.cover(idx)
|
cover = self.cover(idx, index_is_id=index_is_id)
|
||||||
if cover is not None:
|
if cover is not None:
|
||||||
cname = name + '.jpg'
|
cname = name + '.jpg'
|
||||||
cpath = os.path.join(base, cname)
|
cpath = os.path.join(base, cname)
|
||||||
@ -1376,8 +1393,8 @@ ALTER TABLE books ADD COLUMN isbn TEXT DEFAULT "" COLLATE NOCASE;
|
|||||||
mi.write(f)
|
mi.write(f)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
for fmt in self.formats(idx).split(','):
|
for fmt in self.formats(idx, index_is_id=index_is_id).split(','):
|
||||||
data = self.format(idx, fmt)
|
data = self.format(idx, fmt, index_is_id=index_is_id)
|
||||||
fname = name +'.'+fmt.lower()
|
fname = name +'.'+fmt.lower()
|
||||||
fname = sanitize_file_name(fname)
|
fname = sanitize_file_name(fname)
|
||||||
f = open(os.path.join(base, fname), 'w+b')
|
f = open(os.path.join(base, fname), 'w+b')
|
||||||
@ -1517,5 +1534,5 @@ class SearchToken(object):
|
|||||||
return bool(self.pattern.search(text)) ^ self.negate
|
return bool(self.pattern.search(text)) ^ self.negate
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
db = LibraryDatabase('/home/kovid/library1.db')
|
sqlite.enable_callback_tracebacks(True)
|
||||||
|
db = LibraryDatabase('/home/kovid/temp/library1.db.orig')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user