Method: Sequel::Dataset#count

Defined in:
lib/sequel/dataset/actions.rb

#count(arg = (no_arg=true), &block) ⇒ Object

Returns the number of records in the dataset. If an argument is provided, it is used as the argument to count. If a block is provided, it is treated as a virtual row, and the result is used as the argument to count.

DB[:table].count # SELECT count(*) AS count FROM table LIMIT 1
# => 3
DB[:table].count(:column) # SELECT count(column) AS count FROM table LIMIT 1
# => 2
DB[:table].count{foo(column)} # SELECT count(foo(column)) AS count FROM table LIMIT 1
# => 1

99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sequel/dataset/actions.rb', line 99

def count(arg=(no_arg=true), &block)
  if no_arg
    if block
      arg = Sequel.virtual_row(&block)
      aggregate_dataset.get{count(arg).as(:count)}
    else
      aggregate_dataset.get{count{}.*.as(:count)}.to_i
    end
  elsif block
    raise Error, 'cannot provide both argument and block to Dataset#count'
  else
    aggregate_dataset.get{count(arg).as(:count)}
  end
end