简体   繁体   English

是否有一种数据库独立的方法来获取Ruby中的Db2 SQL结果?

[英]Is there a DB independent way to get Db2 SQL results in Ruby?

I have Rake code which I've cobbled together from some sample code in my shop, and the advice of another programmer. 我有一些Rake代码,这些代码是我在商店中的一些示例代码中拼凑而成的,还有其他程序员的建议。 It looks more or less as follows: 它看起来大致如下:

class Ticket666StupidDb2Test < ActiveRecord::Migration
    def up
        lookup('WXYZ3529300')
    end

    def down
        puts "down, boy!"
    end

    def lookup(xiskid)
       qresult = exec_query("SELECT DISTINCT SNARK, GLOOPEE FROM VITA_XISK WHERE XISKID = '#{xiskid}'")
       while row = IBM_DB.fetch_array(qresult) do
           snark = row[0]
           gloopee = row[1]
           puts "snark = #{snark}; gloopee = #{gloopee}"
       end
    end
end
  • Is there a way to use the result set column names in getting the data, instead of integer indices? 有没有一种方法可以使用结果集列名来获取数据,而不是整数索引? I looked at http://rubyibm.rubyforge.org/docs/adapter/2.5.9/doc/ActiveRecord/ConnectionAdapters/IBM_DBAdapter.html and found at least one method, fetch_data , which seemed as though it would allow one to reference column names, but every attempt to try to use it produced an error such as: 我查看了http://rubyibm.rubyforge.org/docs/adapter/2.5.9/doc/ActiveRecord/ConnectionAdapters/IBM_DBAdapter.html ,发现至少有一种方法fetch_data ,似乎可以允许它引用列名称,但每次尝试使用它都会产生错误,例如:

     rake aborted! undefined method `fetch_data' for #<ActiveRecord::ConnectionAdapters::IBM_DBAdapter:0x38c16c0> 
  • What is wrong with this code, which was my first attempt: 此代码有什么问题,这是我的第一次尝试:

     x = exec_query("Select ... blah blah blah...") x.each do |row| puts row['SNARK'] end 

    This was modeled on stuff documented at http://api.rubyonrails.org/classes/ActiveRecord/Result.html , but it failed with a similar exception that x.each is undefined. 这是基于http://api.rubyonrails.org/classes/ActiveRecord/Result.html上记录的内容进行建模的,但是它失败了,但有一个类似的例外,即未定义x.each

  • Regardless, the notion that I have to resort to anything like IBM_DB.whathaveyou strikes me as obscene. 无论如何,我必须诉诸于诸如IBM_DB.whathaveyou类的想法让我感到as亵。 Either IBM doesn't believe in making a standards conforming driver (not my experience using the Db2 driver for Java JDBC), or I just don't know what the supported, straightforward approach to this is. IBM要么不相信制作符合标准的驱动程序(不是我使用Java JDBC的Db2驱动程序的经验),要么我只是不知道支持哪种简单的方法。

Can someone tell me? 有人可以告诉我吗?

ruby -v returns ruby 1.9.3p545 (2014-02-24) [i386-mingw32] . ruby -v返回ruby 1.9.3p545 (2014-02-24) [i386-mingw32]

What is wrong with x = exec_query("Select ... blah blah blah...") ? x = exec_query("Select ... blah blah blah...")什么问题? If your query is not SQL compliant then you've tied yourself to the DBM and will have a lot more work when you need to migrate to some other DBM. 如果您的查询不符合SQL规定,那么您就将自己与DBM绑定在一起, 需要迁移到其他DBM ,它将有更多工作要做。 That's the whole point of using an ORM, to separate the code from the query. 这就是使用ORM将代码与查询分开的全部要点。 It also means you'll need to stand up an instance of DB2 for development, test and production because your code can't adjust for scenarios like using SQLite for development locally, MySQL or PostgreSQL in test and DB2 in production. 这也意味着您需要为开发,测试和生产建立一个DB2实例,因为您的代码无法适应使用SQLite进行本地开发,在测试中使用MySQL或PostgreSQL以及在生产中使用DB2等场景。 That's really easy with a good ORM. 好的ORM确实很容易。

Using or renaming IBM_DB isn't a significant issue in my experience. 根据我的经验,使用或重命名IBM_DB并不是一个重要的问题。 If you don't like calling it IBM_DB , assign that constant to another that is more visually pleasing. 如果您不喜欢将其IBM_DB ,则将该常量分配给另一个更美观的常量。 Sequel code typically uses DB but it's up to us what we want to call it. 续集代码通常使用DB但我们要如何称呼它。 It's just a constant holding the connection information. 它只是保持连接信息的常量。

Having to index into an array is the result of using fetch_array . 必须索引到数组是使用fetch_array的结果。 Typically, instead of using integers to identify fields, I'd define constants that are more symbolic/mnemonic and use them in place of the numbers. 通常,我不使用整数来标识字段,而是定义更具符号/助记符的常量,并使用它们代替数字。 But, again, a good ORM makes it easy to use classes/models based on the table schema, adding layers of convenience and readability to isolate you from the uglier lower level driver's API. 但是,同样,好的ORM使得基于表模式的类/模型的使用变得容易,从而增加了便利性和可读性,从而使您与较丑陋的低级驱动程序API隔离开来。

Look at using Active Record or Sequel . 查看使用Active Record或Sequel Sequel supports DB2 nicely and is easily as powerful as Active Record and works with Rails. Sequel很好地支持DB2,并且与Active Record一样强大,并且可以与Rails一起使用。 Both also work well in a non-Rails script; 两者在非Rails脚本中也能很好地工作。 Sequel is my favorite for that but YMMV. 续集是我最喜欢的,但YMMV。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM