From 7b98ace36ec064ae39654b65fbc355aa97fd180c Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 1 Jul 2012 11:47:45 +0200 Subject: [PATCH 1/3] Ensure that renaming an item removes the old item from its DB table. Tags, series, and publishers didn't do this before. Authors did, as did custom columns. --- src/calibre/library/database2.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index f08b7abc3b..305e12bfa2 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -1455,6 +1455,24 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): if notify: self.notify('metadata', [id]) + def clean_standard_field(self, field, commit=False): + # Don't bother with validity checking. Let the exception fly out so + # we can see what happened + def doit(table, ltable_col): + st = ('DELETE FROM books_%s_link WHERE (SELECT COUNT(id) ' + 'FROM books WHERE id=book) < 1;')%table + self.conn.execute(st) + st = ('DELETE FROM %(table)s WHERE (SELECT COUNT(id) ' + 'FROM books_%(table)s_link WHERE ' + '%(ltable_col)s=%(table)s.id) < 1;') % dict( + table=table, ltable_col=ltable_col) + self.conn.execute(st) + + fm = self.field_metadata[field] + doit(fm['table'], fm['link_column']) + if commit: + self.conn.commit() + def clean(self): ''' Remove orphaned entries. @@ -2557,6 +2575,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): self.set_tags(book_id, new_names, append=True, notify=False, commit=False) self.dirtied(books, commit=False) + self.clean_standard_field('tags', commit=False) self.conn.commit() def delete_tag_using_id(self, id): @@ -2603,6 +2622,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): SET series_index=? WHERE id=?''',(index, book_id,)) self.dirty_books_referencing('series', new_id, commit=False) + self.clean_standard_field('series', commit=False) self.conn.commit() def delete_series_using_id(self, id): @@ -2638,6 +2658,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): # Get rid of the no-longer used publisher self.conn.execute('DELETE FROM publishers WHERE id=?', (old_id,)) self.dirty_books_referencing('publisher', new_id, commit=False) + self.clean_standard_field('publisher', commit=False) self.conn.commit() def delete_publisher_using_id(self, old_id): @@ -2736,7 +2757,6 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): # metadata. Ignore it. pass # Now delete the old author from the DB - bks = self.conn.get('SELECT book FROM books_authors_link WHERE author=?', (old_id,)) self.conn.execute('DELETE FROM authors WHERE id=?', (old_id,)) self.dirtied(books, commit=False) self.conn.commit() @@ -2752,6 +2772,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): self.set_author_sort(book_id, ss) # the caller will do a general refresh, so we don't need to # do one here + # Now delete the old author from the DB return new_id # end convenience methods From 51d866063a59d3cf4c93ef41bd8806b996734423 Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 1 Jul 2012 11:49:30 +0200 Subject: [PATCH 2/3] Add new option to the series_index_auto_increment tweak, no_change, that causes calibre not to change the series_index when the series is changed. --- resources/default_tweaks.py | 1 + src/calibre/gui2/dialogs/metadata_bulk.py | 8 ++++++-- src/calibre/gui2/library/models.py | 7 +++++-- src/calibre/gui2/metadata/basic_widgets.py | 2 +- src/calibre/library/cli.py | 4 +++- src/calibre/library/database2.py | 2 +- 6 files changed, 17 insertions(+), 7 deletions(-) diff --git a/resources/default_tweaks.py b/resources/default_tweaks.py index 9f848dc5ce..10ce9b5c2c 100644 --- a/resources/default_tweaks.py +++ b/resources/default_tweaks.py @@ -21,6 +21,7 @@ defaults. # last_free - First available integer smaller than the largest existing number # Return largest existing + 1 if no free number is found # const - Assign the number 1 always +# no_change - Do not change the series index # a number - Assign that number always. The number is not in quotes. Note that # 0.0 can be used here. # Examples: diff --git a/src/calibre/gui2/dialogs/metadata_bulk.py b/src/calibre/gui2/dialogs/metadata_bulk.py index b7af971a63..09a244debd 100644 --- a/src/calibre/gui2/dialogs/metadata_bulk.py +++ b/src/calibre/gui2/dialogs/metadata_bulk.py @@ -261,8 +261,12 @@ class MyBlockingBusy(QDialog): # {{{ else: next = self.db.get_next_series_num_for(series) self.db.set_series(id, series, notify=False, commit=False) - num = next if do_autonumber and series else 1.0 - self.db.set_series_index(id, num, notify=False, commit=False) + if not series: + self.db.set_series_index(id, 1.0, notify=False, commit=False) + elif do_autonumber: # is True if do_series_restart is True + self.db.set_series_index(id, next, notify=False, commit=False) + elif tweaks['series_index_auto_increment'] != 'no_change': + self.db.set_series_index(id, 1.0, notify=False, commit=False) if do_remove_conv: self.db.delete_conversion_options(id, 'PIPE', commit=False) diff --git a/src/calibre/gui2/library/models.py b/src/calibre/gui2/library/models.py index e0047c2a70..e2706c0a54 100644 --- a/src/calibre/gui2/library/models.py +++ b/src/calibre/gui2/library/models.py @@ -846,7 +846,9 @@ class BooksModel(QAbstractTableModel): # {{{ s_index = float(match.group(1)) val = pat.sub('', val).strip() elif val: - if tweaks['series_index_auto_increment'] != 'const': + # it is OK to leave s_index == None when using 'no_change' + if tweaks['series_index_auto_increment'] != 'const' and \ + tweaks['series_index_auto_increment'] != 'no_change': s_index = self.db.get_next_cc_series_num_for(val, label=label, num=None) elif typ == 'composite': @@ -915,7 +917,8 @@ class BooksModel(QAbstractTableModel): # {{{ self.db.set_series_index(id, float(match.group(1))) val = pat.sub('', val).strip() elif val: - if tweaks['series_index_auto_increment'] != 'const': + if tweaks['series_index_auto_increment'] != 'const' and \ + tweaks['series_index_auto_increment'] != 'no_change': ni = self.db.get_next_series_num_for(val) if ni != 1: self.db.set_series_index(id, ni) diff --git a/src/calibre/gui2/metadata/basic_widgets.py b/src/calibre/gui2/metadata/basic_widgets.py index f152bf6534..250d4ffad2 100644 --- a/src/calibre/gui2/metadata/basic_widgets.py +++ b/src/calibre/gui2/metadata/basic_widgets.py @@ -560,7 +560,7 @@ class SeriesIndexEdit(QDoubleSpinBox): return True def increment(self): - if self.db is not None: + if tweaks['series_index_auto_increment'] != 'no_change' and self.db is not None: try: series = self.series_edit.current_val if series and series != self.original_series_name: diff --git a/src/calibre/library/cli.py b/src/calibre/library/cli.py index 3a798a961b..6c24a1e455 100644 --- a/src/calibre/library/cli.py +++ b/src/calibre/library/cli.py @@ -829,7 +829,9 @@ def parse_series_string(db, label, value): val = pat.sub('', val).strip() s_index = float(match.group(1)) elif val: - if tweaks['series_index_auto_increment'] != 'const': + if tweaks['series_index_auto_increment'] == 'no_change': + pass + elif tweaks['series_index_auto_increment'] != 'const': s_index = db.get_next_cc_series_num_for(val, label=label) else: s_index = 1.0 diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index 305e12bfa2..a8ad3b5d6b 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -2613,7 +2613,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): self.conn.execute('''UPDATE books_series_link SET series=? WHERE series=?''',(new_id, old_id,)) - if change_index: + if change_index and tweaks['series_index_auto_increment'] != 'no_change': # Now set the indices for (book_id,) in books: # Get the next series index From 8eaecf28ee515f8667a0f3c67739db2ce496a7cd Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Sun, 1 Jul 2012 11:51:02 +0200 Subject: [PATCH 3/3] Remove superflous comment in rename_author --- src/calibre/library/database2.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/calibre/library/database2.py b/src/calibre/library/database2.py index a8ad3b5d6b..32cb6737a8 100644 --- a/src/calibre/library/database2.py +++ b/src/calibre/library/database2.py @@ -2772,7 +2772,6 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns): self.set_author_sort(book_id, ss) # the caller will do a general refresh, so we don't need to # do one here - # Now delete the old author from the DB return new_id # end convenience methods