DBrb
DBrb
is a wrapper to facilitate working with Ruby DBI. The
intention is to make the most commonly used DB functionality (as
determined by me) easy and intuitive to use.
Examples
db = DBrb.new("my_dbi_driver_string", "usr", "credentials")
# return single value or array if a single row is selected
i = db.sql("select max(id) from some_table")
n, l = db.sql("SELECT first, last FROM some_name WHERE id = ?", 1000)
should the two examples above return more than one row, the first one is used.
db.sql("SELECT first, last FROM some_name WHERE id < ?", 1000) do |row|
puts "#{row.first} #{row.last}"
# in case of conflicts with existing methods, you can use:
row["last"]
end
The row that the sql iterates over is the standard DBI::Row
, which has
been supplemented with accessor methods named after the columns in case
these don't conflict with existing methods.
Obtaining
You can download the current release here as a tgz
archive or here as a Ruby gem
.
Documentation
RDoc
documentation for the library can be browsed here.
Tests
I've currently tested DBrb
with the following databases:
- Mysql (tested with 4.1/Linux)
- Postgresql (... 8.1/Linux)
Since even these two have already brought some minor inconsitancies in the behaviour of the database driver implementations to light, I would assume that some adjustments might need to be made to use the library with other databases.
TODO
- Handle DB transactions.
- Tests for further databases.
- Add tests for all datatypes defined in ANSI Sql. Currently tested:
VARCHAR
,NUMERIC
,BOOLEAN
,DOUBLE PRECISION
,INTEGER
,DATE
,TIME
,TIMESTAMP
Inconsitancies between Mysql and Postgres Drivers
This list is for me to remember some of the issues I came across, if it turns out to be useful to anyone else, all the better:
- Selecting
boolean
columns returns0|1
in Postgres,false|true
in Mysql. - Selecting
double
columns returns aString
(e.g. "4.4") in Mysql,float
in Postgres. - Selecting a
time
column in Postgres returns aString
, formatted:"%H:%M:%S"
instead of aDBI::Time
- Selecting a
timestamp
column in Mysql returns aString
, formatted:"%Y-%m-%d %H:%M:%S"
- Method
row
ofDBI::StatementHandle
for aSELECT
statment in Postgres returns 0, Mysql returns the actual number of rows selected. Both work as expected forUPDATE
andINSERT
statements.
License
DBrb
is Copyright (c) 2006 by Tim Becker (tim@kuriositaet.de) and is
released under the same license as Ruby.