Module: Sequel::EmulateOffsetWithRowNumber

Defined in:
lib/engine2/post_bootstrap.rb

Instance Method Summary collapse

Instance Method Details

#default_offset_orderObject



47
48
49
# File 'lib/engine2/post_bootstrap.rb', line 47

def default_offset_order
    model.primary_keys_qualified
end

#select_sqlObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/engine2/post_bootstrap.rb', line 21

def select_sql
  return super unless emulate_offset_with_row_number?
  offset = @opts[:offset]
  order = @opts[:order]
  if require_offset_order?
    order ||= default_offset_order
    if order.nil? || order.empty?
      raise(Error, "#{db.database_type} requires an order be provided if using an offset")
    end
  end
  # columns = clone(:append_sql=>'', :placeholder_literal_null=>true).columns
  columns = SequelFixes.fix_aliased_expression(clone(:append_sql=>'', :placeholder_literal_null=>true))
  dsa1 = dataset_alias(1)
  rn = row_number_column
  sql = @opts[:append_sql] || String.new
  subselect_sql_append(sql, unlimited.
    unordered.
    select_append{:ROW_NUMBER.sql_function.over(:order=>order).as(rn)}.
    from_self(:alias=>dsa1).
    select(*columns).
    limit(@opts[:limit]).
    where(SQL::Identifier.new(rn) > offset).
    order(rn))
  sql
end