Class: Squirm::Procedure
- Inherits:
-
Object
- Object
- Squirm::Procedure
- 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
-
#arguments ⇒ Squirm::Procedure::Arguments
readonly
An instance of Arguments encapsulating information about the arguments needed to invoke the procedure.
-
#name ⇒ String
readonly
The Postgres stored procedure’s name.
-
#query ⇒ String
readonly
The SQL query used to invoke the stored procedure.
-
#return_type ⇒ String
readonly
The procedure’s Postgres return type.
-
#schema ⇒ String
readonly
The schema which holds the stored procedure.
Class Method Summary collapse
-
.load(*args) ⇒ Object
Creates procedure and loads it right away.
Instance Method Summary collapse
-
#call(*args, &block) ⇒ Object
(also: #[])
Invokes the procedure.
-
#info_sql ⇒ String
The SQL query used to get meta information about the procedure.
-
#initialize(name, options = {}) ⇒ Procedure
constructor
Creates a new stored procedure.
-
#load ⇒ Squirm::Procedure
Loads meta info about the stored procedure.
-
#quoted_name ⇒ String
The quoted procedure name.
-
#quoted_schema ⇒ String
The quoted schema name.
-
#to_proc ⇒ Object
Gets a Ruby proc that calls this procedure.
Constructor Details
Instance Attribute Details
#arguments ⇒ Squirm::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 |
#name ⇒ String (readonly)
The Postgres stored procedure’s name.
11 12 13 |
# File 'lib/squirm/procedure.rb', line 11 def name @name end |
#query ⇒ String (readonly)
The SQL query used to invoke the stored procedure.
28 29 30 |
# File 'lib/squirm/procedure.rb', line 28 def query @query end |
#return_type ⇒ String (readonly)
The procedure’s Postgres return type
24 25 26 |
# File 'lib/squirm/procedure.rb', line 24 def return_type @return_type end |
#schema ⇒ String (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
.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_sql ⇒ String
The SQL query used to get meta information about the procedure.
75 76 77 |
# File 'lib/squirm/procedure.rb', line 75 def info_sql INFO_SQL end |
#load ⇒ Squirm::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.
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_name ⇒ String
The quoted procedure name.
127 128 129 |
# File 'lib/squirm/procedure.rb', line 127 def quoted_name Squirm.quote_ident name end |
#quoted_schema ⇒ String
The quoted schema name.
133 134 135 |
# File 'lib/squirm/procedure.rb', line 133 def quoted_schema Squirm.quote_ident schema end |
#to_proc ⇒ Object
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 |