/**
  * 分页获取帖子列表
  *
  * @param page 页数,默认为1
  * @param type 参数名叫tab,默认是ask,如果传all,也会变成ask
  * @param limit 每页数量
  * @param mdrender 是否渲染md
  * @api {get} /yvr/api/v1/topics 获取帖子列表
  * @apiGroup Topic
  * @apiVersion 1.0.0
  * @apiParam {int} [page=1] 页数,默认为1
  * @apiParam {String} [tab=ask] 分类
  * @apiParam {int} [limit=10] 分页
  * @apiParam {boolean} [mdrender=true] 是否渲染Markdown
  * @apiSuccess {Object[]} data 帖子列表数据
  * @apiSuccess {String} data.id 唯一标示符
  * @apiSuccess {String} data.title 标题
  * @apiSuccess {String} data.tab 类型
  * @apiSuccess {String} data.content 内容
  * @apiSuccess {String} [data.last_reply_at] 最后回复时间
  * @apiSuccess {boolean} data.top 是否置顶
  * @apiSuccess {boolean} data.good 是否为精华帖
  * @apiSuccess {int} data.reply_count 总回复数量
  * @apiSuccess {int} data.visit_count 总浏览数量
  * @apiSuccess {Object} data.author 作者信息
  * @apiSuccess {String} data.author.id 作者id
  * @apiSuccess {String} data.author.loginname 作者登陆名
  */
 @GET
 @At
 @Aop("redis")
 public Object topics(
     @Param("page") int page,
     @Param("tab") String type,
     @Param("limit") int limit,
     @Param("mdrender") String mdrender) {
   if (page < 1) page = 1;
   if (limit < 0 || limit > pageSize) limit = pageSize;
   Pager pager = dao.createPager(page, limit);
   if (type == null) type = "ask";
   else if ("all".equals(type)) type = "ask";
   HashMap<Integer, UserProfile> authors = new HashMap<Integer, UserProfile>();
   List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
   Set<String> ids =
       jedis()
           .zrevrangeByScore(
               RKEY_TOPIC_UPDATE + type,
               System.currentTimeMillis(),
               0,
               pager.getOffset(),
               pager.getPageSize());
   for (String id : ids) {
     Topic topic = dao.fetch(Topic.class, id);
     if (topic == null) continue;
     list.add(_topic(topic, authors, mdrender));
   }
   return _map("data", list);
 }
Example #2
0
 public void formatQuery(Sql sql) {
   Pager pager = sql.getContext().getPager();
   if (null != pager && pager.getPageNumber() > 0) {
     sql.setSourceSql(
         sql.getSourceSql()
             + String.format(" LIMIT %d OFFSET %d", pager.getPageSize(), pager.getOffset()));
   }
 }
Example #3
0
 public void formatQuery(Sql sql) {
   Pager pager = sql.getContext().getPager();
   if (null != pager && pager.getPageNumber() > 0) {
     String pre = "SELECT * FROM (SELECT ROW_NUMBER() OVER() AS ROWNUM, T.* FROM (";
     String last =
         String.format(
             ") T) AS A WHERE ROWNUM BETWEEN %d AND %d",
             pager.getOffset() + 1, pager.getOffset() + pager.getPageSize());
     sql.setSourceSql(pre + sql.getSourceSql() + last);
   }
 }
Example #4
0
 public void formatQuery(Pojo pojo) {
   Pager pager = pojo.getContext().getPager();
   // 需要进行分页
   if (null != pager && pager.getPageNumber() > 0) {
     // 之前插入
     pojo.insertFirst(
         Pojos.Items.wrap(
             "SELECT * FROM (" + "SELECT ROW_NUMBER() OVER() AS ROWNUM, " + "T.* FROM ("));
     // 之后插入
     pojo.append(
         Pojos.Items.wrapf(
             ") T) AS A WHERE ROWNUM BETWEEN %d AND %d",
             pager.getOffset() + 1, pager.getOffset() + pager.getPageSize()));
   }
 }
Example #5
0
 public void formatQuery(Pojo pojo) {
   Pager pager = pojo.getContext().getPager();
   // 需要进行分页
   if (null != pager && pager.getPageNumber() > 0)
     pojo.append(Pojos.Items.wrapf(" LIMIT %d OFFSET %d", pager.getPageSize(), pager.getOffset()));
 }
Example #6
0
  private void _runSelect(Connection conn, DaoStatement st) throws SQLException {

    Object[][] paramMatrix = st.getParamMatrix();
    // -------------------------------------------------
    // 以下代码,就为了该死的游标分页!!
    // -------------------------------------------------
    int startRow = -1;
    int lastRow = -1;
    if (st.getContext().getResultSetType() == ResultSet.TYPE_SCROLL_INSENSITIVE) {
      Pager pager = st.getContext().getPager();
      if (pager != null) {
        startRow = pager.getOffset();
        lastRow = pager.getOffset() + pager.getPageSize();
      }
    }
    // -------------------------------------------------
    // 生成 Sql 语句
    String sql = st.toPreparedStatement();
    // 打印调试信息

    ResultSet rs = null;
    Statement stat = null;
    try {

      // 木有参数,直接运行
      if (null == paramMatrix || paramMatrix.length == 0 || paramMatrix[0].length == 0) {
        if (log.isDebugEnabled()) log.debug(st);
        stat = conn.createStatement(st.getContext().getResultSetType(), ResultSet.CONCUR_READ_ONLY);
        if (lastRow > 0) stat.setMaxRows(lastRow); // 游标分页,现在总行数
        if (st.getContext().getFetchSize() > 0) stat.setFetchSize(st.getContext().getFetchSize());
        rs = stat.executeQuery(sql);
      }
      // 有参数,用缓冲语句
      else {

        // 打印调试信息
        if (paramMatrix.length > 1) {
          if (log.isWarnEnabled())
            log.warnf("Drop last %d rows parameters for:\n%s", paramMatrix.length - 1, st);
        }
        if (log.isDebugEnabled()) {
          log.debug(st);
        }

        // 准备运行语句
        ValueAdaptor[] adaptors = st.getAdaptors();
        // 创建语句并设置参数
        stat =
            conn.prepareStatement(
                sql, st.getContext().getResultSetType(), ResultSet.CONCUR_READ_ONLY);
        if (lastRow > 0) stat.setMaxRows(lastRow);
        for (int i = 0; i < paramMatrix[0].length; i++) {
          adaptors[i].set((PreparedStatement) stat, paramMatrix[0][i], i + 1);
        }
        rs = ((PreparedStatement) stat).executeQuery();
      }
      if (startRow > 0) rs.absolute(startRow);
      // 执行回调
      st.onAfter(conn, rs);
    } finally {
      Daos.safeClose(stat, rs);
    }
    // 打印更详细的调试信息
    if (log.isTraceEnabled()) log.trace("...DONE");
  }