@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 ArrayList<Relation> queryRelation( Class class1, Class class2, List<String> key1List, List<String> key2List) { acquireReference(); try { SQLStatement stmt = SQLBuilder.buildQueryRelationSql(class1, class2, key1List, key2List); final EntityTable table1 = TableManager.getTable(class1); final EntityTable table2 = TableManager.getTable(class2); final ArrayList<Relation> list = new ArrayList<Relation>(); Querier.doQuery( mHelper.getReadableDatabase(), stmt, new Querier.CursorParser() { @Override public void parseEachCursor(SQLiteDatabase db, Cursor c) throws Exception { Relation relation = new Relation(); relation.key1 = c.getString(c.getColumnIndex(table1.name)); relation.key2 = c.getString(c.getColumnIndex(table2.name)); list.add(relation); } }); return list; } finally { releaseReference(); } }
@Override public boolean dropTable(String tableName) { acquireReference(); try { return SQLBuilder.buildDropTable(tableName).execute(mHelper.getWritableDatabase()); } catch (Exception e) { e.printStackTrace(); } finally { releaseReference(); } return false; }
@Override public boolean dropTable(Object entity) { acquireReference(); try { return SQLBuilder.buildDropTable(TableManager.getTable(entity)) .execute(mHelper.getWritableDatabase()); } catch (Exception e) { e.printStackTrace(); } finally { releaseReference(); } return false; }
/** 主动删除其mapping数据 */ @Override public int delete(Object entity) { acquireReference(); try { return SQLBuilder.buildDeleteSql(entity) .execDeleteWithMapping(mHelper.getWritableDatabase(), entity, mTableManager); } catch (Exception e) { e.printStackTrace(); } finally { releaseReference(); } return SQLStatement.NONE; }
@Override public int update(Object entity, ColumnsValue cvs, ConflictAlgorithm conflictAlgorithm) { acquireReference(); try { SQLiteDatabase db = mHelper.getWritableDatabase(); return SQLBuilder.buildUpdateSql(entity, cvs, conflictAlgorithm) .execUpdateWithMapping(db, entity, mTableManager); } catch (Exception e) { e.printStackTrace(); } finally { releaseReference(); } return SQLStatement.NONE; }
@Override public long save(Object entity) { acquireReference(); try { SQLiteDatabase db = mHelper.getWritableDatabase(); mTableManager.checkOrCreateTable(db, entity); return SQLBuilder.buildReplaceSql(entity).execInsertWithMapping(db, entity, mTableManager); } 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; }
/** 删除从[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 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; }