1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +02:00

network database refactoring; SQLiteUtil class

This commit is contained in:
Nikolay Pultsin 2011-03-03 19:08:53 +00:00
parent b7cc36530b
commit 7c679882f4
6 changed files with 90 additions and 55 deletions

View file

@ -1,8 +1,9 @@
===== 0.99.14 (??? ??, 2011) ===== ===== 0.99.14 (Mar ??, 2011) =====
* Thai localization (by Samphan Pojanasophanakul) * Thai localization (by Samphan Pojanasophanakul)
* Fixed TTF font style detection * Fixed TTF font style detection
* opds:// URLs support has been implemented * opds:// URLs support has been implemented
* Unexpected search call has been fixed * Unexpected search call has been fixed
* Ignore case comparison for zip entry names
===== 0.99.13 (Feb 13, 2011) ===== ===== 0.99.13 (Feb 13, 2011) =====
* Fixed book/position forgetting bug * Fixed book/position forgetting bug

View file

@ -37,6 +37,7 @@ import org.geometerplus.zlibrary.text.view.ZLTextFixedPosition;
import org.geometerplus.fbreader.library.*; import org.geometerplus.fbreader.library.*;
import org.geometerplus.android.util.UIUtil; import org.geometerplus.android.util.UIUtil;
import org.geometerplus.android.util.SQLiteUtil;
public final class SQLiteBooksDatabase extends BooksDatabase { public final class SQLiteBooksDatabase extends BooksDatabase {
private final String myInstanceId; private final String myInstanceId;
@ -107,29 +108,6 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
}, context); }, context);
} }
private static void bindString(SQLiteStatement statement, int index, String value) {
if (value != null) {
statement.bindString(index, value);
} else {
statement.bindNull(index);
}
}
private static void bindDate(SQLiteStatement statement, int index, Date value) {
if (value != null) {
statement.bindLong(index, value.getTime());
} else {
statement.bindNull(index);
}
}
private static Date getDate(Cursor cursor, int index) {
if (cursor.isNull(index)) {
return null;
}
return new Date(cursor.getLong(index));
}
@Override @Override
protected Book loadBook(long bookId) { protected Book loadBook(long bookId) {
Book book = null; Book book = null;
@ -278,8 +256,8 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
); );
} }
myUpdateBookInfoStatement.bindLong(1, fileId); myUpdateBookInfoStatement.bindLong(1, fileId);
bindString(myUpdateBookInfoStatement, 2, encoding); SQLiteUtil.bindString(myUpdateBookInfoStatement, 2, encoding);
bindString(myUpdateBookInfoStatement, 3, language); SQLiteUtil.bindString(myUpdateBookInfoStatement, 3, language);
myUpdateBookInfoStatement.bindString(4, title); myUpdateBookInfoStatement.bindString(4, title);
myUpdateBookInfoStatement.bindLong(5, bookId); myUpdateBookInfoStatement.bindLong(5, bookId);
myUpdateBookInfoStatement.execute(); myUpdateBookInfoStatement.execute();
@ -292,8 +270,8 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
"INSERT OR IGNORE INTO Books (encoding,language,title,file_id) VALUES (?,?,?,?)" "INSERT OR IGNORE INTO Books (encoding,language,title,file_id) VALUES (?,?,?,?)"
); );
} }
bindString(myInsertBookInfoStatement, 1, encoding); SQLiteUtil.bindString(myInsertBookInfoStatement, 1, encoding);
bindString(myInsertBookInfoStatement, 2, language); SQLiteUtil.bindString(myInsertBookInfoStatement, 2, language);
myInsertBookInfoStatement.bindString(3, title); myInsertBookInfoStatement.bindString(3, title);
final FileInfoSet infoSet = new FileInfoSet(file); final FileInfoSet infoSet = new FileInfoSet(file);
myInsertBookInfoStatement.bindLong(4, infoSet.getId(file)); myInsertBookInfoStatement.bindLong(4, infoSet.getId(file));
@ -709,9 +687,9 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
cursor.getLong(1), cursor.getLong(1),
cursor.getString(2), cursor.getString(2),
cursor.getString(3), cursor.getString(3),
getDate(cursor, 4), SQLiteUtil.getDate(cursor, 4),
getDate(cursor, 5), SQLiteUtil.getDate(cursor, 5),
getDate(cursor, 6), SQLiteUtil.getDate(cursor, 6),
(int)cursor.getLong(7), (int)cursor.getLong(7),
cursor.getString(8), cursor.getString(8),
(int)cursor.getLong(9), (int)cursor.getLong(9),
@ -737,9 +715,9 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
cursor.getLong(1), cursor.getLong(1),
cursor.getString(2), cursor.getString(2),
cursor.getString(3), cursor.getString(3),
getDate(cursor, 4), SQLiteUtil.getDate(cursor, 4),
getDate(cursor, 5), SQLiteUtil.getDate(cursor, 5),
getDate(cursor, 6), SQLiteUtil.getDate(cursor, 6),
(int)cursor.getLong(7), (int)cursor.getLong(7),
cursor.getString(8), cursor.getString(8),
(int)cursor.getLong(9), (int)cursor.getLong(9),
@ -775,11 +753,11 @@ public final class SQLiteBooksDatabase extends BooksDatabase {
statement.bindLong(1, bookmark.getBookId()); statement.bindLong(1, bookmark.getBookId());
statement.bindString(2, bookmark.getText()); statement.bindString(2, bookmark.getText());
bindDate(statement, 3, bookmark.getTime(Bookmark.CREATION)); SQLiteUtil.bindDate(statement, 3, bookmark.getTime(Bookmark.CREATION));
bindDate(statement, 4, bookmark.getTime(Bookmark.MODIFICATION)); SQLiteUtil.bindDate(statement, 4, bookmark.getTime(Bookmark.MODIFICATION));
bindDate(statement, 5, bookmark.getTime(Bookmark.ACCESS)); SQLiteUtil.bindDate(statement, 5, bookmark.getTime(Bookmark.ACCESS));
statement.bindLong(6, bookmark.getAccessCount()); statement.bindLong(6, bookmark.getAccessCount());
bindString(statement, 7, bookmark.ModelId); SQLiteUtil.bindString(statement, 7, bookmark.ModelId);
statement.bindLong(8, bookmark.ParagraphIndex); statement.bindLong(8, bookmark.ParagraphIndex);
statement.bindLong(9, bookmark.ElementIndex); statement.bindLong(9, bookmark.ElementIndex);
statement.bindLong(10, bookmark.CharIndex); statement.bindLong(10, bookmark.CharIndex);

View file

@ -56,7 +56,6 @@ public class NetworkCatalogActivity extends NetworkBaseActivity implements UserR
final NetworkTree.Key key = (NetworkTree.Key)intent.getSerializableExtra(CATALOG_KEY_KEY); final NetworkTree.Key key = (NetworkTree.Key)intent.getSerializableExtra(CATALOG_KEY_KEY);
myTree = library.getTreeByKey(key); myTree = library.getTreeByKey(key);
System.err.println("KEY = " + key);
if (myTree == null) { if (myTree == null) {
throw new RuntimeException("Tree not found for key " + key); throw new RuntimeException("Tree not found for key " + key);
} }

View file

@ -31,6 +31,8 @@ import org.geometerplus.zlibrary.ui.android.library.ZLAndroidApplication;
import org.geometerplus.fbreader.network.ICustomNetworkLink; import org.geometerplus.fbreader.network.ICustomNetworkLink;
import org.geometerplus.fbreader.network.NetworkDatabase; import org.geometerplus.fbreader.network.NetworkDatabase;
import org.geometerplus.android.util.SQLiteUtil;
class SQLiteNetworkDatabase extends NetworkDatabase { class SQLiteNetworkDatabase extends NetworkDatabase {
private final SQLiteDatabase myDatabase; private final SQLiteDatabase myDatabase;
@ -69,14 +71,6 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
} }
} }
private static void bindString(SQLiteStatement statement, int index, String value) {
if (value != null) {
statement.bindString(index, value);
} else {
statement.bindNull(index);
}
}
@Override @Override
protected void loadCustomLinks(ICustomLinksHandler handler) { protected void loadCustomLinks(ICustomLinksHandler handler) {
final Cursor cursor = myDatabase.rawQuery("SELECT link_id,title,site_name,summary,icon FROM CustomLinks", null); final Cursor cursor = myDatabase.rawQuery("SELECT link_id,title,site_name,summary,icon FROM CustomLinks", null);
@ -89,7 +83,7 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
final String icon = cursor.getString(4); final String icon = cursor.getString(4);
linksMap.clear(); linksMap.clear();
final Cursor linksCursor = myDatabase.rawQuery("SELECT key,url FROM CustomLinkUrls WHERE link_id = " + id, null); final Cursor linksCursor = myDatabase.rawQuery("SELECT key,url FROM LinkUrls WHERE url NOT NULL AND link_id = " + id, null);
while (linksCursor.moveToNext()) { while (linksCursor.moveToNext()) {
linksMap.put(linksCursor.getString(0), linksCursor.getString(1)); linksMap.put(linksCursor.getString(0), linksCursor.getString(1));
} }
@ -129,8 +123,8 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
statement.bindString(1, link.getTitle()); statement.bindString(1, link.getTitle());
statement.bindString(2, link.getSiteName()); statement.bindString(2, link.getSiteName());
bindString(statement, 3, link.getSummary()); SQLiteUtil.bindString(statement, 3, link.getSummary());
bindString(statement, 4, link.getIcon()); SQLiteUtil.bindString(statement, 4, link.getIcon());
final long id; final long id;
final HashMap<String,String> linksMap = new HashMap<String,String>(); final HashMap<String,String> linksMap = new HashMap<String,String>();
@ -143,7 +137,7 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
statement.bindLong(5, id); statement.bindLong(5, id);
statement.execute(); statement.execute();
final Cursor linksCursor = myDatabase.rawQuery("SELECT key,url FROM CustomLinkUrls WHERE link_id = " + link.getId(), null); final Cursor linksCursor = myDatabase.rawQuery("SELECT key,url FROM LinkUrls WHERE url NOT NULL AND link_id = " + link.getId(), null);
while (linksCursor.moveToNext()) { while (linksCursor.moveToNext()) {
linksMap.put(linksCursor.getString(0), linksCursor.getString(1)); linksMap.put(linksCursor.getString(0), linksCursor.getString(1));
} }
@ -157,13 +151,13 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
if (dbValue == null) { if (dbValue == null) {
if (myInsertCustomLinkUrlStatement == null) { if (myInsertCustomLinkUrlStatement == null) {
myInsertCustomLinkUrlStatement = myDatabase.compileStatement( myInsertCustomLinkUrlStatement = myDatabase.compileStatement(
"INSERT OR REPLACE INTO CustomLinkUrls(url,link_id,key) VALUES (?,?,?)"); "INSERT OR REPLACE INTO LinkUrls(url,link_id,key) VALUES (?,?,?)");
} }
urlStatement = myInsertCustomLinkUrlStatement; urlStatement = myInsertCustomLinkUrlStatement;
} else if (!value.equals(dbValue)) { } else if (!value.equals(dbValue)) {
if (myUpdateCustomLinkUrlStatement == null) { if (myUpdateCustomLinkUrlStatement == null) {
myUpdateCustomLinkUrlStatement = myDatabase.compileStatement( myUpdateCustomLinkUrlStatement = myDatabase.compileStatement(
"UPDATE CustomLinkUrls SET url = ? WHERE link_id = ? AND key = ?"); "UPDATE LinkUrls SET url = ? WHERE link_id = ? AND key = ?");
} }
urlStatement = myUpdateCustomLinkUrlStatement; urlStatement = myUpdateCustomLinkUrlStatement;
} else { } else {
@ -177,7 +171,7 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
for (String key: linksMap.keySet()) { for (String key: linksMap.keySet()) {
if (myDeleteCustomLinkUrlStatement == null) { if (myDeleteCustomLinkUrlStatement == null) {
myDeleteCustomLinkUrlStatement = myDatabase.compileStatement( myDeleteCustomLinkUrlStatement = myDatabase.compileStatement(
"DELETE FROM CustomLinkUrls WHERE link_id = ? AND key = ?"); "DELETE FROM LinkUrls WHERE link_id = ? AND key = ?");
} }
myDeleteCustomLinkUrlStatement.bindLong(1, id); myDeleteCustomLinkUrlStatement.bindLong(1, id);
myDeleteCustomLinkUrlStatement.bindString(2, key); myDeleteCustomLinkUrlStatement.bindString(2, key);
@ -199,7 +193,7 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
final long id = link.getId(); final long id = link.getId();
if (myDeleteAllCustomLinksStatement == null) { if (myDeleteAllCustomLinksStatement == null) {
myDeleteAllCustomLinksStatement = myDatabase.compileStatement( myDeleteAllCustomLinksStatement = myDatabase.compileStatement(
"DELETE FROM CustomLinkUrls WHERE link_id = ?"); "DELETE FROM LinkUrls WHERE link_id = ?");
} }
myDeleteAllCustomLinksStatement.bindLong(1, id); myDeleteAllCustomLinksStatement.bindLong(1, id);
myDeleteAllCustomLinksStatement.execute(); myDeleteAllCustomLinksStatement.execute();
@ -244,5 +238,15 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
"icon TEXT)"); "icon TEXT)");
myDatabase.execSQL("INSERT INTO CustomLinks (link_id,title,site_name,summary,icon) SELECT link_id,title,site_name,summary,icon FROM CustomLinks_Obsolete"); myDatabase.execSQL("INSERT INTO CustomLinks (link_id,title,site_name,summary,icon) SELECT link_id,title,site_name,summary,icon FROM CustomLinks_Obsolete");
myDatabase.execSQL("DROP TABLE CustomLinks_Obsolete"); myDatabase.execSQL("DROP TABLE CustomLinks_Obsolete");
myDatabase.execSQL(
"CREATE TABLE LinkUrls(" +
"key TEXT NOT NULL," +
"link_id INTEGER NOT NULL REFERENCES CustomLinks(link_id)," +
"url TEXT," +
"update_time INTEGER," +
"CONSTRAINT LinkUrls_PK PRIMARY KEY (key, link_id))");
myDatabase.execSQL("INSERT INTO LinkUrls (key,link_id,url) SELECT key,link_id,url FROM CustomLinkUrls");
myDatabase.execSQL("DROP TABLE CustomLinkUrls");
} }
} }

View file

@ -0,0 +1,50 @@
/*
* Copyright (C) 2009-2011 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.android.util;
import java.util.Date;
import android.database.sqlite.SQLiteStatement;
import android.database.Cursor;
public abstract class SQLiteUtil {
public static void bindString(SQLiteStatement statement, int index, String value) {
if (value != null) {
statement.bindString(index, value);
} else {
statement.bindNull(index);
}
}
public static void bindDate(SQLiteStatement statement, int index, Date value) {
if (value != null) {
statement.bindLong(index, value.getTime());
} else {
statement.bindNull(index);
}
}
public static Date getDate(Cursor cursor, int index) {
if (cursor.isNull(index)) {
return null;
}
return new Date(cursor.getLong(index));
}
}

View file

@ -82,6 +82,9 @@ class NetworkOPDSFeedReader implements OPDSFeedReader, OPDSConstants, MimeTypes
if (MIME_APP_ATOM.equals(type) && "next".equals(rel)) { if (MIME_APP_ATOM.equals(type) && "next".equals(rel)) {
myNextURL = ZLNetworkUtil.url(myBaseURL, link.getHref()); myNextURL = ZLNetworkUtil.url(myBaseURL, link.getHref());
} }
if ("search".equals(rel)) {
System.err.println(type + " :: " + link.getHref());
}
} }
return false; return false;
} }