Liming Lian
2007-Oct-08  07:45 UTC
How to get database adapter type programmatically in rails?
Hi,all
I am developing a Rails application supporting both Mysql and Oracle.
I have to use Native SQL statements some place. While we know the SQL
grammar is quite different for Mysql and Oracle, so I have to get
database adapter type programmatically in my codes to process
different native SQL statements. I haven''t found any document for this
situation. After some research, I have found a way to do so like
following:
    if ActiveRecord::Base::connection.is_a?
(ActiveRecord::ConnectionAdapters::OracleAdapter)
       {Process native Oracle SQL statements}
    else if ActiveRecord::Base::connection.is_a?
(ActiveRecord::ConnectionAdapters::MysqlAdapter)
       {Process natvie Mysql SQL statements}
    end
After test, it works. But I am wondering if this is the best way to
achieve my goal, or is there any other smarter way?
Thanks in advance!
Regards,
Liming
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Roderick van Domburg
2007-Oct-08  10:44 UTC
Re: How to get database adapter type programmatically in rai
Liming Lian wrote:> Hi,all > > I am developing a Rails application supporting both Mysql and Oracle. > I have to use Native SQL statements some place. While we know the SQL > grammar is quite different for Mysql and Oracle, so I have to get > database adapter type programmatically in my codes to process > different native SQL statements. I haven''t found any document for this > situation. After some research, I have found a way to do so like > following: > > > if ActiveRecord::Base::connection.is_a? > (ActiveRecord::ConnectionAdapters::OracleAdapter) > {Process native Oracle SQL statements} > else if ActiveRecord::Base::connection.is_a? > (ActiveRecord::ConnectionAdapters::MysqlAdapter) > {Process natvie Mysql SQL statements} > end > > After test, it works. But I am wondering if this is the best way to > achieve my goal, or is there any other smarter way?To my knowledge it''s either that or ActiveRecord::Base.adapter_name. Personally I would opt for a case statement instead of an if statement. -- Roderick van Domburg http://www.nedforce.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---