class Sequel::SQL::Function

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

Represents an SQL function call.

Constants

COMMA_ARRAY = [LiteralString.new(', ').freeze].freeze  
DISTINCT = ["DISTINCT ".freeze].freeze  
WILDCARD = LiteralString.new('*').freeze  

Attributes

args [R]

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

name [R]

The SQL function to call

opts [R]

Options for this function

Public Class methods

new (name, *args)

Set the name and args for the function

[show source]
# File lib/sequel/sql.rb, line 1310
def initialize(name, *args)
  _initialize(name, args, OPTS)
end

Public Instance methods

* (ce=(arg=false;nil))

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

Sequel.function(:count).*  # count(*)
[show source]
# File lib/sequel/sql.rb, line 1322
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 ()

Return a new function with DISTINCT before the method arguments.

Sequel.function(:count, :col).distinct # count(DISTINCT col)
[show source]
# File lib/sequel/sql.rb, line 1334
def distinct
  with_opts(:distinct=>true)
end
filter (*args, &block)

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))
[show source]
# File lib/sequel/sql.rb, line 1342
def filter(*args, &block)
  if args.length == 1
    args = args.first
  else
    args.freeze
  end

  with_opts(:filter=>args, :filter_block=>block)
end
lateral ()

Return a function which will use LATERAL when literalized:

Sequel.function(:foo, :col).lateral # LATERAL foo(col)
[show source]
# File lib/sequel/sql.rb, line 1355
def lateral
  with_opts(:lateral=>true)
end
order (*args)

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)
[show source]
# File lib/sequel/sql.rb, line 1363
def order(*args)
  with_opts(:order=>args.freeze)
end
over (window=OPTS)

Return a new function with an OVER clause (making it a window function). See {SQL::Window} for the list of options over can receive.

Sequel.function(:row_number).over(partition: :col) # row_number() OVER (PARTITION BY col)
[show source]
# File lib/sequel/sql.rb, line 1371
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 ()

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

Sequel.function(:foo).quoted # "foo"()
[show source]
# File lib/sequel/sql.rb, line 1381
def quoted
  with_opts(:quoted=>true)
end
unquoted ()

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

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

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
[show source]
# File lib/sequel/sql.rb, line 1397
def with_ordinality
  with_opts(:with_ordinality=>true)
end
within_group (*expressions)

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)
[show source]
# File lib/sequel/sql.rb, line 1406
def within_group(*expressions)
  with_opts(:within_group=>expressions.freeze)
end