move query setup outside of synchronized blocks where possible

This commit is contained in:
Eric House 2013-11-16 06:30:34 -08:00
parent f303fa2653
commit deff81cfd1

View file

@ -124,9 +124,6 @@ public class DBUtils {
{ {
initDB( context ); initDB( context );
GameSummary summary = null; GameSummary summary = null;
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { ROW_ID, String[] columns = { ROW_ID,
DBHelper.NUM_MOVES, DBHelper.NUM_PLAYERS, DBHelper.NUM_MOVES, DBHelper.NUM_PLAYERS,
DBHelper.MISSINGPLYRS, DBHelper.MISSINGPLYRS,
@ -142,6 +139,8 @@ public class DBUtils {
}; };
String selection = String.format( ROW_ID_FMT, lock.getRowid() ); String selection = String.format( ROW_ID_FMT, lock.getRowid() );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
@ -277,14 +276,9 @@ public class DBUtils {
long rowid = lock.getRowid(); long rowid = lock.getRowid();
String selection = String.format( ROW_ID_FMT, rowid ); String selection = String.format( ROW_ID_FMT, rowid );
initDB( context ); ContentValues values = null;
synchronized( s_dbHelper ) { if ( null != summary ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase(); values = new ContentValues();
if ( null == summary ) {
db.delete( DBHelper.TABLE_NAME_SUM, selection, null );
} else {
ContentValues values = new ContentValues();
values.put( DBHelper.NUM_MOVES, summary.nMoves ); values.put( DBHelper.NUM_MOVES, summary.nMoves );
values.put( DBHelper.NUM_PLAYERS, summary.nPlayers ); values.put( DBHelper.NUM_PLAYERS, summary.nPlayers );
values.put( DBHelper.MISSINGPLYRS, summary.missingPlayers ); values.put( DBHelper.MISSINGPLYRS, summary.missingPlayers );
@ -330,7 +324,15 @@ public class DBUtils {
values.put( DBHelper.SERVERROLE, summary.serverRole.ordinal() ); values.put( DBHelper.SERVERROLE, summary.serverRole.ordinal() );
addThumb( summary.getThumbnail(), values ); addThumb( summary.getThumbnail(), values );
}
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
if ( null == summary ) {
db.delete( DBHelper.TABLE_NAME_SUM, selection, null );
} else {
long result = db.update( DBHelper.TABLE_NAME_SUM, long result = db.update( DBHelper.TABLE_NAME_SUM,
values, selection, null ); values, selection, null );
Assert.assertTrue( result >= 0 ); Assert.assertTrue( result >= 0 );
@ -344,13 +346,13 @@ public class DBUtils {
public static int countGamesUsingLang( Context context, int lang ) public static int countGamesUsingLang( Context context, int lang )
{ {
int result = 0; int result = 0;
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String selection = String.format( "%s = %d", DBHelper.DICTLANG, String selection = String.format( "%s = %d", DBHelper.DICTLANG,
lang ); lang );
// null for columns will return whole rows: bad // null for columns will return whole rows: bad
String[] columns = { DBHelper.DICTLANG }; String[] columns = { DBHelper.DICTLANG };
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
@ -364,9 +366,6 @@ public class DBUtils {
public static int countGamesUsingDict( Context context, String dict ) public static int countGamesUsingDict( Context context, String dict )
{ {
int result = 0; int result = 0;
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String pattern = String.format( "%%%s%s%s%%", String pattern = String.format( "%%%s%s%s%%",
DICTS_SEP, dict, DICTS_SEP ); DICTS_SEP, dict, DICTS_SEP );
String selection = String.format( "%s LIKE '%s'", String selection = String.format( "%s LIKE '%s'",
@ -374,6 +373,9 @@ public class DBUtils {
// null for columns will return whole rows: bad. But // null for columns will return whole rows: bad. But
// might as well make it an int for speed // might as well make it an int for speed
String[] columns = { DBHelper.DICTLANG }; String[] columns = { DBHelper.DICTLANG };
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
result = cursor.getCount(); result = cursor.getCount();
@ -405,11 +407,11 @@ public class DBUtils {
int dflt ) int dflt )
{ {
int result = dflt; int result = dflt;
String selection = String.format( ROW_ID_FMT, rowid );
String[] columns = { column };
initDB( context ); initDB( context );
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase(); SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String selection = String.format( ROW_ID_FMT, rowid );
String[] columns = { column };
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
@ -442,14 +444,13 @@ public class DBUtils {
Bitmap thumb ) Bitmap thumb )
{ {
if ( BuildConstants.THUMBNAIL_SUPPORTED ) { if ( BuildConstants.THUMBNAIL_SUPPORTED ) {
long rowid = lock.getRowid();
String selection = String.format( ROW_ID_FMT, rowid );
ContentValues values = new ContentValues();
addThumb( thumb, values );
initDB( context ); initDB( context );
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase(); SQLiteDatabase db = s_dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
long rowid = lock.getRowid();
String selection = String.format( ROW_ID_FMT, rowid );
addThumb( thumb, values );
long result = db.update( DBHelper.TABLE_NAME_SUM, long result = db.update( DBHelper.TABLE_NAME_SUM,
values, selection, null ); values, selection, null );
@ -465,11 +466,11 @@ public class DBUtils {
public static void clearThumbnails( Context context ) public static void clearThumbnails( Context context )
{ {
if ( BuildConstants.THUMBNAIL_SUPPORTED ) { if ( BuildConstants.THUMBNAIL_SUPPORTED ) {
ContentValues values = new ContentValues();
values.putNull( DBHelper.THUMBNAIL );
initDB( context ); initDB( context );
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase(); SQLiteDatabase db = s_dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.putNull( DBHelper.THUMBNAIL );
long result = db.update( DBHelper.TABLE_NAME_SUM, long result = db.update( DBHelper.TABLE_NAME_SUM,
values, null, null ); values, null, null );
db.close(); db.close();
@ -482,11 +483,11 @@ public class DBUtils {
public static String getRelayID( Context context, long rowid ) public static String getRelayID( Context context, long rowid )
{ {
String result = null; String result = null;
String[] columns = { DBHelper.RELAYID };
String selection = String.format( ROW_ID_FMT, rowid );
initDB( context ); initDB( context );
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase(); SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { DBHelper.RELAYID };
String selection = String.format( ROW_ID_FMT, rowid );
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
@ -502,11 +503,11 @@ public class DBUtils {
public static long[] getRowIDsFor( Context context, String relayID ) public static long[] getRowIDsFor( Context context, String relayID )
{ {
long[] result = null; long[] result = null;
String[] columns = { ROW_ID };
String selection = DBHelper.RELAYID + "='" + relayID + "'";
initDB( context ); initDB( context );
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase(); SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { ROW_ID };
String selection = DBHelper.RELAYID + "='" + relayID + "'";
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
result = new long[cursor.getCount()]; result = new long[cursor.getCount()];
@ -522,11 +523,11 @@ public class DBUtils {
public static long[] getRowIDsFor( Context context, int gameID ) public static long[] getRowIDsFor( Context context, int gameID )
{ {
long[] result = null; long[] result = null;
String[] columns = { ROW_ID };
String selection = String.format( DBHelper.GAMEID + "=%d", gameID );
initDB( context ); initDB( context );
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase(); SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { ROW_ID };
String selection = String.format( DBHelper.GAMEID + "=%d", gameID );
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
result = new long[cursor.getCount()]; result = new long[cursor.getCount()];
@ -546,11 +547,11 @@ public class DBUtils {
public static boolean haveGame( Context context, long rowid ) public static boolean haveGame( Context context, long rowid )
{ {
boolean result = false; boolean result = false;
String[] columns = { ROW_ID };
String selection = String.format( ROW_ID + "=%d", rowid );
initDB( context ); initDB( context );
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase(); SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { ROW_ID };
String selection = String.format( ROW_ID + "=%d", rowid );
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
Assert.assertTrue( 1 >= cursor.getCount() ); Assert.assertTrue( 1 >= cursor.getCount() );
@ -616,9 +617,6 @@ public class DBUtils {
NetLaunchInfo nli ) NetLaunchInfo nli )
{ {
Date result = null; Date result = null;
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { DBHelper.CREATE_TIME }; String[] columns = { DBHelper.CREATE_TIME };
String selection = String selection =
String.format( "%s='%s' AND %s='%s' AND %s=%d AND %s=%d", String.format( "%s='%s' AND %s='%s' AND %s=%d AND %s=%d",
@ -626,6 +624,9 @@ public class DBUtils {
DBHelper.INVITEID, nli.inviteID, DBHelper.INVITEID, nli.inviteID,
DBHelper.DICTLANG, nli.lang, DBHelper.DICTLANG, nli.lang,
DBHelper.NUM_PLAYERS, nli.nPlayersT ); DBHelper.NUM_PLAYERS, nli.nPlayersT );
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, selection, null, null, null,
DBHelper.CREATE_TIME + " DESC" ); // order by DBHelper.CREATE_TIME + " DESC" ); // order by
@ -652,13 +653,13 @@ public class DBUtils {
public static String[] getRelayIDs( Context context, long[][] rowIDs ) public static String[] getRelayIDs( Context context, long[][] rowIDs )
{ {
String[] result = null; String[] result = null;
initDB( context );
ArrayList<String> ids = new ArrayList<String>();
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { ROW_ID, DBHelper.RELAYID }; String[] columns = { ROW_ID, DBHelper.RELAYID };
String selection = DBHelper.RELAYID + " NOT null"; String selection = DBHelper.RELAYID + " NOT null";
ArrayList<String> ids = new ArrayList<String>();
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
@ -688,14 +689,14 @@ public class DBUtils {
public static void addDeceased( Context context, String relayID, public static void addDeceased( Context context, String relayID,
int seed ) int seed )
{ {
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put( DBHelper.RELAYID, relayID ); values.put( DBHelper.RELAYID, relayID );
values.put( DBHelper.SEED, seed ); values.put( DBHelper.SEED, seed );
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
try { try {
long result = db.replaceOrThrow( DBHelper.TABLE_NAME_OBITS, long result = db.replaceOrThrow( DBHelper.TABLE_NAME_OBITS,
"", values ); "", values );
@ -710,11 +711,11 @@ public class DBUtils {
{ {
Obit[] result = null; Obit[] result = null;
ArrayList<Obit> al = new ArrayList<Obit>(); ArrayList<Obit> al = new ArrayList<Obit>();
String[] columns = { DBHelper.RELAYID, DBHelper.SEED };
initDB( context ); initDB( context );
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase(); SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { DBHelper.RELAYID, DBHelper.SEED };
Cursor cursor = db.query( DBHelper.TABLE_NAME_OBITS, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_OBITS, columns,
null, null, null, null, null ); null, null, null, null, null );
if ( 0 < cursor.getCount() ) { if ( 0 < cursor.getCount() ) {
@ -761,10 +762,6 @@ public class DBUtils {
Assert.assertTrue( GROUPID_UNSPEC != groupID ); Assert.assertTrue( GROUPID_UNSPEC != groupID );
GameLock lock = null; GameLock lock = null;
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put( DBHelper.SNAPSHOT, bytes ); values.put( DBHelper.SNAPSHOT, bytes );
@ -772,9 +769,14 @@ public class DBUtils {
values.put( DBHelper.CREATE_TIME, timestamp ); values.put( DBHelper.CREATE_TIME, timestamp );
values.put( DBHelper.LASTPLAY_TIME, timestamp ); values.put( DBHelper.LASTPLAY_TIME, timestamp );
values.put( DBHelper.GROUPID, groupID ); values.put( DBHelper.GROUPID, groupID );
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
values.put( DBHelper.VISID, maxVISID( db ) ); values.put( DBHelper.VISID, maxVISID( db ) );
long rowid = db.insert( DBHelper.TABLE_NAME_SUM, null, values ); long rowid = db.insert( DBHelper.TABLE_NAME_SUM, null, values );
db.close();
setCached( rowid, null ); // force reread setCached( rowid, null ); // force reread
@ -816,12 +818,12 @@ public class DBUtils {
Assert.assertTrue( ROWID_NOTFOUND != rowid ); Assert.assertTrue( ROWID_NOTFOUND != rowid );
byte[] result = getCached( rowid ); byte[] result = getCached( rowid );
if ( null == result ) { if ( null == result ) {
String[] columns = { DBHelper.SNAPSHOT };
String selection = String.format( ROW_ID_FMT, rowid );
initDB( context ); initDB( context );
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase(); SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { DBHelper.SNAPSHOT };
String selection = String.format( ROW_ID_FMT, rowid );
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
@ -853,10 +855,10 @@ public class DBUtils {
public static void deleteGame( Context context, GameLock lock ) public static void deleteGame( Context context, GameLock lock )
{ {
Assert.assertTrue( lock.canWrite() ); Assert.assertTrue( lock.canWrite() );
String selection = String.format( ROW_ID_FMT, lock.getRowid() );
initDB( context ); initDB( context );
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase(); SQLiteDatabase db = s_dbHelper.getWritableDatabase();
String selection = String.format( ROW_ID_FMT, lock.getRowid() );
db.delete( DBHelper.TABLE_NAME_SUM, selection, null ); db.delete( DBHelper.TABLE_NAME_SUM, selection, null );
db.close(); db.close();
} }
@ -866,12 +868,12 @@ public class DBUtils {
public static int getVisID( Context context, long rowid ) public static int getVisID( Context context, long rowid )
{ {
int result = ROWID_NOTFOUND; int result = ROWID_NOTFOUND;
String[] columns = { DBHelper.VISID };
String selection = String.format( ROW_ID_FMT, rowid );
initDB( context ); initDB( context );
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase(); 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, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
@ -889,12 +891,12 @@ public class DBUtils {
public static String getName( Context context, long rowid ) public static String getName( Context context, long rowid )
{ {
String result = null; String result = null;
String[] columns = { DBHelper.GAME_NAME };
String selection = String.format( ROW_ID_FMT, rowid );
initDB( context ); initDB( context );
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase(); SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { DBHelper.GAME_NAME };
String selection = String.format( ROW_ID_FMT, rowid );
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
@ -1097,9 +1099,9 @@ public class DBUtils {
initDB( context ); initDB( context );
String[] columns = { ROW_ID }; String[] columns = { ROW_ID };
String selection = String.format( "%s=%d", DBHelper.GROUPID, groupID ); String selection = String.format( "%s=%d", DBHelper.GROUPID, groupID );
String orderBy = DBHelper.CREATE_TIME + " DESC";
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase(); SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String orderBy = DBHelper.CREATE_TIME + " DESC";
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, // selection selection, // selection
null, // args null, // args
@ -1191,17 +1193,17 @@ public class DBUtils {
public static void deleteGroup( Context context, long groupid ) public static void deleteGroup( Context context, long groupid )
{ {
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
// Nuke games having this group id // Nuke games having this group id
String selection = String selection =
String.format( "%s=%d", DBHelper.GROUPID, groupid ); String.format( "%s=%d", DBHelper.GROUPID, groupid );
db.delete( DBHelper.TABLE_NAME_SUM, selection, null );
// And nuke the group record itself // And nuke the group record itself
selection = String.format( ROW_ID_FMT, groupid ); selection = String.format( ROW_ID_FMT, groupid );
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
db.delete( DBHelper.TABLE_NAME_SUM, selection, null );
db.delete( DBHelper.TABLE_NAME_GROUPS, selection, null ); db.delete( DBHelper.TABLE_NAME_GROUPS, selection, null );
db.close(); db.close();
@ -1240,12 +1242,12 @@ public class DBUtils {
{ {
String result = null; String result = null;
if ( BuildConstants.CHAT_SUPPORTED ) { if ( BuildConstants.CHAT_SUPPORTED ) {
String[] columns = { DBHelper.CHAT_HISTORY };
String selection = String.format( ROW_ID_FMT, rowid );
initDB( context ); initDB( context );
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase(); SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { DBHelper.CHAT_HISTORY };
String selection = String.format( ROW_ID_FMT, rowid );
Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_SUM, columns,
selection, null, null, null, null ); selection, null, null, null, null );
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
@ -1341,15 +1343,15 @@ public class DBUtils {
{ {
Assert.assertTrue( DictLoc.UNKNOWN != loc ); Assert.assertTrue( DictLoc.UNKNOWN != loc );
DictBrowseState result = null; DictBrowseState result = null;
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { DBHelper.ITERPOS, DBHelper.ITERTOP, String[] columns = { DBHelper.ITERPOS, DBHelper.ITERTOP,
DBHelper.ITERMIN, DBHelper.ITERMAX, DBHelper.ITERMIN, DBHelper.ITERMAX,
DBHelper.WORDCOUNTS, DBHelper.ITERPREFIX }; DBHelper.WORDCOUNTS, DBHelper.ITERPREFIX };
String selection = String selection =
String.format( NAMELOC_FMT, DBHelper.DICTNAME, String.format( NAMELOC_FMT, DBHelper.DICTNAME,
name, DBHelper.LOC, loc.ordinal() ); name, DBHelper.LOC, loc.ordinal() );
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
Cursor cursor = db.query( DBHelper.TABLE_NAME_DICTBROWSE, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_DICTBROWSE, columns,
selection, null, null, null, null ); selection, null, null, null, null );
if ( 1 >= cursor.getCount() && cursor.moveToFirst() ) { if ( 1 >= cursor.getCount() && cursor.moveToFirst() ) {
@ -1388,9 +1390,6 @@ public class DBUtils {
DictLoc loc, DictBrowseState state ) DictLoc loc, DictBrowseState state )
{ {
Assert.assertTrue( DictLoc.UNKNOWN != loc ); Assert.assertTrue( DictLoc.UNKNOWN != loc );
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
String selection = String selection =
String.format( NAMELOC_FMT, DBHelper.DICTNAME, String.format( NAMELOC_FMT, DBHelper.DICTNAME,
name, DBHelper.LOC, loc.ordinal() ); name, DBHelper.LOC, loc.ordinal() );
@ -1407,6 +1406,10 @@ public class DBUtils {
} }
values.put( DBHelper.WORDCOUNTS, TextUtils.join( ":", nums ) ); values.put( DBHelper.WORDCOUNTS, TextUtils.join( ":", nums ) );
} }
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
int result = db.update( DBHelper.TABLE_NAME_DICTBROWSE, int result = db.update( DBHelper.TABLE_NAME_DICTBROWSE,
values, selection, null ); values, selection, null );
if ( 0 == result ) { if ( 0 == result ) {
@ -1429,12 +1432,12 @@ public class DBUtils {
// Called from jni // Called from jni
public static void dictsSetMD5Sum( Context context, String name, String sum ) public static void dictsSetMD5Sum( Context context, String name, String sum )
{ {
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
String selection = String.format( NAME_FMT, DBHelper.DICTNAME, name ); String selection = String.format( NAME_FMT, DBHelper.DICTNAME, name );
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put( DBHelper.MD5SUM, sum ); values.put( DBHelper.MD5SUM, sum );
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
int result = db.update( DBHelper.TABLE_NAME_DICTINFO, int result = db.update( DBHelper.TABLE_NAME_DICTINFO,
values, selection, null ); values, selection, null );
if ( 0 == result ) { if ( 0 == result ) {
@ -1448,14 +1451,14 @@ public class DBUtils {
public static DictInfo dictsGetInfo( Context context, String name ) public static DictInfo dictsGetInfo( Context context, String name )
{ {
DictInfo result = null; DictInfo result = null;
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
String[] columns = { DBHelper.LANGCODE, String[] columns = { DBHelper.LANGCODE,
DBHelper.WORDCOUNT, DBHelper.WORDCOUNT,
DBHelper.MD5SUM, DBHelper.MD5SUM,
DBHelper.LOC }; DBHelper.LOC };
String selection = String.format( NAME_FMT, DBHelper.DICTNAME, name ); String selection = String.format( NAME_FMT, DBHelper.DICTNAME, name );
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getReadableDatabase();
Cursor cursor = db.query( DBHelper.TABLE_NAME_DICTINFO, columns, Cursor cursor = db.query( DBHelper.TABLE_NAME_DICTINFO, columns,
selection, null, null, null, null ); selection, null, null, null, null );
if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { if ( 1 == cursor.getCount() && cursor.moveToFirst() ) {
@ -1477,9 +1480,6 @@ public class DBUtils {
public static void dictsSetInfo( Context context, DictUtils.DictAndLoc dal, public static void dictsSetInfo( Context context, DictUtils.DictAndLoc dal,
DictInfo info ) DictInfo info )
{ {
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
String selection = String selection =
String.format( NAME_FMT, DBHelper.DICTNAME, dal.name ); String.format( NAME_FMT, DBHelper.DICTNAME, dal.name );
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
@ -1489,6 +1489,9 @@ public class DBUtils {
values.put( DBHelper.MD5SUM, info.md5Sum ); values.put( DBHelper.MD5SUM, info.md5Sum );
values.put( DBHelper.LOC, dal.loc.ordinal() ); values.put( DBHelper.LOC, dal.loc.ordinal() );
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
int result = db.update( DBHelper.TABLE_NAME_DICTINFO, int result = db.update( DBHelper.TABLE_NAME_DICTINFO,
values, selection, null ); values, selection, null );
if ( 0 == result ) { if ( 0 == result ) {
@ -1502,16 +1505,17 @@ public class DBUtils {
public static void dictsMoveInfo( Context context, String name, public static void dictsMoveInfo( Context context, String name,
DictLoc fromLoc, DictLoc toLoc ) DictLoc fromLoc, DictLoc toLoc )
{ {
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
String selection = String selection =
String.format( NAMELOC_FMT, DBHelper.DICTNAME, String.format( NAMELOC_FMT, DBHelper.DICTNAME,
name, DBHelper.LOC, fromLoc.ordinal() ); name, DBHelper.LOC, fromLoc.ordinal() );
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put( DBHelper.LOC, toLoc.ordinal() ); values.put( DBHelper.LOC, toLoc.ordinal() );
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
db.update( DBHelper.TABLE_NAME_DICTINFO, values, selection, null ); db.update( DBHelper.TABLE_NAME_DICTINFO, values, selection, null );
db.update( DBHelper.TABLE_NAME_DICTBROWSE, values, selection, null); db.update( DBHelper.TABLE_NAME_DICTBROWSE, values, selection, null );
db.close(); db.close();
} }
} }
@ -1519,12 +1523,13 @@ public class DBUtils {
public static void dictsRemoveInfo( Context context, public static void dictsRemoveInfo( Context context,
DictUtils.DictAndLoc dal ) DictUtils.DictAndLoc dal )
{ {
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
String selection = String selection =
String.format( NAMELOC_FMT, DBHelper.DICTNAME, String.format( NAMELOC_FMT, DBHelper.DICTNAME,
dal.name, DBHelper.LOC, dal.loc.ordinal() ); dal.name, DBHelper.LOC, dal.loc.ordinal() );
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
db.delete( DBHelper.TABLE_NAME_DICTINFO, selection, null ); db.delete( DBHelper.TABLE_NAME_DICTINFO, selection, null );
db.delete( DBHelper.TABLE_NAME_DICTBROWSE, selection, null ); db.delete( DBHelper.TABLE_NAME_DICTBROWSE, selection, null );
db.close(); db.close();
@ -1595,12 +1600,12 @@ public class DBUtils {
private static void updateRow( Context context, String table, private static void updateRow( Context context, String table,
long rowid, ContentValues values ) long rowid, ContentValues values )
{ {
String selection = String.format( ROW_ID_FMT, rowid );
initDB( context ); initDB( context );
synchronized( s_dbHelper ) { synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase(); SQLiteDatabase db = s_dbHelper.getWritableDatabase();
String selection = String.format( ROW_ID_FMT, rowid );
int result = db.update( table, values, selection, null ); int result = db.update( table, values, selection, null );
db.close(); db.close();
if ( 0 == result ) { if ( 0 == result ) {