code: private String buildCountQueryStr(Query query) {
      String queryStr = "select count(*) ";
      if (query instanceof SQLQuery) {
        queryStr += "from (" + query.getQueryString() + ")";
      } else {
        String lowerCaseQueryStr = query.getQueryString().toLowerCase();
        String selectWhich = lowerCaseQueryStr.substring(0, query.getQueryString().indexOf("from"));
        int indexOfDistinct = selectWhich.indexOf("distinct");
        int indexOfFrom = lowerCaseQueryStr.indexOf("from");
        // 如果含有distinct
        if (-1 != indexOfDistinct) {
          if (Strings.contains(selectWhich, ",")) {
            queryStr = "select count("
                + query.getQueryString().substring(indexOfDistinct, query.getQueryString().indexOf(",")) + ")";
          } else {
            queryStr = "select count(" + query.getQueryString().substring(indexOfDistinct, indexOfFrom) + ")";
          }
        }
        queryStr += query.getQueryString().substring(indexOfFrom);
      }
      return queryStr;
    }
docs: 构造查询记录数目的查询字符串
  
  @param query
  @return query string