Updated database to use an easier to use format.

Added 3 more tables to store id's of forums, topics, and threads.
I also renamed existing tables to better describe their job.
the following tables are now used:
forums			stores ids of forums
topics			stores ids of topics, and parent forums
threads			stores ids of threads, and parent topics & forums
forum_info		stores key value pairs
topic_info		stores key value pairs
thread_info		stores key value pairs

Signed-off-by: Ricky Barrette <rickbarrette@gmail.com>
This commit is contained in:
2012-10-05 12:14:32 -04:00
parent f06bd3e94c
commit 93dfb68e4f
2 changed files with 59 additions and 19 deletions

View File

@@ -1,5 +1,5 @@
<manifest package="org.RickBarrette.osj.forum" <manifest package="org.RickBarrette.osj.forum"
android:versionCode="264" android:versionCode="269"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto"> android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="auto">
<uses-sdk <uses-sdk

View File

@@ -38,6 +38,7 @@ import org.xmlrpc.android.XMLRPCException;
import android.app.ProgressDialog; import android.app.ProgressDialog;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils; import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
@@ -77,11 +78,20 @@ public class ForumDatabase {
* @author ricky barrette * @author ricky barrette
*/ */
private void createDatabase(final SQLiteDatabase db) { private void createDatabase(final SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + FORUM_TABLE + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)");
db.execSQL("CREATE TABLE " + TOPIC_TABLE + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPIC_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE /*
+ " TEXT)"); * These tables will be used for storing forum, topic, and post id's
db.execSQL("CREATE TABLE " + THREAD_TABLE + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPIC_ID + " TEXT, " + KEY_POST_ID + " TEXT, " */
+ KEY + " TEXT, " + KEY_VALUE + " TEXT)"); db.execSQL("CREATE TABLE " + TABLE_FORUMS + "( id TEXT PRIMARY KEY)");
db.execSQL("CREATE TABLE " + TABLE_TOPICS + "( id TEXT PRIMARY KEY, "+ KEY_FORUM_ID +" TEXT)");
db.execSQL("CREATE TABLE " + TABLE_THREADS + "( id TEXT PRIMARY KEY, "+ KEY_FORUM_ID + "TEXT, "+ KEY_POST_ID +"TEXT)");
/*
* These tables will be used for storing forum, topic, and post information.
*/
db.execSQL("CREATE TABLE " + TABLE_FORUM_INFO + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)");
db.execSQL("CREATE TABLE " + TABLE_TOPIC_INFO + "( id INTEGER PRIMARY KEY, " + KEY_TOPIC_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE+ " TEXT)");
db.execSQL("CREATE TABLE " + TABLE_THREAD_INFO + "( id INTEGER PRIMARY KEY, " + KEY_POST_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)");
} }
/** /**
@@ -94,9 +104,13 @@ public class ForumDatabase {
@Override @Override
public void onCreate(final SQLiteDatabase db) { public void onCreate(final SQLiteDatabase db) {
if (Constraints.DROP_TABLES_EVERY_TIME) { if (Constraints.DROP_TABLES_EVERY_TIME) {
db.execSQL("DROP TABLE IF EXISTS " + FORUM_TABLE); db.execSQL("DROP TABLE IF EXISTS " + TABLE_FORUMS);
db.execSQL("DROP TABLE IF EXISTS " + TOPIC_TABLE); db.execSQL("DROP TABLE IF EXISTS " + TABLE_TOPICS);
db.execSQL("DROP TABLE IF EXISTS " + THREAD_TABLE); db.execSQL("DROP TABLE IF EXISTS " + TABLE_THREADS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FORUM_INFO);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TOPIC_INFO);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_THREAD_INFO);
} }
createDatabase(db); createDatabase(db);
@@ -174,15 +188,19 @@ public class ForumDatabase {
/* /*
* Database tables * Database tables
*/ */
private final String FORUM_TABLE = "forums"; private static final String TABLE_FORUMS = "forums";
private final String TOPIC_TABLE = "topics"; private static final String TABLE_TOPICS = "topics";
private final String THREAD_TABLE = "threads"; private static final String TABLE_THREADS = "threads";
private static final String TABLE_FORUM_INFO = "forum_info";
private static final String TABLE_TOPIC_INFO = "topic_info";
private static final String TABLE_THREAD_INFO = "thread_info";
/* /*
* Database keys * Database keys
*/ */
private static final String KEY = "key"; public static final String KEY_ID = "id";
private static final String KEY_VALUE = "value"; public static final String KEY = "key";
public static final String KEY_VALUE = "value";
private static final String KEY_FORUM_ID = "forum_id"; private static final String KEY_FORUM_ID = "forum_id";
private static final String KEY_TOPIC_ID = "topic_id"; private static final String KEY_TOPIC_ID = "topic_id";
private static final String KEY_POST_ID = "post_id"; private static final String KEY_POST_ID = "post_id";
@@ -343,6 +361,10 @@ public class ForumDatabase {
}).start(); }).start();
} }
public Cursor getForums(){
return mDb.query(TABLE_FORUMS, new String[] { KEY_ID}, null, null, null, null, null);
}
/** /**
* Restores the database from the sdcard * Restores the database from the sdcard
* *
@@ -396,14 +418,17 @@ public class ForumDatabase {
/* /*
* save the forums * save the forums
*/ */
if (result != null) if (result != null){
String id = null;
for (final Object element : result) { for (final Object element : result) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) element; final HashMap<?, ?> contentHash = (HashMap<?, ?>) element;
upsertForumTable((String) contentHash.get("forum_id"), convertHashMapToContentValues(contentHash)); id = (String) contentHash.get("forum_id");
upsertForumTable(id, convertHashMapToContentValues(contentHash));
if (isRecursive) if (isRecursive)
saveTopics(client, contentHash.get("forum_id"), isRecursive); saveTopics(client, contentHash.get("forum_id"), isRecursive);
} }
}
} }
/** /**
@@ -466,10 +491,14 @@ public class ForumDatabase {
private void upsertForumTable(final String forumId, final List<ContentValues> values) throws NullPointerException { private void upsertForumTable(final String forumId, final List<ContentValues> values) throws NullPointerException {
if (values == null) if (values == null)
throw new NullPointerException("values was null"); throw new NullPointerException("values was null");
ContentValues id = new ContentValues();
id.put(KEY_ID, forumId);
upsertTable(TABLE_FORUMS, KEY_ID +" = "+ forumId, id);
for (final ContentValues item : values) { for (final ContentValues item : values) {
item.put(KEY_FORUM_ID, forumId); item.put(KEY_FORUM_ID, forumId);
upsertTable(FORUM_TABLE, upsertTable(TABLE_FORUM_INFO,
KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId)), item); KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId)), item);
} }
} }
@@ -501,13 +530,19 @@ public class ForumDatabase {
private void upsertThreadTable(final String forumId, final String topicId, final String postId, final List<ContentValues> values) throws NullPointerException { private void upsertThreadTable(final String forumId, final String topicId, final String postId, final List<ContentValues> values) throws NullPointerException {
if (values == null) if (values == null)
throw new NullPointerException("values was null"); throw new NullPointerException("values was null");
ContentValues id = new ContentValues();
id.put(KEY_ID, postId);
id.put(KEY_FORUM_ID, forumId);
id.put(KEY_TOPIC_ID, topicId);
upsertTable(TABLE_THREADS, KEY_ID +" = "+ postId, id);
for (final ContentValues item : values) { for (final ContentValues item : values) {
item.put(KEY_FORUM_ID, forumId); item.put(KEY_FORUM_ID, forumId);
item.put(KEY_TOPIC_ID, topicId); item.put(KEY_TOPIC_ID, topicId);
item.put(KEY_POST_ID, postId); item.put(KEY_POST_ID, postId);
upsertTable( upsertTable(
THREAD_TABLE, TABLE_THREAD_INFO,
KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY)) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId) KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY)) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId)
+ " AND " + KEY_TOPIC_ID + " = " + DatabaseUtils.sqlEscapeString(topicId) + " AND " + KEY_POST_ID + " = " + " AND " + KEY_TOPIC_ID + " = " + DatabaseUtils.sqlEscapeString(topicId) + " AND " + KEY_POST_ID + " = "
+ DatabaseUtils.sqlEscapeString(postId), item); + DatabaseUtils.sqlEscapeString(postId), item);
@@ -526,11 +561,16 @@ public class ForumDatabase {
private void upsertTopicTable(final String forumId, final String topicId, final List<ContentValues> values) throws NullPointerException { private void upsertTopicTable(final String forumId, final String topicId, final List<ContentValues> values) throws NullPointerException {
if (values == null) if (values == null)
throw new NullPointerException("values was null"); throw new NullPointerException("values was null");
ContentValues id = new ContentValues();
id.put(KEY_ID, topicId);
id.put(KEY_FORUM_ID, forumId);
upsertTable(TABLE_TOPICS, KEY_ID +" = "+ topicId, id);
for (final ContentValues item : values) { for (final ContentValues item : values) {
item.put(KEY_FORUM_ID, forumId); item.put(KEY_FORUM_ID, forumId);
item.put(KEY_TOPIC_ID, topicId); item.put(KEY_TOPIC_ID, topicId);
upsertTable(TOPIC_TABLE, upsertTable(TABLE_TOPIC_INFO,
KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY)) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId) KEY + " = " + DatabaseUtils.sqlEscapeString(item.getAsString(KEY)) + " AND " + KEY_FORUM_ID + " = " + DatabaseUtils.sqlEscapeString(forumId)
+ " AND " + KEY_TOPIC_ID + " = " + DatabaseUtils.sqlEscapeString(topicId), item); + " AND " + KEY_TOPIC_ID + " = " + DatabaseUtils.sqlEscapeString(topicId), item);
} }