/** Drops the underlying database table. */ public static void dropTable(Database db, boolean ifExists) { String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"ORDER TRANSACTION GROUP BY\""; db.execSQL(sql); }
/** * Run the given Runnable inside a database transaction. If you except a result, consider callInTx. */ public void runInTx(Runnable runnable) { db.beginTransaction(); try { runnable.run(); db.setTransactionSuccessful(); } finally { db.endTransaction(); } }
private long executeInsert(T entity, DatabaseStatement stmt, boolean setKeyAndAttach) { long rowId; if (db.isDbLockedByCurrentThread()) { rowId = insertInsideTx(entity, stmt); } else { // Do TX to acquire a connection before locking the stmt to avoid deadlocks db.beginTransaction(); try { rowId = insertInsideTx(entity, stmt); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } if (setKeyAndAttach) { updateKeyAfterInsertAndAttach(entity, rowId, true); } return rowId; }
public T loadByRowId(long rowId) { String[] idArray = new String[]{Long.toString(rowId)}; Cursor cursor = db.rawQuery(statements.getSelectByRowId(), idArray); return loadUniqueAndCloseCursor(cursor); }
public DatabaseStatement getCountStatement() { if (countStatement == null) { String sql = SqlUtils.createSqlCount(tablename); countStatement = db.compileStatement(sql); } return countStatement; }
/** * Deletes all matching entities without detaching them from the identity scope (aka session/cache). Note that this * method may lead to stale entity objects in the session cache. Stale entities may be returned when loaded by * their * primary key, but not using queries. */ public void executeDeleteWithoutDetachingEntities() { checkThread(); Database db = dao.getDatabase(); if (db.isDbLockedByCurrentThread()) { dao.getDatabase().execSQL(sql, parameters); } else { // Do TX to acquire a connection before locking this to avoid deadlocks // Locking order as described in AbstractDao db.beginTransaction(); try { dao.getDatabase().execSQL(sql, parameters); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } }
/** A raw-style query where you can pass any WHERE clause and arguments. */ public List<RelationSource2> queryDeep(String where, String... selectionArg) { Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg); return loadDeepAllAndCloseCursor(cursor); }
@Override protected DatabaseStatement initialValue() { return db.compileStatement("SELECT 42"); } };
/** Creates the underlying database table. */ public static void createTable(Database db, boolean ifNotExists) { String constraint = ifNotExists? "IF NOT EXISTS ": ""; db.execSQL("CREATE TABLE " + constraint + "\"TREE_ENTITY\" (" + // "\"_id\" INTEGER PRIMARY KEY ," + // 0: id "\"PARENT_ID\" INTEGER);"); // 1: parentId }
/** 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); } }
/** Loads all available entities from the database. */ public List<T> loadAll() { Cursor cursor = db.rawQuery(statements.getSelectAll(), null); return loadAllAndCloseCursor(cursor); }
/** * Calls the given Callable inside a database transaction and returns the result of the Callable. If you don't * except a result, consider runInTx. */ public <V> V callInTx(Callable<V> callable) throws Exception { db.beginTransaction(); try { V result = callable.call(); db.setTransactionSuccessful(); return result; } finally { db.endTransaction(); } }
public DatabaseStatement getDeleteStatement() { if (deleteStatement == null) { String sql = SqlUtils.createSqlDelete(tablename, pkColumns); DatabaseStatement newDeleteStatement = db.compileStatement(sql); synchronized (this) { if (deleteStatement == null) { deleteStatement = newDeleteStatement; } } if (deleteStatement != newDeleteStatement) { newDeleteStatement.close(); } } return deleteStatement; }
/** Drops the underlying database table. */ public static void dropTable(Database db, boolean ifExists) { String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"AUTOINCREMENT_ENTITY\""; db.execSQL(sql); }
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(); } } }
/** A raw-style query where you can pass any WHERE clause and arguments. */ public List<TreeEntity> queryDeep(String where, String... selectionArg) { Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg); return loadDeepAllAndCloseCursor(cursor); }
public static int executeSqlStatementsInTx(Database db, String[] statements) { db.beginTransaction(); try { int count = executeSqlStatements(db, statements); db.setTransactionSuccessful(); return count; } finally { db.endTransaction(); } }
public DatabaseStatement getUpdateStatement() { if (updateStatement == null) { String sql = SqlUtils.createSqlUpdate(tablename, allColumns, pkColumns); DatabaseStatement newUpdateStatement = db.compileStatement(sql); synchronized (this) { if (updateStatement == null) { updateStatement = newUpdateStatement; } } if (updateStatement != newUpdateStatement) { newUpdateStatement.close(); } } return updateStatement; }
/** Drops the underlying database table. */ public static void dropTable(Database db, boolean ifExists) { String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"ABCDEF_ENTITY\""; db.execSQL(sql); }
/** Executes the query and returns a raw android.database.Cursor. Don't forget to close it. */ public Cursor query() { checkThread(); return dao.getDatabase().rawQuery(sql, parameters); }