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 1256 1256: def initialize(name, *args) 1257: @name = name 1258: @args = args 1259: @opts = OPTS 1260: end
# 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
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 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 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 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)
# 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)
# 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 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 1341 1341: def within_group(*expressions) 1342: with_opts(:within_group=>expressions) 1343: end