class Sequel::SQL::VirtualRow

  1. lib/sequel/sql.rb
Superclass: BasicObject

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.

Methods

Public Class

  1. new

Public Instance

  1. method_missing

Included modules

  1. OperatorBuilders

Public Class methods

new()
[show source]
     # File lib/sequel/sql.rb
1918 def initialize
1919   freeze
1920 end

Public Instance methods

method_missing(m, *args)

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.

[show source]
     # File lib/sequel/sql.rb
1927 def method_missing(m, *args)
1928   if args.empty?
1929     if Sequel.split_symbols?
1930       table, column = m.to_s.split('__', 2)
1931       column ? QualifiedIdentifier.new(table, column) : Identifier.new(m)
1932     else
1933       Identifier.new(m)
1934     end
1935   else
1936     Function.new(m, *args)
1937   end
1938 end