From 1546b15997f375e997ba7859fe927f44d64a9883 Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 28 Jan 2013 06:51:39 -0800 Subject: [PATCH] rowid needs to be autoincrement to be useful as a token with new relay protocol. And you can't make it autoincrement except when creating a table. So add new column set equal to current rowid, then create a new table and copy the old data into it. --- .../src/org/eehouse/android/xw4/DBHelper.java | 57 ++++++++++++++++++- .../src/org/eehouse/android/xw4/DBUtils.java | 51 +++++++++++++++++ .../org/eehouse/android/xw4/GameUtils.java | 3 +- 3 files changed, 108 insertions(+), 3 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 b13ded1e4..37e3eb299 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java @@ -25,6 +25,9 @@ import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import android.text.TextUtils; +import java.util.ArrayList; +import java.util.Arrays; public class DBHelper extends SQLiteOpenHelper { @@ -34,9 +37,10 @@ public class DBHelper extends SQLiteOpenHelper { public static final String TABLE_NAME_DICTINFO = "dictinfo"; public static final String TABLE_NAME_GROUPS = "groups"; private static final String DB_NAME = "xwdb"; - private static final int DB_VERSION = 16; + private static final int DB_VERSION = 17; public static final String GAME_NAME = "GAME_NAME"; + public static final String VISID = "VISID"; public static final String NUM_MOVES = "NUM_MOVES"; public static final String TURN = "TURN"; public static final String GIFLAGS = "GIFLAGS"; @@ -85,7 +89,9 @@ public class DBHelper extends SQLiteOpenHelper { private Context m_context; private static final String[] s_summaryColsAndTypes = { - GAME_NAME, "TEXT" + "rowid", "INTEGER PRIMARY KEY AUTOINCREMENT" + ,VISID, "INTEGER" + ,GAME_NAME, "TEXT" ,NUM_MOVES, "INTEGER" ,TURN, "INTEGER" ,GIFLAGS, "INTEGER" @@ -200,6 +206,10 @@ public class DBHelper extends SQLiteOpenHelper { createGroupsTable( db ); case 15: moveToCurGames( db ); + case 16: + addSumColumn( db, VISID ); + setColumnsEqual( db, TABLE_NAME_SUM, VISID, "rowid" ); + makeAutoincrement( db, TABLE_NAME_SUM, s_summaryColsAndTypes ); // nothing yet break; default: @@ -280,4 +290,47 @@ public class DBHelper extends SQLiteOpenHelper { } cursor.close(); } + + private void makeAutoincrement( SQLiteDatabase db, String name, String[] data ) + { + db.beginTransaction(); + try { + String query; + String[] columnNames = DBUtils.getColumns( db, name ); + if ( null != columnNames ) { // no data means no need to copy + query = String.format( "ALTER table %s RENAME TO 'temp_%s'", + name, name ); + db.execSQL( query ); + } + createTable( db, name, data ); + + if ( null != columnNames ) { + ArrayList oldCols = + new ArrayList( Arrays.asList( columnNames ) ); + String[] newColNames = DBUtils.getColumns( db, name ); + ArrayList newCols = + new ArrayList( Arrays.asList( newColNames ) ); + oldCols.retainAll( newCols ); + + String cols = TextUtils.join( ",", oldCols ); + query = + String.format( "INSERT INTO %s (%s) SELECT %s from temp_%s", + name, cols, cols, name ); + db.execSQL( query ); + } + db.execSQL( String.format( "DROP table temp_%s", name ) ); + + db.setTransactionSuccessful(); + } finally { + db.endTransaction(); + } + } + + private void setColumnsEqual( SQLiteDatabase db, String table, + String dest, String src ) + { + String query = String.format( "UPDATE %s set %s = %s", table, + dest, src ); + db.execSQL( query ); + } } 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 30035bcba..d375a45c6 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -695,6 +695,7 @@ public class DBUtils { values.put( DBHelper.LASTPLAY_TIME, timestamp ); values.put( DBHelper.GROUPID, XWPrefs.getDefaultNewGameGroup( context ) ); + values.put( DBHelper.VISID, maxVISID( db ) ); long rowid = db.insert( DBHelper.TABLE_NAME_SUM, null, values ); @@ -791,6 +792,28 @@ public class DBUtils { } } + public static int getVisID( Context context, long rowid ) + { + int result = -1; + initDB( context ); + synchronized( s_dbHelper ) { + SQLiteDatabase db = s_dbHelper.getReadableDatabase(); + + String[] columns = { DBHelper.VISID }; + String selection = String.format( ROW_ID_FMT, rowid ); + Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, + selection, null, null, null, null ); + if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { + result = cursor.getInt( cursor + .getColumnIndex(DBHelper.VISID)); + } + cursor.close(); + db.close(); + } + + return result; + } + // Get either the file name or game name, preferring the latter. public static String getName( Context context, long rowid ) { @@ -1380,6 +1403,15 @@ public class DBUtils { return sdcardDB.exists(); } + public static String[] getColumns( SQLiteDatabase db, String name ) + { + String query = String.format( "SELECT * FROM %s LIMIT 1", name ); + Cursor cursor = db.rawQuery( query, null ); + String[] colNames = cursor.getColumnNames(); + cursor.close(); + return colNames; + } + private static void copyGameDB( Context context, boolean toSDCard ) { String name = DBHelper.getDBName(); @@ -1440,6 +1472,25 @@ public class DBUtils { } } } + + private static int maxVISID( SQLiteDatabase db ) + { + int result = 1; + String query = String.format( "SELECT max(%s) FROM %s", DBHelper.VISID, + DBHelper.TABLE_NAME_SUM ); + Cursor cursor = null; + try { + cursor = db.rawQuery( query, null ); + if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { + result = 1 + cursor.getInt( 0 ); + } + } finally { + if ( null != cursor ) { + cursor.close(); + } + } + return result; + } private static void notifyListeners( long rowid, boolean countChanged ) { 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 6605d5d3d..3a55b792f 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java @@ -227,7 +227,8 @@ public class GameUtils { { String result = DBUtils.getName( context, rowid ); if ( null == result || 0 == result.length() ) { - result = context.getString( R.string.gamef, rowid ); + int visID = DBUtils.getVisID( context, rowid ); + result = context.getString( R.string.gamef, visID ); } return result; }