File: implicit_subquery.rb

package info (click to toggle)
ruby-sequel 5.63.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,408 kB
  • sloc: ruby: 113,747; makefile: 3
file content (48 lines) | stat: -rw-r--r-- 1,562 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
46
47
48
# frozen-string-literal: true
#
# The implicit_subquery extension changes most dataset methods that
# return modified datasets to implicitly call from_self if the database
# currently uses raw SQL.  Sequel's by default does not do this:
#
#   DB["SELECT * FROM table"].select(:column).sql
#   # => "SELECT * FROM table"
#
# With this extension, datasets that use raw SQL are implicitly wrapped
# in a subquery:
#
#   DB["SELECT * FROM table"].select(:column).sql
#   # => "SELECT column FROM (SELECT * FROM table) AS t1"
#
# To add this extension to an existing dataset:
#
#   ds = ds.extension(:implicit_subquery)
#
# To set this as the default behavior for all datasets on a single database:
#
#   DB.extension(:implicit_subquery)
#
# Related module: Sequel::Dataset::ImplicitSubquery

#
module Sequel
  class Dataset
    module ImplicitSubquery
      exceptions = [:add_graph_aliases, :filter, :from, :from_self, :naked, :or, :order_more,
                    :qualify, :reverse, :reverse_order, :select_all, :select_more, :server,
                    :set_graph_aliases, :unfiltered, :ungraphed, :ungrouped, :unlimited, :unordered,
                    :with_sql]
      additions = [:join_table]
      (Dataset::QUERY_METHODS - Dataset::JOIN_METHODS - exceptions + additions).each do |meth|
        define_method(meth) do |*a, &b|
          if opts[:sql]
            from_self.public_send(meth, *a, &b)
          else
            super(*a, &b)
          end
        end
      end
    end

    register_extension(:implicit_subquery, ImplicitSubquery)
  end
end