Fixes to T1 driver

This commit is contained in:
Kolenka 2012-06-03 17:31:34 -07:00
parent 80b3b30578
commit 37884fe173

View File

@ -275,34 +275,34 @@ class PRST1(USBMS):
debug_print('PRST1: finished update_device_database') debug_print('PRST1: finished update_device_database')
def get_database_min_id(self, source_id): def get_database_min_id(self, source_id):
sequence_min = 0 sequence_min = 0L
if source_id == '1': if source_id == '1':
sequence_min = 4294967296 sequence_min = 4294967296L
return sequence_min return sequence_min
def set_database_sequence_id(self, connection, table, sequence_id): def set_database_sequence_id(self, connection, table, sequence_id):
cursor = connection.cursor() cursor = connection.cursor()
# Update the sequence Id if it exists # Update the sequence Id if it exists
query = 'UPDATE sqlite_sequence SET seq = ? WHERE name = ?' query = 'UPDATE sqlite_sequence SET seq = ? WHERE name = ?'
t = (sequence_id, table,) t = (sequence_id, table,)
cursor.execute(query, t) cursor.execute(query, t)
# Insert the sequence Id if it doesn't # Insert the sequence Id if it doesn't
query = ('INSERT INTO sqlite_sequence (name, seq) ' query = ('INSERT INTO sqlite_sequence (name, seq) '
'SELECT ?, ? ' 'SELECT ?, ? '
'WHERE NOT EXISTS (SELECT 1 FROM sqlite_sequence WHERE name = ?)'); 'WHERE NOT EXISTS (SELECT 1 FROM sqlite_sequence WHERE name = ?)');
cursor.execute(query, (table, sequence_id, table,)) cursor.execute(query, (table, sequence_id, table,))
cursor.close() cursor.close()
def read_device_books(self, connection, source_id): def read_device_books(self, connection, source_id):
sequence_min = self.get_database_min_id(source_id) sequence_min = self.get_database_min_id(source_id)
sequence_max = sequence_min sequence_max = sequence_min
sequence_dirty = 0 sequence_dirty = 0
try: try:
cursor = connection.cursor() cursor = connection.cursor()
# Get existing books # Get existing books
@ -319,58 +319,58 @@ class PRST1(USBMS):
' any notes/highlights, etc.')%dbpath)+' Underlying error:' ' any notes/highlights, etc.')%dbpath)+' Underlying error:'
'\n'+tb) '\n'+tb)
# Get the books themselves, but keep track of any that are less than the minimum. # Get the books themselves, but keep track of any that are less than the minimum.
# Record what the max id being used is as well. # Record what the max id being used is as well.
db_books = {} db_books = {}
for i, row in enumerate(cursor): for i, row in enumerate(cursor):
lpath = row[0].replace('\\', '/') lpath = row[0].replace('\\', '/')
db_books[lpath] = row[1] db_books[lpath] = row[1]
if row[1] < sequence_min: if row[1] < sequence_min:
sequence_dirty = 1 sequence_dirty = 1
else: else:
sequence_max = max(sequence_max, row[1]) sequence_max = max(sequence_max, row[1])
# If the database is 'dirty', then we should fix up the Ids and the sequence number # If the database is 'dirty', then we should fix up the Ids and the sequence number
if sequence_dirty == 1: if sequence_dirty == 1:
sequence_max = sequence_max + 1 sequence_max = sequence_max + 1
for book, bookId in db_books.items(): for book, bookId in db_books.items():
if bookId < sequence_min: if bookId < sequence_min:
# Record the new Id and write it to the DB # Record the new Id and write it to the DB
db_books[book] = sequence_max db_books[book] = sequence_max
sequence_max = sequence_max + 1 sequence_max = sequence_max + 1
# Fix the Books DB # Fix the Books DB
query = 'UPDATE books SET _id = ? WHERE file_path = ?' query = 'UPDATE books SET _id = ? WHERE file_path = ?'
t = (db_books[book], book,) t = (db_books[book], book,)
cursor.execute(query, t) cursor.execute(query, t)
# Fix any references so that they point back to the right book # Fix any references so that they point back to the right book
t = (db_books[book], bookId,) t = (db_books[book], bookId,)
query = 'UPDATE collections SET content_id = ? WHERE content_id = ?' query = 'UPDATE collections SET content_id = ? WHERE content_id = ?'
cursor.execute(query, t) cursor.execute(query, t)
query = 'UPDATE annotation SET content_id = ? WHERE content_id = ?' query = 'UPDATE annotation SET content_id = ? WHERE content_id = ?'
cursor.execute(query, t) cursor.execute(query, t)
query = 'UPDATE bookmark SET content_id = ? WHERE content_id = ?' query = 'UPDATE bookmark SET content_id = ? WHERE content_id = ?'
cursor.execute(query, t) cursor.execute(query, t)
query = 'UPDATE current_position SET content_id = ? WHERE content_id = ?' query = 'UPDATE current_position SET content_id = ? WHERE content_id = ?'
cursor.execute(query, t) cursor.execute(query, t)
query = 'UPDATE deleted_markups SET content_id = ? WHERE content_id = ?' query = 'UPDATE deleted_markups SET content_id = ? WHERE content_id = ?'
cursor.execute(query, t) cursor.execute(query, t)
query = 'UPDATE dic_histories SET content_id = ? WHERE content_id = ?' query = 'UPDATE dic_histories SET content_id = ? WHERE content_id = ?'
cursor.execute(query, t) cursor.execute(query, t)
query = 'UPDATE freehand SET content_id = ? WHERE content_id = ?' query = 'UPDATE freehand SET content_id = ? WHERE content_id = ?'
cursor.execute(query, t) cursor.execute(query, t)
query = 'UPDATE history SET content_id = ? WHERE content_id = ?' query = 'UPDATE history SET content_id = ? WHERE content_id = ?'
cursor.execute(query, t) cursor.execute(query, t)
query = 'UPDATE layout_cache SET content_id = ? WHERE content_id = ?' query = 'UPDATE layout_cache SET content_id = ? WHERE content_id = ?'
cursor.execute(query, t) cursor.execute(query, t)
query = 'UPDATE preference SET content_id = ? WHERE content_id = ?' query = 'UPDATE preference SET content_id = ? WHERE content_id = ?'
cursor.execute(query, t) cursor.execute(query, t)
self.set_database_sequence_id(connection, 'books', sequence_max) self.set_database_sequence_id(connection, 'books', sequence_max)
cursor.close() cursor.close()
return db_books return db_books
def update_device_books(self, connection, booklist, source_id, plugboard, def update_device_books(self, connection, booklist, source_id, plugboard,
dbpath): dbpath):
@ -381,9 +381,9 @@ class PRST1(USBMS):
refresh_covers = opts.extra_customization[self.OPT_REFRESH_COVERS] refresh_covers = opts.extra_customization[self.OPT_REFRESH_COVERS]
use_sony_authors = opts.extra_customization[self.OPT_USE_SONY_AUTHORS] use_sony_authors = opts.extra_customization[self.OPT_USE_SONY_AUTHORS]
db_books = self.read_device_books(connection, source_id) db_books = self.read_device_books(connection, source_id)
cursor = connection.cursor() cursor = connection.cursor()
for book in booklist: for book in booklist:
# Run through plugboard if needed # Run through plugboard if needed
if plugboard is not None: if plugboard is not None:
@ -462,12 +462,12 @@ class PRST1(USBMS):
connection.commit() connection.commit()
cursor.close() cursor.close()
def read_device_collections(self, connection, source_id): def read_device_collections(self, connection, source_id):
sequence_min = self.get_database_min_id(source_id) sequence_min = self.get_database_min_id(source_id)
sequence_max = sequence_min sequence_max = sequence_min
sequence_dirty = 0 sequence_dirty = 0
try: try:
cursor = connection.cursor() cursor = connection.cursor()
# Get existing collections # Get existing collections
@ -487,68 +487,68 @@ class PRST1(USBMS):
db_collections = {} db_collections = {}
for i, row in enumerate(cursor): for i, row in enumerate(cursor):
db_collections[row[1]] = row[0] db_collections[row[1]] = row[0]
if row[0] < sequence_min: if row[0] < sequence_min:
sequence_dirty = 1 sequence_dirty = 1
else: else:
sequence_max = max(sequence_max, row[0]) sequence_max = max(sequence_max, row[0])
# If the database is 'dirty', then we should fix up the Ids and the sequence number # If the database is 'dirty', then we should fix up the Ids and the sequence number
if sequence_dirty == 1: if sequence_dirty == 1:
sequence_max = sequence_max + 1 sequence_max = sequence_max + 1
for collection, collectionId in db_collections.items(): for collection, collectionId in db_collections.items():
if collectionId < sequence_min: if collectionId < sequence_min:
# Record the new Id and write it to the DB # Record the new Id and write it to the DB
db_collections[collection] = sequence_max db_collections[collection] = sequence_max
sequence_max = sequence_max + 1 sequence_max = sequence_max + 1
# Fix the collection DB # Fix the collection DB
query = 'UPDATE collection SET _id = ? WHERE title = ?' query = 'UPDATE collection SET _id = ? WHERE title = ?'
t = (db_collections[collection], collection, ) t = (db_collections[collection], collection, )
cursor.execute(query, t) cursor.execute(query, t)
# Fix any references in existing collections # Fix any references in existing collections
query = 'UPDATE collections SET collection_id = ? WHERE collection_id = ?' query = 'UPDATE collections SET collection_id = ? WHERE collection_id = ?'
t = (db_collections[collection], collectionId,) t = (db_collections[collection], collectionId,)
cursor.execute(query, t) cursor.execute(query, t)
self.set_database_sequence_id(connection, 'collection', sequence_max) self.set_database_sequence_id(connection, 'collection', sequence_max)
# Fix up the collections table now... # Fix up the collections table now...
sequence_dirty = 0 sequence_dirty = 0
sequence_max = sequence_min sequence_max = sequence_min
query = 'SELECT _id FROM collections' query = 'SELECT _id FROM collections'
cursor.execute(query) cursor.execute(query)
db_collection_pairs = [] db_collection_pairs = []
for i, row in enumerate(cursor): for i, row in enumerate(cursor):
db_collection_pairs.append(row[0]) db_collection_pairs.append(row[0])
if row[0] < sequence_min: if row[0] < sequence_min:
sequence_dirty = 1 sequence_dirty = 1
else: else:
sequence_max = max(sequence_max, row[0]) sequence_max = max(sequence_max, row[0])
if sequence_dirty == 1: if sequence_dirty == 1:
sequence_max = sequence_max + 1 sequence_max = sequence_max + 1
for pairId in db_collection_pairs: for pairId in db_collection_pairs:
if pairId < sequence_min: if pairId < sequence_min:
# Record the new Id and write it to the DB # Record the new Id and write it to the DB
query = 'UPDATE collections SET _id = ? WHERE _id = ?' query = 'UPDATE collections SET _id = ? WHERE _id = ?'
t = (sequence_max, pairId,) t = (sequence_max, pairId,)
cursor.execute(query, t) cursor.execute(query, t)
sequence_max = sequence_max + 1 sequence_max = sequence_max + 1
self.set_database_sequence_id(connection, 'collection', sequence_max) self.set_database_sequence_id(connection, 'collections', sequence_max)
cursor.close() cursor.close()
return db_collections return db_collections
def update_device_collections(self, connection, booklist, collections, def update_device_collections(self, connection, booklist, collections,
source_id): source_id):
cursor = connection.cursor() cursor = connection.cursor()
if collections: if collections:
db_collections = self.read_device_collections(connection, source_id) db_collections = self.read_device_collections(connection, source_id)
for collection, books in collections.items(): for collection, books in collections.items():
if collection not in db_collections: if collection not in db_collections: