-
-
Save kadru/72973add9b678338ca65bd36efc8b97a to your computer and use it in GitHub Desktop.
Execute stored procedure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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