From e588c37b3bf7c4c28021f08e5676783426e94973 Mon Sep 17 00:00:00 2001 From: Nikolay Pultsin Date: Mon, 14 Jan 2013 07:05:09 +0400 Subject: [PATCH] database parameter in FileInfoSet constructors --- .../libraryService/SQLiteBooksDatabase.java | 6 ++--- .../geometerplus/fbreader/library/Book.java | 6 ++--- .../fbreader/library/BooksDatabase.java | 2 +- .../fbreader/library/FileInfoSet.java | 24 +++++++++++-------- .../fbreader/library/Library.java | 2 +- 5 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/org/geometerplus/android/fbreader/libraryService/SQLiteBooksDatabase.java b/src/org/geometerplus/android/fbreader/libraryService/SQLiteBooksDatabase.java index 832aa17b6..939c65298 100644 --- a/src/org/geometerplus/android/fbreader/libraryService/SQLiteBooksDatabase.java +++ b/src/org/geometerplus/android/fbreader/libraryService/SQLiteBooksDatabase.java @@ -319,7 +319,7 @@ public final class SQLiteBooksDatabase extends BooksDatabase { SQLiteUtil.bindString(myInsertBookInfoStatement, 1, encoding); SQLiteUtil.bindString(myInsertBookInfoStatement, 2, language); myInsertBookInfoStatement.bindString(3, title); - final FileInfoSet infoSet = new FileInfoSet(file); + final FileInfoSet infoSet = new FileInfoSet(this, file); myInsertBookInfoStatement.bindLong(4, infoSet.getId(file)); return myInsertBookInfoStatement.executeInsert(); } @@ -981,7 +981,7 @@ public final class SQLiteBooksDatabase extends BooksDatabase { } private void updateTables4() { - final FileInfoSet fileInfos = new FileInfoSet(); + final FileInfoSet fileInfos = new FileInfoSet(this); final Cursor cursor = myDatabase.rawQuery( "SELECT file_name FROM Books", null ); @@ -1070,7 +1070,7 @@ public final class SQLiteBooksDatabase extends BooksDatabase { ); myDatabase.execSQL("DELETE FROM Files"); - final FileInfoSet infoSet = new FileInfoSet(); + final FileInfoSet infoSet = new FileInfoSet(this); Cursor cursor = myDatabase.rawQuery( "SELECT file_name FROM Books", null ); diff --git a/src/org/geometerplus/fbreader/library/Book.java b/src/org/geometerplus/fbreader/library/Book.java index 84e171fbf..2d44f6c4d 100644 --- a/src/org/geometerplus/fbreader/library/Book.java +++ b/src/org/geometerplus/fbreader/library/Book.java @@ -55,7 +55,7 @@ public class Book { return null; } - FileInfoSet fileInfos = new FileInfoSet(physicalFile); + FileInfoSet fileInfos = new FileInfoSet(BooksDatabase.Instance(), physicalFile); if (fileInfos.check(physicalFile, physicalFile != bookFile)) { return book; } @@ -79,7 +79,7 @@ public class Book { return null; } - final FileInfoSet fileInfos = new FileInfoSet(bookFile); + final FileInfoSet fileInfos = new FileInfoSet(BooksDatabase.Instance(), bookFile); Book book = BooksDatabase.Instance().loadBookByFile(fileInfos.getId(bookFile), bookFile); if (book != null) { @@ -411,7 +411,7 @@ public class Book { database.executeAsATransaction(new Runnable() { public void run() { if (myId >= 0) { - final FileInfoSet fileInfos = new FileInfoSet(File); + final FileInfoSet fileInfos = new FileInfoSet(database, File); database.updateBookInfo(myId, fileInfos.getId(File), myEncoding, myLanguage, myTitle); } else { myId = database.insertBookInfo(File, myEncoding, myLanguage, myTitle); diff --git a/src/org/geometerplus/fbreader/library/BooksDatabase.java b/src/org/geometerplus/fbreader/library/BooksDatabase.java index 4c20cfbc0..f700cbfed 100644 --- a/src/org/geometerplus/fbreader/library/BooksDatabase.java +++ b/src/org/geometerplus/fbreader/library/BooksDatabase.java @@ -39,7 +39,7 @@ public abstract class BooksDatabase { } protected Book createBook(long id, long fileId, String title, String encoding, String language) { - final FileInfoSet infos = new FileInfoSet(fileId); + final FileInfoSet infos = new FileInfoSet(this, fileId); return createBook(id, infos.getFile(fileId), title, encoding, language); } protected Book createBook(long id, ZLFile file, String title, String encoding, String language) { diff --git a/src/org/geometerplus/fbreader/library/FileInfoSet.java b/src/org/geometerplus/fbreader/library/FileInfoSet.java index e32927cf7..2483a69c8 100644 --- a/src/org/geometerplus/fbreader/library/FileInfoSet.java +++ b/src/org/geometerplus/fbreader/library/FileInfoSet.java @@ -60,16 +60,21 @@ public final class FileInfoSet { private final LinkedHashSet myInfosToSave = new LinkedHashSet(); private final LinkedHashSet myInfosToRemove = new LinkedHashSet(); - public FileInfoSet() { - load(BooksDatabase.Instance().loadFileInfos()); + private final BooksDatabase myDatabase; + + public FileInfoSet(BooksDatabase database) { + myDatabase = database; + load(database.loadFileInfos()); } - public FileInfoSet(ZLFile file) { - load(BooksDatabase.Instance().loadFileInfos(file)); + public FileInfoSet(BooksDatabase database, ZLFile file) { + myDatabase = database; + load(database.loadFileInfos(file)); } - FileInfoSet(long fileId) { - load(BooksDatabase.Instance().loadFileInfos(fileId)); + FileInfoSet(BooksDatabase database, long fileId) { + myDatabase = database; + load(database.loadFileInfos(fileId)); } private void load(Collection infos) { @@ -80,16 +85,15 @@ public final class FileInfoSet { } public void save() { - final BooksDatabase database = BooksDatabase.Instance(); - database.executeAsATransaction(new Runnable() { + myDatabase.executeAsATransaction(new Runnable() { public void run() { for (FileInfo info : myInfosToRemove) { - database.removeFileInfo(info.Id); + myDatabase.removeFileInfo(info.Id); myInfosByPair.remove(new Pair(info.Name, info.Parent)); } myInfosToRemove.clear(); for (FileInfo info : myInfosToSave) { - database.saveFileInfo(info); + myDatabase.saveFileInfo(info); } myInfosToSave.clear(); } diff --git a/src/org/geometerplus/fbreader/library/Library.java b/src/org/geometerplus/fbreader/library/Library.java index eb2ef397b..df9f84511 100644 --- a/src/org/geometerplus/fbreader/library/Library.java +++ b/src/org/geometerplus/fbreader/library/Library.java @@ -334,7 +334,7 @@ public final class Library { private void build() { // Step 0: get database books marked as "existing" - final FileInfoSet fileInfos = new FileInfoSet(); + final FileInfoSet fileInfos = new FileInfoSet(myDatabase); final Map savedBooksByFileId = myDatabase.loadBooks(fileInfos, true); final Map savedBooksByBookId = new HashMap(); for (Book b : savedBooksByFileId.values()) {