From a50e18d987f62bd98f4f3ebd90ab1cf221fd5fd0 Mon Sep 17 00:00:00 2001 From: Georg Lukas Date: Sat, 5 Feb 2011 14:34:09 +0100 Subject: [PATCH] outsource resetting of positions table --- src/StorageDatabase.scala | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/StorageDatabase.scala b/src/StorageDatabase.scala index e99ebe6..023480b 100644 --- a/src/StorageDatabase.scala +++ b/src/StorageDatabase.scala @@ -91,25 +91,30 @@ class StorageDatabase(context : Context) extends db.execSQL(Position.TABLE_CREATE) Array("call", "lat", "lon").map(col => db.execSQL(Position.TABLE_INDEX.format(col, col))) } + def resetPositionsTable(db : SQLiteDatabase) { + db.execSQL(Position.TABLE_DROP) + db.execSQL(Position.TABLE_CREATE) + Array("call", "lat", "lon").map(col => db.execSQL(Position.TABLE_INDEX.format(col, col))) + // we can not call getPosts() here due to recursion issues + val c = db.query(Post.TABLE, Post.COLUMNS, "TYPE = 0 OR TYPE = 3", + null, null, null, "_ID DESC", null) + c.moveToFirst() + while (!c.isAfterLast()) { + val message = c.getString(c.getColumnIndexOrThrow(Post.MESSAGE)) + val ts = c.getLong(c.getColumnIndexOrThrow(Post.TS)) + addPosition(ts, message) + c.moveToNext() + } + c.close() + } + def resetPositionsTable() : Unit = resetPositionsTable(getWritableDatabase()) + override def onUpgrade(db: SQLiteDatabase, from : Int, to : Int) { if (from == 1 && to >= 2) { db.execSQL("ALTER TABLE %s ADD COLUMN %s".format(Post.TABLE, "TYPE INTEGER DEFAULT 0")) } if (from <= 4 && to >= 3) { - db.execSQL(Position.TABLE_DROP) - db.execSQL(Position.TABLE_CREATE) - Array("call", "lat", "lon").map(col => db.execSQL(Position.TABLE_INDEX.format(col, col))) - // we can not call getPosts() here due to recursion issues - val c = db.query(Post.TABLE, Post.COLUMNS, "TYPE = 0 OR TYPE = 3", - null, null, null, "_ID DESC", null) - c.moveToFirst() - while (!c.isAfterLast()) { - val message = c.getString(c.getColumnIndexOrThrow(Post.MESSAGE)) - val ts = c.getLong(c.getColumnIndexOrThrow(Post.TS)) - addPosition(ts, message) - c.moveToNext() - } - c.close() + resetPositionsTable(db) } }