Skip to content

Instantly share code, notes, and snippets.

@kadru
Forked from ys/stored_procedure_service.rb
Last active September 14, 2018 18:22
Show Gist options
  • Save kadru/72973add9b678338ca65bd36efc8b97a to your computer and use it in GitHub Desktop.
Save kadru/72973add9b678338ca65bd36efc8b97a to your computer and use it in GitHub Desktop.
Execute stored procedure
class DatabaseHelper
def self.select name, *args, &blk
execute "select", name, *args, &blk
end
def self.call name, *args, &blk
execute "call", name, *args, &blk
end
def self.execute via, name, *args, &blk
ActiveRecord::Base.connection_pool.with_connection do |connection|
args = args.map{|e| connection.quote e}
answer = connection.execute("#{via} #{name}(#{args.join(',')})").each(:as => :hash, :symbolize_keys => true) do |row|
results << OpenStruct.new(row)
end
if blk
blk.call(answer)
else
answer
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment