private Sql makeSQL(T t, String[] fields) throws SqlCreateException {
    Sql sql = new Sql();
    Class<?> clazz = t.getClass();
    // if fields is empty
    if (fields == null || fields.length == 0) {
      fields = ORMConfigBeanUtil.getFields(clazz);
    }
    String table = ORMConfigBeanUtil.getTable(clazz, false);
    StringBuilder values = new StringBuilder();
    ReflectUtil ru = new ReflectUtil(t);
    String[] columns = ORMConfigBeanUtil.getColumns(clazz, fields);
    String idColumn = ORMConfigBeanUtil.getIdColumn(clazz);
    String idField = ORMConfigBeanUtil.getIdField(clazz);
    Method idGetter = ru.getGetter(idField);
    if (idGetter == null) throw new SqlCreateException("can not find id getter.");
    Object idValue = null;
    try {
      idValue = idGetter.invoke(t);
    } catch (Exception e) {
      throw new SqlCreateException(idGetter + " invoke exception " + e.toString(), e);
    }

    for (int i = 0; i < fields.length; i++) {
      String field = fields[i];
      String column = columns[i];
      Method getter = ru.getGetter(field);
      if (getter == null) continue;

      try {
        Object _value = getter.invoke(t);
        if (_value == null) continue;

        Object value = null;

        if (ClassUtil.isPojo(_value.getClass())) {
          Field f = ru.getField(field);
          OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
          if (oneAnn == null) oneAnn = f.getAnnotation(OneToOne.class);

          ManyToOne manyToOneAnn = null;
          if (oneAnn == null) {
            manyToOneAnn = getter.getAnnotation(ManyToOne.class);
            if (manyToOneAnn == null) manyToOneAnn = f.getAnnotation(ManyToOne.class);
          }

          if (oneAnn != null || manyToOneAnn != null) {
            JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
            if (joinColAnn == null) joinColAnn = f.getAnnotation(JoinColumn.class);

            if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
              String refCol = joinColAnn.referencedColumnName();
              String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
              ReflectUtil tarRu = new ReflectUtil(_value);
              Method tarFKGetter = tarRu.getGetter(refField);
              value = tarFKGetter.invoke(_value);
            } else {
              ReflectUtil tarRu = new ReflectUtil(_value);
              String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
              if (tarFKField != null) {
                Method tarFKGetter = tarRu.getGetter(tarFKField);
                value = tarFKGetter.invoke(_value);
              }
            }
          }

          if (value == null) continue;

        } else {
          value = _value;
        }

        if (values.length() > 0) values.append(", ");

        //				values.append(column).append(" = '").append(value).append("'");
        values.append(column).append(" = ? ");
        sql.args.add(value);
      } catch (Exception e) {
        throw new SqlCreateException(idGetter + " invoke exception " + e.toString(), e);
      }
    }

    //		String condition = new StringBuilder().append(idColumn).append(" =
    // ").append("'").append(idValue).append("'").toString();
    String condition = new StringBuilder().append(idColumn).append(" = ? ").toString();
    sql.args.add(idValue);
    sql.sql = String.format("UPDATE %s SET %s WHERE %s ;", table, values, condition);
    return sql;
  }
  private Sql makeSQL(T t) throws SqlCreateException {
    Sql sql = new Sql();
    Class<?> clazz = t.getClass();
    String table;
    String[] columns;
    String[] fields;
    Object[] values = null;
    String idColumn;
    String idField;
    Object idValue = null;
    HashMap<String, Object> map = null;
    if (Map.class.isAssignableFrom(clazz)) {
      map = (HashMap) t;
      table = (String) map.get("table");
      idColumn = (String) map.get("idColumn");
      idField = idColumn;
      idValue = map.get("idValue");
      columns = (String[]) map.get("columns");
      fields = columns;
      values = (Object[]) map.get("values");
    } else {
      table = ORMConfigBeanUtil.getTable(clazz, false);
      columns = ORMConfigBeanUtil.getColumns(clazz);
      fields = ORMConfigBeanUtil.getFields(clazz);
      idColumn = ORMConfigBeanUtil.getIdColumn(clazz);
      idField = ORMConfigBeanUtil.getIdField(clazz);
    }

    StringBuilder valuesSb = new StringBuilder();
    ReflectUtil ru = new ReflectUtil(t);
    try {
      if (map == null) {
        Method idGetter = ru.getGetter(idField);
        if (idGetter == null) throw new SqlCreateException("can not find id getter");
        idValue = idGetter.invoke(t);
      }

      for (int i = 0; i < columns.length; i++) {
        String column = columns[i];
        String field = fields[i];
        Object value = null;
        // id 字段不允许
        if (idColumn != null && idColumn.equalsIgnoreCase(column)) continue;

        if (map != null && values != null) {
          value = values[i];
        } else {
          Method getter = ru.getGetter(field);
          if (getter == null) continue;

          Object _value = getter.invoke(t);
          if (_value == null) continue;

          if (ClassUtil.isPojo(_value.getClass())) {
            Field f = ru.getField(field);
            OneToOne oneAnn = getter.getAnnotation(OneToOne.class);
            if (oneAnn == null) oneAnn = f.getAnnotation(OneToOne.class);

            ManyToOne manyToOneAnn = null;
            if (oneAnn == null) {
              manyToOneAnn = getter.getAnnotation(ManyToOne.class);
              if (manyToOneAnn == null) manyToOneAnn = f.getAnnotation(ManyToOne.class);
            }

            if (oneAnn != null || manyToOneAnn != null) {
              JoinColumn joinColAnn = getter.getAnnotation(JoinColumn.class);
              if (joinColAnn == null) joinColAnn = f.getAnnotation(JoinColumn.class);

              if (joinColAnn != null && joinColAnn.referencedColumnName().trim().length() > 0) {
                String refCol = joinColAnn.referencedColumnName();
                String refField = ORMConfigBeanUtil.getField(_value.getClass(), refCol);
                ReflectUtil tarRu = new ReflectUtil(_value);
                Method tarFKGetter = tarRu.getGetter(refField);
                value = tarFKGetter.invoke(_value);
              } else {
                ReflectUtil tarRu = new ReflectUtil(_value);
                String tarFKField = ORMConfigBeanUtil.getIdField(_value.getClass());
                if (tarFKField != null) {
                  Method tarFKGetter = tarRu.getGetter(tarFKField);
                  value = tarFKGetter.invoke(_value);
                }
              }
            }
            if (value == null) continue;
          } else value = _value;
        }

        if (valuesSb.length() > 0) valuesSb.append(",");

        //				valuesSb.append(column).append(" = '").append(value).append("'");
        valuesSb.append(column).append(" = ? ");
        sql.args.add(value);
      }
    } catch (Exception e) {
      throw new SqlCreateException("" + e.toString(), e);
    }

    //		String condition = new StringBuilder().append(idColumn).append(" =
    // ").append("'").append(idValue).append("'").toString();
    String condition = new StringBuilder().append(idColumn).append(" = ? ").toString();
    sql.args.add(idValue);
    sql.sql = String.format("UPDATE %s SET %s WHERE %s ;", table, valuesSb, condition);
    return sql;
  }
Exemplo n.º 3
0
  /** 多对一级联查询 1.获取当前idVal,然后作为条件查询出其外键值,接着通过其外键值查出主对象数据,注入到当前 */
  public void select() {
    if (this.fields == null || this.fields.size() == 0) return;
    if (idVal == null || "0".equals(idVal) || "".equals(idVal)) {
      return;
    }
    //		else if (DAOFactory.getSelectDAO(dsName).selectOne(t, this.idField) == null) {
    //			// 检查一下当前对象的ID是否存在于数据库
    //			return;
    //		}

    for (Field f : fields) {
      Method tarGetter = ru.getGetter(f.getName());
      if (tarGetter == null) continue;

      String fk = null;
      OneToOne ann = tarGetter.getAnnotation(OneToOne.class);
      if (ann == null) ann = f.getAnnotation(OneToOne.class);

      if (ann == null) {
        ManyToOne moAn = tarGetter.getAnnotation(ManyToOne.class);
        if (moAn == null) moAn = f.getAnnotation(ManyToOne.class);

        if (moAn == null) continue;
      }
      String refCol = null;
      JoinColumn joinCol = f.getAnnotation(JoinColumn.class);
      if (joinCol == null) joinCol = tarGetter.getAnnotation(JoinColumn.class);

      if (joinCol == null) fk = f.getName() + "_id";
      else {
        fk = joinCol.name();
        refCol = joinCol.referencedColumnName();
      }

      Class<?> tarClass = f.getType();

      if (refCol == null || refCol.trim().length() == 0)
        refCol = ORMConfigBeanUtil.getIdColumn(tarClass);

      String refField = ORMConfigBeanUtil.getField(tarClass, refCol);

      try {
        Object _tarObj = tarGetter.invoke(t);
        Object tarObj = null;
        boolean flag = false;
        if (_tarObj != null) {
          Method refFieldGetter = new ReflectUtil(_tarObj).getGetter(refField);
          if (refFieldGetter != null && refFieldGetter.invoke(_tarObj) != null)
            tarObj = DAOFactory.getSelectDAO(dsName).selectOne(_tarObj, refField);
          else flag = true;
        } else flag = true;

        if (flag) {
          // select * from {tarTable} where {referencedColumn} = (select {fk} from {table} where
          // {idColumn} = {idVal})
          String format = "select %s from %s where %s = (select %s from %s where %s = %s )";
          String tarTable = ORMConfigBeanUtil.getTable(tarClass, true);

          String sql =
              String.format(
                  format,
                  ORMConfigBeanUtil.getSelectAllColumn(tarClass),
                  tarTable,
                  refCol,
                  fk,
                  table,
                  idColumn,
                  idVal);
          List<?> tarList = DAOFactory.getSelectDAO(dsName).selectBySQL(tarClass, sql);
          if (tarList == null || tarList.size() == 0) continue;

          tarObj = tarList.get(0);
        }

        if (tarObj == null) continue;

        Method tarSetter = ru.getSetter(f.getName());
        tarSetter.invoke(t, tarObj);
      } catch (Exception e) {
        throw new DAOException("", e);
      }
    }
  }