Created
September 13, 2012 12:50
-
-
Save ys/3714101 to your computer and use it in GitHub Desktop.
Execute stored procedure
This file contains 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 StoredProcedureService | |
def self.instance | |
@instance ||= StoredProcedureService.new | |
end | |
def execute(name, *args) | |
results = [] | |
begin | |
connection.execute("CALL #{name}(#{args.join(',')})").each(as: :hash, symbolize_keys: true) do |row| | |
results << OpenStruct.new(row) | |
end | |
ensure | |
connection.close | |
end | |
results | |
end | |
def connection | |
ActiveRecord::Base.connection | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I do it slightly differently, using the connection pool, and not instantiating the "service":