Class: Sequel::SQL::Function

Inherits:
GenericExpression show all
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

Class Method Summary collapse

Instance Method Summary collapse

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::PGRowOp::ExpressionMethods

#pg_row

Methods included from SubscriptMethods

#sql_subscript

Methods included from StringMethods

#ilike, #like

Methods included from OrderMethods

#asc, #desc

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

#as

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



1243
1244
1245
1246
1247
# File 'lib/sequel/sql.rb', line 1243

def initialize(name, *args)
  @name = name
  @args = args
  @opts = OPTS
end

Instance Attribute Details

#argsObject (readonly)

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



1237
1238
1239
# File 'lib/sequel/sql.rb', line 1237

def args
  @args
end

#nameObject (readonly) Also known as: f

The SQL function to call



1233
1234
1235
# File 'lib/sequel/sql.rb', line 1233

def name
  @name
end

#optsObject (readonly)

Options for this function



1240
1241
1242
# File 'lib/sequel/sql.rb', line 1240

def opts
  @opts
end

Class Method Details

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



1249
1250
1251
1252
1253
# File 'lib/sequel/sql.rb', line 1249

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(*)


1258
1259
1260
1261
1262
1263
1264
1265
# File 'lib/sequel/sql.rb', line 1258

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)


1270
1271
1272
# File 'lib/sequel/sql.rb', line 1270

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)


1278
1279
1280
1281
# File 'lib/sequel/sql.rb', line 1278

def filter(*args, &block)
  args = args.first if args.length == 1
  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)


1286
1287
1288
# File 'lib/sequel/sql.rb', line 1286

def lateral
  with_opts(:lateral=>true)
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)

Raises:



1293
1294
1295
1296
1297
# File 'lib/sequel/sql.rb', line 1293

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"()


1303
1304
1305
# File 'lib/sequel/sql.rb', line 1303

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.expr(:foo).function.unquoted # foo()


1311
1312
1313
# File 'lib/sequel/sql.rb', line 1311

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


1319
1320
1321
# File 'lib/sequel/sql.rb', line 1319

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)


1328
1329
1330
# File 'lib/sequel/sql.rb', line 1328

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