Class: ActiveRecord::ConnectionAdapters::OracleEnhanced::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.

Constant Summary collapse

DEFAULT_TCP_KEEPALIVE_TIME =

:nodoc:

600

Class Method Summary collapse

Class Method Details

.new_connection(config) ⇒ Object



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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb', line 305

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
  schema = config[:schema] && config[:schema].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"]

  # using a connection string via DATABASE_URL
  connection_string = if host == "connection-string"
    database
  # connection using host, port and database name
  elsif host || port
    host ||= "localhost"
    host = "[#{host}]" if /^[^\[].*:/.match?(host)  # IPv6
    port ||= 1521
    database = "/#{database}" unless database.start_with?("/")
    "//#{host}:#{port}#{database}"
  # if no host is specified then assume that
  # database parameter is TNS alias or TNS connection string
  else
    database
  end

  OCI8.properties[:tcp_keepalive] = config[:tcp_keepalive] == false ? false : true
  begin
    OCI8.properties[:tcp_keepalive_time] = config[:tcp_keepalive_time] || DEFAULT_TCP_KEEPALIVE_TIME
  rescue NotImplementedError
  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 if cursor_sharing
  if ActiveRecord.default_timezone == :local
    conn.exec "alter session set time_zone = '#{time_zone}'" unless time_zone.blank?
  elsif ActiveRecord.default_timezone == :utc
    conn.exec "alter session set time_zone = '+00:00'"
  end
  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

  OracleEnhancedAdapter::FIXED_NLS_PARAMETERS.each do |key, value|
    conn.exec "alter session set #{key} = '#{value}'"
  end
  conn
end