コード例 #1
0
  /** 先根据TYPE注解获取FieldContainer,如果null再根据Field获取 都取不到return null */
  static FieldContainer getPrimaryFieldContainer(@NonNull Container container) {

    HashMap<String, FieldContainer> containers = container.getFieldContainers();
    if (containers == null || containers.size() < 1) {
      return null;
    }

    PrimaryKey primaryKey = container.getPrimaryKey();
    if (primaryKey != null) { // 先根据TYPE注解获取FieldContainer
      String name = primaryKey.keyName();
      boolean isAuto = primaryKey.autoIncrement();
      if (!TextUtils.isEmpty(name)) {
        FieldContainer fc = containers.get(name);
        if (fc != null) {
          fc.setIsAuto(isAuto);
          fc.setIsPrimaryKey(true);
          return fc;
        }
      }
    }

    // Field获取
    Set<Map.Entry<String, FieldContainer>> sets = containers.entrySet();
    Iterator<Map.Entry<String, FieldContainer>> iterator = sets.iterator();
    while ((iterator.hasNext())) {
      Map.Entry<String, FieldContainer> entry = iterator.next();
      FieldContainer fieldContainer = entry.getValue();
      if (fieldContainer.isPrimaryKey()) {
        return fieldContainer;
      }
    }
    return null;
  }
コード例 #2
0
  /** get primary key */
  public static String getPrimaryKeySQL(@NonNull Container container) {
    String primary = null;
    FieldContainer fieldContainer = container.getPrimaryField();
    if (fieldContainer == null) {
      fieldContainer = getPrimaryFieldContainer(container);
      container.setPrimaryField(fieldContainer);
    }
    if (fieldContainer != null) {
      primary = getPrimaryString(fieldContainer);
    } else { // 类中找不到与主键同名field
      boolean checkPrimaryKey = CouSyncDb.Config.isCheckPrimaryKey();

      PrimaryKey primaryKey = container.getPrimaryKey(); // 类上注解设置主键
      if ((primaryKey == null || TextUtils.isEmpty(primaryKey.keyName())) && checkPrimaryKey)
        throw new NoPrimaryKeyException(
            "the class " + container.getModelName() + " do not set primary key");
      else if (primaryKey != null && !TextUtils.isEmpty(primaryKey.keyName()))
        primary = getPrimaryStringFromClass(primaryKey.keyName(), primaryKey.autoIncrement());
    }
    return primary;
  }