Class | Sequel::SQL::Function |
In: |
lib/sequel/sql.rb
lib/sequel/extensions/eval_inspect.rb |
Parent: | GenericExpression |
Represents an SQL function call.
WILDCARD | = | LiteralString.new('*').freeze |
DISTINCT | = | ["DISTINCT ".freeze].freeze |
COMMA_ARRAY | = | [LiteralString.new(', ').freeze].freeze |
name | -> | f |
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 1327 1327: def initialize(name, *args) 1328: @name = name 1329: @args = args 1330: @opts = OPTS 1331: end
# File lib/sequel/sql.rb, line 1333 1333: def self.new!(name, args, opts) 1334: f = new(name, *args) 1335: f.instance_variable_set(:@opts, opts) 1336: f 1337: 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 1342 1342: def *(ce=(arg=false;nil)) 1343: if arg == false 1344: raise Error, "Cannot apply * to functions with arguments" unless args.empty? 1345: with_opts("*""*"=>true) 1346: else 1347: super(ce) 1348: end 1349: 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 1362 1362: def filter(*args, &block) 1363: args = args.first if args.length == 1 1364: with_opts(:filter=>args, :filter_block=>block) 1365: end
Return a function which will use LATERAL when literalized:
Sequel.function(:foo, :col).lateral # LATERAL foo(col)
# File lib/sequel/sql.rb, line 1370 1370: def lateral 1371: with_opts(:lateral=>true) 1372: 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)
# File lib/sequel/sql.rb, line 1385 1385: def over(window=OPTS) 1386: raise Error, "function already has a window applied to it" if opts[:over] 1387: window = Window.new(window) unless window.is_a?(Window) 1388: with_opts(:over=>window) 1389: 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 1420 1420: def within_group(*expressions) 1421: with_opts(:within_group=>expressions) 1422: end