Java Code Examples for net.sqlcipher.database.SQLiteDatabase#setTransactionSuccessful()

The following examples show how to use net.sqlcipher.database.SQLiteDatabase#setTransactionSuccessful() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MmsDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void markExpireStarted(Collection<Long> ids, long startedAtTimestamp) {
  SQLiteDatabase db       = databaseHelper.getWritableDatabase();
  long           threadId = -1;

  db.beginTransaction();
  try {
    for (long id : ids) {
      ContentValues contentValues = new ContentValues();
      contentValues.put(EXPIRE_STARTED, startedAtTimestamp);

      db.update(TABLE_NAME, contentValues, ID_WHERE, new String[]{String.valueOf(id)});

      if (threadId < 0) {
        threadId = getThreadIdForMessage(id);
      }
    }

    db.setTransactionSuccessful();
  } finally {
    db.endTransaction();
  }

  DatabaseFactory.getThreadDatabase(context).update(threadId, false);
  notifyConversationListeners(threadId);
}
 
Example 2
Source File: UserDatabaseUpgrader.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
private boolean upgradeThirteenFourteen(SQLiteDatabase db) {
    db.beginTransaction();
    try {
        SqlStorage<FormRecordV2> formRecordSqlStorage = new SqlStorage<>(
                FormRecord.STORAGE_KEY,
                FormRecordV2.class,
                new ConcreteAndroidDbHelper(c, db));

        // Re-store all the form records, forcing new date representation
        // to be used.  Must happen proactively because the date parsing
        // code was updated to handle new representation
        for (FormRecordV2 formRecord : formRecordSqlStorage) {
            formRecordSqlStorage.write(formRecord);
        }

        db.setTransactionSuccessful();
        return true;
    } finally {
        db.endTransaction();
    }
}
 
Example 3
Source File: ThreadDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public void setArchived(@NonNull Map<RecipientId, Boolean> status) {
  SQLiteDatabase db    = databaseHelper.getReadableDatabase();

  db.beginTransaction();
  try {
    String query = RECIPIENT_ID + " = ?";

    for (Map.Entry<RecipientId, Boolean> entry : status.entrySet()) {
      ContentValues values = new ContentValues(1);
      values.put(ARCHIVED, entry.getValue() ? "1" : "0");
      db.update(TABLE_NAME, values, query, new String[] { entry.getKey().serialize() });
    }

    db.setTransactionSuccessful();
  } finally {
    db.endTransaction();
    notifyConversationListListeners();
  }
}
 
Example 4
Source File: UserDatabaseUpgrader.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
/**
 * Depcrecate the old AUser object so that both platforms are using the User object
 * to represents users
 */
private boolean upgradeSevenEight(SQLiteDatabase db) {
    long start = System.currentTimeMillis();
    db.beginTransaction();
    try {
        SqlStorage<Persistable> userStorage = new SqlStorage<>(AUser.STORAGE_KEY, AUser.class, new ConcreteAndroidDbHelper(c, db));
        SqlStorageIterator<Persistable> iterator = userStorage.iterate();
        while (iterator.hasMore()) {
            AUser oldUser = (AUser)iterator.next();
            User newUser = oldUser.toNewUser();
            userStorage.write(newUser);
        }
        db.setTransactionSuccessful();
        return true;
    } finally {
        db.endTransaction();
        Log.d(TAG, "Case model update complete in " + (System.currentTimeMillis() - start) + "ms");
    }
}
 
Example 5
Source File: StickerDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private void deleteStickersInPackExceptCover(@NonNull SQLiteDatabase db, @NonNull String packId) {
  String   selection = PACK_ID + " = ? AND " + COVER + " = ?";
  String[] args      = new String[] { packId, "0" };

  db.beginTransaction();

  try {
    try (Cursor cursor = db.query(TABLE_NAME, null, selection, args, null, null, null)) {
      while (cursor != null && cursor.moveToNext()) {
        long    rowId     = cursor.getLong(cursor.getColumnIndexOrThrow(_ID));
        String  filePath  = cursor.getString(cursor.getColumnIndexOrThrow(FILE_PATH));

        deleteSticker(db, rowId, filePath);
      }
    }

    db.setTransactionSuccessful();
  } finally {
    db.endTransaction();
  }
}
 
Example 6
Source File: IndexedFixturePathUtils.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
public static void insertIndexedFixturePathBases(SQLiteDatabase db, String fixtureName,
                                                 String baseName, String childName, TreeElement attrs) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(INDEXED_FIXTURE_PATHS_COL_BASE, baseName);
    contentValues.put(INDEXED_FIXTURE_PATHS_COL_CHILD, childName);
    contentValues.put(INDEXED_FIXTURE_PATHS_COL_NAME, fixtureName);
    contentValues.put(INDEXED_FIXTURE_PATHS_COL_ATTRIBUTES, SerializationUtil.serialize(attrs));

    db.beginTransaction();
    try {
        long ret = db.insertWithOnConflict(
                INDEXED_FIXTURE_PATHS_TABLE,
                INDEXED_FIXTURE_PATHS_COL_BASE,
                contentValues,
                SQLiteDatabase.CONFLICT_REPLACE);

        if (ret > Integer.MAX_VALUE) {
            throw new RuntimeException("Waaaaaaaaaay too many values");
        }

        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}
 
Example 7
Source File: UserDatabaseUpgrader.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
/**
 * Add index on owner ID to case db
 */
private boolean upgradeSeventeenEighteen(SQLiteDatabase db) {
    db.beginTransaction();
    try {
        db.execSQL(DbUtil.addColumnToTable(
                ACase.STORAGE_KEY,
                "owner_id",
                "TEXT"));

        SqlStorage<ACase> caseStorage = new SqlStorage<>(ACase.STORAGE_KEY, ACasePreV24Model.class,
                new ConcreteAndroidDbHelper(c, db));
        updateModels(caseStorage);

        db.execSQL(DatabaseIndexingUtils.indexOnTableCommand(
                "case_owner_id_index", "AndroidCase", "owner_id"));
        db.setTransactionSuccessful();
        return true;
    } finally {
        db.endTransaction();
    }
}
 
Example 8
Source File: GroupReceiptDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public void setUnidentified(Collection<Pair<RecipientId, Boolean>> results, long mmsId) {
  SQLiteDatabase db  = databaseHelper.getWritableDatabase();

  db.beginTransaction();
  try {
    String query = MMS_ID + " = ? AND " + RECIPIENT_ID + " = ?";

    for (Pair<RecipientId, Boolean> result : results) {
      ContentValues values = new ContentValues(1);
      values.put(UNIDENTIFIED, result.second() ? 1 : 0);

      db.update(TABLE_NAME, values, query, new String[]{ String.valueOf(mmsId), result.first().serialize()});
    }

    db.setTransactionSuccessful();
  } finally {
    db.endTransaction();
  }
}
 
Example 9
Source File: MegaphoneDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public void insert(@NonNull Collection<Event> events) {
  SQLiteDatabase db = databaseHelper.getWritableDatabase();

  db.beginTransaction();
  try {
    for (Event event : events) {
      ContentValues values = new ContentValues();
      values.put(EVENT, event.getKey());

      db.insertWithOnConflict(TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_IGNORE);
    }

    db.setTransactionSuccessful();
  } finally {
    db.endTransaction();
  }
}
 
Example 10
Source File: StickerDatabase.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
public void updatePackOrder(@NonNull List<StickerPackRecord> packsInOrder) {
  SQLiteDatabase db = databaseHelper.getWritableDatabase();

  db.beginTransaction();
  try {
    String selection = PACK_ID + " = ? AND " + COVER + " = ?";

    for (int i = 0; i < packsInOrder.size(); i++) {
      String[]      args   = new String[]{ packsInOrder.get(i).getPackId(), "1" };
      ContentValues values = new ContentValues();

      values.put(PACK_ORDER, i);

      db.update(TABLE_NAME, values, selection, args);
    }

    db.setTransactionSuccessful();
    notifyStickerPackListeners();
  } finally {
    db.endTransaction();
  }
}
 
Example 11
Source File: SmsDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private List<MarkedMessageInfo> setMessagesRead(String where, String[] arguments) {
  SQLiteDatabase          database  = databaseHelper.getWritableDatabase();
  List<MarkedMessageInfo> results   = new LinkedList<>();
  Cursor                  cursor    = null;

  database.beginTransaction();
  try {
    cursor = database.query(TABLE_NAME, new String[] {ID, RECIPIENT_ID, DATE_SENT, TYPE, EXPIRES_IN, EXPIRE_STARTED, THREAD_ID}, where, arguments, null, null, null);

    while (cursor != null && cursor.moveToNext()) {
      if (Types.isSecureType(cursor.getLong(cursor.getColumnIndex(TYPE)))) {
        long           threadId       = cursor.getLong(cursor.getColumnIndex(THREAD_ID));
        RecipientId    recipientId    = RecipientId.from(cursor.getLong(cursor.getColumnIndex(RECIPIENT_ID)));
        long           dateSent       = cursor.getLong(cursor.getColumnIndex(DATE_SENT));
        long           messageId      = cursor.getLong(cursor.getColumnIndex(ID));
        long           expiresIn      = cursor.getLong(cursor.getColumnIndex(EXPIRES_IN));
        long           expireStarted  = cursor.getLong(cursor.getColumnIndex(EXPIRE_STARTED));
        SyncMessageId  syncMessageId  = new SyncMessageId(recipientId, dateSent);
        ExpirationInfo expirationInfo = new ExpirationInfo(messageId, expiresIn, expireStarted, false);

        results.add(new MarkedMessageInfo(threadId, syncMessageId, expirationInfo));
      }
    }

    ContentValues contentValues = new ContentValues();
    contentValues.put(READ, 1);

    database.update(TABLE_NAME, contentValues, where, arguments);
    database.setTransactionSuccessful();
  } finally {
    if (cursor != null) cursor.close();
    database.endTransaction();
  }

  return results;
}
 
Example 12
Source File: UserDatabaseUpgrader.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
private boolean upgradeElevenTwelve(SQLiteDatabase db) {
    db.beginTransaction();
    try {
        db.execSQL("DROP TABLE IF EXISTS " + GeocodeCacheModel.STORAGE_KEY);
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
    return true;
}
 
Example 13
Source File: AppDatabaseUpgrader.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
private boolean upgradeTwelveThirteen(SQLiteDatabase db) {
    db.beginTransaction();
    try {
        db.execSQL(DbUtil.addColumnToTable(
                FormDefRecord.STORAGE_KEY,
                FormDefRecord.META_RESOURCE_VERSION,
                "INTEGER"));

        SqlStorage<FormDefRecordV12> oldFormDefRecordStorage = new SqlStorage<>(
                FormDefRecord.STORAGE_KEY,
                FormDefRecordV12.class,
                new ConcreteAndroidDbHelper(context, db));

        SqlStorage<FormDefRecord> formDefRecordStorage = new SqlStorage<>(
                FormDefRecord.STORAGE_KEY,
                FormDefRecord.class,
                new ConcreteAndroidDbHelper(context, db));

        for (FormDefRecordV12 oldFormDefRecord : oldFormDefRecordStorage) {
            FormDefRecord formDefRecord = new FormDefRecord(oldFormDefRecord);
            formDefRecordStorage.update(oldFormDefRecord.getID(), formDefRecord);
        }

        db.setTransactionSuccessful();
        return true;
    } finally {
        db.endTransaction();
    }
}
 
Example 14
Source File: DatabaseUserOpenHelper.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
public static void dropTable(SQLiteDatabase database,
                             String tableName) {
    database.beginTransaction();
    try {
        database.execSQL("DROP TABLE IF EXISTS '" + tableName + "'");
        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }
}
 
Example 15
Source File: StickerDatabase.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public void deleteOrphanedPacks() {
  SQLiteDatabase db    = databaseHelper.getWritableDatabase();
  String         query = "SELECT " + PACK_ID + " FROM " + TABLE_NAME + " WHERE " + INSTALLED + " = ? AND " +
                         PACK_ID + " NOT IN (" +
                           "SELECT DISTINCT " + AttachmentDatabase.STICKER_PACK_ID + " FROM " + AttachmentDatabase.TABLE_NAME + " " +
                           "WHERE " + AttachmentDatabase.STICKER_PACK_ID + " NOT NULL" +
                         ")";
  String[]      args = new String[] { "0" };

  db.beginTransaction();

  try {
    boolean performedDelete = false;

    try (Cursor cursor = db.rawQuery(query, args)) {
      while (cursor != null && cursor.moveToNext()) {
        String packId = cursor.getString(cursor.getColumnIndexOrThrow(PACK_ID));

        if (!BlessedPacks.contains(packId)) {
          deletePack(db, packId);
          performedDelete = true;
        }
      }
    }

    db.setTransactionSuccessful();

    if (performedDelete) {
      notifyStickerPackListeners();
      notifyStickerListeners();
    }
  } finally {
    db.endTransaction();
  }
}
 
Example 16
Source File: DatabaseGlobalOpenHelper.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(SQLiteDatabase database) {
    database.beginTransaction();
    try {
        TableBuilder builder = new TableBuilder(ApplicationRecord.class);
        database.execSQL(builder.getTableCreateString());
        
        builder = new TableBuilder(AndroidSharedKeyRecord.class);
        database.execSQL(builder.getTableCreateString());

        builder = new TableBuilder(AndroidLogEntry.STORAGE_KEY);
        builder.addData(new AndroidLogEntry());
        database.execSQL(builder.getTableCreateString());

        builder = new TableBuilder(ForceCloseLogEntry.STORAGE_KEY);
        builder.addData(new ForceCloseLogEntry());
        database.execSQL(builder.getTableCreateString());

        builder = new TableBuilder(AppAvailableToInstall.STORAGE_KEY);
        builder.addData(new AppAvailableToInstall());
        database.execSQL(builder.getTableCreateString());

        DbUtil.createNumbersTable(database);

        database.setVersion(GLOBAL_DB_VERSION);

        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }
}
 
Example 17
Source File: UserSandboxUtils.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
private static void finalizeMigration(CommCareApp app, UserKeyRecord incomingSandbox, UserKeyRecord newSandbox) {
    SqlStorage<UserKeyRecord> ukr = app.getStorage(UserKeyRecord.class);

    SQLiteDatabase ukrdb = ukr.getAccessLock();
    ukrdb.beginTransaction();
    try {
        incomingSandbox.setType(UserKeyRecord.TYPE_PENDING_DELETE);
        ukr.write(incomingSandbox);
        newSandbox.setType(UserKeyRecord.TYPE_NORMAL);
        ukr.write(newSandbox);
        ukrdb.setTransactionSuccessful();
    } finally {
        ukrdb.endTransaction();
    }
}
 
Example 18
Source File: UserDatabaseUpgrader.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
private boolean upgradeTwentyTwentyOne(SQLiteDatabase db) {
    db.beginTransaction();
    try {
        UserDbUpgradeUtils.addRelationshipToAllCases(c, db);
        UserDbUpgradeUtils.migrateFormRecordsToV3(c, db);
        db.setTransactionSuccessful();
        return true;
    } finally {
        db.endTransaction();
    }

}
 
Example 19
Source File: DatabaseUserOpenHelper.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
public static void buildTable(SQLiteDatabase database,
                              String tableName,
                              Persistable dataObject) {
    database.beginTransaction();
    try {
        TableBuilder builder = new TableBuilder(tableName);
        builder.addData(dataObject);
        database.execSQL(builder.getTableCreateString());
        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }
}
 
Example 20
Source File: AppDatabaseUpgrader.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
private boolean upgradeFourFive(SQLiteDatabase db) {
    db.beginTransaction();
    try {
        DbUtil.createNumbersTable(db);
        db.setTransactionSuccessful();
        return true;
    } finally {
        db.endTransaction();
    }
}