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)

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
1373 def initialize(name, *args)
1374   _initialize(name, args, OPTS)
1375 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
1385 def *(ce=(arg=false;nil))
1386   if arg == false
1387     raise Error, "Cannot apply * to functions with arguments" unless args.empty?
1388     with_opts(:"*"=>true)
1389   else
1390     super(ce)
1391   end
1392 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
1397 def distinct
1398   with_opts(:distinct=>true)
1399 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
1405 def filter(*args, &block)
1406   if args.length == 1
1407     args = args.first
1408   else
1409     args.freeze
1410   end
1411 
1412   with_opts(:filter=>args, :filter_block=>block)
1413 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
1418 def lateral
1419   with_opts(:lateral=>true)
1420 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
1426 def order(*args)
1427   with_opts(:order=>args.freeze)
1428 end
over(window=OPTS) click to toggle source

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)
     # File lib/sequel/sql.rb
1434 def over(window=OPTS)
1435   raise Error, "function already has a window applied to it" if opts[:over]
1436   window = Window.new(window) unless window.is_a?(Window)
1437   with_opts(:over=>window)
1438 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
1444 def quoted
1445   with_opts(:quoted=>true)
1446 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][:bar].function.unquoted # foo.bar()
     # File lib/sequel/sql.rb
1452 def unquoted
1453   with_opts(:quoted=>false)
1454 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
1460 def with_ordinality
1461   with_opts(:with_ordinality=>true)
1462 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
1469 def within_group(*expressions)
1470   with_opts(:within_group=>expressions.freeze)
1471 end

Private Instance Methods

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

Set name, args, and opts

     # File lib/sequel/sql.rb
1478 def _initialize(name, args, opts)
1479   @name = name
1480   @args = args.freeze
1481   @opts = opts.freeze
1482   freeze
1483 end
inspect_new_method() click to toggle source

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

    # File lib/sequel/extensions/eval_inspect.rb
137 def inspect_new_method
138   :new!
139 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
1486 def with_opts(opts)
1487   self.class.new!(name, args, @opts.merge(opts))
1488 end