class Sequel::SQL::Function
Represents an SQL function call.
Constants
- COMMA_ARRAY
- DISTINCT
- WILDCARD
Attributes
The array of arguments to pass to the function (may be blank)
The SQL function to call
Options for this function
Public Class Methods
Set the name and args for the function
# File lib/sequel/sql.rb 1390 def initialize(name, *args) 1391 _initialize(name, args, OPTS) 1392 end
Public Instance Methods
If no arguments are given, return a new function with the wildcard prepended to the arguments.
Sequel.function(:count).* # count(*)
# File lib/sequel/sql.rb 1402 def *(ce=(arg=false;nil)) 1403 if arg == false 1404 raise Error, "Cannot apply * to functions with arguments" unless args.empty? 1405 with_opts(:"*"=>true) 1406 else 1407 super(ce) 1408 end 1409 end
Return a new function with DISTINCT before the method arguments.
Sequel.function(:count, :col).distinct # count(DISTINCT col)
# File lib/sequel/sql.rb 1414 def distinct 1415 with_opts(:distinct=>true) 1416 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))
# File lib/sequel/sql.rb 1422 def filter(*args, &block) 1423 if args.length == 1 1424 args = args.first 1425 else 1426 args.freeze 1427 end 1428 1429 with_opts(:filter=>args, :filter_block=>block) 1430 end
Return a function which will use LATERAL when literalized:
Sequel.function(:foo, :col).lateral # LATERAL foo(col)
# File lib/sequel/sql.rb 1435 def lateral 1436 with_opts(:lateral=>true) 1437 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)
# File lib/sequel/sql.rb 1443 def order(*args) 1444 with_opts(:order=>args.freeze) 1445 end
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 1451 def over(window=OPTS) 1452 raise Error, "function already has a window applied to it" if opts[:over] 1453 window = Window.new(window) unless window.is_a?(Window) 1454 with_opts(:over=>window) 1455 end
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 1461 def quoted 1462 with_opts(:quoted=>true) 1463 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()
# File lib/sequel/sql.rb 1469 def unquoted 1470 with_opts(:quoted=>false) 1471 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
# File lib/sequel/sql.rb 1477 def with_ordinality 1478 with_opts(:with_ordinality=>true) 1479 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)
# File lib/sequel/sql.rb 1486 def within_group(*expressions) 1487 with_opts(:within_group=>expressions.freeze) 1488 end
Private Instance Methods
Set name, args, and opts
# File lib/sequel/sql.rb 1495 def _initialize(name, args, opts) 1496 @name = name 1497 @args = args.freeze 1498 @opts = opts.freeze 1499 freeze 1500 end
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 141 def inspect_new_method 142 :new! 143 end
Return a new function call with the given opts merged into the current opts.
# File lib/sequel/sql.rb 1503 def with_opts(opts) 1504 self.class.new!(name, args, @opts.merge(opts)) 1505 end