Class: ActiveRecord::ConnectionAdapters::OracleEnhancedOCIFactory

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

Overview

The OracleEnhancedOCIFactory factors out the code necessary to connect and configure an Oracle/OCI connection.

Class Method Summary collapse

Class Method Details

.new_connection(config) ⇒ Object

:nodoc:



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 294

def self.new_connection(config)
  # to_s needed if username, password or database is specified as number in database.yml file
  username = config[:username] && config[:username].to_s
  password = config[:password] && config[:password].to_s
  database = config[:database] && config[:database].to_s
  host, port = config[:host], config[:port]
  privilege = config[:privilege] && config[:privilege].to_sym
  async = config[:allow_concurrency]
  prefetch_rows = config[:prefetch_rows] || 100
  cursor_sharing = config[:cursor_sharing] || 'force'
  # get session time_zone from configuration or from TZ environment variable
  time_zone = config[:time_zone] || ENV['TZ']
  schema = config[:schema]

  # connection using host, port and database name
  connection_string = if host || port
    host ||= 'localhost'
    host = "[#{host}]" if host =~ /^[^\[].*:/  # IPv6
    port ||= 1521
    "//#{host}:#{port}/#{database}"
  # if no host is specified then assume that
  # database parameter is TNS alias or TNS connection string
  else
    database
  end

  conn = OCI8.new username, password, connection_string, privilege
  conn.autocommit = true
  conn.non_blocking = true if async
  conn.prefetch_rows = prefetch_rows
  conn.exec "alter session set cursor_sharing = #{cursor_sharing}" rescue nil
  conn.exec "alter session set time_zone = '#{time_zone}'" unless time_zone.blank?
  conn.exec "alter session set current_schema = #{schema}" unless schema.blank?

  # Initialize NLS parameters
  OracleEnhancedAdapter::DEFAULT_NLS_PARAMETERS.each do |key, default_value|
    value = config[key] || ENV[key.to_s.upcase] || default_value
    if value
      conn.exec "alter session set #{key} = '#{value}'"
    end
  end
  conn
end