Class: PgDiff::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/function.rb

Instance Method Summary collapse

Constructor Details

#initialize(conn, tuple) ⇒ Function

Returns a new instance of Function.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/function.rb', line 3

def initialize(conn, tuple)
  @name = tuple['namespace'] + "." + tuple['function_name']
  @language = tuple['language_name']
  @src = tuple['source_code']
  @returns_set = tuple['returns_set']
  @return_type = format_type(conn, tuple['return_type'])
  @tipes = tuple['function_args'].split(" ")
  if tuple['function_arg_names'] && tuple['function_arg_names'] =~ /^\{(.*)\}$/
    @arnames = $1.split(',')
  elsif tuple['function_arg_names'].is_a? Array # my version of ruby-postgres
    @arnames = tuple['function_arg_names']
  else
    @arnames = [""] * @tipes.length
  end
  alist = []
  @tipes.each_with_index do |typ,idx|
    alist << (@arnames[idx] + " " + format_type(conn, typ))
  end
  @arglist = alist.join(" , ")
  @strict = tuple['proisstrict'] ? ' STRICT' : ''
  @secdef = tuple['prosecdef'] ? ' SECURITY DEFINER' : ''
  @volatile = case tuple['provolatile']
    when 'i' then ' IMMUTABLE'
    when 's' then ' STABLE'
    else ''
  end
end

Instance Method Details

#==(other) ⇒ Object



41
42
43
# File 'lib/function.rb', line 41

def == (other)
  definition == other.definition
end

#definitionObject



35
36
37
38
39
# File 'lib/function.rb', line 35

def definition
  <<-EOT
  CREATE OR REPLACE FUNCTION #{@name} (#{@arglist}) RETURNS #{@returns_set ? 'SETOF' : ''} #{@return_type} AS $_$#{@src}$_$ LANGUAGE '#{@language}' #{@volatile}#{@strict}#{@secdef};
  EOT
end

#format_type(conn, oid) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/function.rb', line 45

def format_type(conn, oid)
  t_query = <<-EOT
  SELECT pg_catalog.format_type(pg_type.oid, typtypmod) AS type_name
   FROM pg_catalog.pg_type
   JOIN pg_catalog.pg_namespace ON (pg_namespace.oid = typnamespace)
   WHERE pg_type.oid =
  EOT
  tuple = conn.query(t_query + oid.to_s).first
  tuple['type_name']
end

#signatureObject



31
32
33
# File 'lib/function.rb', line 31

def signature
  "#{@name}(#{@arglist})"
end