Started working on second database design

This commit is contained in:
2012-09-22 09:54:16 -04:00
parent 7f3eb985f2
commit 4e0cd45852

View File

@@ -80,7 +80,9 @@ 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 " + TABLE + "(" + KEY + " TEXT PRIMARY KEY, " + KEY_VALUE + " TEXT)"); 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_TOPICM_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)");
db.execSQL("CREATE TABLE " + THREAD_TABLE + "( id INTEGER PRIMARY KEY, " + KEY_FORUM_ID + " TEXT, " + KEY_TOPICM_ID + " TEXT, " + KEY_THREAD_ID + " TEXT, " + KEY + " TEXT, " + KEY_VALUE + " TEXT)");
} }
/** /**
@@ -92,8 +94,12 @@ 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 " + TABLE); db.execSQL("DROP TABLE IF EXISTS " + FORUM_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + TOPIC_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + THREAD_TABLE);
}
createDatabase(db); createDatabase(db);
if (mListener != null) if (mListener != null)
mListener.onDatabaseCreate(); mListener.onDatabaseCreate();
@@ -169,10 +175,9 @@ public class ForumDatabase {
/* /*
* Database tables * Database tables
*/ */
private final String TABLE = "table"; private final String FORUM_TABLE = "forums";
private final String FORUM_TABLE = "forum_"; private final String TOPIC_TABLE = "topics";
private final String TOPIC_TABLE = "topic_"; private final String THREAD_TABLE = "threads";
private final String THREAD_TABLE = "thread_";
/* /*
* Database keys * Database keys
@@ -339,22 +344,15 @@ public class ForumDatabase {
* @throws NullPointerException * @throws NullPointerException
* @author ricky barrette * @author ricky barrette
*/ */
public void upsertTable(final String table, final List<ContentValues> values) throws NullPointerException { public void upsertForumTable(final String forumId, final List<ContentValues> values) throws NullPointerException {
if (values == null) if (values == null)
throw new NullPointerException("info was null"); throw new NullPointerException("info was null");
/*
* create a new table if needed
*/
try {
mDb.execSQL("CREATE TABLE " + table + " (" + KEY + " TEXT PRIMARY KEY, " + KEY_VALUE + " TEXT)");
} catch (SQLiteException e) {
// ignore exception, table is already created
}
for(ContentValues item : values){ for(ContentValues item : values){
item.put(KEY_FORUM_ID, forumId);
Log.v(TAG, "upserting: "+ item.getAsString(KEY)); Log.v(TAG, "upserting: "+ item.getAsString(KEY));
if (!(mDb.update(table, item, KEY +" = "+ DatabaseUtils.sqlEscapeString(item.getAsString(KEY)), null) > 0)) if (!(mDb.update(FORUM_TABLE, item, KEY +" = "+ DatabaseUtils.sqlEscapeString(item.getAsString(KEY)), null) > 0))
mDb.insert(table, null, item); mDb.insert(FORUM_TABLE, null, item);
} }
} }
@@ -383,7 +381,7 @@ public class ForumDatabase {
if (result != null) if (result != null)
for (int i = 0; i < result.length; i++) { for (int i = 0; i < result.length; i++) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i]; final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i];
upsertTable(FORUM_TABLE + contentHash.get("forum_id"), convertHashMapToContentValues(contentHash)); upsertForumTable((String)contentHash.get("forum_id"), convertHashMapToContentValues(contentHash));
saveTopics(client, contentHash.get("forum_id")); saveTopics(client, contentHash.get("forum_id"));
@@ -408,7 +406,7 @@ public class ForumDatabase {
if (result != null) if (result != null)
for (int i = 0; i < result.length; i++) { for (int i = 0; i < result.length; i++) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i]; final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i];
upsertTable(TOPIC_TABLE + contentHash.get("topic_id"),convertHashMapToContentValues((HashMap<?, ?>) result[i])); //upsertTable(TOPIC_TABLE + contentHash.get("topic_id"),convertHashMapToContentValues((HashMap<?, ?>) result[i]));
saveThreads(client, contentHash.get("topic_id")); saveThreads(client, contentHash.get("topic_id"));
} }
@@ -433,7 +431,7 @@ public class ForumDatabase {
if (result != null) if (result != null)
for (int i = 0; i < result.length; i++) { for (int i = 0; i < result.length; i++) {
final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i]; final HashMap<?, ?> contentHash = (HashMap<?, ?>) result[i];
upsertTable(THREAD_TABLE + contentHash.get("post_id"),convertHashMapToContentValues((HashMap<?, ?>) result[i])); //upsertTable(THREAD_TABLE + contentHash.get("post_id"),convertHashMapToContentValues((HashMap<?, ?>) result[i]));
} }
} }
@@ -477,4 +475,4 @@ public class ForumDatabase {
Log.v(TAG, "total list size "+ list.size()); Log.v(TAG, "total list size "+ list.size());
return list; return list;
} }
} }