Class: ActiveRecord::ConnectionAdapters::OracleEnhancedOCIConnection

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

Overview

OCI database interface for MRI

Instance Attribute Summary

Attributes inherited from OracleEnhancedConnection

#raw_connection

Instance Method Summary collapse

Methods inherited from OracleEnhancedConnection

create, #oracle_downcase

Constructor Details

#initialize(config) ⇒ OracleEnhancedOCIConnection

Returns a new instance of OracleEnhancedOCIConnection.



29
30
31
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 29

def initialize(config)
  @raw_connection = OCI8EnhancedAutoRecover.new(config, OracleEnhancedOCIFactory)
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 71

def active?
  @raw_connection.active?
end

#auto_retryObject



33
34
35
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 33

def auto_retry
  @raw_connection.auto_retry if @raw_connection
end

#auto_retry=(value) ⇒ Object



37
38
39
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 37

def auto_retry=(value)
  @raw_connection.auto_retry = value if @raw_connection
end

#autocommit=(value) ⇒ Object



58
59
60
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 58

def autocommit=(value)
  @raw_connection.autocommit = value
end

#autocommit?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 54

def autocommit?
  @raw_connection.autocommit?
end

#commitObject



46
47
48
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 46

def commit
  @raw_connection.commit
end

#describe(name) ⇒ Object



170
171
172
173
174
175
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 170

def describe(name)
  quoted_name = OracleEnhancedAdapter.valid_table_name?(name) ? name : "\"#{name}\""
  @raw_connection.describe(quoted_name)
rescue OCIException => e
  raise OracleEnhancedConnectionException, e.message
end

#exec(sql, *bindvars, &block) ⇒ Object



81
82
83
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 81

def exec(sql, *bindvars, &block)
  @raw_connection.exec(sql, *bindvars, &block)
end

#logoffObject



41
42
43
44
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 41

def logoff
  @raw_connection.logoff
  @raw_connection.active = false
end

#pingObject

Checks connection, returns true if active. Note that ping actively checks the connection, while #active? simply returns the last known state.



65
66
67
68
69
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 65

def ping
  @raw_connection.ping
rescue OCIException => e
  raise OracleEnhancedConnectionException, e.message
end

#reset!Object



75
76
77
78
79
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 75

def reset!
  @raw_connection.reset!
rescue OCIException => e
  raise OracleEnhancedConnectionException, e.message
end

#rollbackObject



50
51
52
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 50

def rollback
  @raw_connection.rollback
end

#select(sql, name = nil, return_column_names = false) ⇒ Object



85
86
87
88
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
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 85

def select(sql, name = nil, return_column_names = false)
  cursor = @raw_connection.exec(sql)
  cols = []
  # Ignore raw_rnum_ which is used to simulate LIMIT and OFFSET
  cursor.get_col_names.each do |col_name|
    col_name = oracle_downcase(col_name)
    cols << col_name unless col_name == 'raw_rnum_'
  end
  # Reuse the same hash for all rows
  column_hash = {}
  cols.each {|c| column_hash[c] = nil}
  rows = []
  get_lob_value = !(name == 'Writable Large Object')

  while row = cursor.fetch
    hash = column_hash.dup

    cols.each_with_index do |col, i|
      hash[col] =
        case v = row[i]
        # RSI: added emulate_integers_by_column_name functionality
        when Float
          # if OracleEnhancedAdapter.emulate_integers_by_column_name && OracleEnhancedAdapter.is_integer_column?(col)
          #   v.to_i
          # else
          #   v
          # end
          v == (v_to_i = v.to_i) ? v_to_i : v
        # ruby-oci8 2.0 returns OraNumber - convert it to Integer or BigDecimal
        when OraNumber
          v == (v_to_i = v.to_i) ? v_to_i : BigDecimal.new(v.to_s)
        when String
          v
        when OCI8::LOB
          if get_lob_value
            data = v.read
            # In Ruby 1.9.1 always change encoding to ASCII-8BIT for binaries
            data.force_encoding('ASCII-8BIT') if data.respond_to?(:force_encoding) && v.is_a?(OCI8::BLOB)
            data
          else
            v
          end
        # ruby-oci8 1.0 returns OraDate
        when OraDate
          # RSI: added emulate_dates_by_column_name functionality
          if OracleEnhancedAdapter.emulate_dates && (v.hour == 0 && v.minute == 0 && v.second == 0)
            v.to_date
          else
            # code from Time.time_with_datetime_fallback
            begin
              Time.send(Base.default_timezone, v.year, v.month, v.day, v.hour, v.minute, v.second)
            rescue
              offset = Base.default_timezone.to_sym == :local ? ::DateTime.local_offset : 0
              ::DateTime.civil(v.year, v.month, v.day, v.hour, v.minute, v.second, offset)
            end
          end
        # ruby-oci8 2.0 returns Time or DateTime
        when Time, DateTime
          if OracleEnhancedAdapter.emulate_dates && (v.hour == 0 && v.min == 0 && v.sec == 0)
            v.to_date
          else
            # recreate Time or DateTime using Base.default_timezone
            begin
              Time.send(Base.default_timezone, v.year, v.month, v.day, v.hour, v.min, v.sec)
            rescue
              offset = Base.default_timezone.to_sym == :local ? ::DateTime.local_offset : 0
              ::DateTime.civil(v.year, v.month, v.day, v.hour, v.min, v.sec, offset)
            end
          end
        else v
        end
    end

    rows << hash
  end

  return_column_names ? [rows, cols] : rows
ensure
  cursor.close if cursor
end

#write_lob(lob, value, is_binary = false) ⇒ Object



166
167
168
# File 'lib/active_record/connection_adapters/oracle_enhanced_oci_connection.rb', line 166

def write_lob(lob, value, is_binary = false)
  lob.write value
end