Monday, May 19, 2014

Fire Ruby - howto start with Firebird and Ruby on Ubuntu / Debian

This howto is about installing firebird ruby driver on Ubuntu or any Debian based distro
you might need some firebird dependencies and if you need the stable Firebird server  Firebird 2.5 guide to install it

The very basic firebird package to build only the driver is firebird2.5-dev

sudo apt-get install firebird2.5-dev git-core
and choose yes when asked
Then you need to install ruby and the recommended rails way is to use rbenv 

rbenv install 2.1.2
rbenv global 2.1.2
ruby -v
Best way is to install it from gem

gem install fb

Alternate way  is to build install our gem (latest build-able is located here )

git clone https://github.com/rowland/fb.git
cd fb/
gem build fb.gemspec


You will get something like this in terminal :
 Successfully built RubyGem
  Name: fb
  Version: 0.*.*
  File: fb-0.*.*.gem


Now is time to install it using the gem command

gem install fb-0.*.*.gem



Please read the README.
here is how i tested on my machine

Here is part of the example i ran on my pc

pico test.rb 
require 'fb'
include Fb
# The Database class acts as a factory for Connections.
# It can also create and drop databases.
db = Database.new(
:database => "localhost:/tmp/readme.fdb",
:username => 'sysdba',
:password => 'masterkey')
# :database is the only parameter without a default.
# Let's connect to the database, creating it if it doesn't already exist.
conn = db.connect rescue db.create.connect
# We'll need to create the database schema if this is a new database.
conn.execute("CREATE TABLE TEST (ID INT NOT NULL PRIMARY KEY, NAME VARCHAR(20))") if !conn.table_names.include?("TEST")
# Let's insert some test data using a parameterized query. Note the use of question marks for place holders.
10.times {|id| conn.execute("INSERT INTO TEST VALUES (?, ?)", id, "John #{id}") }
# Here we'll conduct a spot check of the data we have just inserted.
ary = conn.query("SELECT * FROM TEST WHERE ID = 0 OR ID = 9")
ary.each {|row| puts "ID: #{row[0]}, Name: #{row[1]}" }
view raw test.rb hosted with ❤ by GitHub


And here are the results for my ruby test
ruby test.rb 
ID: 0, Name: John 0
ID: 9, Name: John 9

What is next class ? RoR on Firebird