@Override public int deleteAll(Class<?> claxx) { acquireReference(); try { SQLiteDatabase db = mHelper.getWritableDatabase(); SQLStatement stmt = SQLBuilder.buildDeleteAllSql(claxx); int num = stmt.execDelete(db); // 删除关系映射 final MapInfo mapTable = SQLBuilder.buildDelAllMappingSql(claxx); if (mapTable != null && !mapTable.isEmpty()) { Transaction.execute( db, new Worker<Boolean>() { @Override public Boolean doTransaction(SQLiteDatabase db) throws Exception { if (mapTable.delOldRelationSQL != null) { for (SQLStatement st : mapTable.delOldRelationSQL) { long rowId = st.execDelete(db); if (Log.isPrint) { Log.i(TAG, "Exec delete mapping success, nums: " + rowId); } } } return true; } }); } return num; } catch (Exception e) { e.printStackTrace(); } finally { releaseReference(); } return SQLStatement.NONE; }
@Override public <T> ArrayList<T> queryAll(Class<T> claxx) { acquireReference(); try { SQLStatement stmt = new QueryBuilder(claxx).createStatement(); return stmt.query(mHelper.getReadableDatabase(), claxx); } finally { releaseReference(); } }
@Override public long queryCount(QueryBuilder qb) { acquireReference(); try { SQLiteDatabase db = mHelper.getReadableDatabase(); SQLStatement stmt = qb.createStatementForCount(); return stmt.queryForLong(db); } catch (Exception e) { e.printStackTrace(); } finally { releaseReference(); } return SQLStatement.NONE; }
@Override public int update( Collection<?> collection, ColumnsValue cvs, ConflictAlgorithm conflictAlgorithm) { acquireReference(); try { if (!Checker.isEmpty(collection)) { SQLiteDatabase db = mHelper.getWritableDatabase(); Object entity = collection.iterator().next(); SQLStatement stmt = SQLBuilder.buildUpdateAllSql(entity, cvs, conflictAlgorithm); return stmt.execUpdateCollection(db, collection, cvs, mTableManager); } } catch (Exception e) { e.printStackTrace(); } finally { releaseReference(); } return SQLStatement.NONE; }
@Override public int save(Collection<?> collection) { acquireReference(); try { if (!Checker.isEmpty(collection)) { SQLiteDatabase db = mHelper.getWritableDatabase(); Object entity = collection.iterator().next(); SQLStatement stmt = SQLBuilder.buildReplaceAllSql(entity); mTableManager.checkOrCreateTable(db, entity); return stmt.execInsertCollection(db, collection, mTableManager); } } catch (Exception e) { e.printStackTrace(); } finally { releaseReference(); } return SQLStatement.NONE; }
@Override public <T> T queryById(String id, Class<T> claxx) { acquireReference(); try { SQLiteDatabase db = mHelper.getReadableDatabase(); EntityTable table = TableManager.getTable(claxx); SQLStatement stmt = new QueryBuilder(claxx) .where(table.key.column + "=?", new String[] {id}) .createStatement(); ArrayList<T> list = stmt.query(db, claxx); if (!Checker.isEmpty(list)) { return list.get(0); } } finally { releaseReference(); } return null; }
/** 删除从[start,end]的数据 此方法暂不会删除关联映射表里的关系数据 */ @Override public int delete(Class<?> claxx, long start, long end, String orderAscColumn) { acquireReference(); try { if (start < 0 || end < start) { throw new RuntimeException("start must >=0 and smaller than end"); } if (start != 0) { start -= 1; } end = end == Integer.MAX_VALUE ? -1 : end - start; SQLStatement stmt = SQLBuilder.buildDeleteSql(claxx, start, end, orderAscColumn); return stmt.execDelete(mHelper.getWritableDatabase()); } catch (Exception e) { e.printStackTrace(); } finally { releaseReference(); } return SQLStatement.NONE; }
@Override public boolean execute(SQLiteDatabase db, SQLStatement statement) { acquireReference(); try { if (statement != null) { return statement.execute(db); } } catch (Exception e) { e.printStackTrace(); } finally { releaseReference(); } return false; }
@Override public int delete(final Collection<?> collection) { acquireReference(); try { if (!Checker.isEmpty(collection)) { EntityTable table = TableManager.getTable(collection.iterator().next()); if (table.key != null) { SQLStatement stmt = SQLBuilder.buildDeleteSql(collection); return stmt.execDeleteCollection( mHelper.getWritableDatabase(), collection, mTableManager); } else { Integer size = Transaction.execute( mHelper.getWritableDatabase(), new Worker<Integer>() { @Override public Integer doTransaction(SQLiteDatabase db) throws Exception { for (Object entity : collection) { SQLBuilder.buildDeleteSql(entity) .execDeleteWithMapping(db, entity, mTableManager); } if (Log.isPrint) { Log.i(TAG, "Exec delete(no primarykey) :" + collection.size()); } return collection.size(); } }); return size == null ? 0 : size; } } } catch (Exception e) { e.printStackTrace(); } finally { releaseReference(); } return SQLStatement.NONE; }