Search in sources :

Example 1 with DatabaseStatement

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

the class DaoSessionConcurrentTest method _testThreadLocalSpeed.

/**
     * We could put the statements inside ThreadLocals (fast enough), but it comes with initialization penalty for new
     * threads and costs more memory.
     */
public void _testThreadLocalSpeed() {
    final Database db = dao.getDatabase();
    ThreadLocal<DatabaseStatement> threadLocal = new ThreadLocal<DatabaseStatement>() {

        @Override
        protected DatabaseStatement initialValue() {
            return db.compileStatement("SELECT 42");
        }
    };
    threadLocal.get();
    long start = SystemClock.currentThreadTimeMillis();
    for (int i = 0; i < 1000; i++) {
        DatabaseStatement sqLiteStatement = threadLocal.get();
        assertNotNull(sqLiteStatement);
    }
    Long time = SystemClock.currentThreadTimeMillis() - start;
    DaoLog.d("TIME: " + time + "ms");
    // Around 1ms on a S3
    assertTrue(time < 10);
}
Also used : DatabaseStatement(org.greenrobot.greendao.database.DatabaseStatement) Database(org.greenrobot.greendao.database.Database)

Example 2 with DatabaseStatement

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

the class AbstractDao method insertInTx.

/**
     * Inserts the given entities in the database using a transaction. The given entities will become tracked if the PK
     * is set.
     *
     * @param entities      The entities to insert.
     * @param setPrimaryKey if true, the PKs of the given will be set after the insert; pass false to improve
     *                      performance.
     */
public void insertInTx(Iterable<T> entities, boolean setPrimaryKey) {
    DatabaseStatement stmt = statements.getInsertStatement();
    executeInsertInTx(stmt, entities, setPrimaryKey);
}
Also used : DatabaseStatement(org.greenrobot.greendao.database.DatabaseStatement)

Example 3 with DatabaseStatement

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

the class AbstractDao method update.

public void update(T entity) {
    assertSinglePk();
    DatabaseStatement stmt = statements.getUpdateStatement();
    if (db.isDbLockedByCurrentThread()) {
        synchronized (stmt) {
            if (isStandardSQLite) {
                updateInsideSynchronized(entity, (SQLiteStatement) stmt.getRawStatement(), true);
            } else {
                updateInsideSynchronized(entity, stmt, true);
            }
        }
    } else {
        // Do TX to acquire a connection before locking the stmt to avoid deadlocks
        db.beginTransaction();
        try {
            synchronized (stmt) {
                updateInsideSynchronized(entity, stmt, true);
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
}
Also used : DatabaseStatement(org.greenrobot.greendao.database.DatabaseStatement)

Example 4 with DatabaseStatement

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

the class AbstractDao method insertOrReplaceInTx.

/**
     * Inserts or replaces the given entities in the database using a transaction. The given entities will become
     * tracked if the PK is set.
     *
     * @param entities      The entities to insert.
     * @param setPrimaryKey if true, the PKs of the given will be set after the insert; pass false to improve
     *                      performance.
     */
public void insertOrReplaceInTx(Iterable<T> entities, boolean setPrimaryKey) {
    DatabaseStatement stmt = statements.getInsertOrReplaceStatement();
    executeInsertInTx(stmt, entities, setPrimaryKey);
}
Also used : DatabaseStatement(org.greenrobot.greendao.database.DatabaseStatement)

Example 5 with DatabaseStatement

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

the class AbstractDao method deleteByKey.

/** Deletes an entity with the given PK from the database. Currently, only single value PK entities are supported. */
public void deleteByKey(K key) {
    assertSinglePk();
    DatabaseStatement stmt = statements.getDeleteStatement();
    if (db.isDbLockedByCurrentThread()) {
        synchronized (stmt) {
            deleteByKeyInsideSynchronized(key, stmt);
        }
    } else {
        // Do TX to acquire a connection before locking the stmt to avoid deadlocks
        db.beginTransaction();
        try {
            synchronized (stmt) {
                deleteByKeyInsideSynchronized(key, stmt);
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
    if (identityScope != null) {
        identityScope.remove(key);
    }
}
Also used : DatabaseStatement(org.greenrobot.greendao.database.DatabaseStatement)

Aggregations

DatabaseStatement (org.greenrobot.greendao.database.DatabaseStatement)11 SQLiteStatement (android.database.sqlite.SQLiteStatement)1 Database (org.greenrobot.greendao.database.Database)1