class Sequel::SQL::VirtualRow

The purpose of the VirtualRow class is to allow the easy creation of SQL identifiers and functions, in a way that leads to more compact code.

An instance of this class is yielded to the block supplied to Dataset#where, Dataset#order, and Dataset#select (and the other methods that accept a block and pass it to one of those methods). If the block doesn't take an argument, the block is instance_execed in the context of an instance of this class.

VirtualRow uses method_missing to return either an Identifier, Function depending on how it is called.

Function

Returned if any arguments are supplied, using the method name as the function name, and the arguments as the function arguments.

Identifier

Returned otherwise, using the method name.

If splitting symbols has been enabled (not the default), then method calls without arguments will return QualifiedIdentifier instances if the method call includes a double underscore.

Examples:

ds = DB[:t]

# Argument yielded to block
ds.where{|r| r.name < 2} # SELECT * FROM t WHERE (name < 2)

# Block without argument (instance_exec)
ds.where{name < 2} # SELECT * FROM t WHERE (name < 2)

# Functions
ds.where{is_active(1, 'arg2')} # SELECT * FROM t WHERE is_active(1, 'arg2')
ds.select{version.function} # SELECT version() FROM t
ds.select{count.function.*} # SELECT count(*) FROM t
ds.select{count(col1).distinct} # SELECT count(DISTINCT col1) FROM t

# Math Operators
ds.select{|o| o.+(1, :a).as(:b)} # SELECT (1 + a) AS b FROM t
ds.select{|o| o.-(2, :a).as(:b)} # SELECT (2 - a) AS b FROM t
ds.select{|o| o.*(3, :a).as(:b)} # SELECT (3 * a) AS b FROM t
ds.select{|o| o./(4, :a).as(:b)} # SELECT (4 / a) AS b FROM t

# Boolean Operators
ds.where{|o| o.&({a: 1}, :b)}    # SELECT * FROM t WHERE ((a = 1) AND b)
ds.where{|o| o.|({a: 1}, :b)}    # SELECT * FROM t WHERE ((a = 1) OR b)
ds.where{|o| o.~(a: 1)}        # SELECT * FROM t WHERE (a != 1)
ds.where{|o| o.~(a: 1, b: 2)} # SELECT * FROM t WHERE ((a != 1) OR (b != 2))

# Inequality Operators
ds.where{|o| o.>(1, :a)}  # SELECT * FROM t WHERE (1 > a)
ds.where{|o| o.<(2, :a)}  # SELECT * FROM t WHERE (2 < a)
ds.where{|o| o.>=(3, :a)} # SELECT * FROM t WHERE (3 >= a)
ds.where{|o| o.<=(4, :a)} # SELECT * FROM t WHERE (4 <= a)

For a more detailed explanation, see the Virtual Rows guide.

Public Class Methods

new() click to toggle source
     # File lib/sequel/sql.rb
1909 def initialize
1910   freeze
1911 end

Public Instance Methods

method_missing(m, *args) click to toggle source

Return an Identifier, QualifiedIdentifier, or Function, depending on arguments and whether a block is provided. Does not currently call the block. See the class level documentation.

     # File lib/sequel/sql.rb
1917 def method_missing(m, *args)
1918   if args.empty?
1919     if Sequel.split_symbols?
1920       table, column = m.to_s.split('__', 2)
1921       column ? QualifiedIdentifier.new(table, column) : Identifier.new(m)
1922     else
1923       Identifier.new(m)
1924     end
1925   else
1926     Function.new(m, *args)
1927   end
1928 end