Class: Piggly::Dumper::ReifiedProcedure

Inherits:
SkeletonProcedure show all
Defined in:
lib/piggly/dumper/reified_procedure.rb

Overview

Differs from SkeletonProcedure in that the procedure source code is stored as an instance variable.

Constant Summary collapse

MODES =

Rewrite “i”, “o”, and “b”, otherwise pass-through

Hash.new{|h,k| k }.update \
"i" => "in",
"o" => "out",
"b" => "inout",
"v" => "variadic"
VOLATILITY =

Rewrite “i”, “v”, and “s”, otherwise pass-through

Hash.new{|h,k| k }.update \
"i" => "immutable",
"v" => "volatile",
"s" => "stable"

Instance Attribute Summary

Attributes inherited from SkeletonProcedure

#arg_modes, #arg_names, #arg_types, #identifier, #language, #name, #oid, #prokind, #secdef, #setof, #strict, #type, #volatility

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SkeletonProcedure

#==, #arguments, #definition, #load_source, #purge_source, #security, #signature, #source_path, #strictness

Constructor Details

#initialize(source, oid, name, strict, secdef, setof, type, volatility, arg_modes, arg_names, arg_types, arg_defaults, prokind = "f", language = "plpgsql") ⇒ ReifiedProcedure

Returns a new instance of ReifiedProcedure.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/piggly/dumper/reified_procedure.rb', line 10

def initialize(source, oid, name, strict, secdef, setof, type, volatility, arg_modes, arg_names, arg_types, arg_defaults, prokind = "f", language = "plpgsql")
  # Ensure source is UTF-8 encoded
  @source = source.to_s.force_encoding('UTF-8').strip

  if type.name == "record" and type.schema == "pg_catalog" and arg_modes.include?("t")
    prefix       = arg_modes.take_while{|m| m != "t" }.length
    type         = RecordType.new(arg_types[prefix..-1], arg_names[prefix..-1], arg_modes[prefix..-1], arg_defaults[prefix..-1])
    arg_modes    = arg_modes[0, prefix]
    arg_types    = arg_types[0, prefix]
    arg_names    = arg_names[0, prefix]
    arg_defaults = arg_defaults[0, prefix]
    setof        = false
  end

  super(oid, name, strict, secdef, setof, type, volatility, arg_modes, arg_names, arg_types, arg_defaults, prokind, language)
end

Class Method Details

.all(connection) ⇒ Array<ReifiedProcedure>

Returns a list of all PL/pgSQL stored procedures in the current database

Returns:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/piggly/dumper/reified_procedure.rb', line 92

def all(connection)
  connection.query(<<-SQL).map{|x| from_hash(x) }
    select
      pro.oid,
      nschema.nspname   as nschema,
      pro.proname       as name,
      pro.proisstrict   as strict,
      pro.prosecdef     as secdef,
      pro.provolatile   as volatility,
      pro.proretset     as setof,
      rschema.nspname   as tschema,
      ret.typname       as type,
      pro.prosrc        as source,
      pro.pronargs      as arg_count,
      lang.lanname	    as language,
      array_to_string(pro.proargmodes, ',') as arg_modes,
      array_to_string(pro.proargnames, ',') as arg_names,
      case when proallargtypes is not null then
             -- use proalltypes array if its non-null
             array_to_string(array(select format_type(proallargtypes[k], null)
                                   from generate_series(array_lower(proallargtypes, 1),
                                                        array_upper(proallargtypes, 1)) as k), ',')
           else
             -- fallback to oidvector proargtypes
             oidvectortypes(pro.proargtypes)
           end as arg_types,
      pro.pronargdefaults as arg_defaults_count,
      coalesce(pg_get_expr(pro.proargdefaults, 0), '') as arg_defaults,
      coalesce(pro.prokind, 'f') as prokind
    from pg_proc as pro,
         pg_type as ret,
         pg_namespace as nschema,
         pg_namespace as rschema,
         pg_language	as lang
    where pro.pronamespace = nschema.oid
      and pro.prolang = lang.oid
      and ret.typnamespace = rschema.oid
      and pro.proname not like 'piggly_%'
      and pro.prorettype = ret.oid
      and pro.prolang = (select oid from pg_language where lanname = 'plpgsql')
      and pro.pronamespace not in (select oid
                                   from pg_namespace
                                   where nspname like 'pg_%'
                                      or nspname like 'information_schema')
  SQL
end

.coalesce(value, default) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/piggly/dumper/reified_procedure.rb', line 162

def coalesce(value, default)
  if [nil, "", []].include?(value)
    default
  else
    value
  end
end

.defaults(exprs, count, total) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/piggly/dumper/reified_procedure.rb', line 77

def defaults(exprs, count, total)
  exprs = if exprs.nil? then [] else exprs.split(", ") end

  nreqd = total - count

  if nreqd >= 0 and exprs.length == count
    Array.new(nreqd) + exprs
  else
    raise "Couldn't parse default arguments"
  end
end

.from_hash(hash) ⇒ ReifiedProcedure

Construct a ReifiedProcedure from a result row (Hash)

Returns:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/piggly/dumper/reified_procedure.rb', line 142

def from_hash(hash)
  new(hash["source"],
      hash["oid"],
      QualifiedName.new(hash["nschema"].to_s, hash["name"].to_s),
      hash["strict"] == "t",
      hash["secdef"] == "t",
      hash["setof"]  == "t",
      QualifiedType.parse(hash["tschema"].to_s, hash["type"].to_s),
      volatility(hash["volatility"]),
      coalesce(hash["arg_modes"].to_s.split(",").map{|x| mode(x.strip) },
               ["in"]*hash["arg_count"].to_i),
      hash["arg_names"].to_s.split(",").map{|x| QualifiedName.new(nil, x.strip) },
      hash["arg_types"].to_s.split(",").map{|x| QualifiedType.parse(x.strip) },
      defaults(hash["arg_defaults"],
               hash["arg_defaults_count"].to_i,
               hash["arg_count"].to_i),
      hash["prokind"].to_s,
      hash["language"].to_s)
end

.mode(mode) ⇒ Object



69
70
71
# File 'lib/piggly/dumper/reified_procedure.rb', line 69

def mode(mode)
  MODES[mode]
end

.volatility(mode) ⇒ Object



73
74
75
# File 'lib/piggly/dumper/reified_procedure.rb', line 73

def volatility(mode)
  VOLATILITY[mode]
end

Instance Method Details

#skeletonSkeletonProcedure

Returns:



44
45
46
47
48
# File 'lib/piggly/dumper/reified_procedure.rb', line 44

def skeleton
  SkeletonProcedure.new(@oid, @name, @strict, @secdef, @setof, @type,
                        @volatility, @arg_modes, @arg_names, @arg_types,
                        @arg_defaults, @prokind, @language)
end

#skeleton?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/piggly/dumper/reified_procedure.rb', line 50

def skeleton?
  false
end

#source(config) ⇒ String

Returns:

  • (String)


28
29
30
# File 'lib/piggly/dumper/reified_procedure.rb', line 28

def source(config)
  @source
end

#store_source(config) ⇒ void

This method returns an undefined value.



33
34
35
36
37
38
39
40
41
# File 'lib/piggly/dumper/reified_procedure.rb', line 33

def store_source(config)
  if @source.include?("$PIGGLY$")
    raise "Procedure `#{@name}' is already instrumented. " +
          "This means the original source wasn't restored after the " +
          "last coverage run. You must restore the source manually."
  end

  File.open(source_path(config), "wb:UTF-8"){|io| io.write(@source) }
end