Class: Squirm::Procedure

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

Overview

This class wraps access to a Postgres stored procedure, exposing it to Ruby as if it were a Ruby Proc.

Defined Under Namespace

Classes: Arguments, NotFound, TooManyChoices

Constant Summary

INFO_SQL =

The SQL query used to load meta info about the procedure.

Pathname(__FILE__).dirname.join("procedure.sql").read

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Procedure) initialize(name, options = {})

Creates a new stored procedure.



46
47
48
49
50
# File 'lib/squirm/procedure.rb', line 46

def initialize(name, options = {})
  @name      = name
  @schema    = options[:schema] || 'public'
  @arguments = Arguments.new(options[:args]) if options[:args]
end

Instance Attribute Details

- (Squirm::Procedure::Arguments) arguments (readonly)

An instance of Arguments encapsulating information about the arguments needed to invoke the procedure.



20
21
22
# File 'lib/squirm/procedure.rb', line 20

def arguments
  @arguments
end

- (String) name (readonly)

The Postgres stored procedure's name.



11
12
13
# File 'lib/squirm/procedure.rb', line 11

def name
  @name
end

- (String) query (readonly)

The SQL query used to invoke the stored procedure.



28
29
30
# File 'lib/squirm/procedure.rb', line 28

def query
  @query
end

- (String) return_type (readonly)

The procedure's Postgres return type



24
25
26
# File 'lib/squirm/procedure.rb', line 24

def return_type
  @return_type
end

- (String) schema (readonly)

The schema which holds the stored procedure. Defaults to public.



15
16
17
# File 'lib/squirm/procedure.rb', line 15

def schema
  @schema
end

Class Method Details

+ (Object) load(*args)

Creates procedure and loads it right away.



53
54
55
# File 'lib/squirm/procedure.rb', line 53

def self.load(*args)
  new(*args).load
end

Instance Method Details

- (Object) call(*args, &block) Also known as: []

Invokes the procedure.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/squirm/procedure.rb', line 80

def call(*args, &block)
  Squirm.exec query, arguments.format(*args) do |result|
    if block_given?
      yield result
    elsif return_type =~ /\ASETOF/
      result.to_a
    else
      result.getvalue(0,0)
    end
  end
end

- (String) info_sql

The SQL query used to get meta information about the procedure.

See Also:



75
76
77
# File 'lib/squirm/procedure.rb', line 75

def info_sql
  INFO_SQL
end

- (Squirm::Procedure) load

Loads meta info about the stored procedure.

This action is not performed in the constructor to allow instances to be created before a database connection has been established.



63
64
65
66
67
68
69
70
# File 'lib/squirm/procedure.rb', line 63

def load
  query = (arguments or self).info_sql
  Squirm.exec(query, [name, schema]) do |result|
    validate result
    set_values_from result
  end
  self
end

- (String) quoted_name

The quoted procedure name.



127
128
129
# File 'lib/squirm/procedure.rb', line 127

def quoted_name
  Squirm.quote_ident name
end

- (String) quoted_schema

The quoted schema name.



133
134
135
# File 'lib/squirm/procedure.rb', line 133

def quoted_schema
  Squirm.quote_ident schema
end

- (Object) to_proc

Gets a Ruby proc that calls this procedure.



95
96
97
# File 'lib/squirm/procedure.rb', line 95

def to_proc
  ->(*args) {call(*args)}
end