Module: UsesStoredProcedures::ClassMethods
- Defined in:
- lib/uses_stored_procedures.rb
Instance Method Summary collapse
-
#call_stored_proc_verb ⇒ Object
Return the SQL verb for calling a stored procedure.
-
#exec_stored_proc(name, call_stored_proc_verb, filter, args) ⇒ Object
Run the stored procedure with the arguments provided and send them through a filter method or block if specified.
-
#install_stored_proc_methods(name, proc_name, call_stored_proc_verb, filter) ⇒ Object
Define the class and instance methods.
-
#uses_stored_proc(name, *args, &block) ⇒ Object
Add a stored procedure that can ba called on a class or a class instance.
Instance Method Details
#call_stored_proc_verb ⇒ Object
Return the SQL verb for calling a stored procedure.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/uses_stored_procedures.rb', line 44 def call_stored_proc_verb() case ActiveRecord::Base.connection.adapter_name.to_sym when :Mysql2, :MySQL 'call' when :PostgreSQL 'select' when :SQLServer 'exec' else raise "uses_stored_procedurs does not support your connection adapter" end end |
#exec_stored_proc(name, call_stored_proc_verb, filter, args) ⇒ Object
Run the stored procedure with the arguments provided and send them through a filter method or block if specified.
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/uses_stored_procedures.rb', line 74 def exec_stored_proc(name, call_stored_proc_verb, filter, args) #:nodoc: sql = "#{call_stored_proc_verb} #{name} (" + args.map {|a| "'#{a}'"}.join(',') + ")" # Call the stored procedure. Note the need to reset the connection # because the mysql connection tends to hangup when stored procedures # are called. Note that testing for ::connected? doesn't work. # # TODO: investigate why stored procedures cause connection hangups. records = ActiveRecord::Base.connection.select_all(sql) ActiveRecord::Base.connection.reconnect! filter_stored_proc_results(records, filter) end |
#install_stored_proc_methods(name, proc_name, call_stored_proc_verb, filter) ⇒ Object
Define the class and instance methods. Note the need to create a singleton class instance to create the stored procedure class method.
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/uses_stored_procedures.rb', line 60 def install_stored_proc_methods(name, proc_name, call_stored_proc_verb, filter) #:nodoc: define_method name do |*args| self.class.exec_stored_proc(proc_name, call_stored_proc_verb, filter, args) end singleton_class = class << self; self; end singleton_class.send(:define_method, name) do |*args| exec_stored_proc(proc_name, call_stored_proc_verb, filter, args) end end |
#uses_stored_proc(name, *args, &block) ⇒ Object
Add a stored procedure that can ba called on a class or a class instance.
By default, it is assumed that the stored procedure has the same name as a string.
include:
* :proc_name - is the name of the stored procedure. Use this if the
name doesn't match the name paramter
* :filter - is a class method used to filter the return results. The
method should take a HashWithAttributes instance and return an object
for the array entry
and returns an object for the array entry.
34 35 36 37 38 39 40 |
# File 'lib/uses_stored_procedures.rb', line 34 def uses_stored_proc(name, *args, &block) = args. proc_name = [:proc_name] || name.to_s # Install the row mapper block or method self.install_stored_proc_methods(name, proc_name, call_stored_proc_verb, [:filter] || block) end |