From 5dd75d0ea67b7c76a08d183400aa0fe03944b82b Mon Sep 17 00:00:00 2001 From: Kolenka Date: Mon, 4 Jun 2012 09:17:18 -0700 Subject: [PATCH 1/3] Fixes for handling of the cursor during collections writing. --- src/calibre/devices/prst1/driver.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/calibre/devices/prst1/driver.py b/src/calibre/devices/prst1/driver.py index f13c85b4c6..a2b6e3f63c 100644 --- a/src/calibre/devices/prst1/driver.py +++ b/src/calibre/devices/prst1/driver.py @@ -559,10 +559,10 @@ class PRST1(USBMS): def update_device_collections(self, connection, booklist, collections, source_id, dbpath): - cursor = connection.cursor() if collections: db_collections = self.read_device_collections(connection, source_id, dbpath) + cursor = connection.cursor() for collection, books in collections.items(): if collection not in db_collections: @@ -634,9 +634,8 @@ class PRST1(USBMS): cursor.execute(query, t) debug_print('Deleted Collection: ' + collection) - - connection.commit() - cursor.close() + connection.commit() + cursor.close() def rebuild_collections(self, booklist, oncard): debug_print('PRST1: starting rebuild_collections') From 52402e940149c527a52cca00abeadc609008c95d Mon Sep 17 00:00:00 2001 From: Kolenka Date: Mon, 4 Jun 2012 10:17:21 -0700 Subject: [PATCH 2/3] Purge Orphaned T1 Database Entries --- src/calibre/devices/prst1/driver.py | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/calibre/devices/prst1/driver.py b/src/calibre/devices/prst1/driver.py index a2b6e3f63c..dba611d82c 100644 --- a/src/calibre/devices/prst1/driver.py +++ b/src/calibre/devices/prst1/driver.py @@ -268,12 +268,52 @@ class PRST1(USBMS): collections = booklist.get_collections(collections_attributes) with closing(sqlite.connect(dbpath)) as connection: + self.remove_orphaned_records(connection, dbpath) self.update_device_books(connection, booklist, source_id, plugboard, dbpath) self.update_device_collections(connection, booklist, collections, source_id, dbpath) debug_print('PRST1: finished update_device_database') + def remove_orphaned_records(self, connection, dbpath): + from sqlite3 import DatabaseError + + try: + cursor = connection.cursor() + + debug_print("Removing Orphaned Collection Records") + + # Purge any collections references that point into the abyss + query = 'DELETE FROM collections WHERE content_id NOT IN (SELECT _id FROM books)' + cursor.execute(query) + query = 'DELETE FROM collections WHERE collection_id NOT IN (SELECT _id FROM collection)' + cursor.execute(query) + + debug_print("Removing Orphaned Book Records") + + # Purge any references to books not in this database + # Idea is to prevent any spill-over where these wind up applying to some other book + query = 'DELETE FROM %s WHERE content_id NOT IN (SELECT _id FROM books)' + cursor.execute(query%'annotation') + cursor.execute(query%'bookmark') + cursor.execute(query%'current_position') + cursor.execute(query%'freehand') + cursor.execute(query%'history') + cursor.execute(query%'layout_cache') + cursor.execute(query%'preference') + + cursor.close() + except DatabaseError: + import traceback + tb = traceback.format_exc() + raise DeviceError((('The SONY database is corrupted. ' + ' Delete the file %s on your reader and then disconnect ' + ' reconnect it. If you are using an SD card, you ' + ' should delete the file on the card as well. Note that ' + ' deleting this file will cause your reader to forget ' + ' any notes/highlights, etc.')%dbpath)+' Underlying error:' + '\n'+tb) + def get_database_min_id(self, source_id): sequence_min = 0L if source_id == 1: From 2fa9fb85d1dcacaefa7e09593bf42cfe4cbd2075 Mon Sep 17 00:00:00 2001 From: Kolenka Date: Mon, 4 Jun 2012 10:21:31 -0700 Subject: [PATCH 3/3] Tabs --- src/calibre/devices/prst1/driver.py | 56 ++++++++++++++--------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/src/calibre/devices/prst1/driver.py b/src/calibre/devices/prst1/driver.py index dba611d82c..6e231a0d1c 100644 --- a/src/calibre/devices/prst1/driver.py +++ b/src/calibre/devices/prst1/driver.py @@ -268,42 +268,42 @@ class PRST1(USBMS): collections = booklist.get_collections(collections_attributes) with closing(sqlite.connect(dbpath)) as connection: - self.remove_orphaned_records(connection, dbpath) + self.remove_orphaned_records(connection, dbpath) self.update_device_books(connection, booklist, source_id, plugboard, dbpath) self.update_device_collections(connection, booklist, collections, source_id, dbpath) debug_print('PRST1: finished update_device_database') - def remove_orphaned_records(self, connection, dbpath): - from sqlite3 import DatabaseError + def remove_orphaned_records(self, connection, dbpath): + from sqlite3 import DatabaseError try: - cursor = connection.cursor() - - debug_print("Removing Orphaned Collection Records") - - # Purge any collections references that point into the abyss - query = 'DELETE FROM collections WHERE content_id NOT IN (SELECT _id FROM books)' - cursor.execute(query) - query = 'DELETE FROM collections WHERE collection_id NOT IN (SELECT _id FROM collection)' - cursor.execute(query) - - debug_print("Removing Orphaned Book Records") - - # Purge any references to books not in this database - # Idea is to prevent any spill-over where these wind up applying to some other book - query = 'DELETE FROM %s WHERE content_id NOT IN (SELECT _id FROM books)' - cursor.execute(query%'annotation') - cursor.execute(query%'bookmark') - cursor.execute(query%'current_position') - cursor.execute(query%'freehand') - cursor.execute(query%'history') - cursor.execute(query%'layout_cache') - cursor.execute(query%'preference') - - cursor.close() - except DatabaseError: + cursor = connection.cursor() + + debug_print("Removing Orphaned Collection Records") + + # Purge any collections references that point into the abyss + query = 'DELETE FROM collections WHERE content_id NOT IN (SELECT _id FROM books)' + cursor.execute(query) + query = 'DELETE FROM collections WHERE collection_id NOT IN (SELECT _id FROM collection)' + cursor.execute(query) + + debug_print("Removing Orphaned Book Records") + + # Purge any references to books not in this database + # Idea is to prevent any spill-over where these wind up applying to some other book + query = 'DELETE FROM %s WHERE content_id NOT IN (SELECT _id FROM books)' + cursor.execute(query%'annotation') + cursor.execute(query%'bookmark') + cursor.execute(query%'current_position') + cursor.execute(query%'freehand') + cursor.execute(query%'history') + cursor.execute(query%'layout_cache') + cursor.execute(query%'preference') + + cursor.close() + except DatabaseError: import traceback tb = traceback.format_exc() raise DeviceError((('The SONY database is corrupted. '