module Sequel::MSSQL::EmulateLateralWithApply

  1. lib/sequel/extensions/mssql_emulate_lateral_with_apply.rb

Methods

Public Instance

  1. from
  2. join_table
  3. supports_lateral_subqueries?

Public Instance methods

from(*source, &block)

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

[show source]
   # File lib/sequel/extensions/mssql_emulate_lateral_with_apply.rb
58 def from(*source, &block)
59   virtual_row_columns(source, block)
60   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?
61   return super(*source, &nil) if !lateral || lateral.empty?
62 
63   ds = from(*source)
64   lateral.each do |l|
65     l = if l.is_a?(Sequel::SQL::AliasedExpression)
66       l.expression.clone(:lateral=>nil).as(l.alias)
67     else
68       l.clone(:lateral=>nil)
69     end
70     ds = ds.cross_apply(l)
71   end
72   ds
73 end
join_table(type, table, expr=nil, *)

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.

[show source]
   # File lib/sequel/extensions/mssql_emulate_lateral_with_apply.rb
37 def join_table(type, table, expr=nil, *)
38   if table.is_a?(Dataset) && table.opts[:lateral]
39     table = table.clone(:lateral=>nil)
40     case type
41     when :inner
42       type = :cross_apply
43       table = table.where(expr)
44       expr = nil
45     when :cross
46       type = :cross_apply
47     when :left, :left_outer
48       type = :outer_apply
49       table = table.where(expr)
50       expr = nil
51     end
52   end
53   super
54 end
supports_lateral_subqueries?()

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

[show source]
   # File lib/sequel/extensions/mssql_emulate_lateral_with_apply.rb
77 def supports_lateral_subqueries?
78   true
79 end