Ruby on Rails
IBM DB2 (Version #36)

There are currently 3 Ruby/DB2 binding that appear to work.
One of them is the driver and adapter provided directly by IBM.

IBM_DB gem (adapter & driver) for IBM data servers

The most up-to-date ActiveRecord support for IBM data servers, the IBM_DB gem adapter & driver, is available from RubyForge rubyibm project. It supports DB2 version 8 and 9 on all platforms (Linux, Unix, Windows, i5, zOS) and soon enough Informix IDS. The minimum requirement is the IBM DB2 Driver for ODBC and CLI version 9 FixPack 2 (or alternatively version 8 FixPack 15). Any DB2 client also contains DB2 CLI driver which is wrapped by the ibm_db driver and adapter packaged in the IBM_DB gem.
To install IBM_DB as gem:

  • Windows: > gem install ibm_db
  • Linux | Unix:
    $ export IBM_DB_DIR=/opt/ibm/db2/V9.1
    $ export IBM_DB_LIB=/opt/ibm/db2/V9.1/lib32
    $ gem install ibm_db

The adapter and driver name changed from ‘ibm_db2’ to ‘ibm_db’ to reflect its support for all IBM data servers. That has to be considered when registering in active_record.rb and also in the config/database.yml. Documentation is available in the rubyibm project, on RubyForge.

IBM alphaWorks Starter Toolkit for DB2 on Rails

Starter Toolkit for DB2® on Rails is a conveniently-packaged set of products and technologies that enables the quick creation of a configuration for building DB2 Web applications using Ruby on Rails technology.
There are two packages available, a win32 all inclusive toolkit, or the source code only (less than 200KB).

The toolkit for Windows includes:

  • DB2 Express – C, “free to build, deploy, distribute” data server
  • IBM DB2 driver for Ruby
  • IBM DB2 Adapter for Rails

Note: The following update of the Starter Toolkit will contain the latest IBM_DB gem from RubyForge, but currently current version is 0.2.0 (compared to IBM_DB-0.7.0)

The source code package includes code for the driver and the adapter and it’s suitable for Windows®, Linux® and Unix®.

Two types of connection

There are two type of connections available to suite your needs: through an Alias or through TCP/IP address. Typically the Alias based connection is used to connect to a local database (cataloged), while the net address one is used for remote databases. The local or remote DB2 server you are connecting to, must be running on a compatible operating system (currently Windows, Linux and Unix only, but in the future it will work on iSeries through DB2 Connect).

Alias based connection


development:
  adapter:  ibm_db2
  database: demo
  username: db2admin
  password: db2password
  schema:   cangiano

Database represents the alias DEMO. All the fields above are mandatory except for schema. If you don’t specify any schema, the adapter will use the username by default.

TCP/IP connection


development:
  adapter:  ibm_db2
  database: demo
  username: db2admin
  password: db2password
  schema:   cangiano
  host:     myhostname
  port:     50000

‘host’ can be an hostname or an IP address. Again, schema is optional, and defaulted to the specified username.

Notes

Linux/Unix users will need to set the environment with the command: source /home/db2inst1/sqllib/db2profile (or equivalent on your machine). Please also note that you need to specify ‘ibm_db2’ and not ‘db2’ in order to use the IBM adapter. The IBM driver is an evolution of Sam Ruby’s port of the php ibm_db2 extension.

Resources

Information entered by Antonio Cangiano, feel free to email me (run require 'base64';puts Base64.decode64("Y2FuZ2lhbm9AY2EuaWJtLmNvbQ==\n") to get my email address)

ruby-db2 from the Ruby/DBI project

The file you will want to download the most recent version of ruby-db2 (currently ruby-db2-0.4.4.tgz).

Follow the instructions in the README. If you are using DB2 V8.1 you will
need to set the DB2DIR environment variable, as it defaults to compile against
V7.1.


export DB2DIR=/opt/IBM/db2/V8.1
ruby setup.rb config
ruby setup.rb setup
sudo ruby setup.rb install

Once installed, you should be able to use DB2 with a configuration similar to
the following. One additional connection parameter is available for DB2,
:schema.


    sample:
       adapter:     db2
       database:    sample
       username:    sample_user
       password:    my_sample_user_password
       schema:      sample

ruby-odbc

* http://www.ch-werner.de/rubyodbc/

DB2 ships with full ODBC bindings and as such works very well with unixODBC.
Setup the odbcinst.ini and odbc.ini files correctly and ruby-odbc just works.
Unfortunately there does not seem to be a good ActiveRecord odbc driver for
rails quite yet. But once one does exist, this should be a valid option.


# odbcinst.ini
[DB2]    
Description     = DB2 Driver
Driver          = /home/db2inst1/sqllib/lib/libdb2.so
FileUsage       = 1
DontDLClose     = 1


[sample]
Description = sample
Driver = DB2

Note from Alex Pitigoi

The following note below no longer applies to the latest IBM_DB gem (adapter & driver) with regards to numeric values quoting and scaffolding. Some migration limitations are still documented (rename_column and remove_column on zOS) but work-around available.

Notes from DavidHeinemeierHansson.

these may be outdated statements and don’t apply to the IBM DB2 driver and adapter

DB2 effectively doesn’t work. There are too many things not yet supported for
even basic scaffolding to work. Has anyone ever built a website with Rails and
DB2? It seems to be there’s a long way to go before it will be at the level as
MySQL support is today. Is anyone working on this?

Please create tickets for the things you’re finding that “doesn’t work”.
Currently, I believe DB2 is passing just about all the unit tests. So if
there’s something that doesn’t work, we want to know about it. Do note,
though, that DB2 requires things like proper quoting based on the field types.
So you can’t do Person.find("1") and have it work like in MySQL, you need
Person.find(1). So all type-casting is yours to do — DavidHeinemeierHansson.

There are currently 3 Ruby/DB2 binding that appear to work.
One of them is the driver and adapter provided directly by IBM.

IBM_DB gem (adapter & driver) for IBM data servers

The most up-to-date ActiveRecord support for IBM data servers, the IBM_DB gem adapter & driver, is available from RubyForge rubyibm project. It supports DB2 version 8 and 9 on all platforms (Linux, Unix, Windows, i5, zOS) and soon enough Informix IDS. The minimum requirement is the IBM DB2 Driver for ODBC and CLI version 9 FixPack 2 (or alternatively version 8 FixPack 15). Any DB2 client also contains DB2 CLI driver which is wrapped by the ibm_db driver and adapter packaged in the IBM_DB gem.
To install IBM_DB as gem:

  • Windows: > gem install ibm_db
  • Linux | Unix:
    $ export IBM_DB_DIR=/opt/ibm/db2/V9.1
    $ export IBM_DB_LIB=/opt/ibm/db2/V9.1/lib32
    $ gem install ibm_db

The adapter and driver name changed from ‘ibm_db2’ to ‘ibm_db’ to reflect its support for all IBM data servers. That has to be considered when registering in active_record.rb and also in the config/database.yml. Documentation is available in the rubyibm project, on RubyForge.

IBM alphaWorks Starter Toolkit for DB2 on Rails

Starter Toolkit for DB2® on Rails is a conveniently-packaged set of products and technologies that enables the quick creation of a configuration for building DB2 Web applications using Ruby on Rails technology.
There are two packages available, a win32 all inclusive toolkit, or the source code only (less than 200KB).

The toolkit for Windows includes:

  • DB2 Express – C, “free to build, deploy, distribute” data server
  • IBM DB2 driver for Ruby
  • IBM DB2 Adapter for Rails

Note: The following update of the Starter Toolkit will contain the latest IBM_DB gem from RubyForge, but currently current version is 0.2.0 (compared to IBM_DB-0.7.0)

The source code package includes code for the driver and the adapter and it’s suitable for Windows®, Linux® and Unix®.

Two types of connection

There are two type of connections available to suite your needs: through an Alias or through TCP/IP address. Typically the Alias based connection is used to connect to a local database (cataloged), while the net address one is used for remote databases. The local or remote DB2 server you are connecting to, must be running on a compatible operating system (currently Windows, Linux and Unix only, but in the future it will work on iSeries through DB2 Connect).

Alias based connection


development:
  adapter:  ibm_db2
  database: demo
  username: db2admin
  password: db2password
  schema:   cangiano

Database represents the alias DEMO. All the fields above are mandatory except for schema. If you don’t specify any schema, the adapter will use the username by default.

TCP/IP connection


development:
  adapter:  ibm_db2
  database: demo
  username: db2admin
  password: db2password
  schema:   cangiano
  host:     myhostname
  port:     50000

‘host’ can be an hostname or an IP address. Again, schema is optional, and defaulted to the specified username.

Notes

Linux/Unix users will need to set the environment with the command: source /home/db2inst1/sqllib/db2profile (or equivalent on your machine). Please also note that you need to specify ‘ibm_db2’ and not ‘db2’ in order to use the IBM adapter. The IBM driver is an evolution of Sam Ruby’s port of the php ibm_db2 extension.

Resources

Information entered by Antonio Cangiano, feel free to email me (run require 'base64';puts Base64.decode64("Y2FuZ2lhbm9AY2EuaWJtLmNvbQ==\n") to get my email address)

ruby-db2 from the Ruby/DBI project

The file you will want to download the most recent version of ruby-db2 (currently ruby-db2-0.4.4.tgz).

Follow the instructions in the README. If you are using DB2 V8.1 you will
need to set the DB2DIR environment variable, as it defaults to compile against
V7.1.


export DB2DIR=/opt/IBM/db2/V8.1
ruby setup.rb config
ruby setup.rb setup
sudo ruby setup.rb install

Once installed, you should be able to use DB2 with a configuration similar to
the following. One additional connection parameter is available for DB2,
:schema.


    sample:
       adapter:     db2
       database:    sample
       username:    sample_user
       password:    my_sample_user_password
       schema:      sample

ruby-odbc

* http://www.ch-werner.de/rubyodbc/

DB2 ships with full ODBC bindings and as such works very well with unixODBC.
Setup the odbcinst.ini and odbc.ini files correctly and ruby-odbc just works.
Unfortunately there does not seem to be a good ActiveRecord odbc driver for
rails quite yet. But once one does exist, this should be a valid option.


# odbcinst.ini
[DB2]    
Description     = DB2 Driver
Driver          = /home/db2inst1/sqllib/lib/libdb2.so
FileUsage       = 1
DontDLClose     = 1


[sample]
Description = sample
Driver = DB2

Note from Alex Pitigoi

The following note below no longer applies to the latest IBM_DB gem (adapter & driver) with regards to numeric values quoting and scaffolding. Some migration limitations are still documented (rename_column and remove_column on zOS) but work-around available.

Notes from DavidHeinemeierHansson.

these may be outdated statements and don’t apply to the IBM DB2 driver and adapter

DB2 effectively doesn’t work. There are too many things not yet supported for
even basic scaffolding to work. Has anyone ever built a website with Rails and
DB2? It seems to be there’s a long way to go before it will be at the level as
MySQL support is today. Is anyone working on this?

Please create tickets for the things you’re finding that “doesn’t work”.
Currently, I believe DB2 is passing just about all the unit tests. So if
there’s something that doesn’t work, we want to know about it. Do note,
though, that DB2 requires things like proper quoting based on the field types.
So you can’t do Person.find("1") and have it work like in MySQL, you need
Person.find(1). So all type-casting is yours to do — DavidHeinemeierHansson.