module Sequel::EmulateOffsetWithReverseAndCount
Public Instance Methods
empty?()
click to toggle source
Make empty? work with an offset with an order. By default it would break since the order would be based on a column that empty does not select.
Calls superclass method
# File lib/sequel/adapters/utils/emulate_offset_with_reverse_and_count.rb 8 def empty? 9 if o = @opts[:offset] 10 unlimited.count <= o 11 else 12 super 13 end 14 end
select_sql()
click to toggle source
Emulate OFFSET support using reverse order in a subselect, requiring a count of the number of rows.
If offset is used, an order must be provided, since it needs to be reversed in the subselect. Note that the order needs to be unambiguous to work correctly, and you must select all columns that you are ordering on.
Calls superclass method
# File lib/sequel/adapters/utils/emulate_offset_with_reverse_and_count.rb 22 def select_sql 23 return super if @opts[:sql] 24 return super unless o = @opts[:offset] 25 26 order = @opts[:order] || default_offset_order 27 if order.nil? || order.empty? 28 raise(Error, "#{db.database_type} requires an order be provided if using an offset") 29 end 30 31 ds = unlimited 32 row_count = @opts[:offset_total_count] || ds.clone(:append_sql=>String.new, :placeholder_literal_null=>true).count 33 dsa1 = dataset_alias(1) 34 35 if o.is_a?(Symbol) && @opts[:bind_vars] && (match = /\A\$(.*)\z/.match(o.to_s)) 36 # Handle use of bound variable offsets. Unfortunately, prepared statement 37 # bound variable offsets cannot be handled, since the bound variable value 38 # isn't available until later. 39 s = match[1].to_sym 40 if prepared_arg?(s) 41 o = prepared_arg(s) 42 end 43 end 44 45 reverse_offset = row_count - o 46 ds = if reverse_offset > 0 47 ds.limit(reverse_offset). 48 reverse(*order). 49 from_self(:alias=>dsa1). 50 limit(@opts[:limit]). 51 order(*order) 52 else 53 # Sequel doesn't allow a nonpositive limit. If the offset 54 # is greater than the number of rows, the empty result set 55 # shuld be returned, so use a condition that is always false. 56 ds.where(1=>0) 57 end 58 sql = @opts[:append_sql] || String.new 59 subselect_sql_append(sql, ds) 60 sql 61 end
Private Instance Methods
default_offset_order()
click to toggle source
The default order to use for datasets with offsets, if no order is defined. By default, orders by all of the columns in the dataset.
# File lib/sequel/adapters/utils/emulate_offset_with_reverse_and_count.rb 73 def default_offset_order 74 clone(:append_sql=>String.new, :offset=>nil).columns 75 end