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.
This commit is contained in:
Eric House 2013-01-28 06:51:39 -08:00
parent d1621c8d4b
commit 1546b15997
3 changed files with 108 additions and 3 deletions

View file

@ -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<String> oldCols =
new ArrayList<String>( Arrays.asList( columnNames ) );
String[] newColNames = DBUtils.getColumns( db, name );
ArrayList<String> newCols =
new ArrayList<String>( 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 );
}
}

View file

@ -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 )
{

View file

@ -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;
}