Skip to content

Commit

Permalink
Allow users to register their own adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jan 19, 2017
1 parent 5fdaafd commit 6fb972f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/active_record/connection_adapters/odbc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require 'arel/visitors/bind_visitor'
require 'odbc'

require 'odbc_adapter'
require 'odbc_adapter/database_limits'
require 'odbc_adapter/database_statements'
require 'odbc_adapter/quoting'
Expand Down
15 changes: 15 additions & 0 deletions lib/odbc_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
# Requiring with this pattern to mirror ActiveRecord
require 'active_record/connection_adapters/odbc_adapter'

module ODBCAdapter
class << self
def dbms_registry
@dbms_registry ||= {
/my.*sql/i => :MySQL,
/postgres/i => :PostgreSQL
}
end

def register(pattern, superclass, &block)
dbms_registry[pattern] = Class.new(superclass, &block)
end
end
end
22 changes: 12 additions & 10 deletions lib/odbc_adapter/dbms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ def initialize(connection)
end

def adapter_class
require "odbc_adapter/adapters/#{name.downcase}_odbc_adapter"
Adapters.const_get(:"#{name}ODBCAdapter")
return adapter unless adapter.is_a?(Symbol)
require "odbc_adapter/adapters/#{adapter.downcase}_odbc_adapter"
Adapters.const_get(:"#{adapter}ODBCAdapter")
end

def field_for(field)
Expand All @@ -32,16 +33,17 @@ def field_for(field)

# Maps a DBMS name to a symbol
# Different ODBC drivers might return different names for the same DBMS
def name
@name ||=
def adapter
@adapter ||=
begin
reported = field_for(ODBC::SQL_DBMS_NAME).downcase.gsub(/\s/, '')
case reported
when /my.*sql/i then :MySQL
when /postgres/i, 'snowflake' then :PostgreSQL
else
raise ArgumentError, "ODBCAdapter: Unsupported database (#{reported})"
end
found =
ODBCAdapter.dbms_registry.detect do |pattern, adapter|
adapter if reported =~ pattern
end

raise ArgumentError, "ODBCAdapter: Unsupported database (#{reported})" if found.nil?
found.last
end
end
end
Expand Down

0 comments on commit 6fb972f

Please sign in to comment.