public SqlJsonImplementation(Object jsonObject, Field field) {
    this.jsonObject = jsonObject;
    if (jsonObject instanceof String) field.setType("string");
    else if (jsonObject instanceof Number) field.setType("number");
    else if (jsonObject instanceof Boolean) field.setType("boolean");
    else if (jsonObject instanceof List) field.setType("array");
    // map should be json as well
    else field.setType("json");

    this.field = field;
  }
  /**
   * Makes name and column type bindings to given <code>field</code>. It also makes default name
   * bindings to unnamed {@link Index}es (or autoIncrement {@link PrimaryKey} fields since it covers
   * {@link Index})
   *
   * @param entity used to generate index name if not specified.
   * @param field
   * @param namingPolicy
   * @param dataTypeMapper
   */
  public static void makeBinding(
      Entity entity,
      Field field,
      PhysicalNamingPolicy namingPolicy,
      DataTypeMapper dataTypeMapper) {
    /* NAME BINDING */
    if (field.getCustomName() != null) {
      // use custom name.
      Log.trace(
          "Using custom field name on field %s.%s.",
          entity.getOriginalName(), field.getOriginalName());
      field.setGeneratedName(field.getCustomName());
    } else {
      // format name accordingly naming policy.
      field.setGeneratedName(PhysicalNameGenerator.format(field.getOriginalName(), namingPolicy));
    }

    /* DATA TYPE BINDING */
    if (field.getCustomType() != null) {
      // use manual field type.
      Log.trace(
          "Using custom type on field %s.%s.", entity.getOriginalName(), field.getOriginalName());
      field.setType(field.getCustomType());
    } else {
      Class<?> fieldType = null;
      // use Class<?> type of the field.

      String customizedBindingType = null;

      // TODO add OneToMany and ManyToOne and ManyToMany when eligible.
      if (field.isAnnotationPresent(OneToOne.class)) {
        //  there exists 1:1 set type as matched field's Id type.
        Class<?> idType = getAutoIncrementTypeForClass(field.getClazz());
        fieldType = idType;

        // assign whether a customized field type exists with @Column.
        customizedBindingType = getCustomizedBinding(field.getClazz());

        Log.trace(
            "OneToOne mapping detected on field %s.%s.",
            entity.getOriginalName(), field.getOriginalName());
      } else if (MappingSession.entityExists(field.getClazz())) {
        // infer entity @PrimaryKey type if @*to* annotations does not exist
        Class<?> idType = getAutoIncrementTypeForClass(field.getClazz());
        fieldType = idType;

        if (idType == null) {
          throw new NotDeclaredIdException(field.getClazz().getName());
        }

        // assign whether a customized field type exists with @Column.
        customizedBindingType = getCustomizedBinding(field.getClazz());

        Log.trace(
            "Direct entity mapping inferred on field %s.%s.",
            entity.getOriginalName(), field.getOriginalName());
      } else {
        // usual conditions
        fieldType = field.getClazz();
      }

      if (customizedBindingType != null) // there exists a binding with @Column(name=..)
      field.setType(customizedBindingType);
      else {
        // If field is enum, use enum data type mapping.
        if (fieldType != null)
          if (fieldType.isEnum()) {
            field.setType(dataTypeMapper.getTypeFor(Enum.class));
          } else {
            field.setType(dataTypeMapper.getTypeFor(fieldType));
          }
      }

      Log.debug(
          "Field '%s'(%s) mapped to '%s'(%s) using %s.",
          field.getOriginalName(),
          field.getClazz().getName(),
          field.getGeneratedName(),
          field.getType(),
          customizedBindingType != null ? "@Column annotation" : "type of @PrimaryKey field");
    }

    /* INDEX SETTINGS BINDING */
    if (field.getIndex() != null) {
      if (field.getIndex().name() == null || "".equals(field.getIndex().name())) {
        // missing index name, create using field name followed by
        // "Index" policy.
        String indexName = null;

        if (field.isPrimaryKey()) {
          indexName =
              String.format(
                  COMPOSITE_INDEX_FORMAT,
                  PhysicalNameGenerator.capitalize(entity.getGeneratedName()),
                  INDEX_POSTFIX);
        } else {
          indexName =
              String.format(
                  FIELD_INDEX_FORMAT,
                  PhysicalNameGenerator.capitalize(entity.getGeneratedName()),
                  PhysicalNameGenerator.capitalize(field.getGeneratedName()),
                  INDEX_POSTFIX);
        }
        Log.trace("Field '%s' index name binded as '%s'", field.getOriginalName(), indexName);
        field.getIndex().name(indexName);
      }
    }
  }
Beispiel #3
0
 private List<Field> parseFields(List nodes, DocResult outerDoc, Node parent) {
   List<Field> fields = new ArrayList<Field>(nodes.size());
   for (Element node : (List<Element>) nodes) {
     Field field = new Field();
     String name = node.attributeValue("name");
     field.setName(name);
     field.setParent(parent);
     DocResult docResult = parseDoc(node, field);
     String type = node.attributeValue("type");
     field.setType(type);
     field.setRequired(!"optional".equals(node.attributeValue("required")));
     String index = node.attributeValue("index");
     if (index != null) {
       field.setIndex(Integer.parseInt(index));
     }
     String nodeName = node.getName();
     if ("const".equals(nodeName)) { // 常量
       field.setValue(node.element("value").getStringValue());
     } else if ("item".equals(nodeName)) { // 枚举项
       String value = node.attributeValue("value");
       field.setIndex(Integer.parseInt(value)); // 设置枚举项的index为对应value
       field.setValue(value);
     } else if ("ex".equals(nodeName)) { // 异常
       Doc fieldDoc = field.getDoc();
       if (fieldDoc == DocResult.NULL) {
         field.setDoc(fieldDoc = new Doc());
       }
       for (Map.Entry<String, String> entry : docResult.getErrors().entrySet()) { // 将doc中的异常信息写入属性
         fieldDoc.putTag(entry.getKey(), entry.getValue());
       }
       if (outerDoc != null) { // 方法文档不为空
         String outerEx = outerDoc.getError(name); // 读取方法文档中的异常描述
         if (StringUtils.isEmpty(fieldDoc.getDesc()) && outerEx != null) { // 如果自己的文档没有注释,则设置为方法的注释
           fieldDoc.setDesc(outerEx);
         }
         if ("Type.AnyException"
             .equals(type)) { // 如果是通用的AnyException,则将外部定义的类似@error 315 无效用户状态写入属性
           for (Map.Entry<String, String> entry : outerDoc.getErrors().entrySet()) {
             String key = entry.getKey();
             if (!key.endsWith("Exception")) { // 若果是Exception说明不是一个异常代码
               fieldDoc.putTag(entry.getKey(), entry.getValue());
             }
           }
         }
       }
     } else {
       field.setValue(node.attributeValue("default"));
     }
     if (outerDoc != null) {
       Doc outerFieldDoc = outerDoc.getFidleDoc(field.getName()); // 判断外部是否定义了字段的描述文档
       if (outerFieldDoc != null) {
         Doc fieldDoc = field.getDoc();
         if (fieldDoc == DocResult.NULL) {
           field.setDoc(outerFieldDoc);
         } else {
           if (StringUtils.isEmpty(fieldDoc.getDesc())) {
             fieldDoc.setDesc(outerFieldDoc.getDesc());
           }
           for (Map.Entry<String, String> entry : outerFieldDoc.getTags().entrySet()) {
             fieldDoc.putTagIfAbsent(entry.getKey(), entry.getValue()); // 默认内部注视优先
           }
         }
       }
     }
     fields.add(field);
   }
   return fields;
 }
Beispiel #4
0
 public Field createField(String name, Type fieldType) {
   Field f = createField(name);
   f.setType(fieldType);
   return f;
 }