コード例 #1
0
 static String getTypesSQL(@NonNull Container container) {
   StringBuilder builder = new StringBuilder();
   Field[] fields = container.getFields();
   FieldContainer primary = container.getPrimaryField();
   if (primary == null) primary = getPrimaryFieldContainer(container);
   Field primaryField = null;
   if (primary != null) primaryField = primary.getField();
   if (fields != null && fields.length > 0) {
     boolean isFirst = true;
     for (Field f : fields) {
       Ignore ignore = f.getAnnotation(Ignore.class);
       if (ignore != null) {
         LogUtil.d(
             CouSyncDb.TAG,
             CouSyncDb.LOG_HEADER
                 + "ignore field="
                 + f.getName()
                 + " when "
                 + container.getModelName()
                 + "create type column sql");
         continue;
       }
       if (primaryField != null && f.equals(primaryField)) {
         LogUtil.d(
             CouSyncDb.TAG,
             CouSyncDb.LOG_HEADER
                 + "jump primary key field="
                 + f.getName()
                 + " when "
                 + container.getModelName()
                 + "create type column sql");
         continue;
       }
       String typeSql = getTypeString(f);
       if (TextUtils.isEmpty(typeSql)) continue;
       if (isFirst) {
         isFirst = false;
       } else builder.append(Statement.COMMA);
       builder.append(typeSql);
     }
   } else {
     throw new NoFieldException(container.getModelName() + " have no field");
   }
   return builder.toString();
 }
コード例 #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;
  }