From beingthexemplarylists at gmail.com Tue May 22 09:59:15 2007 From: beingthexemplarylists at gmail.com (aaron smith) Date: Tue, 22 May 2007 09:59:15 -0400 Subject: [Lafcadio-users] Help creating a Lafcadio Adapter for RubyAMF Message-ID: Hey All, I need some help making an adapter for Lafcadio. 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 Lafcadio. Here are examples of the output I need in order to correctly implement an adapter for Lafcadio. 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 sort 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','') @con.select_db('mydb') 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 end 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 ActiveRecord::Base To find the desired data for a SINGLE ActiveRecord these statements do the trick: ==================================== ##connect_active_record 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] end 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: ==================================== @@connect_active_record 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] end data << row end 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 Lafcadio adapters. -Thanks Aaron