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

Represents an SQL function call.

Methods

*   distinct   filter   lateral   new   order   over   quoted   unquoted   with_ordinality   within_group  

Constants

WILDCARD = LiteralString.new('*').freeze
DISTINCT = ["DISTINCT ".freeze].freeze
COMMA_ARRAY = [LiteralString.new(', ').freeze].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

Set the name and args for the function

[Source]

      # File lib/sequel/sql.rb, line 1313
1313:       def initialize(name, *args)
1314:         _initialize(name, args, OPTS)
1315:       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 1325
1325:       def *(ce=(arg=false;nil))
1326:         if arg == false
1327:           raise Error, "Cannot apply * to functions with arguments" unless args.empty?
1328:           with_opts("*""*"=>true)
1329:         else
1330:           super(ce)
1331:         end
1332:       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 1337
1337:       def distinct
1338:         with_opts(:distinct=>true)
1339:       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 1345
1345:       def filter(*args, &block)
1346:         if args.length == 1
1347:           args = args.first
1348:         else
1349:           args.freeze
1350:         end
1351: 
1352:         with_opts(:filter=>args, :filter_block=>block)
1353:       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 1358
1358:       def lateral
1359:         with_opts(:lateral=>true)
1360:       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 1366
1366:       def order(*args)
1367:         with_opts(:order=>args.freeze)
1368:       end

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)

[Source]

      # File lib/sequel/sql.rb, line 1374
1374:       def over(window=OPTS)
1375:         raise Error, "function already has a window applied to it" if opts[:over]
1376:         window = Window.new(window) unless window.is_a?(Window)
1377:         with_opts(:over=>window)
1378:       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 1384
1384:       def quoted
1385:         with_opts(:quoted=>true)
1386:       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 # foo.bar()

[Source]

      # File lib/sequel/sql.rb, line 1392
1392:       def unquoted
1393:         with_opts(:quoted=>false)
1394:       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 1400
1400:       def with_ordinality
1401:         with_opts(:with_ordinality=>true)
1402:       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 1409
1409:       def within_group(*expressions)
1410:         with_opts(:within_group=>expressions.freeze)
1411:       end

[Validate]