Class Sequel::SQL::Function
In: lib/sequel/sql.rb
lib/sequel/extensions/eval_inspect.rb
Parent: GenericExpression

Represents an SQL function call.

Methods

*   distinct   filter   lateral   new   new!   order   over   quoted   unquoted   with_ordinality   within_group  

Constants

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

External Aliases

name -> f

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

Set the name and args for the function

[Source]

      # File lib/sequel/sql.rb, line 1327
1327:       def initialize(name, *args)
1328:         @name = name
1329:         @args = args
1330:         @opts = OPTS
1331:       end

[Source]

      # File lib/sequel/sql.rb, line 1333
1333:       def self.new!(name, args, opts)
1334:         f = new(name, *args)
1335:         f.instance_variable_set(:@opts, opts)
1336:         f
1337:       end

Public Instance methods

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

  Sequel.function(:count).*  # count(*)

[Source]

      # File lib/sequel/sql.rb, line 1342
1342:       def *(ce=(arg=false;nil))
1343:         if arg == false
1344:           raise Error, "Cannot apply * to functions with arguments" unless args.empty?
1345:           with_opts("*""*"=>true)
1346:         else
1347:           super(ce)
1348:         end
1349:       end

Return a new function with DISTINCT before the method arguments.

  Sequel.function(:count, :col).distinct # count(DISTINCT col)

[Source]

      # File lib/sequel/sql.rb, line 1354
1354:       def distinct
1355:         with_opts(:distinct=>true)
1356:       end

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)

[Source]

      # File lib/sequel/sql.rb, line 1362
1362:       def filter(*args, &block)
1363:         args = args.first if args.length == 1
1364:         with_opts(:filter=>args, :filter_block=>block)
1365:       end

Return a function which will use LATERAL when literalized:

  Sequel.function(:foo, :col).lateral # LATERAL foo(col)

[Source]

      # File lib/sequel/sql.rb, line 1370
1370:       def lateral
1371:         with_opts(:lateral=>true)
1372:       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)

[Source]

      # File lib/sequel/sql.rb, line 1378
1378:       def order(*args)
1379:         with_opts(:order=>args)
1380:       end

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)

[Source]

      # File lib/sequel/sql.rb, line 1385
1385:       def over(window=OPTS)
1386:         raise Error, "function already has a window applied to it" if opts[:over]
1387:         window = Window.new(window) unless window.is_a?(Window)
1388:         with_opts(:over=>window)
1389:       end

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

  Sequel.function(:foo).quoted # "foo"()

[Source]

      # File lib/sequel/sql.rb, line 1395
1395:       def quoted
1396:         with_opts(:quoted=>true)
1397:       end

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

  Sequel[:foo].function.unquoted # foo()

[Source]

      # File lib/sequel/sql.rb, line 1403
1403:       def unquoted
1404:         with_opts(:quoted=>false)
1405:       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

[Source]

      # File lib/sequel/sql.rb, line 1411
1411:       def with_ordinality
1412:         with_opts(:with_ordinality=>true)
1413:       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)

[Source]

      # File lib/sequel/sql.rb, line 1420
1420:       def within_group(*expressions)
1421:         with_opts(:within_group=>expressions)
1422:       end

[Validate]