class Sequel::SQL::Function

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

Represents an SQL function call.


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


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
1376 def initialize(name, *args)
1377   _initialize(name, args, OPTS)
1378 end

Public Instance methods


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
1388 def *(ce=(arg=false;nil))
1389   if arg == false
1390     raise Error, "Cannot apply * to functions with arguments" unless args.empty?
1391     with_opts(:"*"=>true)
1392   else
1393     super(ce)
1394   end
1395 end

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
1400 def distinct
1401   with_opts(:distinct=>true)
1402 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
1408 def filter(*args, &block)
1409   if args.length == 1
1410     args = args.first
1411   else
1412     args.freeze
1413   end
1415   with_opts(:filter=>args, :filter_block=>block)
1416 end

Return a function which will use LATERAL when literalized:

Sequel.function(:foo, :col).lateral # LATERAL foo(col)
[show source]
     # File lib/sequel/sql.rb
1421 def lateral
1422   with_opts(:lateral=>true)
1423 end

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
1429 def order(*args)
1430   with_opts(:order=>args.freeze)
1431 end

Return a new function with an OVER clause (making it a window function). See Sequel::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
1437 def over(window=OPTS)
1438   raise Error, "function already has a window applied to it" if opts[:over]
1439   window = unless window.is_a?(Window)
1440   with_opts(:over=>window)
1441 end

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
1447 def quoted
1448   with_opts(:quoted=>true)
1449 end

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

Sequel[:foo][:bar].function.unquoted #
[show source]
     # File lib/sequel/sql.rb
1455 def unquoted
1456   with_opts(:quoted=>false)
1457 end

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
1463 def with_ordinality
1464   with_opts(:with_ordinality=>true)
1465 end

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
1472 def within_group(*expressions)
1473   with_opts(:within_group=>expressions.freeze)
1474 end