Class | Sequel::SQL::Function |
In: |
lib/sequel/extensions/eval_inspect.rb
lib/sequel/sql.rb |
Parent: | Object |
Represents an SQL function call.
WILDCARD | = | LiteralString.new('*').freeze |
DISTINCT | = | ["DISTINCT ".freeze].freeze |
COMMA_ARRAY | = | [LiteralString.new(', ').freeze].freeze |
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 |
Set the name and args for the function
# File lib/sequel/sql.rb, line 1313 1313: def initialize(name, *args) 1314: _initialize(name, args, OPTS) 1315: end
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, 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 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, 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)
# 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)
# 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 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, 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 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, line 1409 1409: def within_group(*expressions) 1410: with_opts(:within_group=>expressions.freeze) 1411: end