iBatis for Ruby (aka RBatis) is now finally available in the Apache Subversion repositories on this Subversion URL:
https://svn.apache.org/repos/asf/ibatis/trunk/rb/
For those of you that doesn’t know what iBatis is: It’s a very simple O/R mapper that allows for complete flexibility when it comes to O/R mapping. It doesn’t make any assumptions about the database at all so can therefor handle virtually any (relational) database schema in existance. This flexibility comes to a cost of course, using iBatis is several times more time-consuming and error prone compared to ActiveRecord.
You should not use iBatis unless you really have to: use ActiveRecord instead. I warn you, do not underestimate how much more work it can be to do manual O/R mapping. Or as Chad Fowler so eloquently put it on Rails Core:
Jon, I like what you’re doing, but by God I hope I never have to use it.
Here’s a simple example of a model object implemented using iBatis:
class Product < RBatis::Base
attr_reader :product_id
attr_reader :name
attr_reader :items
attr_reader :description
resultmap :default,
:product_id => [“productid”, String],
:name => [“name”, String],
:description => [“descn”, String],
:items => RBatis::LazyAssociation.new(
:to => Item,
:select => :find_by_product,
:key => :product_id)
statement :select_one, :find do |productid|
[“SELECT * FROM product WHERE productid = ?”, productid]
end
statement :select, :find_by_category_with_limit_and_offset
do |category, limit, offset|
[“SELECT * FROM product WHERE category = ? LIMIT ?, ?”,
category, offset, limit]
end
statement :select, :find_by_category do |category|
[“SELECT * FROM product WHERE category = ?”, category]
end
statement :select_value, :count_in_category,
:result_type => Fixnum
do |category|
[“SELECT COUNT FROM product WHERE category = ?”, category]
end
end
There is a complete application implemented as a demo. It’s a reimplementation of the good old PetStore app, which is also used as a demo for the Java version of iBatis. The PetStore database schema is (in)famous for it’s utter quirkiness database schema, for example one of the domain objects is scattered over three tables. Even if the Ruby on Rails (with iBatis) implementation is significantly more complex than a normal Rails application it’s significantly simpler than the Java counterpart (and just trust me when I say it took significantly shorter time to write). Check out the demo here, it should show how to use iBatis for Ruby in most simple cases (for the complex ones you’re on your own for the time being):
https://svn.apache.org/repos/asf/ibatis/trunk/rb/rpetstore/
You should be able to install the plugin by simply doing:
script/plugin install \
https://svn.apache.org/repos/asf/ibatis/trunk/rb/rbatis/
I have a lot planned for this one but unfortunately just too little time to do it at the moment (something that should hopefully change soon). I want to support things like: outer join fetching, prepared statements and stored procedures. Some of this can be implemented in RBatis but some needs changes to be done in ActiveRecord and in adapters if they are to support this (prepared statements are for example very important with Oracle databases). I am planning at least a beta release before RailsConf. Watch this space for more details. This is an Apache project, so normal Apache procedures will be followed for patches, committership and so forth.
Like this article? Digg it!