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
def self.method_missing(method, params = [], options = {}) self.call_proc(method, params, options) end.
- .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
def self.method_missing(method, params = [], options = {})
self.call_proc(method, params, )
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 = [], = {}) 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 #{[:conditions].first} " : '' order = [:order].present? ? " ORDER BY #{[: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
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 |