Class: Sequelizer::ConnectionMaker

Inherits:
Object
  • Object
show all
Defined in:
lib/sequelizer/connection_maker.rb

Overview

Class that handles loading/interpretting the database options and creates the Sequel connection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ ConnectionMaker

Accepts an optional set of database options

If no options are provided, attempts to read options from config/database.yml

If config/database.yml doesn’t exist, Dotenv is used to try to load a .env file, then uses any SEQUELIZER_* environment variables as database options



20
21
22
# File 'lib/sequelizer/connection_maker.rb', line 20

def initialize(options = nil)
  @options = Options.new(options)
end

Instance Attribute Details

#optionsObject (readonly)

The options for Sequel.connect



10
11
12
# File 'lib/sequelizer/connection_maker.rb', line 10

def options
  @options
end

Instance Method Details

#connectionObject

Returns a Sequel connection to the database



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sequelizer/connection_maker.rb', line 25

def connection
  opts = options.to_hash
  extensions = options.extensions

  conn = if url = (opts.delete(:uri) || opts.delete(:url))
    Sequel.connect(url, opts)
  else
    # Kerberos related options
    realm = opts[:realm]
    host_fqdn = opts[:host_fqdn] || opts[:host]
    principal = opts[:principal]

    adapter = opts[:adapter]
    if adapter =~ /\Ajdbc_/
      user = opts[:user]
      password = opts[:password]
    end

    case opts[:adapter] && opts[:adapter].to_sym
    when :jdbc_hive2
      opts[:adapter]  = :jdbc
      auth = if realm
        ";principal=#{e principal}/#{e host_fqdn}@#{e realm}"
      elsif user
        ";user=#{e user};password=#{e password}"
      else
        ';auth=noSasl'
      end
      opts[:uri] = "jdbc:hive2://#{e opts[:host]}:#{opts.fetch(:port, 21050).to_i}/#{e(opts[:database] || 'default')}#{auth}"
    when :jdbc_impala
      opts[:adapter]  = :jdbc
      auth = if realm
        ";AuthMech=1;KrbServiceName=#{e principal};KrbAuthType=2;KrbHostFQDN=#{e host_fqdn};KrbRealm=#{e realm}"
      elsif user
        if password
          ";AuthMech=3;UID=#{e user};PWD=#{e password}"
        else
          ";AuthMech=2;UID=#{e user}"
        end
      end
      opts[:uri] = "jdbc:impala://#{e opts[:host]}:#{opts.fetch(:port, 21050).to_i}/#{e(opts[:database] || 'default')}#{auth}"
    when :jdbc_postgres
      opts[:adapter]  = :jdbc
      auth = "?user=#{user}#{"&password=#{password}" if password}" if user
      opts[:uri] = "jdbc:postgresql://#{e opts[:host]}:#{opts.fetch(:port, 5432).to_i}/#{e(opts[:database])}#{auth}"
    when :impala
      opts[:database] ||= 'default'
      opts[:port] ||= 21000
      if principal
        # realm doesn't seem to be used?
        opts[:transport] = :sasl
        opts[:sasl_params] = {
          mechanism: "GSSAPI",
          remote_host: host_fqdn,
          remote_principal: principal
        }
      end
    end

    Sequel.connect(opts)
  end
  conn.extension(*extensions)
  conn
end