/**
  * Package a {@link org.litepal.tablemanager.model.AssociationsModel}, and add it into {@link
  * #mAssociationModels} Collection.
  *
  * @param className The class name for {@link org.litepal.tablemanager.model.AssociationsModel}.
  * @param associatedClassName The associated class name for {@link
  *     org.litepal.tablemanager.model.AssociationsModel}.
  * @param classHoldsForeignKey The class which holds foreign key.
  * @param associationType The association type for {@link
  *     org.litepal.tablemanager.model.AssociationsModel}.
  */
 private void addIntoAssociationModelCollection(
     String className,
     String associatedClassName,
     String classHoldsForeignKey,
     int associationType) {
   AssociationsModel associationModel = new AssociationsModel();
   associationModel.setTableName(DBUtility.getTableNameByClassName(className));
   associationModel.setAssociatedTableName(DBUtility.getTableNameByClassName(associatedClassName));
   associationModel.setTableHoldsForeignKey(
       DBUtility.getTableNameByClassName(classHoldsForeignKey));
   associationModel.setAssociationType(associationType);
   mAssociationModels.add(associationModel);
 }
示例#2
0
 /**
  * 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);
     }
   }
 }
示例#3
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;
 }
 /**
  * This method is used to get the table model by the class name passed in. The principle to
  * generate table model is that each field in the class with non-static modifier and has a type
  * among int/Integer, long/Long, short/Short, float/Float, double/Double, char/Character,
  * boolean/Boolean or String, would generate a column with same name as corresponding field. If
  * users don't want some of the fields map a column, declare an ignore annotation with {@link
  * Column#ignore()}.
  *
  * @param className The full name of the class to map in database.
  * @return A table model with table name, class name and the map of column name and column type.
  */
 protected TableModel getTableModel(String className) {
   String tableName = DBUtility.getTableNameByClassName(className);
   TableModel tableModel = new TableModel();
   tableModel.setTableName(tableName);
   tableModel.setClassName(className);
   List<Field> supportedFields = getSupportedFields(className);
   for (Field field : supportedFields) {
     ColumnModel columnModel = convertFieldToColumnModel(field);
     tableModel.addColumnModel(columnModel);
   }
   return tableModel;
 }