Module: PgSaurus::ConnectionAdapters::PostgreSQLAdapter::FunctionMethods

Included in:
PgSaurus::ConnectionAdapters::PostgreSQLAdapter
Defined in:
lib/pg_saurus/connection_adapters/postgresql_adapter/function_methods.rb

Overview

Methods to extend ActiveRecord::ConnectionAdapters::PostgreSQLAdapter to support database functions.

Constant Summary collapse

FUNCTION_PARSE_REGEXP =

Regular expression used in function signature parsing:

/^CREATE[\s\S]+FUNCTION /

Instance Method Summary collapse

Instance Method Details

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

Create a new database function.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/function_methods.rb', line 59

def create_function(function_name, returning, definition, options = {})

  function_name = full_function_name(function_name, options)
  language      = options[:language] || 'plpgsql'
  replace       = if options[:replace] == false
                    ''
                  else
                    'OR REPLACE '
                  end
  volatility    = case options[:volatility]
                  when :volatile, :stable, :immutable
                    "\n  #{options[:volatility].to_s.upcase}"
                  else
                    ""
                  end

  sql = <<-SQL.gsub(/^[ ]{6}/, "")
    CREATE #{replace}FUNCTION #{function_name}
      RETURNS #{returning}
      LANGUAGE #{language}#{volatility}
    AS $function$
    #{definition.strip}
    $function$
  SQL

  execute(sql)
end

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

Drop the given database function.



88
89
90
91
92
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/function_methods.rb', line 88

def drop_function(function_name, options = {})
  function_name = full_function_name(function_name, options)

  execute "DROP FUNCTION #{function_name}"
end

#functionsObject

Return a list of defined DB functions. Ignore function definitions that can’t be parsed.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/function_methods.rb', line 13

def functions
  pg_major = ::PgSaurus::Engine.pg_server_version[0]
  res = select_all <<-SQL
    SELECT n.nspname AS "Schema",
      p.proname AS "Name",
      pg_catalog.pg_get_function_result(p.oid) AS "Returning",
     CASE
      WHEN #{pg_major >= 11 ? "p.prokind = 'w'" : "p.proiswindow"} THEN 'window'
      WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 'trigger'
      ELSE 'normal'
     END   AS "Type",
     p.oid AS "Oid"
    FROM pg_catalog.pg_proc p
         LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
    WHERE pg_catalog.pg_function_is_visible(p.oid)
          AND n.nspname <> 'pg_catalog'
          AND n.nspname <> 'information_schema'
          AND #{pg_major >= 11 ? "p.prokind <> 'a'" : "p.proisagg <> TRUE"}
    ORDER BY 1, 2, 3, 4;
  SQL
  res.inject([]) do |buffer, row|
    returning     = row['Returning']
    function_type = row['Type']
    oid           = row['Oid']

    function_str = select_value("SELECT pg_get_functiondef(#{oid});")

    name       = parse_function_name(function_str)
    language   = parse_function_language(function_str)
    definition = parse_function_definition(function_str)
    volatility = parse_function_volatility(function_str)

    if definition
      buffer << ::PgSaurus::ConnectionAdapters::FunctionDefinition.new(name,
                                                                       returning,
                                                                       definition.strip,
                                                                       function_type,
                                                                       language,
                                                                       oid,
                                                                       volatility)
    end
    buffer
  end
end

#supports_functions?Boolean

Return true.

Returns:

  • (Boolean)


8
9
10
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/function_methods.rb', line 8

def supports_functions?
  true
end