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

Liming L. 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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs