Class: Piggly::Dumper::SkeletonProcedure

Inherits:
Object
  • Object
show all
Defined in:
lib/piggly/dumper/skeleton_procedure.rb

Overview

Encapsulates all the information about a stored procedure, except the procedure’s source code, which is assumed to be on disk, loaded as needed.

Direct Known Subclasses

ReifiedProcedure

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of SkeletonProcedure.



13
14
15
16
17
18
19
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 13

def initialize(oid, name, strict, secdef, setof, type, volatility, arg_modes, arg_names, arg_types, arg_defaults, prokind = "f", language = "plpgsql")
  @oid, @name, @strict, @secdef, @type, @volatility, @setof, @arg_modes, @arg_names, @arg_types, @arg_defaults, @prokind, @language =
    oid, name, strict, secdef, type, volatility, setof, arg_modes, arg_names, arg_types, arg_defaults, prokind, language


  @identifier = Digest::MD5.hexdigest(signature)
end

Instance Attribute Details

#arg_modesObject (readonly)

Returns the value of attribute arg_modes.



10
11
12
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 10

def arg_modes
  @arg_modes
end

#arg_namesObject (readonly)

Returns the value of attribute arg_names.



10
11
12
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 10

def arg_names
  @arg_names
end

#arg_typesObject (readonly)

Returns the value of attribute arg_types.



10
11
12
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 10

def arg_types
  @arg_types
end

#identifierObject (readonly)

Returns the value of attribute identifier.



10
11
12
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 10

def identifier
  @identifier
end

#languageObject (readonly)

Returns the value of attribute language.



10
11
12
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 10

def language
  @language
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 10

def name
  @name
end

#oidObject (readonly)

Returns the value of attribute oid.



10
11
12
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 10

def oid
  @oid
end

#prokindObject (readonly)

Returns the value of attribute prokind.



10
11
12
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 10

def prokind
  @prokind
end

#secdefObject (readonly)

Returns the value of attribute secdef.



10
11
12
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 10

def secdef
  @secdef
end

#setofString (readonly)

Returns source text for return type

Returns:

  • (String)


31
32
33
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 31

def setof
  @setof
end

#strictObject (readonly)

Returns the value of attribute strict.



10
11
12
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 10

def strict
  @strict
end

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 10

def type
  @type
end

#volatilityObject (readonly)

Returns the value of attribute volatility.



10
11
12
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 10

def volatility
  @volatility
end

Instance Method Details

#==(other) ⇒ Object



105
106
107
108
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 105

def ==(other)
  other.is_a?(self.class) and 
    other.identifier == identifier
end

#argumentsString

Returns source text for argument list

Returns:

  • (String)


23
24
25
26
27
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 23

def arguments
  @arg_types.zip(@arg_names, @arg_modes, @arg_defaults).map do |type, name, mode, default|
    "#{mode + " " if mode}#{name.quote + " " if name}#{type.quote}#{" default " + default if default}"
  end.join(", ")
end

#definition(body) ⇒ String

Returns source SQL function/procedure definition statement

Returns:

  • (String)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 49

def definition(body)
  if @prokind == 'p'
    # PostgreSQL PROCEDURE (introduced in PG11)
    [%[create or replace procedure #{name.quote} (#{arguments})],
     %[ language plpgsql #{security} as $__PIGGLY__$],
     body,
     %[$__PIGGLY__$]].join("\n")
  else
    # PostgreSQL FUNCTION
    [%[create or replace function #{name.quote} (#{arguments})],
     %[ returns #{setof}#{type.quote} as $__PIGGLY__$],
     body,
     %[$__PIGGLY__$ language plpgsql #{strictness} #{security} #{@volatility}]].join("\n")
  end
end

#load_source(config) ⇒ String Also known as: source

Returns:

  • (String)


76
77
78
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 76

def load_source(config)
  File.read(source_path(config), encoding: 'UTF-8')
end

#purge_source(config) ⇒ void

This method returns an undefined value.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 84

def purge_source(config)
  path = source_path(config)

  FileUtils.rm_r(path) if File.exist?(path)

  file = Compiler::TraceCompiler.new(config).cache_path(path)
  FileUtils.rm_r(file) if File.exist?(file)

  file = Reporter::Base.new(config).report_path(path, ".html")
  FileUtils.rm_r(file) if File.exist?(file)
end

#securityString

Returns source text for security

Returns:

  • (String)


43
44
45
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 43

def security
  @secdef ? "security definer" : nil
end

#signatureString

Returns:

  • (String)


66
67
68
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 66

def signature
  "#{@name}(#{@arg_modes.zip(@arg_types).map{|m,t| "#{m} #{t}" }.join(", ")})"
end

#skeletonSkeletonProcedure

Returns:



97
98
99
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 97

def skeleton
  self
end

#skeleton?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 101

def skeleton?
  true
end

#source_path(config) ⇒ String

Returns:

  • (String)


71
72
73
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 71

def source_path(config)
  config.mkpath("#{config.cache_root}/Dumper", "#{@identifier}.plpgsql")
end

#strictnessString

Returns source text for strictness

Returns:

  • (String)


37
38
39
# File 'lib/piggly/dumper/skeleton_procedure.rb', line 37

def strictness
  @strict ? "strict" : nil
end