Database interfaces in Ruby are a little off, if you ask me; they’re as usable as PHP’s, for sure (which are really just raw API for whatever database you use, unless you use ADODB), but I expect more out of Ruby’s.
First off, I expect libraries to use basic data structures whenever possible. People are used to them. If there’s no reason to have your own class of stuff, don’t.
From DBI, I would expect queries to return a hash, with fields either being a string as a key, and value as the natural basic Ruby type for whatever type the field is: text
, varchar
and char
fields should be Strings
. int
I would expect to be a Fixnum
.
I expect all the fields in the query to be in the hash, even if the fields are null — given fields foo
and bar
, with bar being null in this row, I would expect the hash to contain { :foo => "foodata", :bar => nil }
.
Using hashes, it's possible to create tuples for update with a little addition to the Hash
class, like so:
class Hash
def import(other)
each_key do |k|
self[k] = other[k] if other[k]
end
end
end
That would update the hash with just new values; if HTTP POST parameters are also handled as a hash, that makes merging data from the web into the database as painless as db.store data.import request.query
.