Bugs item #9252, was opened at 2007-03-13 14:34
You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=7857&aid=9252&group_id=2014

Category: AR-JDBC
Group: None
>Status: Closed
Resolution: None
Priority: 3
Submitted By: Nick Sieger (nicksieger)
Assigned to: Nobody (None)
Summary: Create subclass drivers for common database types to ease configuration

Initial Comment:
It would be great to have some subclass drivers for common databases, like 
MySQL etc. that followed the Rails database.yml conventions.  Example

development:
  adapter: jdbc_mysql
  database: rails_development
  host: localhost
  username: rails
  password:

would be equivalent to the following JDBC config:

development:
  adapter: jdbc
  driver: com.mysql.jdbc.Driver
  url: jdbc:mysql://localhost/rails_development
  username: rails
  password:

Maybe even co-opt the existing drivers, so the extra "jdbc_" in the adapter 
name isn't necessary?



----------------------------------------------------------------------

>Comment By: Ola Bini (olabini)
Date: 2007-08-11 15:24

Message:
Nick have done some great work on this, and you should now be able to use your 
regular DB config with JDBC as back driver when running on JRuby.

----------------------------------------------------------------------

Comment By: Simon Epstein (simondo)
Date: 2007-04-02 19:58

Message:
Hi there,

Had a crack at implementing this.  Modified the mysql and postgresql tests to 
use the database.yml format, just to demonstrate it in action.  If this is 
along the lines of what you had in mind, I'm happy to resubmit the patch with 
separate tests for making a connection with the Rails style config.

Simon

Index: test/mysql_simple_test.rb
===================================================================
--- test/mysql_simple_test.rb   (revision 427)
+++ test/mysql_simple_test.rb   (working copy)
@@ -4,6 +4,8 @@
 #   create database weblog_development;
 #   grant all on weblog_development.* to [EMAIL PROTECTED];
 
+RAILS_CONNECTION_ADAPTERS = %w(mysql)
+
 require 'jdbc_common'
 require 'db/mysql'
 
Index: test/db/mysql.rb
===================================================================
--- test/db/mysql.rb    (revision 427)
+++ test/db/mysql.rb    (working copy)
@@ -1,20 +1,10 @@
+# Rails config style
 config = {
+  :adapter  => 'mysql',
+  :database => 'weblog_development',
+  :host     => 'localhost',
   :username => 'blog',
   :password => ''
 }
 
-if RUBY_PLATFORM =~ /java/
-  config.update({
-    :adapter  => 'jdbc',
-    :driver   => 'com.mysql.jdbc.Driver',
-    :url      => 'jdbc:mysql://localhost:3306/weblog_development',
-  })
-else
-  config.update({
-    :adapter  => 'mysql',
-    :database => 'weblog_development',
-    :host     => 'localhost'
-  })
-end
-
 ActiveRecord::Base.establish_connection(config)
Index: test/db/postgres.rb
===================================================================
--- test/db/postgres.rb (revision 427)
+++ test/db/postgres.rb (working copy)
@@ -1,8 +1,7 @@
+# Rails config style
 config = { 
-  :adapter => 'jdbc',
+  :adapter => 'postgresql',
   :database => 'weblog_development',
-  :url => 'jdbc:postgresql://localhost/weblog_development',
-  :driver => 'org.postgresql.Driver',
   :username => 'blog',
   :password => ''
 }
Index: lib/active_record/connection_adapters/jdbc_adapter.rb
===================================================================
--- lib/active_record/connection_adapters/jdbc_adapter.rb       (revision 427)
+++ lib/active_record/connection_adapters/jdbc_adapter.rb       (working copy)
@@ -5,6 +5,9 @@
 
 module ActiveRecord
   class Base
+    
+    include JdbcAdapterConfig::Base
+    
     def self.jdbc_connection(config)
       connection = ConnectionAdapters::JdbcConnection.new(config)
       ConnectionAdapters::JdbcAdapter.new(connection, logger, config)
Index: lib/active_record/connection_adapters/jdbc_adapter_config.rb
===================================================================
--- lib/active_record/connection_adapters/jdbc_adapter_config.rb        
(revision 0)
+++ lib/active_record/connection_adapters/jdbc_adapter_config.rb        
(revision 0)
@@ -0,0 +1,53 @@
+module JdbcAdapterConfig
+  module Base
+    def self.included(base)
+      base.extend(ClassMethods)
+    end
+    
+    CONFIG = {
+      :mysql => {
+        :url => lambda {|c| 
"jdbc:mysql://#{c[:host]}:#{c[:port]}/#{c[:database]}"},
+        :default_port => 3306,
+        :driver => 'com.mysql.jdbc.Driver'
+      },
+      :postgresql => {
+        :url => lambda {|c| 
"jdbc:postgresql://#{c[:host]}:#{c[:port]}/#{c[:database]}"},
+        :default_port => 5432,
+        :driver => 'org.postgresql.Driver'
+      }
+    }
+
+    module ClassMethods
+      def self.extended(base)
+        class << base
+          CONFIG.keys.each do |adapter|
+            # try load the native adapter so we can patch in jdbc
+            begin
+              require "active_record/connection_adapters/#{adapter}_adapter"
+            rescue
+              nil
+            end
+            define_method "#{adapter}_connection" do |native_config|
+              jdbc_connection(jdbc_config(adapter, native_config))
+            end
+          end
+          
+          private
+          
+          def jdbc_config(adapter, config)
+            config[:host] ||= 'localhost'
+            config[:port] ||= CONFIG[adapter][:default_port]
+            
+            config.update({
+              :adapter  => 'jdbc',
+              :driver   => CONFIG[adapter][:driver],
+              :url      => CONFIG[adapter][:url].call(config),
+            })
+          end
+        end
+        
+      end
+    end
+    
+  end
+end


----------------------------------------------------------------------

Comment By: Ryan Bell (kofno)
Date: 2007-03-13 15:17

Message:
I like the idea of co-opting the existing drivers. What do you think would be 
the impact of that on people who override the native data types for various 
reasons?

----------------------------------------------------------------------

You can respond by visiting: 
http://rubyforge.org/tracker/?func=detail&atid=7857&aid=9252&group_id=2014
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel

Reply via email to