Search in sources :

Example 6 with Database

use of org.greenrobot.greendao.database.Database in project greenDAO by greenrobot.

the class App method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, ENCRYPTED ? "notes-db-encrypted" : "notes-db");
    Database db = ENCRYPTED ? helper.getEncryptedWritableDb("super-secret") : helper.getWritableDb();
    daoSession = new DaoMaster(db).newSession();
}
Also used : Database(org.greenrobot.greendao.database.Database)

Example 7 with Database

use of org.greenrobot.greendao.database.Database in project greenDAO by greenrobot.

the class AsyncOperationExecutor method mergeTxAndExecute.

/** Also checks for other operations in the queue that can be merged into the transaction. */
private void mergeTxAndExecute(AsyncOperation operation1, AsyncOperation operation2) {
    ArrayList<AsyncOperation> mergedOps = new ArrayList<AsyncOperation>();
    mergedOps.add(operation1);
    mergedOps.add(operation2);
    Database db = operation1.getDatabase();
    db.beginTransaction();
    boolean success = false;
    try {
        for (int i = 0; i < mergedOps.size(); i++) {
            AsyncOperation operation = mergedOps.get(i);
            executeOperation(operation);
            if (operation.isFailed()) {
                // Operation may still have changed the DB, roll back everything
                break;
            }
            if (i == mergedOps.size() - 1) {
                AsyncOperation peekedOp = queue.peek();
                if (i < maxOperationCountToMerge && operation.isMergeableWith(peekedOp)) {
                    AsyncOperation removedOp = queue.remove();
                    if (removedOp != peekedOp) {
                        // Paranoia check, should not occur unless threading is broken
                        throw new DaoException("Internal error: peeked op did not match removed op");
                    }
                    mergedOps.add(removedOp);
                } else {
                    // No more ops in the queue to merge, finish it
                    db.setTransactionSuccessful();
                    success = true;
                    break;
                }
            }
        }
    } finally {
        try {
            db.endTransaction();
        } catch (RuntimeException e) {
            DaoLog.i("Async transaction could not be ended, success so far was: " + success, e);
            success = false;
        }
    }
    if (success) {
        int mergedCount = mergedOps.size();
        for (AsyncOperation asyncOperation : mergedOps) {
            asyncOperation.mergedOperationsCount = mergedCount;
            handleOperationCompleted(asyncOperation);
        }
    } else {
        DaoLog.i("Reverted merged transaction because one of the operations failed. Executing operations one by " + "one instead...");
        for (AsyncOperation asyncOperation : mergedOps) {
            asyncOperation.reset();
            executeOperationAndPostCompleted(asyncOperation);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Database(org.greenrobot.greendao.database.Database) DaoException(org.greenrobot.greendao.DaoException)

Example 8 with Database

use of org.greenrobot.greendao.database.Database in project greenDAO by greenrobot.

the class AsyncOperationExecutor method executeTransactionRunnable.

private void executeTransactionRunnable(AsyncOperation operation) {
    Database db = operation.getDatabase();
    db.beginTransaction();
    try {
        ((Runnable) operation.parameter).run();
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}
Also used : Database(org.greenrobot.greendao.database.Database)

Example 9 with Database

use of org.greenrobot.greendao.database.Database in project greenDAO by greenrobot.

the class AsyncOperationExecutor method executeTransactionCallable.

@SuppressWarnings("unchecked")
private void executeTransactionCallable(AsyncOperation operation) throws Exception {
    Database db = operation.getDatabase();
    db.beginTransaction();
    try {
        operation.result = ((Callable<Object>) operation.parameter).call();
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}
Also used : Database(org.greenrobot.greendao.database.Database)

Example 10 with Database

use of org.greenrobot.greendao.database.Database in project greenDAO by greenrobot.

the class AsyncSession method enqueueDatabaseOperation.

private AsyncOperation enqueueDatabaseOperation(OperationType type, Object param, int flags) {
    Database database = daoSession.getDatabase();
    AsyncOperation operation = new AsyncOperation(type, null, database, param, flags | sessionFlags);
    executor.enqueue(operation);
    return operation;
}
Also used : Database(org.greenrobot.greendao.database.Database)

Aggregations

Database (org.greenrobot.greendao.database.Database)21 StandardDatabase (org.greenrobot.greendao.database.StandardDatabase)10 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)9 AbstractDaoMaster (org.greenrobot.greendao.AbstractDaoMaster)9 DevOpenHelper (org.greenrobot.greendao.daotest.DaoMaster.DevOpenHelper)2 Cursor (android.database.Cursor)1 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)1 ArrayList (java.util.ArrayList)1 DaoException (org.greenrobot.greendao.DaoException)1 OpenHelper (org.greenrobot.greendao.daotest.DaoMaster.OpenHelper)1 DatabaseStatement (org.greenrobot.greendao.database.DatabaseStatement)1 DevOpenHelper (org.greenrobot.greendao.example.DaoMaster.DevOpenHelper)1 Before (org.junit.Before)1