/** * 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 modelClass and store the associated tables. The associated tables * might be used when deleting referenced data of a specified row. * * @param modelClass To get associations of this class. */ private void analyzeAssociations(Class<?> modelClass) { Collection<AssociationsInfo> associationInfos = getAssociationInfo(modelClass.getName()); for (AssociationsInfo associationInfo : associationInfos) { String associatedTableName = DBUtility.getTableNameByClassName(associationInfo.getAssociatedClassName()); if (associationInfo.getAssociationType() == Const.Model.MANY_TO_ONE || associationInfo.getAssociationType() == Const.Model.ONE_TO_ONE) { String classHoldsForeignKey = associationInfo.getClassHoldsForeignKey(); if (!modelClass.getName().equals(classHoldsForeignKey)) { getForeignKeyTableToDelete().add(associatedTableName); } } else if (associationInfo.getAssociationType() == Const.Model.MANY_TO_MANY) { String joinTableName = DBUtility.getIntermediateTableName(getTableName(modelClass), associatedTableName); joinTableName = BaseUtility.changeCase(joinTableName); getForeignKeyTableToDelete().add(joinTableName); } } }