Class Sequel::SQL::VirtualRow
In: lib/sequel/sql.rb
Parent: BasicObject

The purpose of this class is to allow the easy creation of SQL identifiers and functions without relying on methods defined on Symbol. This is useful if another library defines the methods defined by Sequel, or if you are running on ruby 1.9.

An instance of this class is yielded to the block supplied to filter, order, and select. If the block doesn‘t take an argument, the block is instance_evaled in the context of a new instance of this class.

VirtualRow uses method_missing to return Identifiers, QualifiedIdentifiers, Functions, or WindowFunctions, depending on how it is called. If a block is not given, creates one of the following objects:

  • Function - returned if any arguments are supplied, using the method name as the function name, and the arguments as the function arguments.
  • QualifiedIdentifier - returned if the method name contains __, with the table being the part before __, and the column being the part after.
  • Identifier - returned otherwise, using the method name.

If a block is given, it returns either a Function or WindowFunction, depending on the first argument to the method. Note that the block is currently not called by the code, though this may change in a future version. If the first argument is:

  • no arguments given - uses a Function with no arguments.
  • :* - uses a Function with a literal wildcard argument (*), mostly useful for COUNT.
  • :distinct - uses a Function that prepends DISTINCT to the rest of the arguments, mostly useful for aggregate functions.
  • :over - uses a WindowFunction. If a second argument is provided, it should be a hash of options which are passed to Window (e.g. :window, :partition, :order, :frame). The arguments to the function itself should be specified as :*=>true for a wildcard, or via the :args option.

Examples:

  ds = DB[:t]
  # Argument yielded to block
  ds.filter{|r| r.name < 2} # SELECT * FROM t WHERE (name < 2)
  # Block without argument (instance_eval)
  ds.filter{name < 2} # SELECT * FROM t WHERE (name < 2)
  # Qualified identifiers
  ds.filter{table__column + 1 < 2} # SELECT * FROM t WHERE ((table.column + 1) < 2)
  # Functions
  ds.filter{is_active(1, 'arg2')} # SELECT * FROM t WHERE is_active(1, 'arg2')
  ds.select{version{}} # SELECT version() FROM t
  ds.select{count(:*){}} # SELECT count(*) FROM t
  ds.select{count(:distinct, col1){}} # SELECT count(DISTINCT col1) FROM t
  # Window Functions
  ds.select{rank(:over){}} # SELECT rank() OVER () FROM t
  ds.select{count(:over, :*=>true){}} # SELECT count(*) OVER () FROM t
  ds.select{sum(:over, :args=>col1, :partition=>col2, :order=>col3){}} # SELECT sum(col1) OVER (PARTITION BY col2 ORDER BY col3) FROM t

Methods

Constants

WILDCARD = LiteralString.new('*').freeze
QUESTION_MARK = LiteralString.new('?').freeze
COMMA_SEPARATOR = LiteralString.new(', ').freeze
DOUBLE_UNDERSCORE = '__'.freeze

Public Instance methods

Return Identifiers, QualifiedIdentifiers, Functions, or WindowFunctions, depending on arguments and whether a block is provided. Does not currently call the block. See the class level documentation.

[Source]

     # File lib/sequel/sql.rb, line 870
870:       def method_missing(m, *args, &block)
871:         if block
872:           if args.empty?
873:             Function.new(m)
874:           else
875:             case arg = args.shift
876:             when :*
877:               Function.new(m, WILDCARD)
878:             when :distinct
879:               Function.new(m, PlaceholderLiteralString.new("DISTINCT #{args.map{QUESTION_MARK}.join(COMMA_SEPARATOR)}", args))
880:             when :over
881:               opts = args.shift || {}
882:               fun_args = ::Kernel.Array(opts[:*] ? WILDCARD : opts[:args])
883:               WindowFunction.new(Function.new(m, *fun_args), Window.new(opts))
884:             else
885:               raise Error, 'unsupported VirtualRow method argument used with block'
886:             end
887:           end
888:         elsif args.empty?
889:           table, column = m.to_s.split(DOUBLE_UNDERSCORE, 2)
890:           column ? QualifiedIdentifier.new(table, column) : Identifier.new(m)
891:         else
892:           Function.new(m, *args)
893:         end
894:       end

[Validate]