DBrb 0.1.0

I’m happy to announce the initial release of DBrb. DBrb is a minimal
database access API that’s implemented as a wrapper around the DBI API.

I’ve written it because I’m to stupid to remember all the different ways
to access DBI, and found myself interspersing my scripts with naked SQL
statements during development, leaving them for later to figure out how
to
actually execute them after finishing the “Ruby” parts of my scripts.
DBrb reflects this style of programming.

Some Examples:

  • Initializing the library:

    db = DBrb.new(“my_dbi_driver_string”, “usr”, “credentials”)

  • Basic SELECT stuff:

    i = db.sql “select max(id) from some_table”
    first, last = db.sql(“SELECT first, last FROM some_name WHERE id =
    ?”, 1000)

(a call to the method sql executes the sql statement and returns the
first row
returned by that call, or nil.)

  • SELECT with a block:

    db.sql(“SELECT first, last FROM some_name WHERE id < ?”, 1000) do
    |row|
    puts “#{row.first} #{row.last}”
    end

(calling sql with a block iterates over all results returned by the
sql statement. The yielded row is a DBI::Row object extended to
respond to the names of the columns in the database. In case of
naming conflicts, you can still use row[COLUMN_NAME])

  • INSERT’s, UPDATE’s, etc. work the same;

    1.upto(1000) do |i|
    db.sql(“INSERT INTO test_table (num) VALUES (?)”, i)
    end

Homepage: DBrb : Ruby Database Access
Rubyforge: http://rubyforge.org/projects/dbrb/

This is an initial release, the main TODOs are implementing a
database indepedant way of handling transactions and creating
tests for different database backends.

The code necessary to implement this library is not very large or
complex. Yet I’ve found it to boost my productivity immensely,
expecially writing ad hoc scripts connecting to a database. I
hope it may turn out to be useful for others.

-Tim B.