Module: Sequel::MSSQL::EmulateLateralWithApply

Defined in:
lib/sequel/extensions/mssql_emulate_lateral_with_apply.rb

Instance Method Summary collapse

Instance Method Details

#from(*source, &block) ⇒ Object

When a FROM entry uses a LATERAL subquery, convert that entry into a CROSS APPLY.


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/sequel/extensions/mssql_emulate_lateral_with_apply.rb', line 58

def from(*source, &block)
  virtual_row_columns(source, block)
  lateral, source = source.partition{|t| t.is_a?(Sequel::Dataset) && t.opts[:lateral] || (t.is_a?(Sequel::SQL::AliasedExpression) && t.expression.is_a?(Sequel::Dataset) && t.expression.opts[:lateral])} unless source.empty?
  return super(*source, &nil) if !lateral || lateral.empty?

  ds = from(*source)
  lateral.each do |l|
    l = if l.is_a?(Sequel::SQL::AliasedExpression)
      l.expression.clone(:lateral=>nil).as(l.alias)
    else
      l.clone(:lateral=>nil)
    end
    ds = ds.cross_apply(l)
  end
  ds
end

#join_table(type, table, expr = nil) ⇒ Object

If the table is a dataset that uses LATERAL, convert it to a CROSS APPLY if it is a INNER or CROSS JOIN, and an OUTER APPLY if it is a LEFT JOIN.


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sequel/extensions/mssql_emulate_lateral_with_apply.rb', line 37

def join_table(type, table, expr=nil, *)
  if table.is_a?(Dataset) && table.opts[:lateral]
    table = table.clone(:lateral=>nil)
    case type
    when :inner
      type = :cross_apply
      table = table.where(expr)
      expr = nil
    when :cross
      type = :cross_apply
    when :left, :left_outer
      type = :outer_apply
      table = table.where(expr)
      expr = nil
    end
  end
  super
end

#supports_lateral_subqueries?Boolean

MSSQL can emulate lateral subqueries via CROSS/OUTER APPLY when using this extension.


77
78
79
# File 'lib/sequel/extensions/mssql_emulate_lateral_with_apply.rb', line 77

def supports_lateral_subqueries?
  true
end