代码之家  ›  专栏  ›  技术社区  ›  Jamal Hansen

确定将由rufus tokyo查询返回的记录计数的最佳方法是什么?

  •  2
  • Jamal Hansen  · 技术社区  · 15 年前

    我想确定在运行查询之前,东京内阁表上的查询将返回的记录数。我使用rufus tokyo ruby gem作为界面。最好的方法是什么?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Jamal Hansen    15 年前

    仔细查看Github上的代码,我想我找到了答案。它将使用返回记录计数的数据库查询计数方法。以下是使用查询计数分页记录的示例:

      db = Rufus::Tokyo::Table.new(@file)
    
      begin
        # Need to find the total number of records that would be returned.
        # could use db#size, except that our query filters out records after today.
    
        total_count = db.query_count { |q|
          q.add_condition 'date', :numle, @today
          q.order_by 'date', :strdesc
        }
    
        pager = IndexCards::Pager.new requested_page_num, total_count
    
        # Now let's pull the slice that we want.
        results = db.query { |q|
          q.add_condition 'date', :numle, @today
          q.order_by 'date', :strdesc
          q.limit(pager.limit, pager.offset)
        }
      ensure
        db.close
      end