From 111c59dd3021281c50c0326623ed910a15e2cde7 Mon Sep 17 00:00:00 2001 From: Andy2 Date: Tue, 23 Nov 2010 06:44:59 -0800 Subject: [PATCH] toward having game deletion get reported even if network isn't live at the moment: create new "obits" table and put dead game info in it. Change constant for current table name to be less generic. --- .../src/org/eehouse/android/xw4/DBHelper.java | 37 ++++++++++++--- .../src/org/eehouse/android/xw4/DBUtils.java | 46 ++++++++++++++----- .../org/eehouse/android/xw4/GameUtils.java | 3 +- 3 files changed, 66 insertions(+), 20 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java index 06bf4ba46..5e34e8c6a 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java @@ -26,9 +26,10 @@ import android.database.sqlite.SQLiteDatabase; public class DBHelper extends SQLiteOpenHelper { - public static final String TABLE_NAME = "summaries"; + public static final String TABLE_NAME_SUM = "summaries"; + public static final String TABLE_NAME_OBITS = "obits"; private static final String DB_NAME = "xwdb"; - private static final int DB_VERSION = 5; + private static final int DB_VERSION = 6; public static final String FILE_NAME = "FILE_NAME"; public static final String NUM_MOVES = "NUM_MOVES"; @@ -61,10 +62,9 @@ public class DBHelper extends SQLiteOpenHelper { super( context, DB_NAME, null, DB_VERSION ); } - @Override - public void onCreate( SQLiteDatabase db ) + private void onCreateSum( SQLiteDatabase db ) { - db.execSQL( "CREATE TABLE " + TABLE_NAME + " (" + db.execSQL( "CREATE TABLE " + TABLE_NAME_SUM + " (" + FILE_NAME + " TEXT PRIMARY KEY," + NUM_MOVES + " INTEGER," + NUM_PLAYERS + " INTEGER," @@ -92,11 +92,34 @@ public class DBHelper extends SQLiteOpenHelper { + ");" ); } + private void onCreateObits( SQLiteDatabase db ) + { + db.execSQL( "CREATE TABLE " + TABLE_NAME_OBITS + " (" + + RELAYID + " TEXT," + + SEED + " INTEGER" + + ");" ); + } + + @Override + public void onCreate( SQLiteDatabase db ) + { + onCreateSum( db ); + onCreateObits( db ); + } + @Override public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion ) { Utils.logf( "onUpgrade: old: %d; new: %d", oldVersion, newVersion ); - db.execSQL( "DROP TABLE " + TABLE_NAME + ";" ); - onCreate( db ); + + if ( newVersion == 6 && oldVersion == 5 ) { + onCreateObits(db); + } else { + db.execSQL( "DROP TABLE " + TABLE_NAME_SUM + ";" ); + if ( oldVersion >= 6 ) { + db.execSQL( "DROP TABLE " + TABLE_NAME_OBITS + ";" ); + } + onCreate( db ); + } } } diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java index bae5c06df..396d808b6 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -53,8 +53,8 @@ public class DBUtils { }; String selection = DBHelper.FILE_NAME + "=\"" + file + "\""; - Cursor cursor = db.query( DBHelper.TABLE_NAME, columns, selection, - null, null, null, null ); + Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, + selection, null, null, null, null ); if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { summary = new GameSummary(); summary.nMoves = cursor.getInt(cursor. @@ -139,7 +139,7 @@ public class DBUtils { if ( null == summary ) { String selection = DBHelper.FILE_NAME + "=\"" + path + "\""; - db.delete( DBHelper.TABLE_NAME, selection, null ); + db.delete( DBHelper.TABLE_NAME_SUM, selection, null ); } else { ContentValues values = new ContentValues(); values.put( DBHelper.FILE_NAME, path ); @@ -171,8 +171,8 @@ public class DBUtils { Utils.logf( "saveSummary: nMoves=%d", summary.nMoves ); try { - long result = db.replaceOrThrow( DBHelper.TABLE_NAME, "", - values ); + long result = db.replaceOrThrow( DBHelper.TABLE_NAME_SUM, + "", values ); } catch ( Exception ex ) { Utils.logf( "ex: %s", ex.toString() ); } @@ -191,8 +191,8 @@ public class DBUtils { + dict + "\'"; // null for columns will return whole rows: bad String[] columns = { DBHelper.DICTNAME }; - Cursor cursor = db.query( DBHelper.TABLE_NAME, columns, selection, - null, null, null, null ); + Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, + selection, null, null, null, null ); result = cursor.getCount(); cursor.close(); @@ -207,7 +207,8 @@ public class DBUtils { SQLiteDatabase db = s_dbHelper.getWritableDatabase(); String cmd = String.format( "UPDATE %s SET %s = 1 WHERE %s = '%s'", - DBHelper.TABLE_NAME, DBHelper.HASMSGS, + DBHelper.TABLE_NAME_SUM, + DBHelper.HASMSGS, DBHelper.RELAYID, relayID ); db.execSQL( cmd ); db.close(); @@ -222,8 +223,8 @@ public class DBUtils { SQLiteDatabase db = s_dbHelper.getReadableDatabase(); String[] columns = { DBHelper.FILE_NAME }; String selection = DBHelper.RELAYID + "='" + relayID + "'"; - Cursor cursor = db.query( DBHelper.TABLE_NAME, columns, selection, - null, null, null, null ); + Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, + selection, null, null, null, null ); if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { result = cursor.getString( cursor .getColumnIndex(DBHelper.FILE_NAME)); @@ -247,8 +248,8 @@ public class DBUtils { String selection = DBHelper.RELAYID + " NOT null AND " + "NOT " + DBHelper.HASMSGS; - Cursor cursor = db.query( DBHelper.TABLE_NAME, columns, selection, - null, null, null, null ); + Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, + selection, null, null, null, null ); if ( 0 < cursor.getCount() ) { cursor.moveToFirst(); @@ -272,6 +273,27 @@ public class DBUtils { return result; } + public static void addDeceased( Context context, String relayID, + int seed ) + { + initDB( context ); + synchronized( s_dbHelper ) { + SQLiteDatabase db = s_dbHelper.getWritableDatabase(); + + ContentValues values = new ContentValues(); + values.put( DBHelper.RELAYID, relayID ); + values.put( DBHelper.SEED, seed ); + + try { + long result = db.replaceOrThrow( DBHelper.TABLE_NAME_OBITS, + "", values ); + } catch ( Exception ex ) { + Utils.logf( "ex: %s", ex.toString() ); + } + db.close(); + } + } + private static void initDB( Context context ) { if ( null == s_dbHelper ) { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java index 7651227ee..4ac8c791a 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java @@ -475,7 +475,8 @@ public class GameUtils { { GameSummary summary = DBUtils.getSummary( context, path ); if ( null != summary.relayID ) { - NetUtils.informOfDeath( context, summary.relayID, summary.seed ); + DBUtils.addDeceased( context, summary.relayID, summary.seed ); + // NetUtils.informOfDeath( context, summary.relayID, summary.seed ); } }