Module: SchemaPlus::Functions::ActiveRecord::ConnectionAdapters::AbstractAdapter

Defined in:
lib/schema_plus/functions/active_record/connection_adapters/abstract_adapter.rb

Instance Method Summary collapse

Instance Method Details

#create_function(function_name, params, definition, options = {}) ⇒ Object

Create a function. Valid options are :force, :allow_replace, and :function_type



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/schema_plus/functions/active_record/connection_adapters/abstract_adapter.rb', line 9

def create_function(function_name, params, definition, options = {})
  SchemaMonkey::Middleware::Migration::CreateFunction.start(connection: self, function_name: function_name, params: params, definition: definition, options: options) do |env|
    function_name = env.function_name
    function_type = (options[:function_type] || :function).to_s.upcase
    params        = env.params
    definition    = env.definition
    options       = env.options

    definition    = definition.to_sql if definition.respond_to? :to_sql
    if options[:force]
      drop_function(function_name, params, function_type: options[:function_type], if_exists: true)
    end

    command = if options[:allow_replace]
                "CREATE OR REPLACE"
              else
                "CREATE"
              end
    execute "#{command} #{function_type} #{function_name}(#{params}) #{definition}"
  end
end

#drop_function(function_name, params, options = {}) ⇒ Object

Remove a function. Valid options are :function_type, :if_exists, and :cascade

If your function type is an aggregate, you must specify the type

drop_function 'my_func', 'int', if_exists: true, cascade: true
drop_function 'my_agg', 'int', function_type: :aggregate


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/schema_plus/functions/active_record/connection_adapters/abstract_adapter.rb', line 38

def drop_function(function_name, params, options = {})
  SchemaMonkey::Middleware::Migration::CreateFunction.start(connection: self, function_name: function_name, params: params, options: options) do |env|
    function_name = env.function_name
    params        = env.params
    options       = env.options
    function_type = (options[:function_type] || :function).to_s.upcase

    sql = "DROP #{function_type}"
    sql += " IF EXISTS" if options[:if_exists]
    sql += " #{function_name}(#{params})"
    sql += " CASCADE" if options[:cascade]

    execute sql
  end
end

#function_definition(function_name, params, name = nil) ⇒ Object

(abstract) Return the Function definition for the named function and parameter set



66
67
68
# File 'lib/schema_plus/functions/active_record/connection_adapters/abstract_adapter.rb', line 66

def function_definition(function_name, params, name = nil)
  raise "Internal Error: Connection adapter did not override abstract function"
end

#functions(name = nil) ⇒ Object

(abstract) Return the Function objects for functions



61
62
63
# File 'lib/schema_plus/functions/active_record/connection_adapters/abstract_adapter.rb', line 61

def functions(name = nil)
  raise "Internal Error: Connection adapter did not override abstract function"
end