class Sequel::SQL::VirtualRow
- For a more detailed explanation, see the Virtual Rows guide.
ds.select{sum(:over, :args=>col1, :partition=>col2, :order=>col3){}} # SELECT sum(col1) OVER (PARTITION BY col2 ORDER BY col3) FROM t
ds.select{count(:over, :*=>true){}} # SELECT count(*) OVER () FROM t
ds.select{rank(:over){}} # SELECT rank() OVER () FROM t
# Window Functions
ds.select{count(:distinct, col1){}} # SELECT count(DISTINCT col1) FROM t
ds.select{count(:*){}} # SELECT count(*) FROM t
ds.select{version{}} # SELECT version() FROM t
ds.filter{is_active(1, ‘arg2’)} # SELECT * FROM t WHERE is_active(1, ‘arg2’)
# Functions
ds.filter{table__column + 1 < 2} # SELECT * FROM t WHERE ((table.column + 1) < 2)
# Qualified identifiers
ds.filter{name < 2} # SELECT * FROM t WHERE (name < 2)
# Block without argument (instance_eval)
ds.filter{|r| r.name < 2} # SELECT * FROM t WHERE (name < 2)
# Argument yielded to block
ds = DB[:t]
Examples:
the:argsoption.
arguments to the function itself should be specified as:*=>truefor a wildcard, or via
of options which are passed to Window (with possible keys :window, :partition, :order, and :frame). The
:over - creates a
WindowFunction. If a second argument is provided, it should be a hash
useful for aggregate functions.
:distinct - creates a
Functionthat prepends DISTINCT to the rest of the arguments, mostly
:* - creates a
Functionwith a literal wildcard argument (*), mostly useful for COUNT.
no arguments given - creates a
Functionwith no arguments.
this may change in a future version. If the first argument is:
argument to the method. Note that the block is currently not called by the code, though
If a block is given, it returns either aFunctionorWindowFunction, depending on the firstIdentifier - Returned otherwise, using the method name.
table being the part before __, and the column being the part after.QualifiedIdentifier - Returned if the method name contains __, with the
as the function name, and the arguments as the function arguments.Function -
Returned if any arguments are supplied, using the method name
If a block is not given, creates one of the following objects:
depending on how it is called.VirtualRowusesmethod_missingto return either anIdentifier,QualifiedIdentifier,Function, orWindowFunction,
a new instance of this class.
If the block doesn’t take an argument, the block is instance_evaled in the context of
(and the other methods that accept a block and pass it to one of those methods).
An instance of this class is yielded to the block supplied toDataset#filter,Dataset#order, andDataset#select
core extensions.
the methods defined by Sequel, if you are running on ruby 1.9, or if you are not using the
without relying on methods defined onSymbol. This is useful if another library defines
The purpose of theVirtualRowclass is to allow the easy creation of SQL identifiers and functions
- Returned if the method name contains __, with the
- Returned otherwise, using the method name.
- creates a
- creates a
- creates a
- creates a
def method_missing(m, *args, &block)
on arguments and whether a block is provided. Does not currently call the block.
Return an +Identifier+, +QualifiedIdentifier+, +Function+, or +WindowFunction+, depending
def method_missing(m, *args, &block) if block if args.empty? Function.new(m) else case arg = args.shift when :* Function.new(m, WILDCARD) when :distinct Function.new(m, PlaceholderLiteralString.new("DISTINCT #{args.map{QUESTION_MARK}.join(COMMA_SEPARATOR)}", args)) when :over opts = args.shift || {} fun_args = ::Kernel.Array(opts[:*] ? WILDCARD : opts[:args]) WindowFunction.new(Function.new(m, *fun_args), Window.new(opts)) else raise Error, 'unsupported VirtualRow method argument used with block' end end elsif args.empty? table, column = m.to_s.split(DOUBLE_UNDERSCORE, 2) column ? QualifiedIdentifier.new(table, column) : Identifier.new(m) else Function.new(m, *args) end end