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
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/sequel/dataset/actions.rb', line 129 def count(arg=(no_arg=true), &block) if no_arg && !block cached_dataset(:_count_ds) do aggregate_dataset.select(COUNT_SELECT).single_value_ds end.single_value!.to_i else if block if no_arg arg = Sequel.virtual_row(&block) else raise Error, 'cannot provide both argument and block to Dataset#count' end end _aggregate(:count, arg) end end |