class Sequel::SQL::Function

Represents an SQL function call.

Constants

COMMA_ARRAY
DISTINCT
WILDCARD

Attributes

args[R]

The array of arguments to pass to the function (may be blank)

f[R]

The SQL function to call

name[R]

The SQL function to call

opts[R]

Options for this function

Public Class Methods

new(name, *args) click to toggle source

Set the name and args for the function

# File lib/sequel/sql.rb, line 1368
def initialize(name, *args)
  _initialize(name, args, OPTS)
end
new!(name, args, opts) click to toggle source
# File lib/sequel/sql.rb, line 1372
def self.new!(name, args, opts)
  allocate.send(:_initialize, name, args, opts)
end

Public Instance Methods

*(ce=(arg=false;nil)) click to toggle source

If no arguments are given, return a new function with the wildcard prepended to the arguments.

Sequel.function(:count).*  # count(*)
Calls superclass method
# File lib/sequel/sql.rb, line 1379
def *(ce=(arg=false;nil))
  if arg == false
    raise Error, "Cannot apply * to functions with arguments" unless args.empty?
    with_opts(:"*"=>true)
  else
    super(ce)
  end
end
distinct() click to toggle source

Return a new function with DISTINCT before the method arguments.

Sequel.function(:count, :col).distinct # count(DISTINCT col)
# File lib/sequel/sql.rb, line 1391
def distinct
  with_opts(:distinct=>true)
end
filter(*args, &block) click to toggle source

Return a new function with FILTER added to it, for filtered aggregate functions:

Sequel.function(:foo, :col).filter(:a=>1) # foo(col) FILTER (WHERE a = 1)
# File lib/sequel/sql.rb, line 1399
def filter(*args, &block)
  if args.length == 1
    args = args.first
  else
    args.freeze
  end

  with_opts(:filter=>args, :filter_block=>block)
end
lateral() click to toggle source

Return a function which will use LATERAL when literalized:

Sequel.function(:foo, :col).lateral # LATERAL foo(col)
# File lib/sequel/sql.rb, line 1412
def lateral
  with_opts(:lateral=>true)
end
order(*args) click to toggle source

Return a new function where the function will be ordered. Only useful for aggregate functions that are order dependent.

Sequel.function(:foo, :a).order(:a, Sequel.desc(:b)) # foo(a ORDER BY a, b DESC)
# File lib/sequel/sql.rb, line 1420
def order(*args)
  with_opts(:order=>args.freeze)
end
over(window=OPTS) click to toggle source

Return a new function with an OVER clause (making it a window function).

Sequel.function(:row_number).over(:partition=>:col) # row_number() OVER (PARTITION BY col)
# File lib/sequel/sql.rb, line 1427
def over(window=OPTS)
  raise Error, "function already has a window applied to it" if opts[:over]
  window = Window.new(window) unless window.is_a?(Window)
  with_opts(:over=>window)
end
quoted() click to toggle source

Return a new function where the function name will be quoted if the database supports quoted functions:

Sequel.function(:foo).quoted # "foo"()
# File lib/sequel/sql.rb, line 1437
def quoted
  with_opts(:quoted=>true)
end
unquoted() click to toggle source

Return a new function where the function name will not be quoted even if the database supports quoted functions:

Sequel[:foo].function.unquoted # foo()
# File lib/sequel/sql.rb, line 1445
def unquoted
  with_opts(:quoted=>false)
end
with_ordinality() click to toggle source

Return a new function that will use WITH ORDINALITY to also return a row number for every row the function returns:

Sequel.function(:foo).with_ordinality # foo() WITH ORDINALITY
# File lib/sequel/sql.rb, line 1453
def with_ordinality
  with_opts(:with_ordinality=>true)
end
within_group(*expressions) click to toggle source

Return a new function that uses WITHIN GROUP ordered by the given expression, useful for ordered-set and hypothetical-set aggregate functions:

Sequel.function(:rank, :a).within_group(:b, :c)
# rank(a) WITHIN GROUP (ORDER BY b, c)
# File lib/sequel/sql.rb, line 1462
def within_group(*expressions)
  with_opts(:within_group=>expressions.freeze)
end

Private Instance Methods

_initialize(name, args, opts) click to toggle source

Set args and opts

# File lib/sequel/sql.rb, line 1471
def _initialize(name, args, opts)
  @name = name
  @args = args.freeze
  @opts = opts.freeze
  freeze
end
inspect_new_method() click to toggle source

Function uses a new! method for creating functions with options, since ::new does not allow for an options hash.

# File lib/sequel/extensions/eval_inspect.rb, line 142
def inspect_new_method
  :new!
end
with_opts(opts) click to toggle source

Return a new function call with the given opts merged into the current opts.

# File lib/sequel/sql.rb, line 1479
def with_opts(opts)
  self.class.new!(name, args, Hash[@opts].merge!(opts))
end