# How to get database adapter type programmatically in rails?

**URL:** <https://www.ruby-forum.com/t/how-to-get-database-adapter-type-programmatically-in-rails/116634>\
**Category:** Rails\
**Created:** [October 8, 2007, 9:46am UTC](https://www.ruby-forum.com/t/how-to-get-database-adapter-type-programmatically-in-rails/116634 "2007-10-08T09:46:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![limlian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b5a626/32.png) [@limlian](https://www.ruby-forum.com/u/limlian)\
**Post date:** [October 8, 2007, 9:46am UTC](https://www.ruby-forum.com/t/how-to-get-database-adapter-type-programmatically-in-rails/116634/1 "2007-10-08T09:46:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![limlian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/b5a626/32.png) [@limlian](https://www.ruby-forum.com/u/limlian)\
**Post date:** [October 8, 2007, 12:44pm UTC](https://www.ruby-forum.com/t/how-to-get-database-adapter-type-programmatically-in-rails/116634/2 "2007-10-08T12:44:36Z")

</div>

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](http://www.nedforce.com)
