是否可以从Ruby调用MySQL存储过程?

当我尝试从Rails调用存储过程时,我得到以下exception:

ActiveRecord::StatementInvalid: Mysql::Error: PROCEDURE pipeline-ws_development.match_save_all can't return a result set in the given context: call match_save_all() from /Users/otto/Projects/Futures/src/pipeline-ws/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:150:in `log' from /Users/otto/Projects/Futures/src/pipeline-ws/vendor/rails/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb:281:in `execute' from (irb):3 

Rails Wiki中有一个页面讨论了解决此问题的MySQL适配器的补丁 ,但它已经过时并且似乎不再起作用了。

配置代码可以正确启用存储过程,但是在存储过程调用和新的call_sp方法不再起作用后,连接仍然存在问题。

有关如何使其工作的任何建议?

这是我正在使用的代码:

 ActiveRecord::Base.connection("call storedproc()") 

无论storedproc()是否返回任何结果,它都会抛出相同的exception。

是否可以将过程包装在函数中? 如果由于没有返回行而导致Ruby的barfing( ...can't return a result set in the given context... ),这可能会解决它:

 DELIMITER $

 CREATE PROCEDURE tProc()
开始
     SET @a ='test';
结束;
 $

 CREATE FUNCTION tFunc()
退货INT
开始
     CALL tProc();
    返回1;
结束;
 $

 DELIMITER;

 SELECT tFunc()FROM DUAL;
 >> 1

 SELECT @a FROM DUAL;
 >>'测试'

虽然,实际上,这不是一个非常可扩展的解决方案。

跟进:我非常熟悉Ruby / ActiveRecord,但这个例子绝对有效

的ActiveRecord :: Base.establish_connection(authopts)

 class TestClass > tf1

使用CALL tProc()导致类似于您的错误。

我已经向Rails 2.3.4提交了一个补丁,它提供了一个配置选项来解决这个问题。 请停止我的机票并显示您对它的支持!

https://rails.lighthouseapp.com/projects/8994/tickets/3151-mysql-adapter-update-to-enable-use-of-stored-procedures

你在使用ActiveRecord :: Base.connection.execute吗? 此方法应该允许您执行一些在Active Record包装器中不天真支持的任意SQL语句。