diff --git a/src/org/thoughtcrime/securesms/database/RecipientDatabase.java b/src/org/thoughtcrime/securesms/database/RecipientDatabase.java index 7b44001f86..c091a172aa 100644 --- a/src/org/thoughtcrime/securesms/database/RecipientDatabase.java +++ b/src/org/thoughtcrime/securesms/database/RecipientDatabase.java @@ -179,94 +179,28 @@ public class RecipientDatabase extends Database { super(context, databaseHelper); } - public RecipientId getOrInsertFromE164(@NonNull String e164) { - if (TextUtils.isEmpty(e164)) { - throw new AssertionError("Phone number cannot be empty."); - } + public @NonNull Optional getByE164(@NonNull String e164) { + return getByColumn(PHONE, e164); + } - SQLiteDatabase db = databaseHelper.getWritableDatabase(); - String query = PHONE + " = ?"; - String[] args = new String[] { e164 }; + public @NonNull Optional getByEmail(@NonNull String email) { + return getByColumn(EMAIL, email); + } - db.beginTransaction(); + public @NonNull Optional getByGroupId(@NonNull String groupId) { + return getByColumn(GROUP_ID, groupId); + } - try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) { - if (cursor != null && cursor.moveToFirst()) { - db.setTransactionSuccessful(); - return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID))); - } else { - ContentValues values = new ContentValues(); - values.put(PHONE, e164); - - long id = db.insertOrThrow(TABLE_NAME, null, values); - if (id < 0) throw new AssertionError("Failed to insert recipient!"); - - db.setTransactionSuccessful(); - return RecipientId.from(id); - } - } finally { - db.endTransaction(); - } + public @NonNull RecipientId getOrInsertFromE164(@NonNull String e164) { + return getOrInsertByColumn(PHONE, e164); } public RecipientId getOrInsertFromEmail(@NonNull String email) { - if (TextUtils.isEmpty(email)) { - throw new AssertionError("Email cannot be empty."); - } - - SQLiteDatabase db = databaseHelper.getWritableDatabase(); - String query = EMAIL + " = ?"; - String[] args = new String[] { email }; - - db.beginTransaction(); - - try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) { - if (cursor != null && cursor.moveToFirst()) { - db.setTransactionSuccessful(); - return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID))); - } else { - ContentValues values = new ContentValues(); - values.put(EMAIL, email); - - long id = db.insertOrThrow(TABLE_NAME, null, values); - if (id < 0) throw new AssertionError("Failed to insert recipient!"); - - db.setTransactionSuccessful(); - return RecipientId.from(id); - } - } finally { - db.endTransaction(); - } + return getOrInsertByColumn(EMAIL, email); } public RecipientId getOrInsertFromGroupId(@NonNull String groupId) { - if (TextUtils.isEmpty(groupId)) { - throw new AssertionError("GroupId cannot be empty."); - } - - SQLiteDatabase db = databaseHelper.getWritableDatabase(); - String query = GROUP_ID + " = ?"; - String[] args = new String[] { groupId }; - - db.beginTransaction(); - - try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) { - if (cursor != null && cursor.moveToFirst()) { - db.setTransactionSuccessful(); - return RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID))); - } else { - ContentValues values = new ContentValues(); - values.put(GROUP_ID, groupId); - - long id = db.insertOrThrow(TABLE_NAME, null, values); - if (id < 0) throw new AssertionError("Failed to insert recipient!"); - - db.setTransactionSuccessful(); - return RecipientId.from(id); - } - } finally { - db.endTransaction(); - } + return getOrInsertByColumn(GROUP_ID, groupId); } public Cursor getBlocked() { @@ -653,6 +587,49 @@ public class RecipientDatabase extends Database { return database.update(TABLE_NAME, contentValues, ID + " = ?", new String[] { id.serialize() }); } + private @NonNull Optional getByColumn(@NonNull String column, String value) { + SQLiteDatabase db = databaseHelper.getWritableDatabase(); + String query = column + " = ?"; + String[] args = new String[] { value }; + + try (Cursor cursor = db.query(TABLE_NAME, ID_PROJECTION, query, args, null, null, null)) { + if (cursor != null && cursor.moveToFirst()) { + return Optional.of(RecipientId.from(cursor.getLong(cursor.getColumnIndexOrThrow(ID)))); + } else { + return Optional.absent(); + } + } + } + + private @NonNull RecipientId getOrInsertByColumn(@NonNull String column, String value) { + if (TextUtils.isEmpty(value)) { + throw new AssertionError(column + " cannot be empty."); + } + + Optional existing = getByColumn(column, value); + + if (existing.isPresent()) { + return existing.get(); + } else { + ContentValues values = new ContentValues(); + values.put(column, value); + + long id = databaseHelper.getWritableDatabase().insert(TABLE_NAME, null, values); + + if (id < 0) { + existing = getByColumn(column, value); + + if (existing.isPresent()) { + return existing.get(); + } else { + throw new AssertionError("Failed to insert recipient!"); + } + } + + return RecipientId.from(id); + } + } + public class BulkOperationsHandle { private final SQLiteDatabase database;