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
1361 def initialize(name, *args)
1362   _initialize(name, args, OPTS)
1363 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
1373 def *(ce=(arg=false;nil))
1374   if arg == false
1375     raise Error, "Cannot apply * to functions with arguments" unless args.empty?
1376     with_opts(:"*"=>true)
1377   else
1378     super(ce)
1379   end
1380 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
1385 def distinct
1386   with_opts(:distinct=>true)
1387 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
1393 def filter(*args, &block)
1394   if args.length == 1
1395     args = args.first
1396   else
1397     args.freeze
1398   end
1399 
1400   with_opts(:filter=>args, :filter_block=>block)
1401 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
1406 def lateral
1407   with_opts(:lateral=>true)
1408 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
1414 def order(*args)
1415   with_opts(:order=>args.freeze)
1416 end
over (window=OPTS)

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
1422 def over(window=OPTS)
1423   raise Error, "function already has a window applied to it" if opts[:over]
1424   window = Window.new(window) unless window.is_a?(Window)
1425   with_opts(:over=>window)
1426 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
1432 def quoted
1433   with_opts(:quoted=>true)
1434 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
1440 def unquoted
1441   with_opts(:quoted=>false)
1442 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
1448 def with_ordinality
1449   with_opts(:with_ordinality=>true)
1450 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
1457 def within_group(*expressions)
1458   with_opts(:within_group=>expressions.freeze)
1459 end