Class: Squirm::Procedure::Arguments

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/squirm/procedure.rb

Overview

A collection of argument definitions for a stored procedure. This class delegates both to an internal hash and to its keys, so it has mixed Array/Hash-like behavior. This allows you to access arguments by offset or name.

This may seem like an odd mix of behaviors but is intended to idiomatically translate Postgres's support for both named and unnamed stored procedure arguments.

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Arguments) initialize(string)

Gets an instance of Arguments from a string.

This string can come from a lookup in the pg_proc catalog, or in the case of overloaded functions, will be specified explicitly by the programmer.



161
162
163
164
# File 'lib/squirm/procedure.rb', line 161

def initialize(string)
  @string = string
  @hash   = self.class.hashify(string)
end

Instance Attribute Details

- (Object) hash (readonly)

Returns the value of attribute hash



146
147
148
# File 'lib/squirm/procedure.rb', line 146

def hash
  @hash
end

Class Method Details

+ (Object) hashify(string)

Converts an argument string to a hash whose keys are argument names and whose values are argument types.



214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/squirm/procedure.rb', line 214

def self.hashify(string)
  hash = {}
  string.split(",").map do |arg|
    arg, type = arg.strip.split(/\s+/, 2)
    type ||= arg
    arg   = arg.gsub(/\s+/, '_').gsub(/\A_/, '')
    count = hash.keys.count {|elem| elem =~ /#{arg}[\d]?/}
    key = count == 0 ? arg : arg + count.next.to_s
    hash[key.to_sym] = type
  end
  hash
end

Instance Method Details

- (Object) [](offset) - (Object) [](key)

Gets an argument's Postgres type by index or offset.

Examples:

arguments[0]            #=> "text"
arguments["created_at"] #=> "timestamp with time zone"


192
193
194
# File 'lib/squirm/procedure.rb', line 192

def [](key)
  (key.kind_of?(Fixnum) ? keys : hash)[key]
end

- (Object) format(*args)

Formats the arguments used to call the stored procedure.

When given anything other than a hash, the arguments are returned without modification.

When given a hash, the return value is an array of arguments in the order needed when calling the procedure. Missing values are replaced by nil.

Examples:

# Assume a stored procedure with a definition like the following:
# print_greeting(greeting text, greeter text, language text)
arguments.format(greeter: "John", greeting: "hello") #=> ["hello", "John", nil]


180
181
182
# File 'lib/squirm/procedure.rb', line 180

def format(*args)
  args.first.kind_of?(Hash) ? map {|name| args[0][name]} : args
end

- (Object) info_sql

Gets an SQL query used to look up meta information about a stored procedure with a matching argument signature.



208
209
210
# File 'lib/squirm/procedure.rb', line 208

def info_sql
  "#{INFO_SQL} AND pg_catalog.pg_get_function_arguments(p.oid) = '#{to_s}'"
end

- (Object) to_params

Gets Postgres-formatted params for use in calling the procedure.

Examples:

arguments.to_params #=> "$1::text, $2::integer, $3::text"


200
201
202
203
204
# File 'lib/squirm/procedure.rb', line 200

def to_params
  @params ||= each_with_index.map do |key, index|
    "$%s::%s" % [index.next, hash[key]]
  end.join(", ")
end