From 2a1f4c2846b0030b95ce267196779cca64edde80 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Thu, 18 Oct 2012 09:54:14 +0400 Subject: bump database to v2 --- src/org/fox/ttcomics/ComicListFragment.java | 5 ++ src/org/fox/ttcomics/CommonActivity.java | 77 ++++++++++++++++++++--------- src/org/fox/ttcomics/DatabaseHelper.java | 38 ++++++++++++-- 3 files changed, 94 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/org/fox/ttcomics/ComicListFragment.java b/src/org/fox/ttcomics/ComicListFragment.java index 80e242f..324c7a0 100644 --- a/src/org/fox/ttcomics/ComicListFragment.java +++ b/src/org/fox/ttcomics/ComicListFragment.java @@ -9,6 +9,7 @@ import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; +import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; @@ -215,6 +216,10 @@ public class ComicListFragment extends Fragment implements OnItemClickListener { } } + public Cursor createCursor() { + return m_activity.getReadableDb().query("comics_cache", null, "path = ?", + new String[] { m_baseDirectory }, null, null, "filename"); + } @Override public void onAttach(Activity activity) { diff --git a/src/org/fox/ttcomics/CommonActivity.java b/src/org/fox/ttcomics/CommonActivity.java index 8934fec..52a10c2 100644 --- a/src/org/fox/ttcomics/CommonActivity.java +++ b/src/org/fox/ttcomics/CommonActivity.java @@ -108,9 +108,11 @@ public class CommonActivity extends FragmentActivity { } public Cursor findComicByFileName(String fileName) { + File file = new File(fileName); + Cursor c = getReadableDb().query("comics_cache", null, - "filename = ?", - new String[] { fileName }, null, null, null); + "filename = ? AND path = ?", + new String[] { file.getName(), file.getParentFile().getAbsolutePath() }, null, null, null); if (c.moveToFirst()) { return c; @@ -118,13 +120,14 @@ public class CommonActivity extends FragmentActivity { c.close(); SQLiteStatement stmt = getWritableDb().compileStatement("INSERT INTO comics_cache " + - "(filename, position, max_position, size) VALUES (?, 0, 0, -1)"); - stmt.bindString(1, fileName); + "(filename, path, position, max_position, size, checksum) VALUES (?, ?, 0, 0, -1, '')"); + stmt.bindString(1, file.getName()); + stmt.bindString(2, file.getParentFile().getAbsolutePath()); stmt.execute(); c = getReadableDb().query("comics_cache", null, - "filename = ?", - new String[] { fileName }, null, null, null); + "filename = ? AND path = ?", + new String[] { file.getName(), file.getParentFile().getAbsolutePath() }, null, null, null); if (c.moveToFirst()) { return c; @@ -137,21 +140,39 @@ public class CommonActivity extends FragmentActivity { } public void setSize(String fileName, int size) { - //Log.d(TAG, "setSize:" + fileName + "=" + size); - Cursor c = findComicByFileName(fileName); if (c != null) { c.close(); - - SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE comics_cache SET size = ? WHERE filename = ?"); + + File file = new File(fileName); + + SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE comics_cache SET size = ? WHERE filename = ? AND path = ?"); stmt.bindLong(1, size); - stmt.bindString(2, fileName); + stmt.bindString(2, file.getName()); + stmt.bindString(3, file.getParentFile().getAbsolutePath()); stmt.execute(); stmt.close(); } } - + + public void setChecksum(String fileName, String checksum) { + Cursor c = findComicByFileName(fileName); + + if (c != null) { + c.close(); + + File file = new File(fileName); + + SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE comics_cache SET checksum = ? WHERE filename = ? AND path = ?"); + stmt.bindString(1, checksum); + stmt.bindString(2, file.getName()); + stmt.bindString(3, file.getParentFile().getAbsolutePath()); + stmt.execute(); + stmt.close(); + } + } + public void setLastPosition(String fileName, int position) { int lastPosition = getLastPosition(fileName); @@ -160,10 +181,13 @@ public class CommonActivity extends FragmentActivity { if (c != null) { c.close(); - SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE comics_cache SET position = ?, max_position = ? WHERE filename = ?"); + File file = new File(fileName); + + SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE comics_cache SET position = ?, max_position = ? WHERE filename = ? AND path = ?"); stmt.bindLong(1, position); stmt.bindLong(2, Math.max(position, lastPosition)); - stmt.bindString(3, fileName); + stmt.bindString(3, file.getName()); + stmt.bindString(4, file.getParentFile().getAbsolutePath()); stmt.execute(); stmt.close(); } @@ -177,9 +201,12 @@ public class CommonActivity extends FragmentActivity { if (c != null) { c.close(); - SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE comics_cache SET max_position = ? WHERE filename = ?"); + File file = new File(fileName); + + SQLiteStatement stmt = getWritableDb().compileStatement("UPDATE comics_cache SET max_position = ? WHERE filename = ? AND path = ?"); stmt.bindLong(1, position); - stmt.bindString(2, fileName); + stmt.bindString(2, file.getName()); + stmt.bindString(3, file.getParentFile().getAbsolutePath()); stmt.execute(); stmt.close(); } @@ -188,9 +215,11 @@ public class CommonActivity extends FragmentActivity { public int getLastPosition(String fileName) { int position = 0; + File file = new File(fileName); + Cursor c = getReadableDb().query("comics_cache", new String[] { "position" }, - "filename = ?", - new String[] { fileName }, null, null, null); + "filename = ? AND path = ?", + new String[] { file.getName(), file.getParentFile().getAbsolutePath() }, null, null, null); if (c.moveToFirst()) { position = c.getInt(c.getColumnIndex("position")); @@ -205,9 +234,11 @@ public class CommonActivity extends FragmentActivity { public int getMaxPosition(String fileName) { int position = 0; + File file = new File(fileName); + Cursor c = getReadableDb().query("comics_cache", new String[] { "max_position" }, - "filename = ?", - new String[] { fileName }, null, null, null); + "filename = ? AND path = ?", + new String[] { file.getName(), file.getParentFile().getAbsolutePath() }, null, null, null); if (c.moveToFirst()) { position = c.getInt(c.getColumnIndex("max_position")); @@ -221,9 +252,11 @@ public class CommonActivity extends FragmentActivity { public int getSize(String fileName) { int size = -1; + File file = new File(fileName); + Cursor c = getReadableDb().query("comics_cache", new String[] { "size" }, - "filename = ?", - new String[] { fileName }, null, null, null); + "filename = ? AND path = ?", + new String[] { file.getName(), file.getParentFile().getAbsolutePath() }, null, null, null); if (c.moveToFirst()) { size = c.getInt(c.getColumnIndex("size")); diff --git a/src/org/fox/ttcomics/DatabaseHelper.java b/src/org/fox/ttcomics/DatabaseHelper.java index 6108e55..15b7a25 100644 --- a/src/org/fox/ttcomics/DatabaseHelper.java +++ b/src/org/fox/ttcomics/DatabaseHelper.java @@ -1,16 +1,18 @@ package org.fox.ttcomics; +import java.io.File; + import android.content.Context; +import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import android.database.sqlite.SQLiteStatement; import android.provider.BaseColumns; public class DatabaseHelper extends SQLiteOpenHelper { - - private final String TAG = this.getClass().getSimpleName(); public static final String DATABASE_NAME = "ComicsCache.db"; - public static final int DATABASE_VERSION = 1; + public static final int DATABASE_VERSION = 2; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); @@ -24,6 +26,8 @@ public class DatabaseHelper extends SQLiteOpenHelper { db.execSQL("CREATE TABLE IF NOT EXISTS comics_cache (" + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + "filename TEXT, " + + "path TEXT, " + //v2 + "checksum TEXT, " + //v2 "size INTEGER, " + "position INTEGER, " + "max_position INTEGER" + @@ -32,7 +36,33 @@ public class DatabaseHelper extends SQLiteOpenHelper { @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - // + if (oldVersion == 1 && newVersion == 2) { + + db.execSQL("ALTER TABLE comics_cache ADD COLUMN path TEXT;"); + db.execSQL("ALTER TABLE comics_cache ADD COLUMN checksum TEXT;"); + + Cursor c = db.query("comics_cache", null, + null, null, null, null, null); + + if (c.moveToFirst()) { + while (!c.isAfterLast()) { + int id = c.getInt(c.getColumnIndex(BaseColumns._ID)); + String fileName = c.getString(c.getColumnIndex("filename")); + + File file = new File(fileName); + + SQLiteStatement stmt = db.compileStatement("UPDATE comics_cache SET filename = ?, path = ? WHERE " + BaseColumns._ID + " = ?"); + stmt.bindString(1, file.getName()); + stmt.bindString(2, file.getParentFile().getAbsolutePath()); + stmt.bindLong(3, id); + stmt.execute(); + + c.moveToNext(); + } + } + + c.close(); + } } } -- cgit v1.2.3