Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/ibm_db_adapter.rb

Class Method Summary collapse

Class Method Details

.ibm_db_connection(config) ⇒ Object

Establishes a connection to a specified database using the credentials provided with the config argument. All the ActiveRecord objects will use this connection



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 97

def self.ibm_db_connection(config)
  # Attempts to load the Ruby driver IBM databases
  # while not already loaded or raises LoadError in case of failure.
  begin
    require 'ibm_db' unless defined? IBM_DB
  rescue LoadError
    raise LoadError, "Failed to load IBM_DB Ruby driver."
  end

  # Converts all +config+ keys to symbols
  config = config.symbolize_keys

  # Retrieves the database alias (local catalog name) or remote name
  # (for remote TCP/IP connections) from the +config+ hash
  # or raises ArgumentError in case of failure.
  if config.has_key?(:database)
    database = config[:database].to_s
  else
    raise ArgumentError, "Missing argument: a database name needs to be specified."
  end

  # Retrieves database user credentials from the +config+ hash
  # or raises ArgumentError in case of failure.
  if !config.has_key?(:username) || !config.has_key?(:password)
    raise ArgumentError, "Missing argument(s): Username/Password for #{config[:database]} is not specified"
  else
    username = config[:username].to_s
    password = config[:password].to_s
  end

  # Providing default schema (username) when not specified
  config[:schema] = config.has_key?(:schema) ? config[:schema].to_s : config[:username].to_s

  # Extract connection options from the database configuration
  # (in support to formatting, audit and billing purposes):
  # Retrieve database objects fields in lowercase
  conn_options = {IBM_DB::ATTR_CASE => IBM_DB::CASE_LOWER}
  config.each do |key, value|
    if !value.nil?
      case key
        when :app_user        # Set connection's user info
          conn_options[IBM_DB::SQL_ATTR_INFO_USERID]     = value
        when :account         # Set connection's account info
          conn_options[IBM_DB::SQL_ATTR_INFO_ACCTSTR]    = value
        when :application     # Set connection's application info
          conn_options[IBM_DB::SQL_ATTR_INFO_APPLNAME]   = value
        when :workstation     # Set connection's workstation info
          conn_options[IBM_DB::SQL_ATTR_INFO_WRKSTNNAME] = value
      end    
    end
  end

  # Checks if a host name or address has been specified. If so, this implies a TCP/IP connection
  # Returns IBM_DB.Connection object upon succesful DB connection to the database
  # If otherwise the connection fails, +false+ is returned
  if config.has_key?(:host)
    # Retrieves the host address/name
    host = config[:host]
    # A net address connection requires a port. If no port has been specified, 50000 is used by default
    port = config[:port] || 50000
    # Connects to the database specified using the hostname, port, authentication type, username and password info
    # Starting with DB2 9.1FP5 secure connections using SSL are supported. 
    # On the client side using CLI this is supported from CLI version V95FP2 and onwards.
    # This feature is set by specifying SECURITY=SSL in the connection string.
    # Below connection string is constructed and SECURITY parameter is appended if the user has specified the :security option
    conn_string = "DRIVER={IBM DB2 ODBC DRIVER};\
                   DATABASE=#{database};\
                   HOSTNAME=#{host};\
                   PORT=#{port};\
                   PROTOCOL=TCPIP;\
                   UID=#{username};\
                   PWD=#{password};"
    conn_string << "SECURITY=#{config[:security]};" if config.has_key?(:security)
    conn_string << "AUTHENTICATION=#{config[:authentication]};" if config.has_key?(:authentication)
    conn_string << "CONNECTTIMEOUT=#{config[:timeout]};" if config.has_key?(:timeout)
    
    connection = IBM_DB.connect conn_string, '', '', conn_options
  else
    # No host implies a local catalog-based connection: +database+ represents catalog alias
    connection = IBM_DB.connect( database, username, password, conn_options )
  end

  # Verifies that the connection was succesfull
  if connection
    # Creates an instance of *IBM_DBAdapter* based on the +connection+
    # and credentials provided in +config+
    ConnectionAdapters::IBM_DBAdapter.new(connection, logger, config, conn_options)
  else
    # If the connection failed, it raises a Runtime error
    raise "Failed to connect to [#{database}] due to: #{IBM_DB.conn_errormsg}"
  end
end