class ActiveRecord::Base
Public Class Methods
odbc_connection(config)
click to toggle source
Build a new ODBC connection with the given configuration.
# File lib/active_record/connection_adapters/odbc_adapter.rb, line 20 def odbc_connection(config) config = config.symbolize_keys connection, config = if config.key?(:dsn) odbc_dsn_connection(config) elsif config.key?(:conn_str) odbc_conn_str_connection(config) else raise ArgumentError, 'No data source name (:dsn) or connection string (:conn_str) specified.' end database_metadata = ::ODBCAdapter::DatabaseMetadata.new(connection, config[:encoding_bug]) ::ODBCAdapter::Db2Adapter.new(connection, logger, config, database_metadata) end
Private Class Methods
odbc_conn_str_connection(config)
click to toggle source
Connect using ODBC connection string Supports DSN-based or DSN-less connections e.g. “DSN=virt5;UID=rails;PWD=rails”
"DRIVER={OpenLink Virtuoso};HOST=carlmbp;UID=rails;PWD=rails"
# File lib/active_record/connection_adapters/odbc_adapter.rb, line 53 def odbc_conn_str_connection(config) attrs = config[:conn_str].split(';').map { |option| option.split('=', 2) }.to_h odbc_module = attrs['ENCODING'] == 'utf8' ? ODBC_UTF8 : ODBC driver = odbc_module::Driver.new driver.name = 'odbc' driver.attrs = attrs connection = odbc_module::Database.new.drvconnect(driver) # encoding_bug indicates that the driver is using non ASCII and has the issue referenced here https://github.com/larskanis/ruby-odbc/issues/2 [connection, config.merge(driver: driver, encoding: attrs['ENCODING'], encoding_bug: attrs['ENCODING'] == 'utf8')] end
odbc_dsn_connection(config)
click to toggle source
Connect using a predefined DSN.
# File lib/active_record/connection_adapters/odbc_adapter.rb, line 39 def odbc_dsn_connection(config) username = config[:username] ? config[:username].to_s : nil password = config[:password] ? config[:password].to_s : nil odbc_module = config[:encoding] == 'utf8' ? ODBC_UTF8 : ODBC connection = odbc_module.connect(config[:dsn], username, password) # encoding_bug indicates that the driver is using non ASCII and has the issue referenced here https://github.com/larskanis/ruby-odbc/issues/2 [connection, config.merge(username: username, password: password, encoding_bug: config[:encoding] == 'utf8')] end