forked from localytics/odbc_adapter
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
75 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,2 @@ | ||
# 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 = Object, &block) | ||
dbms_registry[pattern] = Class.new(superclass, &block) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module ODBCAdapter | ||
class Registry | ||
attr_reader :dbs | ||
|
||
def initialize | ||
@dbs = { | ||
/my.*sql/i => :MySQL, | ||
/postgres/i => :PostgreSQL | ||
} | ||
end | ||
|
||
def adapter_for(reported_name) | ||
reported_name = reported_name.downcase.gsub(/\s/, '') | ||
found = | ||
dbs.detect do |pattern, adapter| | ||
adapter if reported_name =~ pattern | ||
end | ||
|
||
raise ArgumentError, "ODBCAdapter: Unsupported database (#{reported_name})" if found.nil? | ||
normalize_adapter(found.last) | ||
end | ||
|
||
def register(pattern, superclass = Object, &block) | ||
dbs[pattern] = Class.new(superclass, &block) | ||
end | ||
|
||
private | ||
|
||
def normalize_adapter(adapter) | ||
return adapter unless adapter.is_a?(Symbol) | ||
require "odbc_adapter/adapters/#{adapter.downcase}_odbc_adapter" | ||
Adapters.const_get(:"#{adapter}ODBCAdapter") | ||
end | ||
end | ||
|
||
class << self | ||
def adapter_for(reported_name) | ||
registry.adapter_for(reported_name) | ||
end | ||
|
||
def register(pattern, superclass = Object, &block) | ||
registry.register(pattern, superclass, &block) | ||
end | ||
|
||
private | ||
|
||
def registry | ||
@registry ||= Registry.new | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'test_helper' | ||
|
||
class RegistryTest < Minitest::Test | ||
def test_register | ||
registry = ODBCAdapter::Registry.new | ||
|
||
require File.join('odbc_adapter', 'adapters', 'mysql_odbc_adapter') | ||
registry.register(/foobar/, ODBCAdapter::Adapters::MySQLODBCAdapter) do | ||
def initialize | ||
end | ||
|
||
def quoted_true | ||
'foobar' | ||
end | ||
end | ||
|
||
adapter = registry.adapter_for('Foo Bar') | ||
assert_kind_of Class, adapter | ||
assert_equal ODBCAdapter::Adapters::MySQLODBCAdapter, adapter.superclass | ||
assert_equal 'foobar', adapter.new.quoted_true | ||
end | ||
end |