【发布时间】:2015-11-12 21:02:23
【问题描述】:
我需要使用 ActiveRecord 调用一些遗留存储过程。
当程序没有参数或只有 IN 参数时,我可以使用 ActiveRecords 调用它们。
例如
ActiveRecord::Base.connection.execute('call legacy_proc')
或
ActiveRecord::Base.connection.execute("call legacy_proc('param1', 'param2')")
上面的工作非常好。现在,当我需要使用 OUT 参数调用过程时,问题就出现了。我尝试了不同的调用 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 参数)。作为最后一个选择,如果真的不可能,我什至可以跳过读取 OUT 参数,只要我至少可以执行该过程。
编辑:添加错误详细信息以澄清“不工作”。
ActiveRecord::StatementInvalid: ActiveRecord::JDBCError: com.ibm.db2.jcc.am.SqlException: DB2 SQL Error: SQLCODE=-313, SQLSTATE=07004, SQLERRMC=null, DRIVER=4.15.113: call legacy_proc_with_out_params('param1', 'param2', ?, ?)
from arjdbc/jdbc/RubyJdbcConnection.java:547:in `execute'
from /home/devusr.gem/gems/activerecord-jdbc-adapter-1.3.16/lib/arjdbc/jdbc/adapter.rb:581:in `_execute'
from /home/devusr.gem/gems/activerecord-jdbc-adapter-1.3.16/lib/arjdbc/jdbc/adapter.rb:557:in `execute'
from /home/devusr.gem/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `log'
from /home/devusr.gem/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/devusr.gem/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log'
from /home/devusr.gem/gems/activerecord-jdbc-adapter-1.3.16/lib/arjdbc/jdbc/adapter.rb:557:in `execute'
from (irb):30: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/devusr.gem/gems/railties-4.1.8/lib/rails/commands/console.rb:90:in `start'
from /home/devusr.gem/gems/railties-4.1.8/lib/rails/commands/console.rb:9:in `start'
from /home/devusr.gem/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/devusr.gem/gems/railties-4.1.8/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/devusr.gem/gems/railties-4.1.8/lib/rails/commands.rb:17:in `(root)'
from org/jruby/RubyKernel.java:1083:in `require'
编辑 2:尝试了 @mustaccio 的建议(即通过 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)'
【问题讨论】:
-
定义“不工作”。
-
@mustaccio 错误详情添加
标签: ruby stored-procedures activerecord db2 jruby