Represents an SQL
function call.
Methods
Public Class
Public Instance
Constants
COMMA_ARRAY | = | [LiteralString.new(', ').freeze].freeze | ||
DISTINCT | = | ["DISTINCT ".freeze].freeze | ||
WILDCARD | = | LiteralString.new('*').freeze |
Attributes
Public Class methods
Set the name and args for the function
# File lib/sequel/sql.rb 1385 def initialize(name, *args) 1386 _initialize(name, args, OPTS) 1387 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 1397 def *(ce=(arg=false;nil)) 1398 if arg == false 1399 raise Error, "Cannot apply * to functions with arguments" unless args.empty? 1400 with_opts(:"*"=>true) 1401 else 1402 super(ce) 1403 end 1404 end
Return a new function with DISTINCT
before the method arguments.
Sequel.function(:count, :col).distinct # count(DISTINCT col)
# File lib/sequel/sql.rb 1409 def distinct 1410 with_opts(:distinct=>true) 1411 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 1417 def filter(*args, &block) 1418 if args.length == 1 1419 args = args.first 1420 else 1421 args.freeze 1422 end 1423 1424 with_opts(:filter=>args, :filter_block=>block) 1425 end
Return a function which will use LATERAL when literalized:
Sequel.function(:foo, :col).lateral # LATERAL foo(col)
# File lib/sequel/sql.rb 1430 def lateral 1431 with_opts(:lateral=>true) 1432 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 1438 def order(*args) 1439 with_opts(:order=>args.freeze) 1440 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 1446 def over(window=OPTS) 1447 raise Error, "function already has a window applied to it" if opts[:over] 1448 window = Window.new(window) unless window.is_a?(Window) 1449 with_opts(:over=>window) 1450 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 1456 def quoted 1457 with_opts(:quoted=>true) 1458 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 1464 def unquoted 1465 with_opts(:quoted=>false) 1466 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 1472 def with_ordinality 1473 with_opts(:with_ordinality=>true) 1474 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 1481 def within_group(*expressions) 1482 with_opts(:within_group=>expressions.freeze) 1483 end