コード例 #1
0
ファイル: BaseDAO.java プロジェクト: happyzhaosong/DataTiger
  private List<Object> selectDtoList(Class objClass, String originalSql, DataSource ds)
      throws Exception {
    List<Object> ret = new ArrayList<Object>();
    Connection conn = null;
    Exception ex = null;
    StringBuffer sqlBuf = new StringBuffer();

    try {
      sqlBuf.append(originalSql);

      this.appendWhereBuffer(sqlBuf);
      this.appendOrderByBuffer(sqlBuf, objClass);
      this.appendLimitBuffer(sqlBuf);

      String sql = sqlBuf.toString();

      LogTool.debugText("sql = " + sql, this.getClass().getName());

      conn = ds.getConnection();
      Statement stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery(sql);
      if (rs != null) {
        while (rs.next()) {
          if (ClassTool.isNullObj(objClass) || "list".equalsIgnoreCase(objClass.getName())) {
            ret.add(DBTool.getResultSetColumnInList(rs));
          } else {
            ret.add(objClass.cast(ClassTool.extractValueFromResultSet(objClass, rs)));
          }
        }
      }
    } catch (Exception e) {
      LogTool.logError(e, this.getClass().getName());
      LogTool.logText(sqlBuf.toString(), this.getClass().getName());
      ex = e;
    } finally {
      try {
        if (ex != null) {
          throw ex;
        } else {
          if (!ClassTool.isListEmpty(ret)) {
            if (ret.get(0) != null) {
              String selectCountSql = "";
              if (!StringTool.isEmpty(originalSql)) {
                selectCountSql = DBTool.getSelectCountSqlFromSelectSql(originalSql);
              } else {
                selectCountSql = DBTool.getSelectCountSqlFromObjectClass(objClass);
              }
              if (!StringTool.isEmpty(selectCountSql)) {
                long totalRecordsCountInThisSearch = this.selectDtoListCount(selectCountSql, conn);
                if (ClassTool.isNullObj(objClass) || "list".equalsIgnoreCase(objClass.getName())) {
                  if (ret.get(0) instanceof List) {
                    List tmpList = (List) ret.get(0);
                    if (!ClassTool.isListEmpty(tmpList)) {
                      BasePageDTO.class
                          .cast(tmpList.get(0))
                          .setTotalRecordsCountInThisSearch(totalRecordsCountInThisSearch);
                    }
                  }
                } else {
                  BasePageDTO.class
                      .cast(ret.get(0))
                      .setTotalRecordsCountInThisSearch(totalRecordsCountInThisSearch);
                }
              }
            }
          }
        }
      } catch (Exception e) {
        LogTool.logError(e, this.getClass().getName());
        ex = e;
      } finally {
        DBManager.getInstance().closeDBConn(conn);

        if (ex != null) {
          throw ex;
        }
        return ret;
      }
    }
  }