Is it possible to call a MySQL stored procedure from Ruby?

后端 未结 3 1238
借酒劲吻你
借酒劲吻你 2020-12-10 07:55

When I try to call a stored procedure from Rails, I get this exception:

ActiveRecord::StatementInvalid: Mysql::Error: PROCEDURE pipeline-ws_development.match         


        
相关标签:
3条回答
  • 2020-12-10 08:25

    I've submitted a patch to Rails 2.3.4 which gives a configuration option to fix this problem. Please stop by my ticket and show your support for it!

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

    0 讨论(0)
  • 2020-12-10 08:29

    Are you using ActiveRecord::Base.connection.execute? This method should allow you to execute some arbitrary SQL statement that isn't naively supported in the Active Record wrapper.

    0 讨论(0)
  • 2020-12-10 08:38

    Would it work to wrap the procedure in a function? If Ruby's barfing due to no rows returned (...can't return a result set in the given context...), this may fix it:

    DELIMITER $
    
    CREATE PROCEDURE tProc()
    BEGIN
        SET @a = 'test';
    END;
    $
    
    CREATE FUNCTION tFunc()
    RETURNS INT
    BEGIN
        CALL tProc();
        RETURN 1;
    END;
    $
    
    DELIMITER ;
    
    SELECT tFunc() FROM DUAL;
    >> 1
    
    SELECT @a FROM DUAL;
    >> 'test'
    

    Although, realistically, this isn't a very extensible solution.

    Followup: I'm pretty n00by at Ruby/ActiveRecord, but this example definitely works

    ActiveRecord::Base.establish_connection(authopts)
    
    class TestClass < ActiveRecord::Base
    end
    
    test_class = TestClass.new
    puts %{#{test_class.connection.select_one('SELECT tFunc() AS tf FROM DUAL')}}
    >> tf1
    

    Using CALL tProc() resulted in an error similar to yours.

    0 讨论(0)
提交回复
热议问题