Class: StoredProcedure
- Inherits:
-
Object
- Object
- StoredProcedure
- Defined in:
- lib/call_sp/stored_procedure.rb
Class Method Summary collapse
- .call_proc(proc_name, params = [], options = {}) ⇒ Object
- .execute_sp(sql, *bindings) ⇒ Object
- .fetch_sp(sql, *bindings) ⇒ Object
- .fetch_sp_val(sql, *bindings) ⇒ Object
Class Method Details
.call_proc(proc_name, params = [], options = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/call_sp/stored_procedure.rb', line 3 def self.call_proc(proc_name, params = [], = {}) params.delete_if { |p| p.nil? } select = [:select].present? ? [:select].map {|k, v| "#{k} AS #{v}" }.join(', ') : '*' schema = [:schema] || 'public' conditions = [:conditions] || [] where = [:conditions].present? ? " WHERE #{options[:conditions].first} " : '' order = [: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 = [: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
31 32 33 |
# File 'lib/call_sp/stored_procedure.rb', line 31 def self.execute_sp(sql, *bindings) perform_sp(:execute, sql, *bindings) end |
.fetch_sp(sql, *bindings) ⇒ Object
35 36 37 |
# File 'lib/call_sp/stored_procedure.rb', line 35 def self.fetch_sp(sql, *bindings) perform_sp(:select_all, sql, *bindings) end |
.fetch_sp_val(sql, *bindings) ⇒ Object
39 40 41 |
# File 'lib/call_sp/stored_procedure.rb', line 39 def self.fetch_sp_val(sql, *bindings) perform_sp(:select_value, sql, *bindings) end |