Class: StoredProcedure

Inherits:
Object
  • Object
show all
Defined in:
lib/call_sp/stored_procedure.rb

Class Method Summary collapse

Class Method Details

.call_proc(proc_name, params = [], options = {}) ⇒ Object

def self.method_missing(method, params = [], options = {})

self.call_proc(method, params, options)

end



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/call_sp/stored_procedure.rb', line 7

def self.call_proc(proc_name, params = [], options = {})
  params.delete_if { |p| p.nil? }

  select = options[:select].present? ? options[:select].map {|k, v| "#{k} AS #{v}" }.join(', ') : '*'
  schema = options[:schema] || 'public'
  conditions = options[:conditions] || []
  where = options[:conditions].present? ? " WHERE #{options[:conditions].first} " : ''
  order = options[:order].present? ? " ORDER BY #{options[:order]} " : ''

  sql = "SELECT #{select} FROM #{schema}.#{proc_name}(#{Array.new(params.count) { '?' }.join(', ')})#{where}#{order}"

  conditions.shift
  params.push(*conditions)

  mode = options[:mode] || :fetch_sp
  case mode
    when :fetch_sp_val
      self.fetch_sp_val(sql, *params)
    when :execute_sp
      self.execute_sp(sql, *params)
    when :fetch_sp
      self.fetch_sp(sql, *params)
    else
      raise 'Undefined mode'
  end

end

.execute_sp(sql, *bindings) ⇒ Object



35
36
37
# File 'lib/call_sp/stored_procedure.rb', line 35

def self.execute_sp(sql, *bindings)
  perform_sp(:execute, sql, *bindings)
end

.fetch_sp(sql, *bindings) ⇒ Object



39
40
41
# File 'lib/call_sp/stored_procedure.rb', line 39

def self.fetch_sp(sql, *bindings)
  perform_sp(:select_all, sql, *bindings)
end

.fetch_sp_val(sql, *bindings) ⇒ Object



43
44
45
# File 'lib/call_sp/stored_procedure.rb', line 43

def self.fetch_sp_val(sql, *bindings)
  perform_sp(:select_value, sql, *bindings)
end