Class: Sequel::SQL::Function
- Inherits:
-
GenericExpression
- Object
- Expression
- GenericExpression
- Sequel::SQL::Function
- Defined in:
- lib/sequel/sql.rb,
lib/sequel/extensions/eval_inspect.rb
Overview
Represents an SQL function call.
Constant Summary collapse
- WILDCARD =
LiteralString.new('*').freeze
- DISTINCT =
["DISTINCT ".freeze].freeze
- COMMA_ARRAY =
[LiteralString.new(', ').freeze].freeze
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
The array of arguments to pass to the function (may be blank).
-
#name ⇒ Object
(also: #f)
readonly
The SQL function to call.
-
#opts ⇒ Object
readonly
Options for this function.
Class Method Summary collapse
Instance Method Summary collapse
-
#*(ce = (arg=false;nil)) ⇒ Object
If no arguments are given, return a new function with the wildcard prepended to the arguments.
-
#distinct ⇒ Object
Return a new function with DISTINCT before the method arguments.
-
#filter(*args, &block) ⇒ Object
Return a new function with FILTER added to it, for filtered aggregate functions:.
-
#initialize(name, *args) ⇒ Function
constructor
Set the name and args for the function.
-
#lateral ⇒ Object
Return a function which will use LATERAL when literalized:.
-
#order(*args) ⇒ Object
Return a new function where the function will be ordered.
-
#over(window = OPTS) ⇒ Object
Return a new function with an OVER clause (making it a window function).
-
#quoted ⇒ Object
Return a new function where the function name will be quoted if the database supports quoted functions:.
-
#unquoted ⇒ Object
Return a new function where the function name will not be quoted even if the database supports quoted functions:.
-
#with_ordinality ⇒ Object
Return a new function that will use WITH ORDINALITY to also return a row number for every row the function returns:.
-
#within_group(*expressions) ⇒ Object
Return a new function that uses WITHIN GROUP ordered by the given expression, useful for ordered-set and hypothetical-set aggregate functions:.
Methods included from Postgres::HStoreOpMethods
Methods included from Postgres::RangeOpMethods
Methods included from Postgres::ArrayOpMethods
Methods included from Postgres::JSONOpMethods
Methods included from Postgres::InetOpMethods
Methods included from Postgres::PGRowOp::ExpressionMethods
Methods included from SubscriptMethods
Methods included from StringMethods
Methods included from PatternMatchMethods
Methods included from OrderMethods
Methods included from NumericMethods
Methods included from ComplexExpressionMethods
#extract, #sql_boolean, #sql_number, #sql_string
Methods included from CastMethods
#cast, #cast_numeric, #cast_string
Methods included from BooleanMethods
Methods included from AliasMethods
Methods inherited from Expression
#==, attr_reader, #eql?, #hash, inherited, #inspect, #lit, #sql_literal
Constructor Details
#initialize(name, *args) ⇒ Function
Set the name and args for the function
1324 1325 1326 1327 1328 |
# File 'lib/sequel/sql.rb', line 1324 def initialize(name, *args) @name = name @args = args @opts = OPTS end |
Instance Attribute Details
#args ⇒ Object (readonly)
The array of arguments to pass to the function (may be blank)
1318 1319 1320 |
# File 'lib/sequel/sql.rb', line 1318 def args @args end |
#name ⇒ Object (readonly) Also known as: f
The SQL function to call
1314 1315 1316 |
# File 'lib/sequel/sql.rb', line 1314 def name @name end |
#opts ⇒ Object (readonly)
Options for this function
1321 1322 1323 |
# File 'lib/sequel/sql.rb', line 1321 def opts @opts end |
Class Method Details
.new!(name, args, opts) ⇒ Object
1330 1331 1332 1333 1334 |
# File 'lib/sequel/sql.rb', line 1330 def self.new!(name, args, opts) f = new(name, *args) f.instance_variable_set(:@opts, opts) f end |
Instance Method Details
#*(ce = (arg=false;nil)) ⇒ Object
If no arguments are given, return a new function with the wildcard prepended to the arguments.
Sequel.function(:count).* # count(*)
1339 1340 1341 1342 1343 1344 1345 1346 |
# File 'lib/sequel/sql.rb', line 1339 def *(ce=(arg=false;nil)) if arg == false raise Error, "Cannot apply * to functions with arguments" unless args.empty? with_opts(:"*"=>true) else super(ce) end end |
#distinct ⇒ Object
Return a new function with DISTINCT before the method arguments.
Sequel.function(:count, :col).distinct # count(DISTINCT col)
1351 1352 1353 |
# File 'lib/sequel/sql.rb', line 1351 def distinct with_opts(:distinct=>true) end |
#filter(*args, &block) ⇒ Object
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)
1359 1360 1361 1362 |
# File 'lib/sequel/sql.rb', line 1359 def filter(*args, &block) args = args.first if args.length == 1 with_opts(:filter=>args, :filter_block=>block) end |
#lateral ⇒ Object
Return a function which will use LATERAL when literalized:
Sequel.function(:foo, :col).lateral # LATERAL foo(col)
1367 1368 1369 |
# File 'lib/sequel/sql.rb', line 1367 def lateral with_opts(:lateral=>true) end |
#order(*args) ⇒ Object
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)
1375 1376 1377 |
# File 'lib/sequel/sql.rb', line 1375 def order(*args) with_opts(:order=>args) end |
#over(window = OPTS) ⇒ Object
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)
1382 1383 1384 1385 1386 |
# File 'lib/sequel/sql.rb', line 1382 def over(window=OPTS) raise Error, "function already has a window applied to it" if opts[:over] window = Window.new(window) unless window.is_a?(Window) with_opts(:over=>window) end |
#quoted ⇒ Object
Return a new function where the function name will be quoted if the database supports quoted functions:
Sequel.function(:foo).quoted # "foo"()
1392 1393 1394 |
# File 'lib/sequel/sql.rb', line 1392 def quoted with_opts(:quoted=>true) end |
#unquoted ⇒ Object
Return a new function where the function name will not be quoted even if the database supports quoted functions:
Sequel[:foo].function.unquoted # foo()
1400 1401 1402 |
# File 'lib/sequel/sql.rb', line 1400 def unquoted with_opts(:quoted=>false) end |
#with_ordinality ⇒ Object
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
1408 1409 1410 |
# File 'lib/sequel/sql.rb', line 1408 def with_ordinality with_opts(:with_ordinality=>true) end |
#within_group(*expressions) ⇒ Object
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)
1417 1418 1419 |
# File 'lib/sequel/sql.rb', line 1417 def within_group(*expressions) with_opts(:within_group=>expressions) end |