Ruby 如何使用ActiveRecord调用没有参数的DB2存储过程?

Ruby 如何使用ActiveRecord调用没有参数的DB2存储过程?,ruby,stored-procedures,activerecord,db2,jruby,Ruby,Stored Procedures,Activerecord,Db2,Jruby,我需要使用ActiveRecord调用一些遗留存储过程 我可以使用ActiveRecords调用没有参数或只有参数的过程 e、 g 或 上述方法非常有效。现在,当我需要调用一个没有参数的过程时,问题就出现了。我尝试了不同的方式呼叫SP,但无法使其正常工作 ActiveRecord::Base.connection.execute("call legacy_proc_with_out_params('param1', 'param2', ?, ?)") ActiveRecord::Base.

我需要使用ActiveRecord调用一些遗留存储过程

我可以使用ActiveRecords调用没有参数或只有参数的过程

e、 g

上述方法非常有效。现在,当我需要调用一个没有参数的过程时,问题就出现了。我尝试了不同的方式呼叫SP,但无法使其正常工作

 ActiveRecord::Base.connection.execute("call legacy_proc_with_out_params('param1', 'param2', ?, ?)")

 ActiveRecord::Base.connection.execute("call legacy_proc_with_out_params('param1', 'param2', null, null)")

 ActiveRecord::Base.connection.execute("call legacy_proc_with_out_params('param1', 'param2', '', '')")
以上工作均无效(最后两个参数为OUT参数)。作为最后一个选项,如果真的不可能,我甚至可以跳过读取参数,只要我至少可以执行这个过程

编辑:添加错误详细信息以澄清“不工作”

编辑2:尝试了@Mustacio的建议(即通过
raw_connection
使用
prepare
方法,但是DB2适配器没有实现该方法。如果有其他途径可以在DB2中实现类似的方法,这肯定会解决问题

我还忘了提到我正在使用jRuby,因此,我正在使用用于DB2的jdbc适配器

 irb(main):049:0> ActiveRecord::ConnectionAdapters.constants.grep /DB2/
 => [:DB2Column, :DB2JdbcConnection]

 irb(main):050:0> ActiveRecord::Base.connection.raw_connection.methods.sort.grep /prep/
 => []

 irb(main):051:0>  ActiveRecord::Base.connection.raw_connection.prepare
 NoMethodError: undefined method `prepare' for #<ActiveRecord::ConnectionAdapters::DB2JdbcConnection:0xe6bc1cd0>
    from (irb):51:in `evaluate'
    from org/jruby/RubyKernel.java:1119:in `eval'
    from org/jruby/RubyKernel.java:1519:in `loop'
    from org/jruby/RubyKernel.java:1282:in `catch'
    from org/jruby/RubyKernel.java:1282:in `catch'
    from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/console.rb:90:in `start'
    from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/console.rb:9:in `start'
    from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands.rb:17:in `(root)'
    from org/jruby/RubyKernel.java:1083:in `require'
    from bin/rails:4:in `(root)'
irb(main):049:0>ActiveRecord::ConnectionAdapters.constants.grep/DB2/
=>[:DB2Column,:DB2JdbcConnection]
irb(main):050:0>ActiveRecord::Base.connection.raw_connection.methods.sort.grep/prep/
=> []
irb(主):051:0>ActiveRecord::Base.connection.raw\u connection.prepare
NoMethodError:未定义的“准备”方法#
来自(irb):51:in“evaluate”
来自org/jruby/RubyKernel.java:1119:in'eval'
来自org/jruby/RubyKernel.java:1519:in'loop'
来自org/jruby/RubyKernel.java:1282:in'catch'
来自org/jruby/RubyKernel.java:1282:in'catch'
from/home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/console.rb:90:in'start'
from/home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/console.rb:9:in'start'
from/home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/commands\u tasks.rb:69:在“控制台”中
from/home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/commands\u tasks.rb:40:in'run\u command!'
from/home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands.rb:17:in`(root)'
来自org/jruby/RubyKernel.java:1083:in'require'
从bin/rails:4:in`(root)'

我找到的解决方案是使用BEGIN-END块并声明伪变量来绑定OUT参数

举个例子:

ActiveRecord::Base.connection.execute("call legacy_proc_with_out_params('param1', 'param2', ?, ?)")
如果第一个OUT参数是INTEGER,第二个是SMALLINT,则完整调用如下所示:

 beginend_stmt = "BEGIN
                      DECLARE out_Param1 INTEGER;
                      DECLARE out_Param2 SMALLINT;

                      call legacy_proc_with_out_params('param1', 'param2', out_Param1, out_Param2);
                  END"

  ActiveRecord::Base.connection.execute beginend_stmt

定义“不工作”。@mustaccio错误详细信息已添加
 irb(main):049:0> ActiveRecord::ConnectionAdapters.constants.grep /DB2/
 => [:DB2Column, :DB2JdbcConnection]

 irb(main):050:0> ActiveRecord::Base.connection.raw_connection.methods.sort.grep /prep/
 => []

 irb(main):051:0>  ActiveRecord::Base.connection.raw_connection.prepare
 NoMethodError: undefined method `prepare' for #<ActiveRecord::ConnectionAdapters::DB2JdbcConnection:0xe6bc1cd0>
    from (irb):51:in `evaluate'
    from org/jruby/RubyKernel.java:1119:in `eval'
    from org/jruby/RubyKernel.java:1519:in `loop'
    from org/jruby/RubyKernel.java:1282:in `catch'
    from org/jruby/RubyKernel.java:1282:in `catch'
    from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/console.rb:90:in `start'
    from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/console.rb:9:in `start'
    from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:69:in `console'
    from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
    from /home/srpec/.gem/gems/railties-4.1.8/lib/rails/commands.rb:17:in `(root)'
    from org/jruby/RubyKernel.java:1083:in `require'
    from bin/rails:4:in `(root)'
ActiveRecord::Base.connection.execute("call legacy_proc_with_out_params('param1', 'param2', ?, ?)")
 beginend_stmt = "BEGIN
                      DECLARE out_Param1 INTEGER;
                      DECLARE out_Param2 SMALLINT;

                      call legacy_proc_with_out_params('param1', 'param2', out_Param1, out_Param2);
                  END"

  ActiveRecord::Base.connection.execute beginend_stmt