Wednesday, January 14, 2009

using ActiveRecord to access Microsoft SQL Server via ODBC

Recently I need to insert 400+ rows of data into an eight year old SQL Server 2000 database. It's really a pain in the ass if I have to do it by hand or learning a new tool to do it.

Luckily, I can use ActiveRecord to do that easily thanks to activerecord-sqlserver-adapter and and auto table field mapping of ActiveRecord.

Here is a quick step-by-step guide:

Install Ruby on Windows machine (that run SQL server)
Download One-Click Ruby Installer for Windows for Ruby 1.8.6 P26 at
http://rubyforge.org/frs/download.php/29263/ruby186-26.exe

And install it. If you use the default setting, it will setup ruby at c:\ruby folder with rubygem 0.9.3

Update RubyGems
Open "Command Prompt"
cd c:\ruby\bin gem update --system

Install ActiveRecord with SQLServer Adapter
gem install activerecord-sqlserver-adapter --source=http://gems.rubyonrails.org
If the installation
Successfully installed activesupport-2.2.2 Successfully installed activerecord-2.2.2 Successfully installed activerecord-sqlserver-adapter-1.0.0.9250 3 gems installed
Create a DSN (Data Source Name) Luckily I can reuse DNS from current ASP project so I don't need to create new one.
If you have to, you can follow guideline in [1].
Setup connection and create property ActiveRecord based classes
require "active_record"
ActiveRecord::Base.establish_connection(
:adapter => "sqlserver",
:mode => "odbc",
:username => "yourusername",
:password => "yourpassword",
:dsn => "yourDSN"
)
class AnyNameYouWant < ActiveRecord::Base
set_table_name "TableNeedToAccess"
set_primary_key "PrimaryID"
end
Start irb by:
cd c:\ruby\bin irb

And paste above chuck of code in to irb. From now, you can use ActiveRecord based classes to read/write/add/remove SQL Server table as in a Rails script/console environment.

I also got a problem with Time.mktime and need to some code adjust by hand

Open
c:\ruby\lib\ruby\gems\1.8\gems\activerecord-sqlserver-adapter-1.0.0.9250\lib\active_record\connection_adapters\sqlserver_adapter.rb

Replace "Time.mktime" by "Datetime.new" will fix the problem [2]

References
[1] http://piao-tech.blogspot.com/2008/02/using-activerecord-with-microsoft-sql.html
[2] http://papillon.nwnx.org/index.php/archives/19

Friday, November 14, 2008

spatial operating environment


g-speak overview 1828121108 from john underkoffler on Vimeo.

Thursday, November 13, 2008

Intro to REST

source: Google

Tuesday, October 14, 2008

I join alonetone.com open source project

I feel excited to work with Sudara, an artist and an Engine Yard's programmer, to improve alonetone.com.

Sudara created the site in Jan 2008 as a home for musicians where they can upload their music and freely share it on the Internet. Few month later, alonetone had evolved as a community for musicians who love to write music, talk about music and want to distribute their work freely.

More facts about alonetone:
  • 2922 original & legally uploaded mp3s
  • around 300 musicians
  • 7.5 days of continuous free music
  • 11.7 gigabytes of mp3
  • 500 avg listens per day
Will alonetone be very successful or be failure? I don't care much. The point is that Sudara have been trying very hard to make his dream come true. And he need help, need to be understood, and to be inspired by other people.

I will do more work on back-end so Sudara have time to improve design, refine the direction of alonetone and to create great songs like this:

Back to the cold, by Sudara


Sudara, can you create a song for our programmers like this song? :D

Wednesday, September 24, 2008

Ruby's Best Feature?

It's the title of Rich Kilmer's 2008 first post. You should read it even if you are not a Ruby programmer.
.
.
.
.
.
.
He concluded that:
In my experience, this is the single most important feature of the Ruby language---capturing the domain in a language the entire team can speak. If you believe clear communications is an essential quality of highly effective teams, this feature is something that you should strive to exploit in your own Ruby projects.
Do you want to know what do I feel after reading this post?

I want to read Martin Fowler's Domain Specific Languages Book immediately and write DSL's patterns described in the book in Ruby.

Google behind the screen

Review



Related posts:
* "Speed is king" or "how to prepare for future?"
* The Machine is Us/ing Us

Next generation databases for Web 2.0 written in Erlang

Just purchased "CouchDB with Rails" screencast from PeepCode.

If you don't want to purchase above screencast to learn about an next generation, document-oriented database. You can view some CouchDB talks (source CouchDB Wiki):


2008 August 27th: CouchDB to BBC web developers (Slides, Notes)




2008 June 27th: CouchDB, Erlang eXchange (Slides)




2008 March 17th: CouchDB from cover to cover (Slides)

Part 1:

Part 2:


Another database I'm tracking is Scalaris: a young, peer-to-peer, distributed, academic awards storage engine.
The concept of Scalaris is very simple: Scalaris is a transactional data store for online services based on the peer-to-peer principle. It provides self-management and scalability by replicating services among peers. Without system interruption it scales from a few PCs to thousands of servers. Servers can be added or removed on the fly without any service downtime.

Scalable Wikipedia with Erlang, Google Scalability Conference, Seattle, June 2008 (slides)


Building a transactional distributed data store with Erlang, Erlang eXchange, London, June 2008 (slides)