Class: Sequel::SQL::Function

Inherits:
GenericExpression show all
Defined in:
lib/sequel/sql.rb,
lib/sequel/extensions/eval_inspect.rb
more...

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IsDistinctFrom::Methods

#is_distinct_from

Methods included from Sequel::SQLite::JSONOpMethods

#sqlite_json_op, #sqlite_jsonb_op

Methods included from Postgres::HStoreOpMethods

#hstore

Methods included from Postgres::RangeOpMethods

#pg_range

Methods included from Postgres::ArrayOpMethods

#pg_array

Methods included from Postgres::JSONOpMethods

#pg_json, #pg_jsonb

Methods included from Postgres::InetOpMethods

#pg_inet

Methods included from Postgres::PGRowOp::ExpressionMethods

#pg_row

Methods included from SubscriptMethods

#sql_subscript

Methods included from StringMethods

#escaped_ilike, #escaped_like, #ilike, #like

Methods included from PatternMatchMethods

#!~, #=~

Methods included from OrderMethods

#asc, #desc

Methods included from NumericMethods

#+, #coerce

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

#as

Methods inherited from Expression

#==, attr_reader, #clone, #eql?, #hash, inherited, #inspect

Constructor Details

#initialize(name, *args) ⇒ Function

Set the name and args for the function

[View source]

1385
1386
1387
# File 'lib/sequel/sql.rb', line 1385

def initialize(name, *args)
  _initialize(name, args, OPTS)
end

Instance Attribute Details

#argsObject (readonly)

The array of arguments to pass to the function (may be blank)


1379
1380
1381
# File 'lib/sequel/sql.rb', line 1379

def args
  @args
end

#nameObject (readonly)

The SQL function to call


1376
1377
1378
# File 'lib/sequel/sql.rb', line 1376

def name
  @name
end

#optsObject (readonly)

Options for this function


1382
1383
1384
# File 'lib/sequel/sql.rb', line 1382

def opts
  @opts
end

Class Method Details

.new!(name, args, opts) ⇒ Object

Set the name, args, and options, for internal use only.

[View source]

1390
1391
1392
# File 'lib/sequel/sql.rb', line 1390

def self.new!(name, args, opts) # :nodoc:
  allocate.send(:_initialize, name, args, opts)
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(*)
[View source]

1397
1398
1399
1400
1401
1402
1403
1404
# File 'lib/sequel/sql.rb', line 1397

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

#distinctObject

Return a new function with DISTINCT before the method arguments.

Sequel.function(:count, :col).distinct # count(DISTINCT col)
[View source]

1409
1410
1411
# File 'lib/sequel/sql.rb', line 1409

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))
[View source]

1417
1418
1419
1420
1421
1422
1423
1424
1425
# File 'lib/sequel/sql.rb', line 1417

def filter(*args, &block)
  if args.length == 1
    args = args.first
  else
    args.freeze
  end

  with_opts(:filter=>args, :filter_block=>block)
end

#lateralObject

Return a function which will use LATERAL when literalized:

Sequel.function(:foo, :col).lateral # LATERAL foo(col)
[View source]

1430
1431
1432
# File 'lib/sequel/sql.rb', line 1430

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)
[View source]

1438
1439
1440
# File 'lib/sequel/sql.rb', line 1438

def order(*args)
  with_opts(:order=>args.freeze)
end

#over(window = OPTS) ⇒ Object

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)

Raises:

[View source]

1446
1447
1448
1449
1450
# File 'lib/sequel/sql.rb', line 1446

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

#quotedObject

Return a new function where the function name will be quoted if the database supports quoted functions:

Sequel.function(:foo).quoted # "foo"()
[View source]

1456
1457
1458
# File 'lib/sequel/sql.rb', line 1456

def quoted
  with_opts(:quoted=>true)
end

#unquotedObject

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()
[View source]

1464
1465
1466
# File 'lib/sequel/sql.rb', line 1464

def unquoted
  with_opts(:quoted=>false)
end

#with_ordinalityObject

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
[View source]

1472
1473
1474
# File 'lib/sequel/sql.rb', line 1472

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)
[View source]

1481
1482
1483
# File 'lib/sequel/sql.rb', line 1481

def within_group(*expressions)
  with_opts(:within_group=>expressions.freeze)
end