ruby-oci8 2.0をRuby1.9(mswin32)で使ってみる

ruby-oci8

以前にも紹介したが、ruby-oci8はoracle oci8クライアントのrubyインタフェース。version 2.0でruby1.9への対応を表明している。
ruby-oci8 - News in 2.0

そもそもRuby 1.9環境では rub-oci8-1.x系のインストールが通らないのだ。

ERROR:  Error installing ruby-oci8:
        ruby-oci8 requires Ruby version ~> 1.8.0

ms-win32版が無いのは解りつつもインストール。
パッケージはhttp://rubyforge.org/frs/download.php/51406/ruby-oci8-2.0.0.gemを使用。他の拡張ライブラリィと同様に試してみよう。

>"%VS80COMNTOOLS%vsvars32.bat"
Setting environment for using Microsoft Visual Studio 2005 x86 tools.

>gem install ruby-oci8-2.0.0 --local
Building native extensions.  This could take a while...
ERROR:  Error installing ruby-oci8-2.0.0:
        ERROR: Failed to build gem native extension.

e:/ruby/bin/ruby.exe extconf.rb install ruby-oci8-2.0.0 --local
checking for load library path...
  PATH...
  :
  略
  :
    checking E:\oracle\product\10.2.0\client\bin... yes
  E:/oracle/product/10.2.0/client/bin/oci.dll looks like a full client.
checking for cc... ok
  :
  略
  :
e:/ruby/lib/ruby/gems/1.9.1/gems/ruby-oci8-2.0.0/ext/oci8/oraconf.rb:778:in `get_home': RuntimeError (RuntimeError)
  :
  略
ok
                                                                                                    • -
error messages: Set the environment variable ORACLE_HOME if Oracle Full Client. Append the path of Oracle client libraries to PATH if Oracle Instant Client.
                                                                                                    • -
See: * http://ruby-oci8.rubyforge.org/en/HowToInstall.html * http://ruby-oci8.rubyforge.org/en/ReportInstallProblem.html

内部でextconf.rbを実行しているのが解るが、コンパイルのためにMakeFileを作成しようとして失敗しているらしい。
oci.dllの探索には成功しているが、それとは別にORACLE_HOMEの取得が必須らしい。

ということで、環境変数 ORACLE_HOMEを設定してから、同様に実行してみた。

>set ORACLE_HOME=E:\oracle\product\10.2.0\client
>gem install ruby-oci8-2.0.0 --local
Building native extensions.  This could take a while...
Successfully installed ruby-oci8-2.0.0
1 gem installed
Installing ri documentation for ruby-oci8-2.0.0...
Installing RDoc documentation for ruby-oci8-2.0.0...

OK。インストール出来たぞ。

ポイントは二つだ。

  • oci.dllがパス上で見つかること
  • 環境変数ORACLE_HOMEが正しく設定されていること

じゃあ、早速oci8-2.0.0のテストをしてみよう。

E:\ruby>irb
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'oci8'
=> true
irb(main):003:0> con = OCI8.new("scott", "tiger")
=> #
irb(main):004:0> con.exec('select * from DUAL') do |r| puts r.join(', '); end
X
=> 1
irb(main):005:0>

OK。これでRuby1.9でもruby-oci8が使える。