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



89
90
91
92
93
94
95
96
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
# File 'lib/active_record/connection_adapters/ibm_db_adapter.rb', line 89

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): Database configuration #{config} \
requires credentials: username and password"
  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|
    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

  # 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 using the database, host, port, username and password specified
    connection = IBM_DB::connect "DRIVER={IBM DB2 ODBC DRIVER};\
                                  DATABASE=#{database};\
                                  HOSTNAME=#{host};\
                                  PORT=#{port};\
                                  PROTOCOL=TCPIP;\
                                  UID=#{username};\
                                  PWD=#{password};", '', '', 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 the [#{database}] due to: #{IBM_DB::conn_errormsg}"
  end
end