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:



314
315
316
317
318
319
320
321
322
323
324
# File 'lib/active_record/postgresql_extensions/functions.rb', line 314

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.



312
313
314
# File 'lib/active_record/postgresql_extensions/functions.rb', line 312

def body
  @body
end

#languageObject

Returns the value of attribute language.



312
313
314
# File 'lib/active_record/postgresql_extensions/functions.rb', line 312

def language
  @language
end

#returnsObject

Returns the value of attribute returns.



312
313
314
# File 'lib/active_record/postgresql_extensions/functions.rb', line 312

def returns
  @returns
end

Instance Method Details

#to_sqlObject Also known as: to_s

:nodoc:



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/active_record/postgresql_extensions/functions.rb', line 326

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