com.raizlabs.android.dbflow.structure.database.transaction.ITransaction Java Examples

The following examples show how to use com.raizlabs.android.dbflow.structure.database.transaction.ITransaction. 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: DatabaseHelperDelegate.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
/**
 * Saves the database as a backup on the {@link DefaultTransactionQueue}.
 * This will create a THIRD database to use as a backup to the backup in case somehow the overwrite fails.
 */
public void backupDB() {
    if (!getDatabaseDefinition().backupEnabled() || !getDatabaseDefinition().areConsistencyChecksEnabled()) {
        throw new IllegalStateException("Backups are not enabled for : " + getDatabaseDefinition().getDatabaseName() + ". Please consider adding " +
            "both backupEnabled and consistency checks enabled to the Database annotation");
    }

    getDatabaseDefinition().beginTransactionAsync(new ITransaction() {
        @SuppressWarnings("ResultOfMethodCallIgnored")
        @Override
        public void execute(DatabaseWrapper databaseWrapper) {
            Context context = FlowManager.getContext();
            File backup = context.getDatabasePath(getTempDbFileName());
            File temp = context.getDatabasePath(TEMP_DB_NAME + "-2-" + getDatabaseDefinition().getDatabaseFileName());

            // if exists we want to delete it before rename
            if (temp.exists()) {
                temp.delete();
            }

            backup.renameTo(temp);
            if (backup.exists()) {
                backup.delete();
            }
            File existing = context.getDatabasePath(getDatabaseDefinition().getDatabaseFileName());

            try {
                backup.getParentFile().mkdirs();
                writeDB(backup, new FileInputStream(existing));

                temp.delete();
            } catch (Exception e) {
                FlowLog.logError(e);

            }
        }
    }).build().execute();

}
 
Example #2
Source File: BaseContentProvider.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
@Override
public int bulkInsert(@NonNull final Uri uri, @NonNull final ContentValues[] values) {
    final int[] count = {0};
    getDatabase().executeTransaction(new ITransaction() {
        @Override
        public void execute(DatabaseWrapper databaseWrapper) {
            for (ContentValues contentValues : values) {
                count[0] += bulkInsert(uri, contentValues);
            }
        }
    });
    //noinspection ConstantConditions
    getContext().getContentResolver().notifyChange(uri, null);
    return count[0];
}
 
Example #3
Source File: BaseAsyncObject.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
protected void executeTransaction(@NonNull ITransaction transaction) {
    cancel();
    currentTransaction = databaseDefinition
        .beginTransactionAsync(transaction)
        .error(error)
        .success(success)
        .build();
    currentTransaction.execute();
}
 
Example #4
Source File: DatabaseDefinition.java    From Meteorite with Apache License 2.0 5 votes vote down vote up
public void executeTransaction(@NonNull ITransaction transaction) {
    DatabaseWrapper database = getWritableDatabase();
    try {
        database.beginTransaction();
        transaction.execute(database);
        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }
}
 
Example #5
Source File: DatabaseDefinition.java    From Meteorite with Apache License 2.0 4 votes vote down vote up
@NonNull
public Transaction.Builder beginTransactionAsync(@NonNull ITransaction transaction) {
    return new Transaction.Builder(transaction, this);
}
 
Example #6
Source File: DBFlowExecutor.java    From android-orm-benchmark-updated with Apache License 2.0 4 votes vote down vote up
@Override
public long writeWholeData() throws SQLException
{
    final List<User> users = new LinkedList<User>();
    for(int i = 0; i < NUM_USER_INSERTS; i++)
    {
        User newUser = new User();
        newUser.lastName = (getRandomString(10));
        newUser.firstName = (getRandomString(10));

        users.add(newUser);
    }

    final List<Message> messages = new LinkedList<Message>();
    for (int i = 0; i < NUM_MESSAGE_INSERTS; i++) {
        Message newMessage = new Message();
        newMessage.commandId = i;
        newMessage.sortedBy = System.nanoTime();
        newMessage.content = Util.getRandomString(100);
        newMessage.clientId = System.currentTimeMillis();
        newMessage
                .senderId = (Math.round(Math.random() * NUM_USER_INSERTS));
        newMessage
                .channelId = (Math.round(Math.random() * NUM_USER_INSERTS));
        newMessage.createdAt = ((int) (System.currentTimeMillis() / 1000L));

        messages.add(newMessage);
    }

    long start = System.nanoTime();

    FlowManager.getDatabase(DatabaseModule.NAME).executeTransaction(new ITransaction() {
        @Override
        public void execute(DatabaseWrapper databaseWrapper) {
            for (User user : users) {
                user.save();
            }
            Log.d(TAG, "Done, wrote " + NUM_USER_INSERTS + " users");

            for (Message message : messages) {
                message.save();
            }
            Log.d(TAG, "Done, wrote " + NUM_MESSAGE_INSERTS + " messages");
        }
    });

    return System.nanoTime() - start;
}
 
Example #7
Source File: PerfTestDbFlow.java    From android-database-performance with Apache License 2.0 4 votes vote down vote up
@NonNull
private <TModel extends Model> ITransaction insertTransaction(Collection entities, Class<TModel> clazz) {
    ModelAdapter<? extends Model> modelAdapter = FlowManager.getModelAdapter(clazz);
    return FastStoreModelTransaction.insertBuilder(modelAdapter).addAll(entities).build();
}
 
Example #8
Source File: PerfTestDbFlow.java    From android-database-performance with Apache License 2.0 4 votes vote down vote up
@NonNull
private <TModel extends Model> ITransaction updateTransaction(Collection entities, Class<TModel> clazz) {
    ModelAdapter<? extends Model> modelAdapter = FlowManager.getModelAdapter(clazz);
    return FastStoreModelTransaction.updateBuilder(modelAdapter).addAll(entities).build();
}