Class: ActiveRecord::ConnectionAdapters::PostgreSQLFunctionDefinition

Inherits:
PostgreSQLFunction
  • Object
show all
Defined in:
lib/active_record/postgresql_extensions/functions.rb

Overview

Creates a PostgreSQL function definition. You’re generally going to want to use the create_function method instead of accessing this class directly.

Instance Attribute Summary collapse

Attributes inherited from PostgreSQLFunction

#args, #base, #name, #options

Instance Method Summary collapse

Constructor Details

#initialize(base, name, args, returns, language, body, options = {}) ⇒ PostgreSQLFunctionDefinition

:nodoc:



264
265
266
267
268
269
270
271
272
273
274
# File 'lib/active_record/postgresql_extensions/functions.rb', line 264

def initialize(base, name, args, returns, language, body, options = {}) #:nodoc:
  assert_valid_behavior(options[:behavior])
  assert_valid_on_null_input(options[:on_null_input])
  assert_valid_security(options[:security])

  @language, @returns, @body = language, returns, body
  options = {
    :delimiter => '$$'
  }.merge options
  super(base, name, args, options)
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



262
263
264
# File 'lib/active_record/postgresql_extensions/functions.rb', line 262

def body
  @body
end

#languageObject

Returns the value of attribute language.



262
263
264
# File 'lib/active_record/postgresql_extensions/functions.rb', line 262

def language
  @language
end

#returnsObject

Returns the value of attribute returns.



262
263
264
# File 'lib/active_record/postgresql_extensions/functions.rb', line 262

def returns
  @returns
end

Instance Method Details

#to_sqlObject Also known as: to_s

:nodoc:



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/active_record/postgresql_extensions/functions.rb', line 276

def to_sql #:nodoc:
  sql = 'CREATE '
  sql << 'OR REPLACE ' if options[:force]
  sql << "FUNCTION #{base.quote_function(name)}(#{args}) RETURNS #{returns} AS "
  if language == 'C'
    "#{base.quote_function(body[0])}, #{base.quote_generic(body[1])}"
  else
    sql << "#{options[:delimiter]}\n#{body}\n#{options[:delimiter]}\n"
  end
  sql << "LANGUAGE #{base.quote_language(language)}\n"
  sql << "    #{options[:behavior].to_s.upcase}\n" if options[:behavior]
  sql << "    #{options[:on_null_input].to_s.upcase}\n" if options[:on_null_input]
  sql << "    COST #{options[:cost].to_i}\n" if options[:cost]
  sql << "    ROWS #{options[:rows].to_i}\n" if options[:rows]
  sql << "    " << (set_options(options[:set]) * "\n    ") if options[:set]
  "#{sql.strip};"
end