From 6fb972f757c8b9ce59c414bd82c13b93975d677b Mon Sep 17 00:00:00 2001 From: Kevin Deisz Date: Thu, 19 Jan 2017 11:41:57 -0500 Subject: [PATCH] Allow users to register their own adapter --- .../connection_adapters/odbc_adapter.rb | 1 + lib/odbc_adapter.rb | 15 +++++++++++++ lib/odbc_adapter/dbms.rb | 22 ++++++++++--------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/lib/active_record/connection_adapters/odbc_adapter.rb b/lib/active_record/connection_adapters/odbc_adapter.rb index 8b588c56..10881dde 100644 --- a/lib/active_record/connection_adapters/odbc_adapter.rb +++ b/lib/active_record/connection_adapters/odbc_adapter.rb @@ -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' diff --git a/lib/odbc_adapter.rb b/lib/odbc_adapter.rb index 194fb562..16e08551 100644 --- a/lib/odbc_adapter.rb +++ b/lib/odbc_adapter.rb @@ -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 diff --git a/lib/odbc_adapter/dbms.rb b/lib/odbc_adapter/dbms.rb index fd40876e..12a92775 100644 --- a/lib/odbc_adapter/dbms.rb +++ b/lib/odbc_adapter/dbms.rb @@ -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) @@ -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