Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/arjdbc/teradata/connection_methods.rb

Class Method Summary collapse

Class Method Details

.teradata_connection(config) ⇒ Object Also known as: jdbcteradata_connection



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
# File 'lib/arjdbc/teradata/connection_methods.rb', line 3

def teradata_connection(config)
  begin
    require 'jdbc/teradata'
    ::Jdbc::Teradata.load_driver(:require) if defined?(::Jdbc::Teradata.load_driver)
  rescue LoadError # assuming driver.jar is on the class-path
  end

  if config[:jndi]
    jndi = config[:jndi].to_s
    ctx = javax.naming.InitialContext.new
    ds = nil

    # Taken from oracle-enhanced (https://github.com/rsim/oracle-enhanced)
    # tomcat needs first lookup method, oc4j (and maybe other application servers) need second method
    begin
      env = ctx.lookup('java:/comp/env')
      ds = env.lookup(jndi)
    rescue
      ds = ctx.lookup(jndi)
    end

    # For ARJDBC we only need the URL, username, and password
    # We set the database config entry because it's used by the adapter to determine database_name
    if ds.respond_to?('getJdbcUrl')
      config[:url] = ds.getJdbcUrl
    else
      config[:url] = ds.getUrl
    end
    config[:username] ||= ds.getUsername
    config[:database] ||= config[:url][/DATABASE=(.*?),/m, 1] unless config[:url].nil?
    config[:password] ||= ds.getPassword

  else
    config[:username] ||= Java::JavaLang::System.get_property('user.name')
    config[:host] ||= 'localhost'
    config[:port] ||= 1025
    config[:tmode] ||= 'ANSI' # ANSI, Teradata, DEFAULT
    config[:charset] ||= 'UTF8'
    config[:cop] ||= 'OFF'
    config[:log_level] ||= 'ERROR'
    config[:xviews] ||= 'OFF'
    config[:url] ||= "jdbc:teradata://#{config[:host]}/DATABASE=#{config[:database]},DBS_PORT=#{config[:port]},COP=#{config[:cop]},tmode=#{config[:tmode]},charset=#{config[:charset]},LOG=#{config[:log_level]},USEXVIEWS=#{config[:xviews]}"
  end
    config[:driver] ||= 'com.teradata.jdbc.TeraDriver'
    config[:adapter_class] = ActiveRecord::ConnectionAdapters::TeradataAdapter
    config[:adapter_spec] = ::ArJdbc::Teradata
    jdbc_connection(config)
end