Example #1
0
  // 生成查询Sql,以主键为查询条件,如果有指定读入的键那只读该键
  private OperateSet generateReadOperateSet(Object obj, String... fieldnames) {
    Class<? extends Object> clazz = obj.getClass();
    // 获取tablecache中的对应的info
    String typeName = clazz.getName();
    TableInfo tbinfo = null;
    String autoKeyName = "";

    if (typeName != null) {
      tbinfo = oManager.getTableCache().getByTN(typeName);
    }

    if (tbinfo == null) {
      // 提示错误
      System.out.println(
          "[generateReadOperateSet error]-Model<" + typeName + "> has not registered!");
      return null;
    }
    String strSql = "select ";
    String strField = "";
    String strWhere = "";
    Vector<Object> param = new Vector<Object>();
    if (fieldnames.length != 0) {

      if (fieldnames.length == 1) {
        if (fieldnames[0].equals("")) {
          return null;
        }
      }

      for (String s : fieldnames) {
        strField += s + ",";
      }
      strField = strField.substring(0, strField.length() - 1);
      strSql += strField + " from " + tbinfo.getTableName();
      Vector<FieldInfo> fins = tbinfo.getAllFieldInfos();
      for (FieldInfo fin : fins) {
        // 以主键为查询条件,如果不是主键的继续,暂时不支持联合主键
        if (!fin.isPrimaryKey()) {
          continue;
        }
        autoKeyName = fin.getColumnName();
        strWhere += fin.getColumnName() + "=?";
        Field field = null;
        try {
          field = obj.getClass().getDeclaredField(fin.getFieldName());
        } catch (NoSuchFieldException e) {
          // TODO 自动生成的 catch 块
          this.oManager.deBugInfo(e.getMessage());
          return null;
        } catch (SecurityException e) {
          // TODO 自动生成的 catch 块
          this.oManager.deBugInfo(e.getMessage());
          return null;
        }
        field.setAccessible(true);

        try {
          param.add(field.get(obj));
        } catch (IllegalArgumentException e) {
          // TODO 自动生成的 catch 块
          this.oManager.deBugInfo(e.getMessage());
          return null;
        } catch (IllegalAccessException e) {
          // TODO 自动生成的 catch 块
          this.oManager.deBugInfo(e.getMessage());
          return null;
        }
      }

      if (strWhere.equals("")) {
        // 提示错误
        System.out.println(
            "[generateReadOperateSet error]-Model<"
                + typeName
                + "> has not set PrimaryKey to achieve this query!");
        return null;
      }

      strSql += " where " + strWhere + " limit 1";
      OperateSet oSet = new OperateSet(strSql, param, tbinfo);
      oSet.setAutoKeyName(autoKeyName);
      return oSet;
    } else {
      strSql += "* from " + tbinfo.getTableName();
      Vector<FieldInfo> fins = tbinfo.getAllFieldInfos();
      for (FieldInfo fin : fins) {
        // 以主键为查询条件,如果不是主键的继续,暂时不支持联合主键
        if (!fin.isPrimaryKey()) {
          continue;
        }
        autoKeyName = fin.getColumnName();
        strWhere += fin.getColumnName() + "=?";
        Field field = null;
        try {
          field = obj.getClass().getDeclaredField(fin.getFieldName());
        } catch (NoSuchFieldException e) {
          // TODO 自动生成的 catch 块
          this.oManager.deBugInfo(e.getMessage());
          return null;
        } catch (SecurityException e) {
          // TODO 自动生成的 catch 块
          this.oManager.deBugInfo(e.getMessage());
          return null;
        }
        field.setAccessible(true);

        try {
          param.add(field.get(obj));
        } catch (IllegalArgumentException e) {
          // TODO 自动生成的 catch 块
          this.oManager.deBugInfo(e.getMessage());
          return null;
        } catch (IllegalAccessException e) {
          // TODO 自动生成的 catch 块
          this.oManager.deBugInfo(e.getMessage());
          return null;
        }
      }

      if (strWhere.equals("")) {
        // 提示错误
        System.out.println(
            "[generateReadOperateSet error]-Model<"
                + typeName
                + "> has not set PrimaryKey to achieve this query!");
        return null;
      }

      strSql += " where " + strWhere + " limit 1";
      OperateSet oSet = new OperateSet(strSql, param, tbinfo);
      oSet.setAutoKeyName(autoKeyName);
      return oSet;
    }
  }
Example #2
0
  private OperateSet generateInsertOperateSet(Object obj) {
    Class<? extends Object> clazz = obj.getClass();
    // 获取tablecache中的对应的info
    String typeName = clazz.getName();
    TableInfo tbinfo = null;
    String autoKeyName = "";

    if (typeName != null) {
      tbinfo = oManager.getTableCache().getByTN(typeName);
    }

    if (tbinfo == null) {
      // 提示错误
      System.out.println(
          "[generateInsertOperateSet error]-Model<" + typeName + "> has not registered!");
      return null;
    }

    String strSql = "insert into " + tbinfo.getTableName();
    String strField = "";
    String strValue = "";
    Vector<Object> param = new Vector<Object>();
    Vector<FieldInfo> fins = tbinfo.getAllFieldInfos();
    for (FieldInfo fin : fins) {
      // 跳过自动增长的字段
      if (fin.isAutoGenerate()) {
        autoKeyName = fin.getColumnName();
        continue;
      }
      strField += fin.getColumnName() + ",";
      strValue += "?,";
      // 获取值
      Field fie = null;
      try {
        fie = clazz.getDeclaredField(fin.getFieldName());
        fie.setAccessible(true);
      } catch (NoSuchFieldException e) {
        // TODO 自动生成的 catch 块
        this.oManager.deBugInfo(e.getMessage());
        return null;
      } catch (SecurityException e) {
        // TODO 自动生成的 catch 块
        this.oManager.deBugInfo(e.getMessage());
        return null;
      }
      if (fie != null) {
        try {
          param.add(fie.get(obj));
        } catch (IllegalArgumentException e) {
          // TODO 自动生成的 catch 块
          this.oManager.deBugInfo(e.getMessage());
          return null;
        } catch (IllegalAccessException e) {
          // TODO 自动生成的 catch 块
          this.oManager.deBugInfo(e.getMessage());
          return null;
        }
      }
    }

    // 只有自增字段错误退出
    if (strField.equals("")) {
      System.out.println(
          "[generateInsertOperateSet error]-Model<" + typeName + "> only have autogenerate field!");
      return null;
    }
    // 去除尾部的,号
    strField = strField.substring(0, strField.length() - 1);
    strValue = strValue.substring(0, strValue.length() - 1);
    strSql += " (" + strField + ") values(" + strValue + ")";
    OperateSet os = new OperateSet(strSql, param, tbinfo);
    os.setAutoKeyName(autoKeyName);
    return os;
  }
Example #3
0
  /* @author Comdex
   * @see com.reflectsky.cozy.Ormer#read(java.lang.Object, java.lang.String[])
   */
  @Override
  public boolean read(Object obj, String... fieldnames) {
    // TODO 自动生成的方法存根
    OperateSet oSet = generateReadOperateSet(obj, fieldnames);
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    boolean isOk = false;
    if (oSet == null) {
      return isOk;
    }

    try {
      pstmt = conn.prepareStatement(oSet.getStrSql());

      ormDebug(oSet);
    } catch (SQLException e) {
      // TODO 自动生成的 catch 块
      this.oManager.deBugInfo(e.getMessage());
    }

    if (pstmt != null) {
      Vector<Object> param = oSet.getParam();
      for (int i = 0; i < param.size(); i++) {
        Object object = param.get(i);

        try {
          pstmt.setObject(i + 1, object);
        } catch (SQLException e) {
          // TODO 自动生成的 catch 块
          this.oManager.deBugInfo(e.getMessage());
        }
      }

      if (callbackobj != null) {
        Class<?> clazz = callbackobj.getClass();
        Method[] mtds = clazz.getDeclaredMethods();
        for (Method mtd : mtds) {
          if (mtd.getName().equals("beforeRead")) {
            Class<?>[] params = mtd.getParameterTypes();
            if (params.length == 2) {
              if (params[0] == obj.getClass() && params[1] == Ormer.class) {
                // 用于回调的注入参数Ormer
                Ormer ormer = this.oManager.NewOrm();
                mtd.setAccessible(true);
                try {
                  mtd.invoke(callbackobj, obj, ormer);
                } catch (IllegalAccessException e) {
                  // TODO 自动生成的 catch 块
                  this.oManager.deBugInfo(e.getMessage());

                } catch (IllegalArgumentException e) {
                  // TODO 自动生成的 catch 块
                  this.oManager.deBugInfo(e.getMessage());

                } catch (InvocationTargetException e) {
                  // TODO 自动生成的 catch 块
                  this.oManager.deBugInfo(e.getMessage());
                }
              }
            }
          }
        }
      }

      try {
        rs = pstmt.executeQuery();
        // 获取查询结果的值
        Vector<Object> values = new Vector<Object>();
        if (fieldnames.length != 0) {
          if (rs != null) {
            if (rs.next()) {
              for (String s : fieldnames) {
                values.add(rs.getObject(s));
              }
            }
          }
          // 获取fieldname对应的字段名
          Vector<String> fields = new Vector<String>();
          Vector<FieldInfo> fins = oSet.getTbinfo().getAllFieldInfos();
          for (String s : fieldnames) {
            for (FieldInfo fin : fins) {
              if (fin.getColumnName().equalsIgnoreCase(s)) {
                fields.add(fin.getFieldName());
              }
            }
          }

          // 为对应的field设值
          for (int i = 0; i < fields.size(); i++) {
            try {
              Field field = obj.getClass().getDeclaredField(fields.get(i));
              field.setAccessible(true);
              field.set(obj, values.get(i));
            } catch (NoSuchFieldException e) {
              // TODO 自动生成的 catch 块
              this.oManager.deBugInfo(e.getMessage());

            } catch (SecurityException e) {
              // TODO 自动生成的 catch 块
              this.oManager.deBugInfo(e.getMessage());

            } catch (Exception e) {
              // TODO: handle exception
              this.oManager.deBugInfo(e.getMessage());
            }
          }
          this.oManager.closeRs(rs);
          this.oManager.closeStmt(pstmt);
          return isOk;
        } else {
          // 获取该对象所有字段名
          Vector<String> fields = new Vector<String>();
          Vector<FieldInfo> fins = oSet.getTbinfo().getAllFieldInfos();
          if (rs != null) {
            if (rs.next()) {
              for (FieldInfo fin : fins) {
                values.add(rs.getObject(fin.getColumnName()));
              }
            }
          }

          // 为对应的Field设值
          for (int i = 0; i < fins.size(); i++) {
            Field field = null;
            try {
              field = obj.getClass().getDeclaredField(fins.get(i).getFieldName());
            } catch (NoSuchFieldException e) {
              // TODO 自动生成的 catch 块
              this.oManager.deBugInfo(e.getMessage());

            } catch (SecurityException e) {
              // TODO 自动生成的 catch 块
              this.oManager.deBugInfo(e.getMessage());
            }
            field.setAccessible(true);
            try {
              field.set(obj, values.get(i));

            } catch (IllegalArgumentException e) {
              // TODO 自动生成的 catch 块
              this.oManager.deBugInfo(e.getMessage());

            } catch (IllegalAccessException e) {
              // TODO 自动生成的 catch 块
              this.oManager.deBugInfo(e.getMessage());
            }
          }
          this.oManager.closeRs(rs);
          this.oManager.closeStmt(pstmt);

          if (callbackobj != null) {
            Class<?> clazz = callbackobj.getClass();
            Method[] mtds = clazz.getDeclaredMethods();
            for (Method mtd : mtds) {
              if (mtd.getName().equals("afterRead")) {
                Class<?>[] params = mtd.getParameterTypes();
                if (params.length == 2) {
                  if (params[0] == obj.getClass() && params[1] == Ormer.class) {
                    // 用于回调的注入参数Ormer
                    Ormer ormer = this.oManager.NewOrm();
                    mtd.setAccessible(true);
                    try {
                      mtd.invoke(callbackobj, obj, ormer);
                    } catch (IllegalAccessException e) {
                      // TODO 自动生成的 catch 块
                      this.oManager.deBugInfo(e.getMessage());

                    } catch (IllegalArgumentException e) {
                      // TODO 自动生成的 catch 块
                      this.oManager.deBugInfo(e.getMessage());

                    } catch (InvocationTargetException e) {
                      // TODO 自动生成的 catch 块
                      this.oManager.deBugInfo(e.getMessage());
                    }
                  }
                }
              }
            }
          }

          this.oManager.closeRs(rs);
          this.oManager.closeStmt(pstmt);
          return isOk;
        }
      } catch (SQLException e) {
        // TODO 自动生成的 catch 块
        this.oManager.deBugInfo(e.getMessage());
      }
    }
    if (rs != null) {
      this.oManager.closeRs(rs);
    }
    if (pstmt != null) {
      this.oManager.closeStmt(pstmt);
    }

    return isOk;
  }