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 self.to_sql if other.nil?

  if other.identify != self.identify
    raise "Not the Same Objects!"
  end

  return nil if other.to_sql == self.to_sql

  if other.result_type != self.result_type
     "    \#{self.drop}\n    \#{other.to_sql}\n    SQL\n  else\n    other.to_sql\n  end\nend\n".gsub(/^ {8}/, '')

#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 "    SELECT\n      p.proname as name,\n      pg_catalog.pg_get_function_result(p.oid) as result_type,\n      pg_catalog.pg_get_function_arguments(p.oid) as arg_types,\n      CASE\n        WHEN p.proisagg THEN 'agg'\n        WHEN p.proiswindow THEN 'window'\n        WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 'trigger'\n        ELSE 'normal'\n      END as \"type\",\n      CASE\n        WHEN p.provolatile = 'i' THEN 'immutable'\n        WHEN p.provolatile = 's' THEN 'stable'\n        WHEN p.provolatile = 'v' THEN 'volatile'\n      END as volatility,\n      proisstrict as is_strict,\n      l.lanname as language,\n      p.prosrc as \"source\",\n      pg_catalog.obj_description(p.oid, 'pg_proc') as description,\n      CASE WHEN p.proisagg THEN 'agg_dummy' ELSE pg_get_functiondef(p.oid) END as definition\n\n    FROM pg_catalog.pg_proc p\n    LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang\n    WHERE pg_catalog.pg_function_is_visible(p.oid)\n      AND p.oid = \#{oid};\n  SQL\n\n  result.first.each do |k,v|\n    send(\"\#{k}=\",v) rescue nil\n  end\n\n  result.first\nend\n"

#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 self.to_sql if other.nil?

  if other.identify != self.identify
    raise "Not the Same Objects!"
  end

  return nil if other.to_sql == self.to_sql

  if other.result_type != self.result_type
     "    \#{self.drop}\n    \#{self.to_sql}\n    SQL\n  else\n    self.to_sql\n  end\nend\n".gsub(/^ {8}/, '')