Search in sources :

Example 1 with DaoException

use of org.greenrobot.greendao.DaoException in project greenDAO by greenrobot.

the class TestEntityTest method testRefreshIllegal.

public void testRefreshIllegal() {
    TestEntity entity = createEntity(1l);
    try {
        dao.refresh(entity);
        fail("Exception expected");
    } catch (DaoException expected) {
    }
    dao.insert(entity);
    dao.delete(entity);
    try {
        dao.refresh(entity);
        fail("Exception expected");
    } catch (DaoException expected) {
    }
}
Also used : TestEntity(org.greenrobot.greendao.daotest.TestEntity) DaoException(org.greenrobot.greendao.DaoException)

Example 2 with DaoException

use of org.greenrobot.greendao.DaoException 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 3 with DaoException

use of org.greenrobot.greendao.DaoException in project greenDAO by greenrobot.

the class AnActiveEntityTest method testThrowWhenDetached.

public void testThrowWhenDetached() {
    AnActiveEntity entity = new AnActiveEntity();
    try {
        entity.delete();
        fail("Should fail for detached entity");
    } catch (DaoException e) {
    // OK, expected
    }
    try {
        entity.refresh();
        fail("Should fail for detached entity");
    } catch (DaoException e) {
    // OK, expected
    }
    try {
        entity.update();
        fail("Should fail for detached entity");
    } catch (DaoException e) {
    // OK, expected
    }
}
Also used : AnActiveEntity(org.greenrobot.greendao.daotest.AnActiveEntity) DaoException(org.greenrobot.greendao.DaoException)

Example 4 with DaoException

use of org.greenrobot.greendao.DaoException in project greenDAO by greenrobot.

the class CountQuery method count.

/** Returns the count (number of results matching the query). Uses SELECT COUNT (*) sematics. */
public long count() {
    checkThread();
    Cursor cursor = dao.getDatabase().rawQuery(sql, parameters);
    try {
        if (!cursor.moveToNext()) {
            throw new DaoException("No result for count");
        } else if (!cursor.isLast()) {
            throw new DaoException("Unexpected row count: " + cursor.getCount());
        } else if (cursor.getColumnCount() != 1) {
            throw new DaoException("Unexpected column count: " + cursor.getColumnCount());
        }
        return cursor.getLong(0);
    } finally {
        cursor.close();
    }
}
Also used : Cursor(android.database.Cursor) DaoException(org.greenrobot.greendao.DaoException)

Example 5 with DaoException

use of org.greenrobot.greendao.DaoException in project greenDAO by greenrobot.

the class WhereCollector method checkProperty.

void checkProperty(Property property) {
    if (dao != null) {
        Property[] properties = dao.getProperties();
        boolean found = false;
        for (Property property2 : properties) {
            if (property == property2) {
                found = true;
                break;
            }
        }
        if (!found) {
            throw new DaoException("Property '" + property.name + "' is not part of " + dao);
        }
    }
}
Also used : DaoException(org.greenrobot.greendao.DaoException) Property(org.greenrobot.greendao.Property)

Aggregations

DaoException (org.greenrobot.greendao.DaoException)7 ArrayList (java.util.ArrayList)2 Property (org.greenrobot.greendao.Property)2 TestEntity (org.greenrobot.greendao.daotest.TestEntity)2 Cursor (android.database.Cursor)1 Field (java.lang.reflect.Field)1 AnActiveEntity (org.greenrobot.greendao.daotest.AnActiveEntity)1 Database (org.greenrobot.greendao.database.Database)1 IdentityScopeObject (org.greenrobot.greendao.identityscope.IdentityScopeObject)1