From d3e2c90a231c342f522d9a99ce998a1b98a011fc Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Wed, 18 Aug 2010 08:08:28 +0100 Subject: [PATCH] Add bulk setting of the rest of the custom column types --- src/calibre/gui2/custom_column_widgets.py | 20 ++++++++-------- src/calibre/library/custom_columns.py | 29 +++++++++++++++++++---- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/calibre/gui2/custom_column_widgets.py b/src/calibre/gui2/custom_column_widgets.py index d624d5320d..8108454db3 100644 --- a/src/calibre/gui2/custom_column_widgets.py +++ b/src/calibre/gui2/custom_column_widgets.py @@ -403,9 +403,7 @@ class BulkBase(Base): val = self.getter() val = self.normalize_ui_val(val) if val != self.initial_val: - for book_id in book_ids: - QCoreApplication.processEvents() - self.db.set_custom(book_id, val, num=self.col_id, notify=notify) + self.db.set_custom_bulk(book_ids, val, num=self.col_id, notify=notify) class BulkBool(BulkBase, Bool): pass @@ -448,18 +446,21 @@ class BulkSeries(BulkBase): val = self.normalize_ui_val(val) update_indices = self.idx_widget.checkState() if val != '': + extras = [] + next_index = self.db.get_next_cc_series_num_for(val, num=self.col_id) for book_id in book_ids: QCoreApplication.processEvents() if update_indices: if tweaks['series_index_auto_increment'] == 'next': - s_index = self.db.get_next_cc_series_num_for\ - (val, num=self.col_id) + s_index = next_index + next_index += 1 else: s_index = 1.0 else: s_index = self.db.get_custom_extra(book_id, num=self.col_id, index_is_id=True) - self.db.set_custom(book_id, val, extra=s_index, + extras.append(s_index) + self.db.set_custom_bulk(book_ids, val, extras=extras, num=self.col_id, notify=notify) class RemoveTags(QWidget): @@ -540,14 +541,13 @@ class BulkText(BulkBase): add = set([v.strip() for v in txt.split(',')]) else: add = set() - self.db.set_custom_bulk(book_ids, add=add, remove=remove, num=self.col_id) + self.db.set_custom_bulk_multiple(book_ids, add=add, remove=remove, + num=self.col_id) else: val = self.getter() val = self.normalize_ui_val(val) if val != self.initial_val: - for book_id in book_ids: - QCoreApplication.processEvents() - self.db.set_custom(book_id, val, num=self.col_id, notify=notify) + self.db.set_custom_bulk(book_ids, val, num=self.col_id, notify=notify) def getter(self, original_value = None): if self.col_metadata['is_multiple']: diff --git a/src/calibre/library/custom_columns.py b/src/calibre/library/custom_columns.py index 7c613295b9..f02294a102 100644 --- a/src/calibre/library/custom_columns.py +++ b/src/calibre/library/custom_columns.py @@ -313,7 +313,7 @@ class CustomColumns(object): self.conn.commit() return changed - def set_custom_bulk(self, ids, add=[], remove=[], + def set_custom_bulk_multiple(self, ids, add=[], remove=[], label=None, num=None, notify=False): ''' Fast algorithm for updating custom column is_multiple datatypes. @@ -394,7 +394,30 @@ class CustomColumns(object): if notify: self.notify('metadata', ids) - def set_custom(self, id_, val, label=None, num=None, + def set_custom_bulk(self, ids, val, label=None, num=None, + append=False, notify=True, extras=None): + ''' + Change the value of a column for a set of books. The ids parameter is a + list of book ids to change. The extra field must be None or a list the + same length as ids. + ''' + if extras is not None and len(extras) != len(ids): + raise ValueError('Lentgh of ids and extras is not the same') + ev = None + for idx,id in enumerate(ids): + if extras is not None: + ev = extras[idx] + self._set_custom(id, val, label=label, num=num, append=append, + notify=notify, extra=ev) + self.conn.commit() + + def set_custom(self, id, val, label=None, num=None, + append=False, notify=True, extra=None): + self._set_custom(id, val, label=label, num=num, append=append, + notify=notify, extra=extra) + self.conn.commit() + + def _set_custom(self, id_, val, label=None, num=None, append=False, notify=True, extra=None): if label is not None: data = self.custom_column_label_map[label] @@ -450,7 +473,6 @@ class CustomColumns(object): self.conn.execute( '''INSERT INTO %s(book, value) VALUES (?,?)'''%lt, (id_, xid)) - self.conn.commit() nval = self.conn.get( 'SELECT custom_%s FROM meta2 WHERE id=?'%data['num'], (id_,), all=False) @@ -462,7 +484,6 @@ class CustomColumns(object): self.conn.execute( 'INSERT INTO %s(book,value) VALUES (?,?)'%table, (id_, val)) - self.conn.commit() nval = self.conn.get( 'SELECT custom_%s FROM meta2 WHERE id=?'%data['num'], (id_,), all=False)