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!   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 1256
1256:       def initialize(name, *args)
1257:         @name = name
1258:         @args = args
1259:         @opts = OPTS
1260:       end

[Source]

      # File lib/sequel/sql.rb, line 1262
1262:       def self.new!(name, args, opts)
1263:         f = new(name, *args)
1264:         f.instance_variable_set(:@opts, opts)
1265:         f
1266:       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 1271
1271:       def *(ce=(arg=false;nil))
1272:         if arg == false
1273:           raise Error, "Cannot apply * to functions with arguments" unless args.empty?
1274:           with_opts("*""*"=>true)
1275:         else
1276:           super(ce)
1277:         end
1278:       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 1283
1283:       def distinct
1284:         with_opts(:distinct=>true)
1285:       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 1291
1291:       def filter(*args, &block)
1292:         args = args.first if args.length == 1
1293:         with_opts(:filter=>args, :filter_block=>block)
1294:       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 1299
1299:       def lateral
1300:         with_opts(:lateral=>true)
1301:       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 1306
1306:       def over(window=OPTS)
1307:         raise Error, "function already has a window applied to it" if opts[:over]
1308:         window = Window.new(window) unless window.is_a?(Window)
1309:         with_opts(:over=>window)
1310:       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 1316
1316:       def quoted
1317:         with_opts(:quoted=>true)
1318:       end

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

  Sequel.expr(:foo).function.unquoted # foo()

[Source]

      # File lib/sequel/sql.rb, line 1324
1324:       def unquoted
1325:         with_opts(:quoted=>false)
1326:       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 1332
1332:       def with_ordinality
1333:         with_opts(:with_ordinality=>true)
1334:       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 1341
1341:       def within_group(*expressions)
1342:         with_opts(:within_group=>expressions)
1343:       end

[Validate]