/** * Clear associated models' save state. After this method, the associated models of baseObj which * data is removed from database will become unsaved. * * @param baseObj The record to delete. * @param associationInfos The associated info. */ private void clearAssociatedModelSaveState( DataSupport baseObj, Collection<AssociationsInfo> associationInfos) { try { for (AssociationsInfo associationInfo : associationInfos) { if (associationInfo.getAssociationType() == Const.Model.MANY_TO_ONE && !baseObj.getClassName().equals(associationInfo.getClassHoldsForeignKey())) { Collection<DataSupport> associatedModels = getAssociatedModels(baseObj, associationInfo); if (associatedModels != null && !associatedModels.isEmpty()) { for (DataSupport model : associatedModels) { if (model != null) { model.resetBaseObjId(); } } } } else if (associationInfo.getAssociationType() == Const.Model.ONE_TO_ONE) { DataSupport model = getAssociatedModel(baseObj, associationInfo); if (model != null) { model.resetBaseObjId(); } } } } catch (Exception e) { throw new DataSupportException(e.getMessage()); } }
/** * Delete the referenced rows of baseObj in associated tables(Many2One and One2One conditions). * * @param baseObj The record to delete. Now contains associations info. * @return The number of rows affected in all associated tables. */ private int deleteAssociatedForeignKeyRows(DataSupport baseObj) { int rowsAffected = 0; Map<String, Set<Long>> associatedModelMap = baseObj.getAssociatedModelsMapWithFK(); for (String associatedTableName : associatedModelMap.keySet()) { String fkName = getForeignKeyColumnName(baseObj.getTableName()); rowsAffected += mDatabase.delete(associatedTableName, fkName + " = " + baseObj.getBaseObjId(), null); } return rowsAffected; }
/** * The open interface for other classes in CRUD package to delete. Using baseObj to decide which * record to delete. The baseObj must be saved already(using {@link * org.litepal.crud.DataSupport#isSaved()} to test), or nothing will be deleted. This method can * action cascade delete. When the record is deleted from database, all the referenced data such * as foreign key value will be removed too. * * @param baseObj The record to delete. * @return The number of rows affected. Including cascade delete rows. */ int onDelete(DataSupport baseObj) { if (baseObj.isSaved()) { Collection<AssociationsInfo> associationInfos = analyzeAssociations(baseObj); int rowsAffected = deleteCascade(baseObj); rowsAffected += mDatabase.delete(baseObj.getTableName(), "id = " + baseObj.getBaseObjId(), null); clearAssociatedModelSaveState(baseObj, associationInfos); return rowsAffected; } return 0; }
/** * Delete the referenced rows of baseObj in intermediate join tables(Many2Many condition). * * @param baseObj The record to delete. Now contains associations info. * @return The number of rows affected in all intermediate join tables. */ private int deleteAssociatedJoinTableRows(DataSupport baseObj) { int rowsAffected = 0; Set<String> associatedTableNames = baseObj.getAssociatedModelsMapForJoinTable().keySet(); for (String associatedTableName : associatedTableNames) { String joinTableName = DBUtility.getIntermediateTableName(baseObj.getTableName(), associatedTableName); String fkName = getForeignKeyColumnName(baseObj.getTableName()); rowsAffected += mDatabase.delete(joinTableName, fkName + " = " + baseObj.getBaseObjId(), null); } return rowsAffected; }
/** * Analyze the associations of baseObj and store the result in it. The associations will be used * when deleting referenced data of baseObj. * * @param baseObj The record to delete. */ private Collection<AssociationsInfo> analyzeAssociations(DataSupport baseObj) { try { Collection<AssociationsInfo> associationInfos = getAssociationInfo(baseObj.getClassName()); analyzeAssociatedModels(baseObj, associationInfos); return associationInfos; } catch (Exception e) { throw new DataSupportException(e.getMessage()); } }