Module Sequel::EmulateOffsetWithReverseAndCount
In: lib/sequel/adapters/utils/emulate_offset_with_reverse_and_count.rb

Methods

empty?   select_sql  

Public Instance methods

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.

[Source]

    # File lib/sequel/adapters/utils/emulate_offset_with_reverse_and_count.rb, line 6
 6:     def empty?
 7:       if o = @opts[:offset]
 8:         unlimited.count <= o
 9:       else
10:         super
11:       end
12:     end

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.

[Source]

    # File lib/sequel/adapters/utils/emulate_offset_with_reverse_and_count.rb, line 20
20:     def select_sql
21:       return super unless o = @opts[:offset]
22: 
23:       order = @opts[:order] || default_offset_order
24:       if order.nil? || order.empty?
25:         raise(Error, "#{db.database_type} requires an order be provided if using an offset")
26:       end
27: 
28:       ds = unlimited
29:       row_count = @opts[:offset_total_count] || ds.clone(:append_sql=>'').count
30:       dsa1 = dataset_alias(1)
31: 
32:       if o.is_a?(Symbol) && @opts[:bind_vars] && (match = Sequel::Dataset::PreparedStatementMethods::PLACEHOLDER_RE.match(o.to_s))
33:         # Handle use of bound variable offsets.  Unfortunately, prepared statement
34:         # bound variable offsets cannot be handled, since the bound variable value
35:         # isn't available until later.
36:         s = match[1].to_sym
37:         if prepared_arg?(s)
38:           o = prepared_arg(s)
39:         end
40:       end
41: 
42:       reverse_offset = row_count - o
43:       ds = if reverse_offset > 0
44:         ds.limit(reverse_offset).
45:           reverse_order(*order).
46:           from_self(:alias=>dsa1).
47:           limit(@opts[:limit]).
48:           order(*order)
49:       else
50:         # Sequel doesn't allow a nonpositive limit.  If the offset
51:         # is greater than the number of rows, the empty result set
52:         # shuld be returned, so use a condition that is always false.
53:         ds.where(1=>0)
54:       end
55:       sql = @opts[:append_sql] || ''
56:       subselect_sql_append(sql, ds)
57:       sql
58:     end

[Validate]