[Rubyibm-developers] help creating rubyibm adapter for RubyAMF

aaron smith beingthexemplarylists at gmail.com
Tue May 22 10:12:56 EDT 2007

Hey All,

I need some help making an adapter for rubyibm. It's for the project RubyAMF.
What I'm looking for is some logic from your database library when doing
database queries and getting results back.

Currently I have Mysql and ActiveRecord adapters and am looking to expand
upon those with rubyibm. Here are examples of the output I need in order
to correctly implement an adapter for rubyibm.

There are 4 important things I need after a query or DB operation has been
performed. Note that the only operation ever returned is a Query of some

1: The result class and data structure
2: The column names returned in the query
3: The row count
4: The actual returned data

An example Mysql set of statements that gives me the desired output is:
@con = Mysql.connect('localhost','root','')
result = @con.query("SELECT * FROM MYTABLE")
puts result.class.to_s  			#1-> Mysql::Result
fields = result.fetch_fields		
column_names = [] 					#2
fields.each do |field|				
 column_names << field				
row_count = result.num_rows 		#3
data = result  						#4

In the above example #1,#2,#3 and #4 correlate to the data needed that
is listed above.

Now for an ORM (such as ActiveRecord) I need the same info but for two different
situations. Becaue ActiveRecord results with more than one row are always an
Array of ActiveRecord::Base or if there is one result it is just an

To find the desired data for a SINGLE ActiveRecord these statements do
the trick:
r = SomeModel.find(1)
puts r.class.to_s					#1-> ActiveRecord::Base
puts r.class.column_names 			#2
puts "1" #-> one row 				#3
column_names.each do |key|
 data << r.attributes[key]
puts data #-> 						#4

Now for multiple records found from ActiveRecord, an Array of ActiveRecord::Base
is returned. To get the desired info I have to do this now:
r = SomeModel.find(:all)
puts r.class.to_s     					#1->  Array
puts r[0].class.superclass.name  		#1->  ActiveRecord::Base
column_names = r[0].class.column_names  #2
row_count = r.length 					#3
data = []
0.upto(r.length) do |i|
 row = []
 column_names.each do |key|
  row << r[i].attributes[key]
 data << row
data 									#4

In the multiple ActiveRecord::Base example above. #1 is referened 2
times because those two
pieces of information help me determine that it is indeed an ActiveRecord..
(if(r.class == "Array" && r[0].class.superclass == "ActiveRecord::Base") .. )

So if anyone can help me, I would really appreciate it. As having to
install every single
database is kind of a pain and not really the best option. If whoever
helps would provide
code snippets that they had to write in order to get the same data, I
can use that in
creating the rubyibm adapters.


More information about the Rubyibm-developers mailing list