Class: Dumbo::Function

Inherits:
PgObject show all
Defined in:
lib/dumbo/function.rb

Instance Attribute Summary collapse

Attributes inherited from PgObject

#oid

Instance Method Summary collapse

Methods inherited from PgObject

#execute, identfied_by, #identify

Constructor Details

#initialize(oid) ⇒ Function

Returns a new instance of Function.



6
7
8
9
# File 'lib/dumbo/function.rb', line 6

def initialize(oid)
  super
  get
end

Instance Attribute Details

#arg_typesObject

Returns the value of attribute arg_types.



3
4
5
# File 'lib/dumbo/function.rb', line 3

def arg_types
  @arg_types
end

#definitionObject

Returns the value of attribute definition.



3
4
5
# File 'lib/dumbo/function.rb', line 3

def definition
  @definition
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/dumbo/function.rb', line 3

def name
  @name
end

#result_typeObject

Returns the value of attribute result_type.



3
4
5
# File 'lib/dumbo/function.rb', line 3

def result_type
  @result_type
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/dumbo/function.rb', line 3

def type
  @type
end

Instance Method Details

#downgrade(other) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/dumbo/function.rb', line 42

def downgrade(other)
  return to_sql if other.nil?

  if other.identify != identify
    fail 'Not the Same Objects!'
  end

  return nil if other.to_sql == to_sql

  if other.result_type != result_type
    <<-SQL.gsub(/^ {8}/, '')
    #{drop}
    #{other.to_sql}
    SQL
  else
    other.to_sql
  end
end

#dropObject



19
20
21
# File 'lib/dumbo/function.rb', line 19

def drop
  "DROP FUNCTION #{name}(#{arg_types});"
end

#getObject



11
12
13
14
15
16
17
# File 'lib/dumbo/function.rb', line 11

def get
  if type == 'agg'
    Aggregate.new(oid)
  else
    self
  end
end

#load_attributesObject



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
86
87
88
89
90
91
92
93
94
95
# File 'lib/dumbo/function.rb', line 61

def load_attributes
  result = execute <<-SQL
    SELECT
      p.proname as name,
      pg_catalog.pg_get_function_result(p.oid) as result_type,
      pg_catalog.pg_get_function_arguments(p.oid) as arg_types,
      CASE
        WHEN p.proisagg THEN 'agg'
        WHEN p.proiswindow THEN 'window'
        WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 'trigger'
        ELSE 'normal'
      END as "type",
      CASE
        WHEN p.provolatile = 'i' THEN 'immutable'
        WHEN p.provolatile = 's' THEN 'stable'
        WHEN p.provolatile = 'v' THEN 'volatile'
      END as volatility,
      proisstrict as is_strict,
      l.lanname as language,
      p.prosrc as "source",
      pg_catalog.obj_description(p.oid, 'pg_proc') as description,
      CASE WHEN p.proisagg THEN 'agg_dummy' ELSE pg_get_functiondef(p.oid) END as definition

    FROM pg_catalog.pg_proc p
    LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang
    WHERE pg_catalog.pg_function_is_visible(p.oid)
      AND p.oid = #{oid};
  SQL

  result.first.each do |k, v|
    send("#{k}=", v) rescue nil
  end

  result.first
end

#to_sqlObject



97
98
99
# File 'lib/dumbo/function.rb', line 97

def to_sql
  definition.gsub("public.#{name}", name)
end

#upgrade(other) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dumbo/function.rb', line 23

def upgrade(other)
  return to_sql if other.nil?

  if other.identify != identify
    fail 'Not the Same Objects!'
  end

  return nil if other.to_sql == to_sql

  if other.result_type != result_type
    <<-SQL.gsub(/^ {8}/, '')
    #{drop}
    #{to_sql}
    SQL
  else
    to_sql
  end
end