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 collapse

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

#initialize(name, options = {}) ⇒ Procedure

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

#argumentsSquirm::Procedure::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

#nameString (readonly)

The Postgres stored procedure’s name.

Returns:

  • (String)


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

def name
  @name
end

#queryString (readonly)

The SQL query used to invoke the stored procedure.

Returns:

  • (String)


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

def query
  @query
end

#return_typeString (readonly)

The procedure’s Postgres return type

Returns:

  • (String)


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

def return_type
  @return_type
end

#schemaString (readonly)

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

Returns:

  • (String)


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

def schema
  @schema
end

Class Method Details

.load(*args) ⇒ Object

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

#call(*args, &block) ⇒ Object 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

#info_sqlString

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

Returns:

  • (String)

See Also:



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

def info_sql
  INFO_SQL
end

#loadSquirm::Procedure

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.

Returns:



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

#quoted_nameString

The quoted procedure name.

Returns:

  • (String)


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

def quoted_name
  Squirm.quote_ident name
end

#quoted_schemaString

The quoted schema name.

Returns:

  • (String)


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

def quoted_schema
  Squirm.quote_ident schema
end

#to_procObject

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